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 = "ironchests:crystal_chest_3"
- local ENCHANTED_CHEST = "ironchests:crystal_chest_0"
- local NORMAL_CHEST = "ironchests:crystal_chest_1"
- local OVERFLOW_CHEST = "ironchests:crystal_chest_2"
- local MONITOR_NAME = "monitor_4"
- -----------------------------------------------------------
- -- ░█ 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) -- fine-grained text size :contentReference[oaicite:2]{index=2}
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.clear()
- monitor.setCursorPos(1,1)
- local monW, monH = monitor.getSize() -- fetch width/height :contentReference[oaicite:3]{index=3}
- local line = 1 -- current Y cursor
- -----------------------------------------------------------
- -- ░█ COLOUR MAP + LOGGER
- -----------------------------------------------------------
- local chestLabels = {
- [ENCHANTED_CHEST] = "Enchanted",
- [NORMAL_CHEST] = "Normal",
- [OVERFLOW_CHEST] = "Overflow",
- }
- local chestColours = { -- colour constants :contentReference[oaicite:4]{index=4}
- [ENCHANTED_CHEST] = colors.green,
- [NORMAL_CHEST] = colors.red,
- [OVERFLOW_CHEST] = colors.blue,
- }
- --- Print to PC *and* monitor, colouring the whole line on the monitor.
- ---@param msg string
- ---@param dest string network name of destination chest
- local function log(msg, dest)
- -- console
- print(msg)
- -- monitor
- monitor.setTextColour(chestColours[dest] or colors.white)
- monitor.setCursorPos(1, line)
- monitor.clearLine()
- monitor.write(msg)
- line = line + 1
- if line > monH then
- monitor.scroll(1) -- keep newest line visible :contentReference[oaicite:5]{index=5}
- line = monH
- end
- monitor.setTextColour(colors.white) -- reset for safety
- end
- -----------------------------------------------------------
- -- ░█ HELPER FUNCTIONS
- -----------------------------------------------------------
- local function moveStack(fromChest, slot, destName, itemDetail, moved)
- log(string.format(
- "%3d %-20.20s -> %s",
- moved, itemDetail.displayName, chestLabels[destName]),
- destName)
- end
- local function isStackable(item) return item.maxCount and item.maxCount > 1 end
- local function isEnchanted(item) return item.enchantments and #item.enchantments > 0 end
- -----------------------------------------------------------
- -- ░█ MAIN SORT PASS
- -----------------------------------------------------------
- local function sortOnce()
- local totalSlots = inputChest.size() -- slot count API :contentReference[oaicite:6]{index=6}
- for slot = 1, totalSlots do
- local detail = inputChest.getItemDetail(slot)
- if detail then
- local moved, dest = 0, nil
- if isStackable(detail) then
- dest = OVERFLOW_CHEST
- moved = inputChest.pushItems(dest, slot) -- inventory transfer :contentReference[oaicite:7]{index=7}
- else
- if isEnchanted(detail) then
- dest = ENCHANTED_CHEST
- else
- dest = NORMAL_CHEST
- end
- moved = inputChest.pushItems(dest, slot)
- end
- moveStack(inputChest, slot, dest, detail, moved)
- end
- end
- end
- -----------------------------------------------------------
- -- ░█ CONTINUOUS RUN
- -----------------------------------------------------------
- log("=== enchantSorter started ===", nil)
- while true do
- sortOnce()
- os.sleep(5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement