April_The_Sergal

disenchanterSorter.lua

Jan 27th, 2026 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.86 KB | Gaming | 0 0
  1. -- ============================================
  2. -- ENCHANTMENT EXTRACTOR SORTER
  3. -- ============================================
  4. -- Checks armory cabinet every 30 seconds
  5. -- Enchanted items -> Extractor
  6. -- Non-enchanted items -> Trash
  7. -- ============================================
  8.  
  9. -- ============================================
  10. -- CONFIGURATION - ADJUST THESE!
  11. -- ============================================
  12.  
  13. local CONFIG = {
  14.     -- Peripheral names (use "peripheral.getNames()" to find them)
  15.     -- Or use sides like "top", "bottom", "left", "right", "front", "back"
  16.     cabinetName = nil,       -- Will auto-detect, or set manually
  17.     disenchanterName = nil,  -- Will auto-detect (works with disenchanter or extractor)
  18.     trashName = "bottom",    -- Trash can below computer
  19.    
  20.     -- Timing
  21.     checkInterval = 30,  -- Seconds between checks
  22.    
  23.     -- Debug mode (prints extra info)
  24.     debug = true,
  25. }
  26.  
  27. -- ============================================
  28. -- PERIPHERAL DETECTION
  29. -- ============================================
  30.  
  31. local cabinet = nil
  32. local disenchanter = nil
  33. local trash = nil
  34.  
  35. local function findPeripherals()
  36.     local names = peripheral.getNames()
  37.    
  38.     print("Scanning peripherals...")
  39.    
  40.     for _, name in ipairs(names) do
  41.         local pType = peripheral.getType(name)
  42.        
  43.         if CONFIG.debug then
  44.             print("  Found: " .. name .. " (" .. pType .. ")")
  45.         end
  46.        
  47.         -- Auto-detect cabinet (Functional Storage) - check TYPE
  48.         if not cabinet and (pType:find("armory_cabinet") or pType:find("functionalstrorage")) then
  49.             if CONFIG.cabinetName == nil or name == CONFIG.cabinetName then
  50.                 cabinet = peripheral.wrap(name)
  51.                 CONFIG.cabinetName = name
  52.                 print("Cabinet: " .. name)
  53.             end
  54.         end
  55.        
  56.         -- Auto-detect disenchanter/extractor - check TYPE
  57.         if not disenchanter and (pType:find("disenchanter") or pType:find("enchantment_extractor")) then
  58.             if CONFIG.disenchanterName == nil or name == CONFIG.disenchanterName then
  59.                 disenchanter = peripheral.wrap(name)
  60.                 CONFIG.disenchanterName = name
  61.                 print("Extractor: " .. name)
  62.             end
  63.         end
  64.        
  65.         -- Auto-detect trash - check TYPE
  66.         if not trash and pType:find("trash") then
  67.             if CONFIG.trashName == nil or name == CONFIG.trashName then
  68.                 trash = peripheral.wrap(name)
  69.                 CONFIG.trashName = name
  70.                 print("Trash: " .. name)
  71.             end
  72.         end
  73.     end
  74.    
  75.     -- Fallback: wrap trash by configured name if not auto-detected
  76.     if not trash and CONFIG.trashName then
  77.         trash = peripheral.wrap(CONFIG.trashName)
  78.         if trash then
  79.             print("Trash: " .. CONFIG.trashName)
  80.         end
  81.     end
  82.    
  83.     -- Check what we found
  84.     local ok = true
  85.     if not cabinet then
  86.         print("ERROR: Cabinet not found!")
  87.         ok = false
  88.     end
  89.     if not disenchanter then
  90.         print("ERROR: Extractor not found!")
  91.         ok = false
  92.     end
  93.     if not trash then
  94.         print("ERROR: Trash not found!")
  95.         ok = false
  96.     end
  97.    
  98.     return ok
  99. end
  100.  
  101. -- ============================================
  102. -- ENCHANTMENT DETECTION
  103. -- ============================================
  104.  
  105. local function hasEnchantments(itemDetail)
  106.     if not itemDetail then return false end
  107.    
  108.     -- Check for enchantments table
  109.     if itemDetail.enchantments and #itemDetail.enchantments > 0 then
  110.         return true
  111.     end
  112.    
  113.     -- Some mods use different field names
  114.     if itemDetail.nbt then
  115.         -- Items with NBT data containing enchantments
  116.         -- The presence of certain NBT often indicates enchantments
  117.         if type(itemDetail.nbt) == "string" and itemDetail.nbt:find("ench") then
  118.             return true
  119.         end
  120.     end
  121.    
  122.     -- Check display name for "Enchanted" (fallback)
  123.     if itemDetail.displayName and itemDetail.displayName:find("Enchanted") then
  124.         return true
  125.     end
  126.    
  127.     return false
  128. end
  129.  
  130. -- ============================================
  131. -- ITEM SORTING
  132. -- ============================================
  133.  
  134. local function sortItems()
  135.     if not cabinet then
  136.         print("No cabinet connected!")
  137.         return
  138.     end
  139.    
  140.     local items = cabinet.list()
  141.     if not items or next(items) == nil then
  142.         if CONFIG.debug then
  143.             print("Cabinet is empty")
  144.         end
  145.         return
  146.     end
  147.    
  148.     local enchantedCount = 0
  149.     local trashedCount = 0
  150.    
  151.     for slot, item in pairs(items) do
  152.         -- Get detailed item info to check enchantments
  153.         local detail = cabinet.getItemDetail(slot)
  154.        
  155.         if detail then
  156.             if hasEnchantments(detail) then
  157.                 -- Move to extractor
  158.                 if disenchanter then
  159.                     local moved = cabinet.pushItems(CONFIG.disenchanterName, slot)
  160.                     if moved and moved > 0 then
  161.                         enchantedCount = enchantedCount + moved
  162.                         if CONFIG.debug then
  163.                             print("  -> Extractor: " .. detail.displayName)
  164.                         end
  165.                     end
  166.                 end
  167.             else
  168.                 -- Move to trash
  169.                 if trash then
  170.                     local moved = cabinet.pushItems(CONFIG.trashName, slot)
  171.                     if moved and moved > 0 then
  172.                         trashedCount = trashedCount + moved
  173.                         if CONFIG.debug then
  174.                             print("  -> Trash: " .. detail.displayName)
  175.                         end
  176.                     end
  177.                 end
  178.             end
  179.         end
  180.     end
  181.    
  182.     if enchantedCount > 0 or trashedCount > 0 then
  183.         print(string.format("Sorted: %d enchanted, %d trashed", enchantedCount, trashedCount))
  184.     end
  185. end
  186.  
  187. -- ============================================
  188. -- MAIN LOOP
  189. -- ============================================
  190.  
  191. local function main()
  192.     term.clear()
  193.     term.setCursorPos(1, 1)
  194.    
  195.     print("=== ENCHANTMENT EXTRACTOR SORTER ===")
  196.     print("")
  197.    
  198.     if not findPeripherals() then
  199.         print("")
  200.         print("Fix peripheral connections and reboot.")
  201.         print("")
  202.         print("Available peripherals:")
  203.         for _, name in ipairs(peripheral.getNames()) do
  204.             print("  " .. name)
  205.         end
  206.         return
  207.     end
  208.    
  209.     print("")
  210.     print("Running every " .. CONFIG.checkInterval .. " seconds...")
  211.     print("Press Ctrl+T to stop")
  212.     print("")
  213.    
  214.     while true do
  215.         local time = textutils.formatTime(os.time(), false)
  216.         print("[" .. time .. "] Checking cabinet...")
  217.        
  218.         sortItems()
  219.        
  220.         os.sleep(CONFIG.checkInterval)
  221.     end
  222. end
  223.  
  224. -- Run
  225. main()
Tags: lua
Advertisement
Add Comment
Please, Sign In to add comment