Advertisement
ccraftersanonmoose

Auto-sorter

Feb 17th, 2025
108
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. -- Peripheral names
  2. local chestName = "chest"  -- Main input chest
  3. local fallbackChest = "minecraft:chest_1"  -- Fallback chest for unrecognized items
  4.  
  5. -- Function to find all storage bins dynamically
  6. local function getStorageBins()
  7.     local bins = {}
  8.     for _, name in pairs(peripheral.getNames()) do
  9.         if peripheral.hasType(name, "inventory") and name ~= chestName and name ~= fallbackChest then
  10.             table.insert(bins, name)
  11.         end
  12.     end
  13.     return bins
  14. end
  15.  
  16. -- Function to scan storage bins and build sorting rules
  17. local function buildSortingRules()
  18.     local sortingRules = {}
  19.     local bins = getStorageBins()
  20.  
  21.     for _, bin in pairs(bins) do
  22.         local inventory = peripheral.wrap(bin)
  23.         local items = inventory.list()
  24.  
  25.         for _, item in pairs(items) do
  26.             sortingRules[item.name] = bin  -- Map item type to the detected bin
  27.         end
  28.     end
  29.  
  30.     return sortingRules
  31. end
  32.  
  33. -- Validate chest and fallback chest
  34. if not peripheral.isPresent(chestName) then
  35.     error("Main chest not found! Check connections and name.")
  36. end
  37. if not peripheral.isPresent(fallbackChest) then
  38.     error("Fallback chest not found! Check connections and name.")
  39. end
  40.  
  41. -- Main sorting loop
  42. while true do
  43.     local sortingRules = buildSortingRules()  -- Refresh sorting rules each cycle
  44.     local chest = peripheral.wrap(chestName)
  45.     local items = chest.list()
  46.  
  47.     for slot, item in pairs(items) do
  48.         local itemName = item.name
  49.         local targetBin = sortingRules[itemName]
  50.  
  51.         if targetBin and peripheral.isPresent(targetBin) then
  52.             -- Move the item to the correct bin
  53.             local moved = chest.pushItems(targetBin, slot)
  54.             if moved > 0 then
  55.                 print("Moved " .. moved .. "x " .. itemName .. " to " .. targetBin)
  56.             else
  57.                 print("Failed to move " .. itemName .. " to " .. targetBin)
  58.             end
  59.         else
  60.             -- Move to fallback chest
  61.             local moved = chest.pushItems(fallbackChest, slot)
  62.             if moved > 0 then
  63.                 print("Moved " .. moved .. "x " .. itemName .. " to fallback chest")
  64.             else
  65.                 print("Failed to move " .. itemName .. " to fallback chest")
  66.             end
  67.         end
  68.     end
  69.  
  70.     sleep(5)  -- Wait before checking again
  71. end
Advertisement
Comments
  • ccraftersanonmoose
    2 days (edited)
    # text 0.17 KB | 0 0
    1. Auto sorting script to sort any item placed into a chest into its respective storage bin. Works with cc tweaked and storagebins. Originally developed for plexiglass mountain.
Add Comment
Please, Sign In to add comment
Advertisement