From 2e2ede2ebf3321f4b26fe263b4e52ea8452d0744 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 23 May 2021 01:44:01 +0200 Subject: [PATCH] added yl_speak_up.input_add_trade_simple --- trade_simple.lua | 106 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/trade_simple.lua b/trade_simple.lua index 4d6ca68..97e1f40 100644 --- a/trade_simple.lua +++ b/trade_simple.lua @@ -5,7 +5,89 @@ yl_speak_up.trade_fail_fs = "size[6,2]".. "label[0.2,0.5;Ups! The trade is not possible.\nPlease notify an admin.]".. "button_exit[2,1.5;1,0.9;exit;Exit]" --- TODO: when closing the formspec: give the items in the pay slot back + +-- the player wants to add a simple trade; handle formspec input +yl_speak_up.input_add_trade_simple = function(player, formname, fields) + if(not(player)) then + return 0 + end + local pname = player:get_player_name() + -- the trade can only be changed in edit mode + if(yl_speak_up.edit_mode[pname] ~= yl_speak_up.speak_to[pname].n_id + or not(yl_speak_up.speak_to[pname].n_id)) then + return 0 + end + + -- we return from showing an error message (the player may not have noticed + -- a chat message while viewing a formspec) + if(fields.back_from_error_msg) then + minetest.show_formspec(pname, "yl_speak_up:add_trade_simple", + yl_speak_up.get_fs_trade_simple(player)) + return + end + + -- which trade are we talking about? + local trade = yl_speak_up.trade[pname] + + local d_id = yl_speak_up.speak_to[pname].d_id + local n_id = yl_speak_up.speak_to[pname].n_id + + -- this also contains the inventory list "setup" where the player placed the items + local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname}) + + -- fields.abort_trade_simple can be ignored as it is similar to ESC + + local pay = trade_inv:get_stack("setup", 1) + local buy = trade_inv:get_stack("setup", 2) + + -- store the new trade + if(fields.store_trade_simple) then + local error_msg = "" + -- check for error conditions + if(pay:is_empty()) then + error_msg = "What shall the customer pay?\nWe don't give away stuff for free here!" + elseif(buy:is_empty()) then + error_msg = "What shall your NPC sell?\nCustomers won't pay for nothing!" + elseif(pay:get_wear() > 0 or buy:get_wear() > 0) then + error_msg = "Selling used items is not possible." + elseif(not(minetest.registered_items[ pay:get_name() ]) + or not(minetest.registered_items[ buy:get_name() ])) then + error_msg = "Unkown items cannot be traded." + elseif(pay:get_name() == buy:get_name()) then + error_msg = "Selling *and* buying the same item\nat the same time makes no sense." + else + -- TODO: *can* we store the trade? + -- TODO no trade stored for that option yet? + error_msg = "So far so good!" + end + -- show error message (that leads back to this formspec) + if(error_msg) then + minetest.show_formspec(pname, "yl_speak_up:add_trade_simple", + "size[6,2]".. + "label[0.2,0.5;"..error_msg.."]".. + "button[2,1.5;1,0.9;back_from_error_msg;Back]") + return + end + end + + -- give the items back to the player (he took them from his inventory and had no + -- real chance to put them elsewhere - so there really ought to be room enough) + local player_inv = player:get_inventory() + if( player_inv:room_for_item("main", pay)) then + player_inv:add_item("main", pay) + trade_inv:set_stack("setup", 1, "") + end + if( player_inv:room_for_item("main", buy)) then + player_inv:add_item("main", buy) + trade_inv:set_stack("setup", 2, "") + end + + -- TODO: or show the trade we just stored directly? + -- ..and go back to the edit options formspec + minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog", + yl_speak_up.get_fs_edit_option_dialog(player, n_id, d_id, trade.o_id)) +end + -- can this trade be made? called in allow_take yl_speak_up.can_trade_simple = function(player, count) @@ -140,8 +222,9 @@ yl_speak_up.get_fs_trade_simple = function(player, player_gives, npc_gives) -- the NPCs' inventory local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(trade.n_id)}) - -- show edit button for the owner - if(yl_speak_up.may_edit_npc(player, trade.n_id)) then + -- show edit button for the owner if in edit_mode + if(yl_speak_up.may_edit_npc(player, trade.n_id) + and (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id)) then formspec = formspec.. "button[0.2,1.6;0.8,0.9;edit_trade_simple;Edit]".. "tooltip[edit_trade_simple;Edit this trade. You can do so only ".. @@ -242,6 +325,23 @@ minetest.register_on_joinplayer(function(player, last_login) if(listname == "buy") then return 0 end + -- 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 + end return stack:get_count() end, allow_take = function(inv, listname, index, stack, player)