Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Peripheral names
- local chestName = "chest" -- Main input chest
- local fallbackChest = "minecraft:chest_1" -- Fallback chest for unrecognized items
- -- Function to find all storage bins dynamically
- local function getStorageBins()
- local bins = {}
- for _, name in pairs(peripheral.getNames()) do
- if peripheral.hasType(name, "inventory") and name ~= chestName and name ~= fallbackChest then
- table.insert(bins, name)
- end
- end
- return bins
- end
- -- Function to scan storage bins and build sorting rules
- local function buildSortingRules()
- local sortingRules = {}
- local bins = getStorageBins()
- for _, bin in pairs(bins) do
- local inventory = peripheral.wrap(bin)
- local items = inventory.list()
- for _, item in pairs(items) do
- sortingRules[item.name] = bin -- Map item type to the detected bin
- end
- end
- return sortingRules
- end
- -- Validate chest and fallback chest
- if not peripheral.isPresent(chestName) then
- error("Main chest not found! Check connections and name.")
- end
- if not peripheral.isPresent(fallbackChest) then
- error("Fallback chest not found! Check connections and name.")
- end
- -- Main sorting loop
- while true do
- local sortingRules = buildSortingRules() -- Refresh sorting rules each cycle
- local chest = peripheral.wrap(chestName)
- local items = chest.list()
- for slot, item in pairs(items) do
- local itemName = item.name
- local targetBin = sortingRules[itemName]
- if targetBin and peripheral.isPresent(targetBin) then
- -- Move the item to the correct bin
- local moved = chest.pushItems(targetBin, slot)
- if moved > 0 then
- print("Moved " .. moved .. "x " .. itemName .. " to " .. targetBin)
- else
- print("Failed to move " .. itemName .. " to " .. targetBin)
- end
- else
- -- Move to fallback chest
- local moved = chest.pushItems(fallbackChest, slot)
- if moved > 0 then
- print("Moved " .. moved .. "x " .. itemName .. " to fallback chest")
- else
- print("Failed to move " .. itemName .. " to fallback chest")
- end
- end
- end
- sleep(5) -- Wait before checking again
- end
Advertisement
Comments
-
- 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