diff --git a/tools.lua b/tools.lua index 63fc178..3dd00c5 100644 --- a/tools.lua +++ b/tools.lua @@ -1,28 +1,44 @@ + +yl_speak_up.allow_staff_use = function(itemstack, user, pointed_thing) + local has_priv = minetest.check_player_privs(user, {npc_master=true}) + local p_name = user:get_player_name() + + if not has_priv then + minetest.chat_send_player(p_name,yl_speak_up.message_tool_taken_because_of_lacking_priv) + minetest.log("action","[MOD] yl_speak_up: User "..p_name.. + " used an admin item: "..itemstack:get_name().. + " at pos "..minetest.pos_to_string(user:get_pos(),0)) + itemstack:take_item(1) + return + end + + if (pointed_thing.type ~= "object") then + return + end + + local obj = pointed_thing.ref + local luaentity = obj:get_luaentity() + + -- Is the thing we clicked really one of our NPCs? + if not(luaentity) or (luaentity and not(luaentity.yl_speak_up)) then + return + end + return obj +end + + minetest.register_tool("yl_speak_up:staff_of_i_said_so", { description = "Staff of I-said-so", inventory_image = "yl_speak_up_staff_of_i_said_so.png", on_use = function(itemstack, user, pointed_thing) - local has_priv = minetest.check_player_privs(user, {npc_master=true}) - local p_name = user:get_player_name() - - if not has_priv then - minetest.chat_send_player(p_name,yl_speak_up.message_tool_taken_because_of_lacking_priv) - minetest.log("action","[MOD] yl_speak_up: User "..p_name.." used an admin item: "..itemstack:get_name().." at pos "..minetest.pos_to_string(user:get_pos(),0)) - itemstack:take_item(1) + local obj = yl_speak_up.allow_staff_use(itemstack, user, pointed_thing) + if(not(obj)) then return itemstack end - if (pointed_thing.type == "object") then - local obj = pointed_thing.ref - local luaentity = obj:get_luaentity() - - if luaentity ~= nil and luaentity.yl_speak_up == nil then return itemstack end -- Is the thing we clicked really one of our NPCs? - - yl_speak_up.config(user, obj) - - return itemstack - end + yl_speak_up.config(user, obj) + return itemstack end }) @@ -31,28 +47,22 @@ minetest.register_tool("yl_speak_up:staff_of_shut_up", { inventory_image = "yl_speak_up_staff_of_shut_up.png", on_use = function(itemstack, user, pointed_thing) - local has_priv = minetest.check_player_privs(user, {npc_master=true}) + local obj = yl_speak_up.allow_staff_use(itemstack, user, pointed_thing) + if(not(obj)) then + return itemstack + end + local p_name = user:get_player_name() + local luaentity = obj:get_luaentity() + local npc = luaentity.yl_speak_up.id + luaentity.yl_speak_up.talk = false - if not has_priv then - minetest.chat_send_player(p_name,yl_speak_up.message_tool_taken_because_of_lacking_priv) - minetest.log("action","[MOD] yl_speak_up: User "..p_name.." used an admin item: "..itemstack:get_name().." at pos "..minetest.pos_to_string(user:get_pos(),0)) - itemstack:take_item(1) - return itemstack - end - - if (pointed_thing.type == "object") then - local obj = pointed_thing.ref - local luaentity = obj:get_luaentity() - - if luaentity ~= nil and luaentity.yl_speak_up == nil then return itemstack end -- Is the thing we clicked really one of our NPCs? - - luaentity.yl_speak_up.talk = false - local npc = luaentity.yl_speak_up.id - minetest.chat_send_player(p_name,"NPC with ID "..npc.." will shut up at pos "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - minetest.log("action","[MOD] yl_speak_up: NPC with ID n_"..npc.." will shut up at pos "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - return itemstack - end + minetest.chat_send_player(p_name,"NPC with ID "..npc.." will shut up at pos ".. + minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) + minetest.log("action","[MOD] yl_speak_up: NPC with ID n_"..npc.. + " will shut up at pos "..minetest.pos_to_string(obj:get_pos(),0).. + " on command of "..p_name) + return itemstack end }) @@ -61,28 +71,22 @@ minetest.register_tool("yl_speak_up:staff_of_dawai_dawai", { inventory_image = "yl_speak_up_staff_of_dawai_dawai.png", on_use = function(itemstack, user, pointed_thing) - local has_priv = minetest.check_player_privs(user, {npc_master=true}) + local obj = yl_speak_up.allow_staff_use(itemstack, user, pointed_thing) + if(not(obj)) then + return itemstack + end + local p_name = user:get_player_name() + local luaentity = obj:get_luaentity() + local npc = luaentity.yl_speak_up.id + luaentity.yl_speak_up.talk = true - if not has_priv then - minetest.chat_send_player(p_name,yl_speak_up.message_tool_taken_because_of_lacking_priv) - minetest.log("action","[MOD] yl_speak_up: User "..p_name.." used an admin item: "..itemstack:get_name().." at pos "..minetest.pos_to_string(user:get_pos(),0)) - itemstack:take_item(1) - return itemstack - end - - if (pointed_thing.type == "object") then - local obj = pointed_thing.ref - local luaentity = obj:get_luaentity() - - if luaentity ~= nil and luaentity.yl_speak_up == nil then return itemstack end -- Is the thing we clicked really one of our NPCs? - - luaentity.yl_speak_up.talk = true - local npc = luaentity.yl_speak_up.id - minetest.chat_send_player(p_name,"NPC with ID "..npc.." will resume speech at pos "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - minetest.log("action","[MOD] yl_speak_up: NPC with ID n_"..npc.." will resume speech at pos "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - return itemstack - end + minetest.chat_send_player(p_name,"NPC with ID "..npc.." will resume speech at pos ".. + minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) + minetest.log("action","[MOD] yl_speak_up: NPC with ID n_"..npc.. + " will resume speech at pos "..minetest.pos_to_string(obj:get_pos(),0).. + " on command of "..p_name) + return itemstack end }) @@ -91,28 +95,22 @@ minetest.register_tool("yl_speak_up:staff_of_game_over", { inventory_image = "yl_speak_up_staff_of_game_over.png", on_use = function(itemstack, user, pointed_thing) - local has_priv = minetest.check_player_privs(user, {npc_master=true}) + local obj = yl_speak_up.allow_staff_use(itemstack, user, pointed_thing) + if(not(obj)) then + return itemstack + end + local p_name = user:get_player_name() + local luaentity = obj:get_luaentity() + local npc = luaentity.yl_speak_up.id - if not has_priv then - minetest.chat_send_player(p_name,yl_speak_up.message_tool_taken_because_of_lacking_priv) - minetest.log("action","[MOD] yl_speak_up: User "..p_name.." used an admin item: "..itemstack:get_name().." at pos "..minetest.pos_to_string(user:get_pos(),0)) - itemstack:take_item(1) - return itemstack - end - - if (pointed_thing.type == "object") then - local obj = pointed_thing.ref - local luaentity = obj:get_luaentity() - - if luaentity ~= nil and luaentity.yl_speak_up == nil then return itemstack end -- Is the thing we clicked really one of our NPCs? - - local npc = luaentity.yl_speak_up.id - minetest.chat_send_player(p_name,"NPC with ID "..npc.." removed from position "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - minetest.log("action","[MOD] yl_speak_up: NPC with ID "..npc.." removed from position "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - obj:remove() - return itemstack - end + minetest.chat_send_player(p_name,"NPC with ID "..npc.." removed from position ".. + minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) + minetest.log("action","[MOD] yl_speak_up: NPC with ID "..npc.. + " removed from position "..minetest.pos_to_string(obj:get_pos(),0).. + " on command of "..p_name) + obj:remove() + return itemstack end }) @@ -121,27 +119,20 @@ minetest.register_tool("yl_speak_up:staff_of_fashion", { inventory_image = "yl_speak_up_staff_of_fashion.png", on_use = function(itemstack, user, pointed_thing) - local has_priv = minetest.check_player_privs(user, {npc_master=true}) + local obj = yl_speak_up.allow_staff_use(itemstack, user, pointed_thing) + if(not(obj)) then + return itemstack + end + local p_name = user:get_player_name() + local luaentity = obj:get_luaentity() + local npc = luaentity.yl_speak_up.id - if not has_priv then - minetest.chat_send_player(p_name,yl_speak_up.message_tool_taken_because_of_lacking_priv) - minetest.log("action","[MOD] yl_speak_up: User "..p_name.." used an admin item: "..itemstack:get_name().." at pos "..minetest.pos_to_string(user:get_pos(),0)) - itemstack:take_item(1) - return itemstack - end - - if (pointed_thing.type == "object") then - local obj = pointed_thing.ref - local luaentity = obj:get_luaentity() - - if luaentity ~= nil and luaentity.yl_speak_up == nil then return itemstack end -- Is the thing we clicked really one of our NPCs? - - yl_speak_up.fashion(user, obj) - local npc = luaentity.yl_speak_up.id - minetest.chat_send_player(p_name,"NPC with ID n_"..npc.." will redress at pos "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - minetest.log("action","[MOD] yl_speak_up: NPC with ID n_"..npc.." will redress at pos "..minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) - return itemstack - end + yl_speak_up.fashion(user, obj) + minetest.chat_send_player(p_name,"NPC with ID n_"..npc.." will redress at pos ".. + minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) + minetest.log("action","[MOD] yl_speak_up: NPC with ID n_"..npc.." will redress at pos ".. + minetest.pos_to_string(obj:get_pos(),0).." on command of "..p_name) + return itemstack end -}) \ No newline at end of file +})