split api_trade_inv.lua into edit_mode and normal mode

This commit is contained in:
Sokomine 2024-02-09 05:35:34 +01:00
parent d10b394204
commit e7369f3075
3 changed files with 42 additions and 7 deletions

View File

@ -116,12 +116,7 @@ yl_speak_up.trade_inv_allow_put = function(inv, listname, index, stack, player)
end
-- allow putting something in in edit mode - but not otherwise
if(listname == "npc_gives") then
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
-- only in edit mode! else the NPC manages this slot
if(not(n_id) or yl_speak_up.edit_mode[pname] ~= n_id) then
return 0
end
return 0
end
return stack:get_count()
end
@ -170,7 +165,7 @@ yl_speak_up.trade_inv_on_take = function(inv, listname, index, stack, player)
-- but only if not in edit mode
if(trade and trade.trade_done > 0
and not(trade.trade_is_trade_list)
and yl_speak_up.edit_mode[pname] ~= trade.n_id) then
and not(trade.dry_run_no_exec)) then
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
-- return surplus items from the pay slot
local pay = trade_inv:get_stack("pay", 1)

View File

@ -147,6 +147,7 @@ yl_speak_up.add_to_command_help_text = yl_speak_up.add_to_command_help_text..
-- dofile(modpath .. "fs/fs_edit_trade_limit.lua")
-- -- trade one item(stack) against one other item(stack)
-- dofile(modpath .. "api/api_trade_inv.lua")
dofile(modpath .. "trade_in_edit_mode.lua")
-- dofile(modpath .. "fs/fs_do_trade_simple.lua")
-- handle back button diffrently when editing a trade as an action:
dofile(modpath .. "fs/fs_do_trade_simple_in_edit_mode.lua")

View File

@ -0,0 +1,39 @@
-- overrides for api/api_trade_inv.lua:
-- the player *can* place something into the npc_gives inventory list in edit_mode:
local old_trade_inv_allow_put = yl_speak_up.trade_inv_allow_put
yl_speak_up.trade_inv_allow_put = function(inv, listname, index, stack, player)
if(not(player)) then
return 0
end
-- allow putting something in in edit mode - but not otherwise
if(listname and listname == "npc_gives") then
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
-- only in edit mode! else the NPC manages this slot
if(n_id and yl_speak_up.in_edit_mode(pname)) then
return stack:get_count()
end
end
return old_trade_inv_allow_put(inv, listname, index, stack, player)
end
-- prevent do_trade_simple from executing trade and reporting successful action:
local old_do_trade_simple = yl_speak_up.do_trade_simple
yl_speak_up.do_trade_simple = function(player, count)
if(not(player)) then
return
end
local pname = player:get_player_name()
-- which trade are we talking about?
local trade = yl_speak_up.trade[pname]
if(trade.n_id and yl_speak_up.edit_mode[pname] == trade.n_id) then
-- instruct old_do_trade_simple to neither execute the trade nor see this
-- as an action that was executed
trade.dry_run_no_exec = true
end
return old_do_trade_simple(player, count)
end