Advertisement
Wassaa

tests

Jun 25th, 2025 (edited)
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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     = "minecraft:chest_25"
  8. local ENCHANTED_CHEST = "minecraft:chest_29"
  9. local NORMAL_CHEST    = "minecraft:chest_30"
  10. local OVERFLOW_CHEST  = "minecraft:chest_31"
  11. local MONITOR_NAME    = "monitor_4"      -- wrapped below
  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)            -- 1 = readable but roomy  :contentReference[oaicite:0]{index=0}
  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()    -- width/height of the monitor  :contentReference[oaicite:1]{index=1}
  32. local line = 1                          -- current cursor Y on the monitor
  33.  
  34. local function log(msg)
  35.   -- print to computer terminal
  36.   print(msg)
  37.  
  38.   -- print to monitor, scrolling when needed
  39.   monitor.setCursorPos(1, line)
  40.   monitor.clearLine()
  41.   monitor.write(msg)
  42.   line = line + 1
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement