0
0
Fork 0

Simplify mcl_util.move_item_container

This commit is contained in:
Wuzzy 2017-08-04 02:19:47 +02:00
parent fb261454df
commit 8388235600
3 changed files with 23 additions and 28 deletions

View File

@ -163,19 +163,18 @@ end
-- Moves a single item from one container node into another. Performs a variety of high-level
-- checks to prevent invalid transfers such as shulker boxes into shulker boxes
--- source_pos: Position ({x,y,z}) of the node to take the item from
--- source_list: (optional) List name of the source inventory from which to take the item. Default is normally "main"; "dst" for furnace
--- source_stack_id: The inventory position ID of the source inventory to take the item from (-1 for first occupied slot)
--- destination_pos: Position ({x,y,z}) of the node to put the item into
--- destination_list: (optional) list name of the destination inventory. Default is normalls "main"; "dst" for furnace
-- Returns true on success and false on failure
function mcl_util.move_item_container(source_pos, source_list, source_stack_id, destination_pos, destination_list)
--- source_list (optional): List name of the source inventory from which to take the item. Default is normally "main"; "dst" for furnace
--- source_stack_id (optional): The inventory position ID of the source inventory to take the item from (-1 for first occupied slot; -1 is default)
--- destination_list (optional): List name of the destination inventory. Default is normally "main"; "src" for furnace
-- Returns true on success and false on failure.
function mcl_util.move_item_container(source_pos, destination_pos, source_list, source_stack_id, destination_list)
local dpos = table.copy(destination_pos)
local snode = minetest.get_node(source_pos)
local dnode = minetest.get_node(destination_pos)
local dctype = minetest.get_item_group(dnode.name, "container")
local sctype = minetest.get_item_group(dnode.name, "container")
local sctype = minetest.get_item_group(snode.name, "container")
-- Normalize double container by forcing to always use the left segment first
if dctype == 6 then
@ -204,10 +203,16 @@ function mcl_util.move_item_container(source_pos, source_list, source_stack_id,
source_list = "main"
-- Furnace: output
elseif sctype == 4 then
destination_list = "dst"
source_list = "dst"
-- Unknown source container type. Bail out
else
return false
end
end
if not source_stack_id then
source_stack_id = -1
end
if source_stack_id == -1 then
source_stack_id = mcl_util.get_first_occupied_inventory_slot(sinv, source_list)
if source_stack_id == nil then

View File

@ -80,7 +80,7 @@ local dropperdef = {
local stack_id = stacks[r].stackpos
-- If it's a container, attempt to put it into the container
local dropped = mcl_util.move_item_container(pos, "main", stack_id, droppos)
local dropped = mcl_util.move_item_container(pos, droppos, nil, stack_id)
-- No container?
if not dropped and not dropnodedef.groups.container then
-- Drop item normally

View File

@ -285,17 +285,13 @@ minetest.register_abm({
local upnode = minetest.get_node(uppos)
if not minetest.registered_nodes[upnode.name] then return end
local g = minetest.registered_nodes[upnode.name].groups.container
if g == 2 or g == 3 or g == 5 or g == 6 then
-- Typical container inventory
mcl_util.move_item_container(uppos, "main", -1, pos)
elseif g == 4 then
-- Furnace output
mcl_util.move_item_container(uppos, "dst", -1, pos)
mcl_util.move_item_container(uppos, pos)
-- Also suck in non-fuel items from fuel slot
-- Also suck in non-fuel items from furnace fuel slot
if g == 4 then
local finv = minetest.get_inventory({type="node", pos=uppos})
if finv and not mcl_util.is_fuel(finv:get_stack("fuel", 1)) then
mcl_util.move_item_container(uppos, "fuel", -1, pos)
mcl_util.move_item_container(uppos, pos, "fuel")
end
end
@ -311,7 +307,7 @@ minetest.register_abm({
slot_id = get_eligible_transfer_item(sinv, "main", dinv, "main", is_not_shulker_box)
end
if slot_id then
mcl_util.move_item_container(pos, "main", slot_id, downpos)
mcl_util.move_item_container(pos, downpos, nil, slot_id)
end
end,
})
@ -344,25 +340,19 @@ minetest.register_abm({
local abovenode = minetest.get_node(above)
if not minetest.registered_nodes[abovenode.name] then return end
local g = minetest.registered_nodes[abovenode.name].groups.container
if g == 4 then
-- Furnace output
mcl_util.move_item_container(above, "dst", -1, pos)
else
-- Typical container inventory
mcl_util.move_item_container(above, "main", -1, pos)
end
mcl_util.move_item_container(above, pos)
-- Move an item from the hopper into the container to which the hopper points to
local g = minetest.registered_nodes[frontnode.name].groups.container
if g == 2 or g == 5 or g == 6 then
mcl_util.move_item_container(pos, "main", -1, front)
mcl_util.move_item_container(pos, front)
elseif g == 3 then
-- Put non-shulker boxes into shulker box
local sinv = minetest.get_inventory({type="node", pos = pos})
local dinv = minetest.get_inventory({type="node", pos = front})
local slot_id = get_eligible_transfer_item(sinv, "main", dinv, "main", is_not_shulker_box)
if slot_id then
mcl_util.move_item_container(pos, "main", slot_id, front)
mcl_util.move_item_container(pos, front, nil, slot_id)
end
elseif g == 4 then
-- Put fuel into fuel slot
@ -370,7 +360,7 @@ minetest.register_abm({
local dinv = minetest.get_inventory({type="node", pos = front})
local slot_id, stack = get_eligible_transfer_item(sinv, "main", dinv, "fuel", is_transferrable_fuel)
if slot_id then
mcl_util.move_item_container(pos, "main", slot_id, front, "fuel")
mcl_util.move_item_container(pos, front, nil, slot_id, "fuel")
end
end
end