Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- math.randomseed(os.epoch("utc"))
- local lastChest = nil
- local lastSlot = nil
- local REDSTONE_SIDE = "front" -- change if needed
- local output = peripheral.wrap("right")
- local left = peripheral.wrap("left")
- local top = peripheral.wrap("top")
- local bottom = peripheral.wrap("bottom")
- local back = peripheral.wrap("back")
- local function isEmpty(chest)
- return next(chest.list()) == nil
- end
- local function pullRandom(chest)
- if not chest then
- print("Chest not found!")
- return
- end
- local chestName = peripheral.getName(chest)
- while true do
- local occupied = {}
- for slot, item in pairs(chest.list()) do
- if not (chestName == lastChest and slot == lastSlot) then
- table.insert(occupied, slot)
- end
- end
- if #occupied > 0 then
- local slot = occupied[math.random(#occupied)]
- lastChest = chestName
- lastSlot = slot
- local moved = output.pullItems(chestName, slot, 1)
- if moved > 0 then
- print("Pulled from " .. chestName .. " slot " .. slot)
- else
- print("Failed to pull from " .. chestName)
- end
- sleep(0.25)
- return
- end
- -- Supply chest is empty: pause and wait for the user
- print("!! " .. chestName .. " is empty !!")
- print("Refill it, then press any key to continue...")
- os.pullEvent("key")
- sleep(0.5) -- small debounce before re-checking
- end
- end
- -- Make sure signal starts off
- redstone.setOutput(REDSTONE_SIDE, false)
- while true do
- print("Dealing 9 cards...")
- for i = 1, 6 do
- pullRandom(left)
- end
- pullRandom(top)
- pullRandom(bottom)
- pullRandom(back)
- print("Finished sending 9 cards")
- -- Signal on immediately: cards are ready
- redstone.setOutput(REDSTONE_SIDE, true)
- -- Wait for the output chest to be emptied
- print("Waiting for output chest to be emptied...")
- repeat
- sleep(2)
- until isEmpty(output)
- -- Signal off as soon as it's empty
- redstone.setOutput(REDSTONE_SIDE, false)
- -- Pause before the next deal
- print("Signal off - waiting 5 seconds before next deal...")
- sleep(5)
- end
Advertisement
Add Comment
Please, Sign In to add comment