0
0
Fork 0

Allow droppers to put items into containers

This commit is contained in:
Wuzzy 2017-02-14 00:10:37 +01:00
parent d6a404ce92
commit 23a6d123bc
3 changed files with 42 additions and 6 deletions

View File

@ -97,3 +97,21 @@ function mcl_util.rotate_axis(itemstack, placer, pointed_thing)
return itemstack
end
-- Moves a single item from one inventory to another
-- Returns true on success and false on failure
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)
local item = stack:get_name()
if not stack:is_empty() then
if not destination_inventory:room_for_item(destination_list, item) then
return false
end
stack:take_item()
source_inventory:set_stack(source_list, source_stack_id, stack)
destination_inventory:add_item(destination_list, item)
return true
end
end
return false
end

View File

@ -1 +1,2 @@
mesecons
mcl_util

View File

@ -45,9 +45,9 @@ minetest.register_node("mcl_dropper:dropper", {
local inv = meta:get_inventory()
local droppos = vector.subtract(pos, minetest.facedir_to_dir(node.param2))
local dropnode = minetest.get_node(droppos)
-- Do not drop into solid nodes
if minetest.registered_nodes[dropnode.name].walkable then
-- TODO: Drop into container
-- Do not drop into solid nodes, unless they are containers
local dropnodedef = minetest.registered_nodes[dropnode.name]
if dropnodedef.walkable and not dropnodedef.groups.container then
return
end
local stacks = {}
@ -61,9 +61,26 @@ minetest.register_node("mcl_dropper:dropper", {
local r = math.random(1, #stacks)
local stack = stacks[r].stack
local dropitem = ItemStack(stack:get_name())
minetest.add_item(droppos, dropitem)
stack:take_item()
inv:set_stack("main", stacks[r].stackpos, stack)
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
minetest.add_item(droppos, dropitem)
stack:take_item()
inv:set_stack("main", stack_id, stack)
end
end
end
}}