From 4885b48fe85181e82765993575810c2955007f66 Mon Sep 17 00:00:00 2001 From: D4VID Date: Sat, 16 Nov 2024 22:08:16 +0100 Subject: [PATCH] Restock torches and pickaxe --- chest.lua | 22 ++++++++++++++++++++++ mine.lua | 54 +++++++++++++++++++++++++++++++++++++----------------- test.lua | 3 +++ 3 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 test.lua diff --git a/chest.lua b/chest.lua index 0b03d46..68e0e0d 100644 --- a/chest.lua +++ b/chest.lua @@ -44,6 +44,28 @@ local function store(whitelist) end end +---Take an item from the facing inventory and put it in the specified internal slot +---@param item string +---@param internal_slot number +---@return boolean success +local function take(item, internal_slot) + local slots = inventory.getInventorySize(sides.forward) + + for slot = 1, slots do + local stack = inventory.getStackInSlot(sides.forward, slot) + if stack then + if stack.name == item then + robot.select(internal_slot) + inventory.suckFromSlot(sides.forward, slot, robot.space()) -- take as many items to fill the slot + return true + end + end + end + + return false +end + return { store = store, + take = take, } diff --git a/mine.lua b/mine.lua index 977ce68..10c4879 100644 --- a/mine.lua +++ b/mine.lua @@ -2,6 +2,9 @@ local robot = require("robot") local nav = require("navigator") local chest = require("chest") local miner = require("miner") +local component = require("component") + +local inventory = component.inventory_controller; ORES = { 'minecraft:iron_ore', @@ -14,20 +17,37 @@ ORES = { print("Mining...") --- mine, starts facing east, goes east and scans south -miner.mine(32) --- ends facing "west" - -robot.turnRight() - --- store items -print("Storing ores") -chest.store(ORES) -robot.turnLeft() -nav.forward(2) -robot.turnRight() -print("Storing the rest") -chest.store() -robot.turnRight() -nav.forward(2) --- ends facing east +while true do + + -- mine, starts facing east, goes east and scans south + miner.mine(32) + -- ends facing "west" + + robot.turnRight() + + -- store items + print("Storing ores") + chest.store(ORES) + robot.turnLeft() + nav.forward(2) + robot.turnRight() + print("Storing the rest") + chest.store() + robot.turnRight() + nav.forward(2) + + -- restock + robot.turnLeft() + chest.take('minecraft:torch', 1) + local can_continue = chest.take('minecraft:iron_pickaxe', 2) + robot.turnRight() + + if not can_continue then + break + end + + robot.select(2) + inventory.equip() + + -- ends facing east +end diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..b810eb5 --- /dev/null +++ b/test.lua @@ -0,0 +1,3 @@ +local count = ... + +print(count) \ No newline at end of file