Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- ---Desc: A storage control program which acts as a drawer controller
- ---Barrels can be defined below, and their contents will be added to drawers
- ---if no drawer is found with the contents then it's placed randomly
- ---
- --By: Spank
- -- Define peripherals IMPORTANT: change these based on the outputs for your farms
- local barrels = {
- peripheral.wrap("minecraft:barrel_0"), -- First barrel
- peripheral.wrap("minecraft:barrel_3"), -- Third barrel
- peripheral.wrap("minecraft:barrel_2"), -- Second barrel
- peripheral.wrap("minecraft:barrel_5"), -- Fifth barrel
- peripheral.wrap("minecraft:hopper_0") -- First hopper
- }
- -- Function to dynamically find all connected storage drawers
- local function getConnectedDrawers()
- local drawers = {}
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name):find("storagedrawers:standard_drawers_1") then
- local drawer = peripheral.wrap(name)
- table.insert(drawers, {name = name, drawer = drawer}) -- Store both name and wrapped drawer
- end
- end
- return drawers
- end
- -- Function to check if a drawer contains a specific item
- local function findDrawerForItem(drawers, itemName)
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- if drawerContents[2] and drawerContents[2].name == itemName then
- return drawerInfo -- Return the drawerInfo which includes the name
- end
- end
- return nil -- Return nil if no drawer currently contains the item
- end
- -- Function to push items from a single barrel to drawers
- local function pushItemsFromBarrel(barrel)
- local barrelContents = barrel.list()
- if not barrelContents or next(barrelContents) == nil then
- return true -- No items in barrel, nothing to process
- end
- -- Get the updated list of connected drawers
- local drawers = getConnectedDrawers()
- for slot, item in pairs(barrelContents) do
- local targetDrawerInfo = findDrawerForItem(drawers, item.name)
- -- If no specific drawer for the item, find an empty one
- if not targetDrawerInfo then
- for _, drawerInfo in ipairs(drawers) do
- if not drawerInfo.drawer.list()[2] then
- targetDrawerInfo = drawerInfo
- break
- end
- end
- end
- -- Attempt to push items to the drawer
- if targetDrawerInfo then
- local drawerName = peripheral.getName(targetDrawerInfo.drawer) -- Get the peripheral name of the drawer
- print("Attempting to push to drawer: " .. drawerName)
- -- Push items to the drawer and check success
- local success = barrel.pushItems(drawerName, slot, item.count, 2)
- if success == 0 then
- print("Cannot push item to drawer: " .. drawerName .. " from barrel " .. peripheral.getName(barrel) .. "! Check storage capacity.")
- return false -- Failed push, continue checking other barrels
- end
- else
- print("No drawer available for item " .. item.name .. " in barrel " .. peripheral.getName(barrel) .. "! Add or upgrade a drawer.")
- return false -- No drawer available for this item
- end
- end
- return true -- Successfully processed all items in this barrel
- end
- -- Main loop function
- local function main()
- while true do
- local allSuccess = true
- for _, barrel in ipairs(barrels) do
- -- Try pushing items from the current barrel
- local success = pushItemsFromBarrel(barrel)
- if not success then
- allSuccess = false -- Mark as false if any barrel fails
- end
- end
- -- Wait before rechecking if all barrels were successful, or retry immediately if there was an issue
- if allSuccess then
- os.sleep(10) -- Wait 10 seconds before checking again
- else
- os.sleep(2) -- Short delay to allow immediate retry if a barrel fails
- end
- end
- end
- -- Start the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement