Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- User configurable
- local CHECK_DELAY = 60 -- Delay between each batch crafting operation.
- local BUFFER_DELAY_REMOVAL = 5 -- Time subtracted from the delay-between-crafting-operations per item that had to be stored in the buffer chest. Default case = 5 with 60 delay will mean 12 items in the buffer chest will cause the crafting operation to instantly resume.
- local INPUT_CHEST = "minecraft:shulker_box_0" -- The input chest's (or other inventory) name on the network.
- local OUTPUT_CHEST = "minecraft:shulker_box_1" -- The output chest's (or other inventory) name on the network.
- local BUFFER_CHEST = "minecraft:chest_1" -- A third chest (or other inventory) on the network. The turtle will move things here temporarily if the input inventory is full and it cannot push damaged items back into it.
- local ERROR_DELAY = 2 -- Delay between retries if an error occurs (ie: failing to move items to the output inventory)
- -- Don't touch
- local modem = peripheral.wrap("front") --[[@as WiredModem]]
- if not modem or not modem.getNameLocal or not modem.getNameLocal() then
- error("Expected activated wired modem in front!", 0)
- end
- local local_name = modem.getNameLocal() --[[@as string we have already verified this will return an appropriate value]]
- local inv = peripheral.wrap(INPUT_CHEST) --[[@as Inventory]]
- if not inv or not inv.list then
- error("Expected input inventory on network!", 0)
- end
- local output = peripheral.wrap(OUTPUT_CHEST) --[[@as Inventory]]
- if not output or not output.list then
- error("Expected output inventory to be on the network!", 0)
- end
- local buffer = peripheral.wrap(BUFFER_CHEST) --[[@as Inventory]]
- if not buffer or not buffer.list then
- error("Expected buffer inventory to be on the network!", 0)
- end
- --- Grab an item from the input inventory and move it to the turtle.
- ---@param slot integer The slot to pull from.
- local function get_item(slot)
- print("Pulling item from input slot", slot, "to the turtle.")
- return inv.pushItems(local_name, slot)
- end
- --- Move item in the turtle to the output chest, if fully repaired.
- ---@return boolean outputted If the item was outputted (ie: was fully repaired).
- local function attempt_output()
- for slot = 1, 16 do
- -- Need detailed mode for damage.
- local item = turtle.getItemDetail(slot, true) --[[@as turtleDetailsDetailed|nil]]
- if item then
- if item.damage == 0 then
- -- To the output chest!
- print("Pulling repaired item from turtle.")
- -- While we fail to move items, display an error.
- while output.pullItems(local_name, slot) == 0 do
- printError("Output chest is likely full. Cannot do anything until it is emptied!")
- sleep(ERROR_DELAY)
- end
- print("Successfully repaired and outputted an item.")
- return true
- end
- end
- end
- return false
- end
- local function force_output()
- for slot = 1, 16 do
- -- Need detailed mode for damage.
- local item = turtle.getItemDetail(slot, true) --[[@as turtleDetailsDetailed|nil]]
- if item then
- -- To the output chest!
- print("Pulling item from turtle to output chest.")
- while output.pullItems(local_name, slot) == 0 do
- printError("Output chest is likely full. Cannot do anything until it is emptied!")
- sleep(ERROR_DELAY)
- end
- end
- end
- end
- --- Empty the buffer chest
- ---@return integer buffered The amount of items that were taken out of the buffer.
- local function empty_buffer()
- local buffered = 0
- for slot, item in pairs(buffer.list()) do
- buffered = buffered + item.count
- inv.pullItems(BUFFER_CHEST, slot)
- end
- return buffered
- end
- --- Dump the contents of the turtle to the buffer chest
- ---@return integer buffered The amount of items that were taken out of the buffer, if the buffer needed to be emptied during this cycle.
- local function dump_to_buffer()
- local buffered = 0
- for slot = 1, 16 do
- if turtle.getItemCount(slot) > 0 then
- if buffer.pullItems(local_name, slot) == 0 then
- buffered = buffered + empty_buffer()
- while buffer.pullItems(local_name, slot) == 0 do
- printError("Buffer chest and input chest are likely full. Cannot do anything until they are emptied!")
- sleep(ERROR_DELAY)
- end
- end
- end
- end
- return buffered
- end
- while true do
- local size = inv.size()
- local repairs = 0
- local full_health = 0
- local buffered = 0
- for test_slot = 1, size - 1 do
- -- Get the item in that slot.
- local item = inv.getItemDetail(test_slot)
- if item then
- -- An item exists in this slot.
- -- Then, check if we even need to combine this item with anything...
- if item.damage == 0 then
- print("Found an undamaged item in the chest, moving it to output.")
- full_health = full_health + 1
- while output.pullItems(INPUT_CHEST, test_slot) == 0 do
- printError("Output chest is likely full. Cannot do anything until it is emptied!")
- sleep(ERROR_DELAY)
- end
- else -- Item is damaged,
- get_item(test_slot)
- -- we will try to combine it until we can no longer do so.
- for secondary_slot = test_slot + 1, size do
- -- Get the item in that slot
- local second_item = inv.getItemDetail(secondary_slot)
- if second_item then
- -- First, check if the item is undamaged already (it's a waste to craft these).
- if second_item.damage == 0 then
- -- Move it to the output chest if undamaged.
- print("Found an undamaged item in the chest, moving it to output.")
- full_health = full_health + 1
- while output.pullItems(INPUT_CHEST, secondary_slot) == 0 do
- printError("Output chest is likely full. Cannot do anything until it is emptied!")
- sleep(ERROR_DELAY)
- end
- elseif item.name == second_item.name then -- Check if the two item names are equal.
- -- Items are the same!
- get_item(secondary_slot)
- if not turtle.craft() then
- printError("Items were not craftable!")
- print("Moving them into the output chest.")
- force_output()
- -- We do not break out of the loop here: if more of this item remain, it will get pulled and moved to the output immediately.
- end
- -- And if we were able to output this item, break out of this inner loop.
- if attempt_output() then
- repairs = repairs + 1
- break
- end
- end
- end
- end
- -- If we went through all the items in the chest and failed to repair it, throw it into the buffer chest.
- buffered = buffered + dump_to_buffer()
- end
- end
- end
- -- Empty the contents of the buffer inventory into the main inventory.
- buffered = buffered + empty_buffer()
- print(("Done cycle.\n Successful repairs: %d\n Full-health items found: %d\n Items buffered: %d\n Time until next cycle: %d seconds"):format(
- repairs,
- full_health,
- buffered,
- math.max(CHECK_DELAY - BUFFER_DELAY_REMOVAL * buffered, 0)
- ))
- sleep(math.max(CHECK_DELAY - BUFFER_DELAY_REMOVAL * buffered, 0))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement