Drop items from oversized stacks on the ground

This commit is contained in:
Andrey Kozlovskiy 2019-10-20 02:16:56 +03:00
parent 334f0a5065
commit dfe948ae0a
2 changed files with 22 additions and 13 deletions

View File

@ -315,18 +315,19 @@ Note that function accepts multiple source lists and destination list
can be one of the source lists. can be one of the source lists.
If destination list position is already occupied with some other item If destination list position is already occupied with some other item
then function tries to move it to the source lists if possible. then function tries to move it to the source lists or drop it to the
ground if possible.
Warning!!! Moving oversized stacks is undefined and can lead to item loss!
Arguments: Arguments:
inv: minetest inventory reference player: minetest player object
src_lists: names of source lists src_lists: names of source lists
dst_list: name of destination list dst_list: name of destination list
match_table: table of matched items match_table: table of matched items
amount: amount of items per every position amount: amount of items per every position
--]] --]]
function unified_inventory.move_match(inv, src_lists, dst_list, match_table, amount) function unified_inventory.move_match(player, src_lists, dst_list, match_table, amount)
local inv = player:get_inventory()
local item_drop = minetest.item_drop
local moved_positions = {} local moved_positions = {}
for item, pos_set in pairs(match_table) do for item, pos_set in pairs(match_table) do
@ -361,7 +362,11 @@ function unified_inventory.move_match(inv, src_lists, dst_list, match_table, amo
if not leftover:is_empty() then if not leftover:is_empty() then
inv:set_stack(dst_list, pos, leftover) inv:set_stack(dst_list, pos, leftover)
unified_inventory.add_item(inv, src_lists, current) local oversize = unified_inventory.add_item(inv, src_lists, current)
if not oversize:is_empty() then
item_drop(oversize, player, player:get_pos())
end
end end
end end
@ -370,7 +375,11 @@ function unified_inventory.move_match(inv, src_lists, dst_list, match_table, amo
-- Pass 4: Re-add remainder stacks -- Pass 4: Re-add remainder stacks
for _, current in pairs(remained) do for _, current in pairs(remained) do
unified_inventory.add_item(inv, src_lists, current) local oversize = unified_inventory.add_item(inv, src_lists, current)
if not oversize:is_empty() then
item_drop(oversize, player, player:get_pos())
end
end end
end end
@ -389,13 +398,15 @@ This is used to indicate "craft All" case.
Note that function accepts multiple source lists. Note that function accepts multiple source lists.
Arguments: Arguments:
inv: minetest inventory reference player: minetest player object
src_lists: names of source lists src_lists: names of source lists
dst_list: name of destination list dst_list: name of destination list
craft: minetest craft recipe craft: minetest craft recipe
amount: desired amount of output items amount: desired amount of output items
--]] --]]
function unified_inventory.craftguide_match_craft(inv, src_lists, dst_list, craft, amount) function unified_inventory.craftguide_match_craft(player, src_lists, dst_list, craft, amount)
local inv = player:get_inventory()
local counts = unified_inventory.count_items(inv, src_lists) local counts = unified_inventory.count_items(inv, src_lists)
local positions = unified_inventory.count_craft_positions(craft) local positions = unified_inventory.count_craft_positions(craft)
local match_table, match_count = unified_inventory.match_items(counts, positions) local match_table, match_count = unified_inventory.match_items(counts, positions)
@ -408,5 +419,5 @@ function unified_inventory.craftguide_match_craft(inv, src_lists, dst_list, craf
amount = match_count amount = match_count
end end
unified_inventory.move_match(inv, src_lists, dst_list, match_table, amount) unified_inventory.move_match(player, src_lists, dst_list, match_table, amount)
end end

View File

@ -456,8 +456,6 @@ local function craftguide_craft(player, formname, fields)
local output = unified_inventory.current_item[player_name] or "" local output = unified_inventory.current_item[player_name] or ""
if output == "" then return end if output == "" then return end
local player_inv = player:get_inventory()
local crafts = unified_inventory.crafts_for[ local crafts = unified_inventory.crafts_for[
unified_inventory.current_craft_direction[player_name]][output] or {} unified_inventory.current_craft_direction[player_name]][output] or {}
if #crafts == 0 then return end if #crafts == 0 then return end
@ -467,7 +465,7 @@ local function craftguide_craft(player, formname, fields)
local craft = crafts[alternate] local craft = crafts[alternate]
if craft.width > 3 then return end if craft.width > 3 then return end
unified_inventory.craftguide_match_craft(player_inv, {"main", "craft"}, "craft", craft, amount) unified_inventory.craftguide_match_craft(player, {"main", "craft"}, "craft", craft, amount)
unified_inventory.set_inventory_formspec(player, "craft") unified_inventory.set_inventory_formspec(player, "craft")
end end