From 3f85f83e35a18d8e4ac5c2eb4ca799fc1920a449 Mon Sep 17 00:00:00 2001 From: Novatux Date: Sun, 2 Nov 2014 09:35:43 +0100 Subject: [PATCH] Make items in autocrafters virtual as well --- autocrafter.lua | 61 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/autocrafter.lua b/autocrafter.lua index b2e50c1..a6d8ad1 100644 --- a/autocrafter.lua +++ b/autocrafter.lua @@ -77,6 +77,17 @@ local function autocraft(inventory, pos) end end +local function update_autocrafter(pos) + local meta = minetest.get_meta(pos) + if meta:get_string("virtual_items") == "" then + meta:set_string("virtual_items", "1") + local inv = meta:get_inventory() + for _, stack in ipairs(inv:get_list("recipe")) do + minetest.item_drop(stack, "", pos) + end + end +end + minetest.register_node("pipeworks:autocrafter", { description = "Autocrafter", drawtype = "normal", @@ -103,15 +114,18 @@ minetest.register_node("pipeworks:autocrafter", { "list[current_name;dst;4,0;4,3;]".. "list[current_player;main;0,7;8,4;]") meta:set_string("infotext", "Autocrafter") + meta:set_string("virtual_items", "1") local inv = meta:get_inventory() inv:set_size("src", 3*8) inv:set_size("recipe", 3*3) inv:set_size("dst", 4*3) - end, + end, + on_punch = update_autocrafter, can_dig = function(pos, player) - local meta = minetest.get_meta(pos); + update_autocrafter(pos) + local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - return (inv:is_empty("src") and inv:is_empty("recipe") and inv:is_empty("dst")) + return (inv:is_empty("src") and inv:is_empty("dst")) end, after_place_node = function(pos) pipeworks.scan_for_tube_objects(pos) @@ -119,7 +133,46 @@ minetest.register_node("pipeworks:autocrafter", { after_dig_node = function(pos) pipeworks.scan_for_tube_objects(pos) autocrafterCache[minetest.hash_node_position(pos)] = nil - end + end, + allow_metadata_inventory_put = function(pos, listname, index, stack, player) + update_autocrafter(pos) + local inv = minetest.get_meta(pos):get_inventory() + if listname == "recipe" then + local stack_copy = ItemStack(stack) + stack_copy:set_count(1) + inv:set_stack(listname, index, stack_copy) + return 0 + else + return stack:get_count() + end + end, + allow_metadata_inventory_take = function(pos, listname, index, stack, player) + update_autocrafter(pos) + local inv = minetest.get_meta(pos):get_inventory() + if listname == "recipe" then + inv:set_stack(listname, index, ItemStack("")) + return 0 + else + return stack:get_count() + end + end, + allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) + update_autocrafter(pos) + local inv = minetest.get_meta(pos):get_inventory() + local stack = inv:get_stack(from_list, from_index) + stack:set_count(count) + if from_list == "recipe" then + inv:set_stack(from_list, from_index, ItemStack("")) + return 0 + elseif to_list == "recipe" then + local stack_copy = ItemStack(stack) + stack_copy:set_count(1) + inv:set_stack(to_list, to_index, stack_copy) + return 0 + else + return stack:get_count() + end + end, }) minetest.register_abm({nodenames = {"pipeworks:autocrafter"}, interval = 1, chance = 1,