From a074e052c9b220efab6a69af002eb20b873e43a5 Mon Sep 17 00:00:00 2001 From: D4VID Date: Sun, 6 Oct 2024 11:59:39 +0200 Subject: [PATCH] Storing items in inventory --- craft.lua | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/craft.lua b/craft.lua index b36d927..fa7b87a 100644 --- a/craft.lua +++ b/craft.lua @@ -26,6 +26,62 @@ local slot_map = { 9,10,11 } +-- local function read_chest_contents() +-- local size = inventory.getInventorySize(sides.forward) +-- for slot = 1, size do +-- local stack = inventory.getStackInSlot(sides.forward, slot) +-- if stack then +-- if stack.name == item then +-- inventory.suckFromSlot(sides.forward, slot, 1) +-- return true +-- end +-- end +-- end +-- end + +-- store the item in the selected slot into the facing inventory +local function store_item() +-- check if that item is present in the inventory + local item = inventory.getStackInInternalSlot() + + local size = inventory.getInventorySize(sides.forward) + local empty_slot = -1 + for slot = 1, size do + local stack = inventory.getStackInSlot(sides.forward, slot) + if stack then + if stack.name == item.name then + inventory.dropIntoSlot(sides.forward, slot) + -- check if all items have been stored + local remaining = inventory.getStackInInternalSlot() + if not remaining then + print('Stored successfully') + return true + end + end + else + if empty_slot == -1 then + empty_slot = slot + end + end + end + + -- item not present in inventory + if empty_slot ~= -1 then + -- store in first empty slot + local result, error = inventory.dropIntoSlot(sides.forward, empty_slot) + if not result then + print('Cannot store item: ' .. error .. "\n") + return false + else + print('Stored successfully') + return true + end + else + print('Cannot store item - Inventory full') + return false + end +end + -- Search the facing chest for "item" and take "count" items and place them in "slot" -- Returns number of items fetched local function fetch_item(item, output_slot) @@ -75,4 +131,5 @@ end -- return -- end -craft_recipe(iron_pickaxe_recipe) +-- craft_recipe(iron_pickaxe_recipe) +store_item()