take items back into npc inventory when npc_gives failed instead of forgetting them

This commit is contained in:
Sokomine 2024-12-15 14:36:19 +01:00
parent bfeed37767
commit 0316afdfa9
2 changed files with 24 additions and 1 deletions

View File

@ -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

View File

@ -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)