Trading: Fix price items not being accounted for.

This commit is contained in:
Zorman2000 2017-03-27 10:53:21 -04:00
parent 626ca68eb5
commit d18d1ce897
1 changed files with 45 additions and 41 deletions

View File

@ -522,18 +522,19 @@ function npc.trade.perform_trade(self, player_name, offer)
-- Check player has the item being buyed -- Check player has the item being buyed
if inv:contains_item("main", item_stack) then if inv:contains_item("main", item_stack) then
-- Check if there is enough room to add the price item to player -- Check if there is enough room to add the price item to player
if inv:room_for_item("main", price_stack) then for i = 1, #price_stacks do
if inv:room_for_item("main", price_stacks[i]) then
-- Remove item from player -- Remove item from player
inv:remove_item("main", item_stack) inv:remove_item("main", item_stack)
-- Remove price item(s) from NPC -- Remove price item(s) from NPC
for i = 1, #price_stacks do for j = 1, #price_stacks do
npc.take_item_from_inventory_itemstring(self, price_stacks[i]) npc.take_item_from_inventory_itemstring(self, price_stacks[j])
end end
-- Add item to NPC's inventory -- Add item to NPC's inventory
npc.add_item_to_inventory_itemstring(self, offer.item) npc.add_item_to_inventory_itemstring(self, offer.item)
-- Add price items to player -- Add price items to player
for i = 1, #price_stacks do for j = 1, #price_stacks do
inv:add_item("main", price_stacks[i]) inv:add_item("main", price_stacks[j])
end end
-- Send message to player -- Send message to player
minetest.chat_send_player(player_name, "Thank you!") minetest.chat_send_player(player_name, "Thank you!")
@ -543,6 +544,7 @@ function npc.trade.perform_trade(self, player_name, offer)
"Looks like you can't get what I'm giving you for payment!") "Looks like you can't get what I'm giving you for payment!")
return false return false
end end
end
else else
minetest.chat_send_player(player_name, "Looks like you don't have what I want to buy...") minetest.chat_send_player(player_name, "Looks like you don't have what I want to buy...")
return false return false
@ -550,13 +552,14 @@ function npc.trade.perform_trade(self, player_name, offer)
else else
-- If NPC is selling to the player, then player gives price and gets -- If NPC is selling to the player, then player gives price and gets
-- item, NPC loses item and gets price. -- item, NPC loses item and gets price.
for i = 1, #price_stacks do
-- Check NPC has the required item to pay -- Check NPC has the required item to pay
if inv:contains_item("main", price_stack) then if inv:contains_item("main", price_stacks[i]) then
-- Check if there is enough room to add the item to player -- Check if there is enough room to add the item to player
if inv:room_for_item("main", item_stack) then if inv:room_for_item("main", item_stack) then
-- Remove price item from player -- Remove price item from player
for i = 1, #price_stacks do for j = 1, #price_stacks do
inv:remove_item("main", price_stacks[i]) inv:remove_item("main", price_stacks[j])
end end
-- Remove sell item from NPC -- Remove sell item from NPC
npc.take_item_from_inventory_itemstring(self, offer.item) npc.take_item_from_inventory_itemstring(self, offer.item)
@ -578,6 +581,7 @@ function npc.trade.perform_trade(self, player_name, offer)
return false return false
end end
end end
end
end end
-- Handler for chat formspec -- Handler for chat formspec