From 23a6d123bcb8545818144fe00485f8f4e48cef34 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 14 Feb 2017 00:10:37 +0100 Subject: [PATCH] Allow droppers to put items into containers --- mods/mcl_util/init.lua | 18 +++++++++++++++++ mods/redstone/mcl_dropper/depends.txt | 1 + mods/redstone/mcl_dropper/init.lua | 29 +++++++++++++++++++++------ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/mods/mcl_util/init.lua b/mods/mcl_util/init.lua index e24bdf45..83b3bb6b 100644 --- a/mods/mcl_util/init.lua +++ b/mods/mcl_util/init.lua @@ -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 diff --git a/mods/redstone/mcl_dropper/depends.txt b/mods/redstone/mcl_dropper/depends.txt index acaa9241..95222145 100644 --- a/mods/redstone/mcl_dropper/depends.txt +++ b/mods/redstone/mcl_dropper/depends.txt @@ -1 +1,2 @@ mesecons +mcl_util diff --git a/mods/redstone/mcl_dropper/init.lua b/mods/redstone/mcl_dropper/init.lua index 9b362522..041da50a 100644 --- a/mods/redstone/mcl_dropper/init.lua +++ b/mods/redstone/mcl_dropper/init.lua @@ -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 }}