talking npc can now be picked up

This commit is contained in:
Sokomine 2023-09-10 22:06:01 +02:00
parent dc2d8e6c1e
commit b3c06c988d

View File

@ -76,9 +76,9 @@ npc_talk.talking_npc_entity_prototype = {
npc_talk.talking_npc_get_staticdata = function(self)
-- taken basicly from mobs_redo
local data, t = {}
local data = {}
for _, v in pairs(self) do
t = type(v)
local t = type(v)
if( t ~= "function" and t ~= "nil" and t ~= "userdata" and _ ~= "object" and _ ~= "_cmi_components") then
data[_] = self[_]
end
@ -91,9 +91,8 @@ npc_talk.talking_npc_on_activate = function(self, staticdata, dtime)
-- this is taken from mobs_redo and copies all staticdata to self
local tmp = minetest.deserialize(staticdata)
if tmp then
local t
for _,stat in pairs(tmp) do
t = type(stat)
local t = type(stat)
if t ~= "function" and t ~= "nil" and t ~= "userdata" then
self[_] = stat
end
@ -171,7 +170,8 @@ npc_talk.talking_npc_on_place = function(itemstack, placer, pointed_thing)
local mob = "npc_talk:talking_npc"
pos.y = pos.y + 1
local data = itemstack:get_metadata()
local data_str = itemstack:get_metadata()
local data = minetest.deserialize(data_str)
local smob = minetest.add_entity(pos, mob, data)
local ent = smob and smob:get_luaentity()
if(not(ent)) then
@ -185,9 +185,38 @@ npc_talk.talking_npc_on_place = function(itemstack, placer, pointed_thing)
end
npc_talk.talking_npc_pick_up = function(self, player)
-- taken from/inspired by mobs_redo as well
if(not(self) or not(player:is_player()) or not(player:get_inventory())) then
return false
end
local pname = player:get_player_name()
if(not(minetest.check_player_privs(pname, "protection_bypass"))
and self.owner ~= pname) then
minetest.chat_send_player(pname, "This NPC is owned by "..tostring(self.owner or "?")..".")
return false
end
local mobname = "npc_talk:talking_npc_item"
if(not(player:get_inventory():room_for_item("main", mobname))) then
minetest.chat_send_player(pname, "You have not enough room in your inventory.")
return false
end
local stack = ItemStack(mobname)
local data = minetest.serialize(npc_talk.talking_npc_get_staticdata(self))
stack:set_metadata(data)
local inv = player:get_inventory()
if(inv:room_for_item("main", stack)) then
inv:add_item("main", stack)
else
minetest.add_item(player:get_pos(), stack)
end
self.object:remove()
return stack
end
-- add the necessary data for skin configuration etc.
npc_talk.enable_talk_to_talking_npc = function()
-- the following information is needed in order to handle skin changes
-- this model is used by mobs_npc
@ -224,6 +253,14 @@ npc_talk.enable_talk_to_talking_npc = function()
npc_talk.talking_npc_texture.."^[colorizehsl:-120",
npc_talk.talking_npc_texture.."^[colorizehsl:-180",
}
yl_speak_up.add_on_rightclick_entry["npc_talk:talking_npc"] = {
text_if_true = "[Pick this NPC up.]",
text_if_false = "[Something wrent wrong. This is for picking the mob up.]",
condition = true,
execute_function = npc_talk.talking_npc_pick_up,
}
end
-- make sure the NPC can be configured regarding its skin