From b3c06c988d61ceefd41bec869716af0ab3cb934b Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 10 Sep 2023 22:06:01 +0200 Subject: [PATCH] talking npc can now be picked up --- talking_npc.lua | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/talking_npc.lua b/talking_npc.lua index 182bed9..b8f6082 100644 --- a/talking_npc.lua +++ b/talking_npc.lua @@ -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