More powerful item movement util function

This commit is contained in:
Wuzzy 2017-02-14 00:44:23 +01:00
parent 23a6d123bc
commit 77c458e4fa
2 changed files with 45 additions and 15 deletions

View File

@ -98,7 +98,14 @@ function mcl_util.rotate_axis(itemstack, placer, pointed_thing)
end
-- Moves a single item from one inventory to another
--- source_inventory: Inventory to take the item from
--- source_list: List name of the source inventory from which to take the item
--- source_stack_id: The inventory position ID of the source inventory to take the item from
--- destination_inventory: Put item into this inventory
--- destination_list: List name of the destination inventory to which to put the item into
-- Returns true on success and false on failure
-- Possible failures: No item in source slot, destination inventory full
function mcl_util.move_item(source_inventory, source_list, source_stack_id, destination_inventory, destination_list)
if not source_inventory:is_empty(source_list) then
local stack = source_inventory:get_stack(source_list, source_stack_id)
@ -115,3 +122,35 @@ function mcl_util.move_item(source_inventory, source_list, source_stack_id, dest
end
return false
end
-- Moves a single item from one container node into another.
--- source_pos: Position ({x,y,z}) of the node to take the item from
--- source_list: List name of the source inventory from which to take the item
--- source_stack_id: The inventory position ID of the source inventory to take the item from
--- destination_pos: Position ({x,y,z}) of the node to put the item into
-- Returns true on success and false on failure
function mcl_util.move_item_container(source_pos, source_list, source_stack_id, destination_pos)
local smeta = minetest.get_meta(source_pos)
local dmeta = minetest.get_meta(destination_pos)
local sinv = smeta:get_inventory()
local dinv = dmeta:get_inventory()
local snodedef = minetest.registered_nodes[minetest.get_node(source_pos).name]
local dnodedef = minetest.registered_nodes[minetest.get_node(destination_pos).name]
-- If it's a container, put it into the container
if dnodedef.groups.container then
if dnodedef.groups.container == 2 or snodedef.groups.continer == 3 then
return mcl_util.move_item(sinv, source_list, source_stack_id, dinv, "main")
elseif dnodedef.groups.container == 3 then
local stack = sinv:get_stack(source_list, source_stack_id)
if stack and (not stack:is_empty()) and (not minetest.registered_nodes[stack:get_name()].groups.shulker_box) then
return mcl_util.move_item(sinv, source_list, source_stack_id, dinv, "main")
end
elseif dnodedef.groups.container == 4 then
return mcl_util.move_item(sinv, source_list, source_stack_id, dinv, "src")
end
end
return false
end

View File

@ -62,21 +62,12 @@ minetest.register_node("mcl_dropper:dropper", {
local stack = stacks[r].stack
local dropitem = ItemStack(stack:get_name())
local stack_id = stacks[r].stackpos
-- If it's a container, put it into the container
if dropnodedef.groups.container then
local dropmeta = minetest.get_meta(droppos)
local dropinv = dropmeta:get_inventory()
if dropnodedef.groups.container == 2 then
mcl_util.move_item(inv, "main", stack_id, dropinv, "main")
elseif dropnodedef.groups.container == 3 then
if not minetest.registered_nodes[stack:get_name()].groups.shulker_box then
mcl_util.move_item(inv, "main", stack_id, dropinv, "main")
end
elseif dropnodedef.groups.container == 4 then
mcl_util.move_item(inv, "main", stack_id, dropinv, "src")
end
else
-- Drop item normally
-- If it's a container, attempt to put it into the container
local dropped = mcl_util.move_item_container(pos, "main", stack_id, droppos)
-- No container?
if not dropped and not dropnodedef.groups.container then
-- Drop item normally
minetest.add_item(droppos, dropitem)
stack:take_item()
inv:set_stack("main", stack_id, stack)