Advertisement
Wassaa

enchComputer

Jun 25th, 2025 (edited)
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | Source Code | 0 0
  1. -- enchantSorter.lua
  2. -- Sorts items from an input chest into three outputs and logs to a monitor.
  3.  
  4. -----------------------------------------------------------
  5. -- ░█  CONFIG
  6. -----------------------------------------------------------
  7. local INPUT_CHEST     = "ironchests:crystal_chest_3"
  8. local ENCHANTED_CHEST = "ironchests:crystal_chest_0"
  9. local NORMAL_CHEST    = "ironchests:crystal_chest_1"
  10. local OVERFLOW_CHEST  = "ironchests:crystal_chest_2"
  11. local MONITOR_NAME    = "monitor_4"
  12.  
  13. -----------------------------------------------------------
  14. -- ░█  WRAP PERIPHERALS
  15. -----------------------------------------------------------
  16. local inputChest = peripheral.wrap(INPUT_CHEST)
  17. local monitor    = peripheral.wrap(MONITOR_NAME)
  18.  
  19. if not inputChest then error("Input chest not found: "..INPUT_CHEST) end
  20. if not monitor   then error("Monitor not found: "..MONITOR_NAME) end
  21.  
  22. -----------------------------------------------------------
  23. -- ░█  MONITOR INITIALISATION
  24. -----------------------------------------------------------
  25. monitor.setTextScale(0.5)                                            -- fine-grained text size :contentReference[oaicite:2]{index=2}
  26. monitor.setBackgroundColor(colors.black)
  27. monitor.setTextColor(colors.white)
  28. monitor.clear()
  29. monitor.setCursorPos(1,1)
  30.  
  31. local monW, monH = monitor.getSize()                                 -- fetch width/height :contentReference[oaicite:3]{index=3}
  32. local line = 1                                                       -- current Y cursor
  33.  
  34. -----------------------------------------------------------
  35. -- ░█  COLOUR MAP + LOGGER
  36. -----------------------------------------------------------
  37. local chestLabels = {
  38.   [ENCHANTED_CHEST] = "Enchanted",
  39.   [NORMAL_CHEST]    = "Normal",
  40.   [OVERFLOW_CHEST]  = "Overflow",
  41. }
  42. local chestColours = {                                               -- colour constants :contentReference[oaicite:4]{index=4}
  43.   [ENCHANTED_CHEST] = colors.green,
  44.   [NORMAL_CHEST]    = colors.red,
  45.   [OVERFLOW_CHEST]  = colors.blue,
  46. }
  47.  
  48. --- Print to PC *and* monitor, colouring the whole line on the monitor.
  49. ---@param msg string
  50. ---@param dest string  network name of destination chest
  51. local function log(msg, dest)
  52.   -- console
  53.   print(msg)
  54.  
  55.   -- monitor
  56.   monitor.setTextColour(chestColours[dest] or colors.white)
  57.   monitor.setCursorPos(1, line)
  58.   monitor.clearLine()
  59.   monitor.write(msg)
  60.   line = line + 1
  61.   if line > monH then
  62.     monitor.scroll(1)                                               -- keep newest line visible :contentReference[oaicite:5]{index=5}
  63.     line = monH
  64.   end
  65.   monitor.setTextColour(colors.white)                               -- reset for safety
  66. end
  67.  
  68. -----------------------------------------------------------
  69. -- ░█  HELPER FUNCTIONS
  70. -----------------------------------------------------------
  71. local function moveStack(fromChest, slot, destName, itemDetail, moved)
  72.   log(string.format(
  73.         "%3d %-20.20s -> %s",
  74.         moved, itemDetail.displayName, chestLabels[destName]),
  75.       destName)
  76. end
  77.  
  78. local function isStackable(item)      return item.maxCount and item.maxCount > 1 end
  79. local function isEnchanted(item)      return item.enchantments and #item.enchantments > 0 end
  80.  
  81. -----------------------------------------------------------
  82. -- ░█  MAIN SORT PASS
  83. -----------------------------------------------------------
  84. local function sortOnce()
  85.   local totalSlots = inputChest.size()                              -- slot count API :contentReference[oaicite:6]{index=6}
  86.   for slot = 1, totalSlots do
  87.     local detail = inputChest.getItemDetail(slot)
  88.     if detail then
  89.       local moved, dest = 0, nil
  90.       if isStackable(detail) then
  91.         dest   = OVERFLOW_CHEST
  92.         moved  = inputChest.pushItems(dest, slot)                  -- inventory transfer :contentReference[oaicite:7]{index=7}
  93.       else
  94.         if isEnchanted(detail) then
  95.           dest  = ENCHANTED_CHEST
  96.         else
  97.           dest  = NORMAL_CHEST
  98.         end
  99.         moved = inputChest.pushItems(dest, slot)
  100.       end
  101.       moveStack(inputChest, slot, dest, detail, moved)
  102.     end
  103.   end
  104. end
  105.  
  106. -----------------------------------------------------------
  107. -- ░█  CONTINUOUS RUN
  108. -----------------------------------------------------------
  109. log("=== enchantSorter started ===", nil)
  110. while true do
  111.   sortOnce()
  112.   os.sleep(5)
  113. end
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement