SHOW:
|
|
- or go back to the newest paste.
| 1 | - | -- enchantSorter.lua |
| 1 | + | -- enchantTurtle.lua |
| 2 | - | -- Sorts items from an input chest into three outputs and logs to a monitor. |
| 2 | + | |
| 3 | -- Wrap the input chest in front | |
| 4 | - | ----------------------------------------------------------- |
| 4 | + | local frontChest = peripheral.wrap("front")
|
| 5 | - | -- ░█ CONFIG |
| 5 | + | if not frontChest then |
| 6 | - | ----------------------------------------------------------- |
| 6 | + | error("❌ No chest detected in front of the turtle")
|
| 7 | - | local INPUT_CHEST = "ironchests:crystal_chest_3" |
| 7 | + | |
| 8 | - | local ENCHANTED_CHEST = "ironchests:crystal_chest_0" |
| 8 | + | |
| 9 | - | local NORMAL_CHEST = "ironchests:crystal_chest_1" |
| 9 | + | -- Helper to test if the chest is empty |
| 10 | - | local OVERFLOW_CHEST = "ironchests:crystal_chest_2" |
| 10 | + | local function isChestEmpty(chest) |
| 11 | - | local MONITOR_NAME = "monitor_4" |
| 11 | + | -- chest.list() returns a sparse table of slots → items |
| 12 | return next(chest.list()) == nil | |
| 13 | - | ----------------------------------------------------------- |
| 13 | + | |
| 14 | - | -- ░█ WRAP PERIPHERALS |
| 14 | + | |
| 15 | - | ----------------------------------------------------------- |
| 15 | + | -- Main sorting loop |
| 16 | - | local inputChest = peripheral.wrap(INPUT_CHEST) |
| 16 | + | while not isChestEmpty(frontChest) do |
| 17 | - | local monitor = peripheral.wrap(MONITOR_NAME) |
| 17 | + | -- Always work in turtle slot 1 |
| 18 | turtle.select(1) | |
| 19 | - | if not inputChest then error("Input chest not found: "..INPUT_CHEST) end
|
| 19 | + | -- Grab exactly one item from the chest in front |
| 20 | - | if not monitor then error("Monitor not found: "..MONITOR_NAME) end
|
| 20 | + | -- (succeeds only if there was at least one) |
| 21 | if not turtle.suck(1) then | |
| 22 | - | ----------------------------------------------------------- |
| 22 | + | -- nothing left? |
| 23 | - | -- ░█ MONITOR INITIALISATION |
| 23 | + | break |
| 24 | - | ----------------------------------------------------------- |
| 24 | + | |
| 25 | - | monitor.setTextScale(0.5) -- fine-grained text size :contentReference[oaicite:2]{index=2}
|
| 25 | + | |
| 26 | - | monitor.setBackgroundColor(colors.black) |
| 26 | + | -- Inspect what we just pulled |
| 27 | - | monitor.setTextColor(colors.white) |
| 27 | + | local tDetail = turtle.getItemDetail(1) |
| 28 | - | monitor.clear() |
| 28 | + | if tDetail and tDetail.enchantments and #tDetail.enchantments > 0 then |
| 29 | - | monitor.setCursorPos(1,1) |
| 29 | + | -- Enchanted → drop up |
| 30 | if not turtle.dropUp() then | |
| 31 | - | local monW, monH = monitor.getSize() -- fetch width/height :contentReference[oaicite:3]{index=3}
|
| 31 | + | error("Could not drop enchanted item above!")
|
| 32 | - | local line = 1 -- current Y cursor |
| 32 | + | |
| 33 | print(("🔼 Enchanted: %s ×1"):format(tDetail.name))
| |
| 34 | - | ----------------------------------------------------------- |
| 34 | + | else |
| 35 | - | -- ░█ COLOUR MAP + LOGGER |
| 35 | + | -- Unenchanted → drop down |
| 36 | - | ----------------------------------------------------------- |
| 36 | + | if not turtle.dropDown() then |
| 37 | - | local chestLabels = {
|
| 37 | + | error("Could not drop normal item below!")
|
| 38 | - | [ENCHANTED_CHEST] = "Enchanted", |
| 38 | + | |
| 39 | - | [NORMAL_CHEST] = "Normal", |
| 39 | + | print(("🔽 Normal: %s ×1"):format(tDetail and tDetail.name or "<unknown>"))
|
| 40 | - | [OVERFLOW_CHEST] = "Overflow", |
| 40 | + | |
| 41 | - | } |
| 41 | + | |
| 42 | - | local chestColours = { -- colour constants :contentReference[oaicite:4]{index=4}
|
| 42 | + | |
| 43 | - | [ENCHANTED_CHEST] = colors.green, |
| 43 | + | print("✅ All items sorted.") |