diff --git a/inventory.lua b/inventory.lua index 26c3e8d..2fd6c53 100644 --- a/inventory.lua +++ b/inventory.lua @@ -112,6 +112,39 @@ yl_speak_up.save_npc_inventory = function( n_id ) end +-- helper function for yl_speak_up.load_npc_inventory and +-- minetest.register_on_joinplayer +yl_speak_up.inventory_allow_item = function(player, stack, input_to) + if(not(player) or not(stack) or not(input_to)) then + return 0 + end + local pname = player:get_player_name() + local n_id = yl_speak_up.speak_to[pname].n_id + if(not(n_id) or not(yl_speak_up.may_edit_npc(player, n_id))) then + return 0 + end + local error_msg = nil + if(stack:get_wear() > 0) then + error_msg = "Your NPC accepts only undammaged items.\n".. + "Trading dammaged items would be unfair." + -- items with metadata cannot be traded + elseif(yl_speak_up.check_stack_has_meta(player, stack)) then + error_msg = "Your NPC cannot sell items that contain\n".. + "additional (meta-) data." + end + if(error_msg) then + yl_speak_up.show_fs(player, "msg", { + input_to = input_to, + formspec = "size[6,2]".. + "label[0.2,-0.2;"..tostring(error_msg).."]".. + "button[2,1.5;1,0.9;back_from_error_msg;".. + "OK]"}) + return 0 + end + return stack:get_count() +end + + -- create and load the detached inventory in yl_speak_up.after_activate; -- direct access to this inventory is only possible for players with the right privs -- (this is an inventory for the *NPC*, which is stored to disk sometimes) @@ -135,28 +168,8 @@ yl_speak_up.load_npc_inventory = function(n_id) -- Return value: number of items allowed to move. allow_put = function(inv, listname, index, stack, player) - if(not(yl_speak_up.may_edit_npc(player, n_id))) then - return 0 - end - local error_msg = nil - if(stack:get_wear() > 0) then - error_msg = "Your NPC accepts only undammaged items.\n".. - "Trading dammaged items would be unfair." - -- items with metadata cannot be traded - elseif(yl_speak_up.check_stack_has_meta(player, stack)) then - error_msg = "Your NPC cannot sell items that contain\n".. - "additional (meta-) data." - end - if(error_msg) then - yl_speak_up.show_fs(player, "msg", { - input_to = "yl_speak_up:inventory", - formspec = "size[6,2]".. - "label[0.2,-0.2;"..tostring(error_msg).."]".. - "button[2,1.5;1,0.9;back_from_error_msg;".. - "OK]"}) - return 0 - end - return stack:get_count() + -- check if player can edit NPC, item is undammaged and contains no metadata + return yl_speak_up.inventory_allow_item(player, stack, "yl_speak_up:inventory") end, -- Called when a player wants to put something into the inventory. -- Return value: number of items allowed to put. diff --git a/trade_simple.lua b/trade_simple.lua index 898a87b..2856cd2 100644 --- a/trade_simple.lua +++ b/trade_simple.lua @@ -722,19 +722,9 @@ minetest.register_on_joinplayer(function(player, last_login) -- do not allow used items or items with metadata in the setup slots -- (they can't really be traded later on anyway) if(listname == "setup") then - if(stack:get_wear() > 0) then - minetest.chat_send_player(player:get_player_name(), - "Your NPC accepts only undammaged items. ".. - "Trading dammaged items would be unfair.") - return 0 - end - -- items with metadata cannot be traded - if(yl_speak_up.check_stack_has_meta(player, stack)) then - minetest.chat_send_player(player:get_player_name(), - "Your NPC cannot sell items that contain ".. - "additional (meta-) data.") - return 0 - end + -- check if player can edit NPC, item is undammaged and contains no metadata + return yl_speak_up.inventory_allow_item(player, stack, + "yl_speak_up:add_trade_simple") end return stack:get_count() end,