From 0316afdfa9bbe40eb08ae23910b7f0af62423a1f Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sun, 15 Dec 2024 14:36:19 +0100 Subject: [PATCH] take items back into npc inventory when npc_gives failed instead of forgetting them --- exec_actions.lua | 21 ++++++++++++++++++++- fs/fs_action_npc_gives.lua | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/exec_actions.lua b/exec_actions.lua index 90130f4..250468a 100644 --- a/exec_actions.lua +++ b/exec_actions.lua @@ -312,6 +312,22 @@ yl_speak_up.get_action_by_player = function(player) end +-- did the NPC try to give something to the player already - and the player didn't take it? +-- then give that old item back to the NPC +yl_speak_up.action_take_back_failed_npc_gives = function(trade_inv, npc_inv) + if(not(trade_inv) or not(npc_inv)) then + return + end + local last_stack = trade_inv:get_stack("npc_gives", 1) + if(not(last_stack:is_empty())) then + -- strip any metadata to avoid stacking problems + npc_inv:add_item("npc_main", last_stack:get_name().." "..last_stack:get_count()) + -- clear the stack + trade_inv:set_stack("npc_gives", 1, "") + end +end + + -- Create the quest item by taking a raw item (i.e. a general piece of paper) out -- of the NPC's inventory, applying a description (if given) and quest id (if -- given); place the quest item in the trade inv of the player in the npc_gives slot. @@ -331,6 +347,10 @@ yl_speak_up.action_quest_item_prepare = function(player) local stack = ItemStack(a.a_value) -- get the inventory of the NPC local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(n_id)}) + + local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname}) + yl_speak_up.action_take_back_failed_npc_gives(trade_inv, npc_inv) + -- does the NPC have the item we are looking for? if(not(npc_inv:contains_item("npc_main", stack))) then local o_id = yl_speak_up.speak_to[pname].o_id @@ -360,7 +380,6 @@ yl_speak_up.action_quest_item_prepare = function(player) -- put the stack in the npc_gives-slot of the trade inventory of the player -- (as that slot is managed by the NPC alone we don't have to worry about -- anything else in the slot) - local trade_inv = minetest.get_inventory({type="detached", name="yl_speak_up_player_"..pname}) -- actually put the stack in there trade_inv:set_stack("npc_gives", 1, new_stack) return true diff --git a/fs/fs_action_npc_gives.lua b/fs/fs_action_npc_gives.lua index e775f15..53a255c 100644 --- a/fs/fs_action_npc_gives.lua +++ b/fs/fs_action_npc_gives.lua @@ -33,6 +33,10 @@ yl_speak_up.input_fs_action_npc_gives = function(player, formname, fields) -- the npc_gives slot does not accept input - so we don't have to check for any misplaced items -- but if the player aborts, give the item back to the NPC if(fields.back_to_talk) then + -- actually take the item back into the NPC's inventory + local n_id = yl_speak_up.speak_to[pname].n_id + local npc_inv = minetest.get_inventory({type="detached", name="yl_speak_up_npc_"..tostring(n_id)}) + yl_speak_up.action_take_back_failed_npc_gives(trade_inv, npc_inv) -- strip the quest item info from the stack (so that it may stack again) -- and give that (hopefully) stackable stack back to the NPC yl_speak_up.action_quest_item_take_back(player)