Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- enchantSorter.lua
- -- Sorts items from an input chest into three outputs and logs to a monitor.
- -----------------------------------------------------------
- -- ░█ CONFIG
- -----------------------------------------------------------
- local INPUT_CHEST = "minecraft:chest_25"
- local ENCHANTED_CHEST = "minecraft:chest_29"
- local NORMAL_CHEST = "minecraft:chest_30"
- local OVERFLOW_CHEST = "minecraft:chest_31"
- local MONITOR_NAME = "monitor_4" -- wrapped below
- -----------------------------------------------------------
- -- ░█ WRAP PERIPHERALS
- -----------------------------------------------------------
- local inputChest = peripheral.wrap(INPUT_CHEST)
- local monitor = peripheral.wrap(MONITOR_NAME)
- if not inputChest then error("Input chest not found: "..INPUT_CHEST) end
- if not monitor then error("Monitor not found: "..MONITOR_NAME) end
- -----------------------------------------------------------
- -- ░█ MONITOR INITIALISATION
- -----------------------------------------------------------
- monitor.setTextScale(0.5) -- 1 = readable but roomy :contentReference[oaicite:0]{index=0}
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.clear()
- monitor.setCursorPos(1,1)
- local monW, monH = monitor.getSize() -- width/height of the monitor :contentReference[oaicite:1]{index=1}
- local line = 1 -- current cursor Y on the monitor
- local function log(msg)
- -- print to computer terminal
- print(msg)
- -- print to monitor, scrolling when needed
- monitor.setCursorPos(1, line)
- monitor.clearLine()
- monitor.write(msg)
- line = line + 1
- if line > monH then
- monitor.scroll(1) -- scroll display up one line :contentReference[oaicite:2]{index=2}
- line = monH
- end
- end
- -----------------------------------------------------------
- -- ░█ HELPER FUNCTIONS
- -----------------------------------------------------------
- local chestLabels = {
- [ENCHANTED_CHEST] = "Enchanted Chest",
- [NORMAL_CHEST] = "Normal Chest",
- [OVERFLOW_CHEST] = "Overflow Chest",
- }
- local function moveStack(fromChest, slot, destName, itemDetail)
- local moved = fromChest.pushItems(destName, slot) -- move whole stack :contentReference[oaicite:3]{index=3}
- log(string.format(
- "Moved %-3d %-30s -> %s",
- moved, itemDetail.displayName, chestLabels[destName] or destName
- ))
- end
- local function isStackable(item) -- multi-stack items go to overflow
- return item.maxCount and item.maxCount > 1
- end
- local function isEnchanted(item)
- return item.enchantments and #item.enchantments > 0
- end
- -----------------------------------------------------------
- -- ░█ MAIN SORT LOOP
- -----------------------------------------------------------
- local function sortOnce()
- local totalSlots = inputChest.size() -- :contentReference[oaicite:4]{index=4}
- for slot = 1, totalSlots do
- local detail = inputChest.getItemDetail(slot)
- if detail then
- if isStackable(detail) then
- moveStack(inputChest, slot, OVERFLOW_CHEST, detail)
- else
- if isEnchanted(detail) then
- moveStack(inputChest, slot, ENCHANTED_CHEST, detail)
- else
- moveStack(inputChest, slot, NORMAL_CHEST, detail)
- end
- end
- end
- end
- end
- -----------------------------------------------------------
- -- ░█ CONTINUOUS RUN
- -----------------------------------------------------------
- log("=== enchantSorter started ===")
- while true do
- sortOnce()
- os.sleep(5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement