Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- sequence_feeder.lua
- -- Feed items into a GregTech input bus in STRICT order, one step at a time,
- -- using ComputerCraft (CC:Tweaked). Each step is pushed only after the
- -- previous one has been consumed by the machine.
- --
- -- Wiring: put a wired modem on the GT input bus AND on the buffer chest/barrel,
- -- connect them (and a modem on this computer) with networking cable.
- -- Run `peripheral.getNames()` in the Lua prompt to find the exact names,
- -- then fill them in below.
- --========================= CONFIG =========================--
- local BUFFER = "minecraft:barrel_1" -- inventory holding items to feed
- local MACHINE = "gtceu:luv_dual_input_hatch_1" -- the GT input bus to feed into
- local MACHINE_SLOT = 1 -- which bus slot to feed
- local OUTPUT = "ae2:interface_1" -- where leftover items go when done
- local POLL = 0.5 -- seconds between checks
- local TIMEOUT = 0 -- max seconds to wait per step (0 = wait forever)
- local NAG = 15 -- print a "still waiting" note every N seconds
- local RS_SIDE = "right" -- side to pulse redstone on when done
- local RS_PULSE = 0.5 -- seconds to hold redstone on
- local OUTPUT_ITEMS = {
- ["kubejs:chromatic_capacitor_empty"] = true,
- }
- local SEQUENCE = {
- { name = "kubejs:chromatic_capacitor_yellow", count = 1 },
- { name = "kubejs:active_prismatic_core", count = 1 },
- { name = "kubejs:chromatic_capacitor_yellow", count = 1 },
- { name = "kubejs:orange_prismatic_core", count = 1 },
- { name = "kubejs:chromatic_stabilizer", count = 2 },
- { name = "kubejs:chromatic_capacitor_green", count = 1 },
- { name = "kubejs:lime_prismatic_core", count = 1 },
- { name = "kubejs:chromatic_capacitor_cyan", count = 1 },
- { name = "kubejs:teal_prismatic_core", count = 1 },
- { name = "kubejs:chromatic_stabilizer", count = 2 },
- { name = "kubejs:chromatic_capacitor_blue", count = 1 },
- { name = "kubejs:azure_prismatic_core", count = 1 },
- { name = "kubejs:chromatic_capacitor_magenta", count = 1 },
- { name = "kubejs:indigo_prismatic_core", count = 1 },
- }
- --==========================================================--
- local buffer = peripheral.wrap(BUFFER) or error("No inventory named " .. BUFFER)
- local machine = peripheral.wrap(MACHINE) or error("No inventory named " .. MACHINE)
- local output = peripheral.wrap(OUTPUT) or error("No inventory named " .. OUTPUT)
- -- Find first buffer slot containing `name`.
- local function findSlot(inv, name)
- for slot, item in pairs(inv.list()) do
- if item.name == name then return slot end
- end
- return nil
- end
- -- True when the target bus slot is empty (previous item consumed).
- local function slotEmpty()
- return machine.list()[MACHINE_SLOT] == nil
- end
- -- Block until `cond()` is true. Waits forever if TIMEOUT == 0.
- -- Prints `msg` every NAG seconds so you can see what it's stuck on.
- local function waitFor(cond, msg)
- local waited = 0
- while not cond() do
- sleep(POLL)
- waited = waited + POLL
- if NAG > 0 and waited % NAG < POLL then
- print(" ...waiting: " .. msg)
- end
- if TIMEOUT > 0 and waited >= TIMEOUT then
- error("Timed out: " .. msg)
- end
- end
- end
- for i, step in ipairs(SEQUENCE) do
- print(("Step %d/%d: %s x%d"):format(i, #SEQUENCE, step.name, step.count))
- -- make sure the slot is clear before pushing
- waitFor(slotEmpty, "slot " .. MACHINE_SLOT .. " to clear")
- local remaining = step.count
- while remaining > 0 do
- local src = findSlot(buffer, step.name)
- if not src then
- -- not in the buffer yet (e.g. an intermediate still being produced); wait
- waitFor(function() return findSlot(buffer, step.name) ~= nil end,
- step.name .. " to appear in buffer")
- else
- local moved = buffer.pushItems(MACHINE, src, remaining, MACHINE_SLOT)
- if moved == 0 then
- sleep(POLL) -- slot busy/full; wait for the machine to pull, then retry
- else
- remaining = remaining - moved
- end
- end
- end
- waitFor(slotEmpty, step.name .. " to be consumed") -- wait for the machine to eat it
- end
- print("Sequence complete. Pulsing redstone on " .. RS_SIDE .. "...")
- -- Pulse redstone on/off before moving output.
- redstone.setOutput(RS_SIDE, true)
- sleep(RS_PULSE)
- redstone.setOutput(RS_SIDE, false)
- print("Moving selected items to output...")
- -- Move only whitelisted items from the buffer to the output peripheral.
- local drained = 0
- for slot, item in pairs(buffer.list()) do
- if OUTPUT_ITEMS[item.name] then
- drained = drained + buffer.pushItems(OUTPUT, slot)
- end
- end
- print(("Moved %d item(s) to %s."):format(drained, OUTPUT))
Advertisement
Add Comment
Please, Sign In to add comment