SHOW:
|
|
- or go back to the newest paste.
| 1 | - | -- enchantTurtle.lua |
| 1 | + | -- enchantSorter.lua |
| 2 | -- Sorts items from an input chest into three outputs and logs to a monitor. | |
| 3 | - | -- Wrap the input chest in front |
| 3 | + | |
| 4 | - | local frontChest = peripheral.wrap("front")
|
| 4 | + | ----------------------------------------------------------- |
| 5 | - | if not frontChest then |
| 5 | + | -- ░█ CONFIG |
| 6 | - | error("❌ No chest detected in front of the turtle")
|
| 6 | + | ----------------------------------------------------------- |
| 7 | local INPUT_CHEST = "minecraft:chest_25" | |
| 8 | local ENCHANTED_CHEST = "minecraft:chest_29" | |
| 9 | - | -- Helper to test if the chest is empty |
| 9 | + | local NORMAL_CHEST = "minecraft:chest_30" |
| 10 | - | local function isChestEmpty(chest) |
| 10 | + | local OVERFLOW_CHEST = "minecraft:chest_31" |
| 11 | - | -- chest.list() returns a sparse table of slots → items |
| 11 | + | local MONITOR_NAME = "monitor_4" -- wrapped below |
| 12 | - | return next(chest.list()) == nil |
| 12 | + | |
| 13 | ----------------------------------------------------------- | |
| 14 | -- ░█ WRAP PERIPHERALS | |
| 15 | - | -- Main sorting loop |
| 15 | + | ----------------------------------------------------------- |
| 16 | - | while not isChestEmpty(frontChest) do |
| 16 | + | local inputChest = peripheral.wrap(INPUT_CHEST) |
| 17 | - | -- Always work in turtle slot 1 |
| 17 | + | local monitor = peripheral.wrap(MONITOR_NAME) |
| 18 | - | turtle.select(1) |
| 18 | + | |
| 19 | - | -- Grab exactly one item from the chest in front |
| 19 | + | if not inputChest then error("Input chest not found: "..INPUT_CHEST) end
|
| 20 | - | -- (succeeds only if there was at least one) |
| 20 | + | if not monitor then error("Monitor not found: "..MONITOR_NAME) end
|
| 21 | - | if not turtle.suck(1) then |
| 21 | + | |
| 22 | - | -- nothing left? |
| 22 | + | ----------------------------------------------------------- |
| 23 | - | break |
| 23 | + | -- ░█ MONITOR INITIALISATION |
| 24 | ----------------------------------------------------------- | |
| 25 | monitor.setTextScale(0.5) -- 1 = readable but roomy :contentReference[oaicite:0]{index=0}
| |
| 26 | - | -- Inspect what we just pulled |
| 26 | + | monitor.setBackgroundColor(colors.black) |
| 27 | - | local tDetail = turtle.getItemDetail(1) |
| 27 | + | monitor.setTextColor(colors.white) |
| 28 | - | if tDetail and tDetail.enchantments and #tDetail.enchantments > 0 then |
| 28 | + | monitor.clear() |
| 29 | - | -- Enchanted → drop up |
| 29 | + | monitor.setCursorPos(1,1) |
| 30 | - | if not turtle.dropUp() then |
| 30 | + | |
| 31 | - | error("Could not drop enchanted item above!")
|
| 31 | + | local monW, monH = monitor.getSize() -- width/height of the monitor :contentReference[oaicite:1]{index=1}
|
| 32 | local line = 1 -- current cursor Y on the monitor | |
| 33 | - | print(("🔼 Enchanted: %s ×1"):format(tDetail.name))
|
| 33 | + | |
| 34 | - | else |
| 34 | + | local function log(msg) |
| 35 | - | -- Unenchanted → drop down |
| 35 | + | -- print to computer terminal |
| 36 | - | if not turtle.dropDown() then |
| 36 | + | print(msg) |
| 37 | - | error("Could not drop normal item below!")
|
| 37 | + | |
| 38 | -- print to monitor, scrolling when needed | |
| 39 | - | print(("🔽 Normal: %s ×1"):format(tDetail and tDetail.name or "<unknown>"))
|
| 39 | + | monitor.setCursorPos(1, line) |
| 40 | monitor.clearLine() | |
| 41 | monitor.write(msg) | |
| 42 | line = line + 1 | |
| 43 | - | print("✅ All items sorted.") |
| 43 | + | if line > monH then |
| 44 | monitor.scroll(1) -- scroll display up one line :contentReference[oaicite:2]{index=2}
| |
| 45 | line = monH | |
| 46 | end | |
| 47 | end | |
| 48 | ||
| 49 | ----------------------------------------------------------- | |
| 50 | -- ░█ HELPER FUNCTIONS | |
| 51 | ----------------------------------------------------------- | |
| 52 | local chestLabels = {
| |
| 53 | [ENCHANTED_CHEST] = "Enchanted Chest", | |
| 54 | [NORMAL_CHEST] = "Normal Chest", | |
| 55 | [OVERFLOW_CHEST] = "Overflow Chest", | |
| 56 | } | |
| 57 | ||
| 58 | local function moveStack(fromChest, slot, destName, itemDetail) | |
| 59 | local moved = fromChest.pushItems(destName, slot) -- move whole stack :contentReference[oaicite:3]{index=3}
| |
| 60 | log(string.format( | |
| 61 | "Moved %-3d %-30s -> %s", | |
| 62 | moved, itemDetail.displayName, chestLabels[destName] or destName | |
| 63 | )) | |
| 64 | end | |
| 65 | ||
| 66 | local function isStackable(item) -- multi-stack items go to overflow | |
| 67 | return item.maxCount and item.maxCount > 1 | |
| 68 | end | |
| 69 | ||
| 70 | local function isEnchanted(item) | |
| 71 | return item.enchantments and #item.enchantments > 0 | |
| 72 | end | |
| 73 | ||
| 74 | ----------------------------------------------------------- | |
| 75 | -- ░█ MAIN SORT LOOP | |
| 76 | ----------------------------------------------------------- | |
| 77 | local function sortOnce() | |
| 78 | local totalSlots = inputChest.size() -- :contentReference[oaicite:4]{index=4}
| |
| 79 | for slot = 1, totalSlots do | |
| 80 | local detail = inputChest.getItemDetail(slot) | |
| 81 | if detail then | |
| 82 | if isStackable(detail) then | |
| 83 | moveStack(inputChest, slot, OVERFLOW_CHEST, detail) | |
| 84 | else | |
| 85 | if isEnchanted(detail) then | |
| 86 | moveStack(inputChest, slot, ENCHANTED_CHEST, detail) | |
| 87 | else | |
| 88 | moveStack(inputChest, slot, NORMAL_CHEST, detail) | |
| 89 | end | |
| 90 | end | |
| 91 | end | |
| 92 | end | |
| 93 | end | |
| 94 | ||
| 95 | ----------------------------------------------------------- | |
| 96 | -- ░█ CONTINUOUS RUN | |
| 97 | ----------------------------------------------------------- | |
| 98 | log("=== enchantSorter started ===")
| |
| 99 | while true do | |
| 100 | sortOnce() | |
| 101 | os.sleep(5) | |
| 102 | end |