make sure items are given back when setting wielded items via inventory slots

This commit is contained in:
Sokomine 2021-07-27 19:28:45 +02:00
parent ea276c04d8
commit 8367a60c0f

View File

@ -243,6 +243,24 @@ yl_speak_up.get_fs_fashion = function(pname)
return table.concat(formspec, "")
end
yl_speak_up.fashion_wield_give_items_back = function(player, pname)
-- move the item back to the player's inventory (if possible)
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
local player_inv = player:get_inventory()
local left_stack = trade_inv:get_stack("wield", 1)
local right_stack = trade_inv:get_stack("wield", 2)
if(left_stack and not(left_stack:is_empty())
and player_inv:add_item("main", left_stack)) then
trade_inv:set_stack("wield", 1, "")
end
if(right_stack and not(right_stack:is_empty())
and player_inv:add_item("main", right_stack)) then
trade_inv:set_stack("wield", 2, "")
end
end
yl_speak_up.input_fashion = function(player, formname, fields)
if formname ~= "yl_speak_up:fashion" then
return
@ -261,6 +279,7 @@ yl_speak_up.input_fashion = function(player, formname, fields)
-- catch ESC as well
if(not(fields) or (fields.quit or fields.button_cancel or fields.button_exit)) then
yl_speak_up.fashion_wield_give_items_back(player, pname)
yl_speak_up.show_fs(player, "talk", {n_id = yl_speak_up.speak_to[pname].n_id,
d_id = yl_speak_up.speak_to[pname].d_id})
return
@ -268,6 +287,7 @@ yl_speak_up.input_fashion = function(player, formname, fields)
elseif(fields.button_wield_left
or fields.button_wield_right) then
local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname})
local player_inv = player:get_inventory()
local left_stack = trade_inv:get_stack("wield", 1)
local right_stack = trade_inv:get_stack("wield", 2)
if(left_stack and left_stack:get_name() and fields.button_wield_left) then
@ -280,6 +300,7 @@ yl_speak_up.input_fashion = function(player, formname, fields)
yl_speak_up.log_change(pname, n_id,
"(fashion) shield changed to "..tostring(fields.set_shield)..".")
end
yl_speak_up.fashion_wield_give_items_back(player, pname)
-- only change cape if there really is a diffrent one selected
elseif(fields.set_cape and fields.set_cape ~= textures[1]) then
@ -317,6 +338,7 @@ yl_speak_up.input_fashion = function(player, formname, fields)
yl_speak_up.log_change(pname, n_id,
"(fashion) saved changes.")
end
yl_speak_up.fashion_wield_give_items_back(player, pname)
end
yl_speak_up.show_fs(player, "fashion")
end