Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- ---Program to automate the washing of items using encased fan and depot
- ---Exports washable items from drawers to depot
- ---Imports washed items from depot to drawers
- ---Inventory balancing prevents processing if ingredients < outputs
- ---washable and washed lists can be modified to include/exclude items
- ---review IMPORTANT comments, these are configurables that will likely change based on your setup
- ---
- --By: hornedcommando
- -- Define peripherals
- local depot = peripheral.wrap("create:depot_0") -- IMPORTANT: Change this to the depot where washed items are stored
- -- List of washable items
- local washable = {
- ["biomesoplenty:orange_sand"] = true,
- ["biomesoplenty:white_sand"] = true,
- ["minecraft:sand"] = true,
- ["create:wheat_flour"] = true,
- ["minecraft:red_sand"] = true,
- ["minecraft:gravel"] = true,
- ["minecraft:soul_sand"] = true,
- }
- -- List of washed items
- local washedItems = {
- ["minecraft:clay_ball"] = true,
- ["minecraft:gold_nugget"] = true,
- ["minecraft:iron_nugget"] = true,
- ["minecraft:quartz"] = true,
- }
- -- 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 count specific items in the drawers
- local function countItems(drawers, itemName)
- local count = 0
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- for slot, item in pairs(drawerContents) do
- print("Checking slot " .. slot .. " in drawer " .. drawerInfo.name .. " for item " .. itemName)
- if item.name == itemName then
- count = count + item.count
- print("Found " .. item.count .. " of " .. itemName .. " in slot " .. slot)
- end
- end
- end
- return count
- end
- -- Function to push items from drawers to the depot
- local function pushItems(drawers, itemName)
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- for slot, item in pairs(drawerContents) do
- if item.name == itemName then
- local success = drawerInfo.drawer.pushItems(peripheral.getName(depot), slot, item.count)
- if success == 0 then
- print("Failed to push item: " .. item.name)
- return false -- Failed push, continue checking other drawers
- else
- print("Pushed " .. item.count .. " of " .. item.name .. " to depot.")
- end
- end
- end
- end
- return true -- Successfully processed all items in the drawers
- end
- -- Function to process sand
- local function processSand(drawers)
- print("Processing sand...")
- -- Count the items in the drawers
- local clayCount = countItems(drawers, "minecraft:clay_ball")
- local sandTypes = {
- "minecraft:sand",
- "biomesoplenty:white_sand",
- "biomesoplenty:orange_sand"
- }
- local highestSandCount = 0
- local suitableSandType = nil
- for _, sandType in ipairs(sandTypes) do
- local sandCount = countItems(drawers, sandType)
- print("Clay count: " .. clayCount .. ", " .. sandType .. " count: " .. sandCount)
- if sandCount > highestSandCount then
- highestSandCount = sandCount
- suitableSandType = sandType
- end
- end
- if highestSandCount > clayCount then
- return pushItems(drawers, suitableSandType)
- else
- print("Skipping sand processing as no suitable sand type is available.")
- return true
- end
- end
- -- Function to process red sand
- local function processRedSand(drawers)
- print("Processing red sand...")
- local goldNuggetCount = countItems(drawers, "minecraft:gold_nugget")
- local redSandCount = countItems(drawers, "minecraft:red_sand")
- print("Gold nugget count: " .. goldNuggetCount .. ", Red sand count: " .. redSandCount)
- if goldNuggetCount > redSandCount then
- print("Skipping red sand processing as gold nugget count is greater than red sand count.")
- return true
- end
- return pushItems(drawers, "minecraft:red_sand")
- end
- -- Function to process gravel
- local function processGravel(drawers)
- print("Processing gravel...")
- local ironNuggetCount = countItems(drawers, "minecraft:iron_nugget")
- local gravelCount = countItems(drawers, "minecraft:gravel")
- print("Iron nugget count: " .. ironNuggetCount .. ", Gravel count: " .. gravelCount)
- if ironNuggetCount > gravelCount then
- print("Skipping gravel processing as iron nugget count is greater than gravel count.")
- return true
- end
- return pushItems(drawers, "minecraft:gravel")
- end
- -- Function to process soul sand
- local function processSoulSand(drawers)
- print("Processing soul sand...")
- local quartzCount = countItems(drawers, "minecraft:quartz")
- local soulSandCount = countItems(drawers, "minecraft:soul_sand")
- print("Quartz count: " .. quartzCount .. ", Soul sand count: " .. soulSandCount)
- if quartzCount > soulSandCount then
- print("Skipping soul sand processing as quartz count is greater than soul sand count.")
- return true
- end
- return pushItems(drawers, "minecraft:soul_sand")
- end
- -- Function to find a drawer for a specific item or an empty drawer
- 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
- for _, drawerInfo in ipairs(drawers) do
- local drawerContents = drawerInfo.drawer.list()
- if not drawerContents[2] then
- return drawerInfo -- Return the first empty drawer
- end
- end
- return nil -- Return nil if no drawer currently contains the item and no empty drawer is found
- end
- -- Function to move washed items from the depot to the appropriate drawer
- local function moveWashedItemsToDrawer(drawers)
- local depotItems = depot.list()
- for slot, item in pairs(depotItems) do
- if washedItems[item.name] then
- local targetDrawerInfo = findDrawerForItem(drawers, item.name)
- if targetDrawerInfo then
- local drawerName = peripheral.getName(targetDrawerInfo.drawer) -- Get the peripheral name of the drawer
- print("Attempting to push washed item to drawer: " .. drawerName)
- local success = depot.pushItems(drawerName, slot, item.count, 2)
- if success == 0 then
- print("Cannot push washed item to drawer: " .. drawerName .. "! Check storage capacity.")
- return false -- Failed push, continue checking other items
- else
- print("Moved " .. item.count .. " of " .. item.name .. " to drawer: " .. drawerName)
- end
- else
- print("No drawer available for washed item! Add or upgrade a drawer.")
- return false -- No drawer available for this item
- end
- else
- print("Item " .. item.name .. " is not a washed item.")
- end
- end
- return true -- Successfully processed all washed items
- end
- -- Main loop function
- local function main()
- while true do
- local drawers = getConnectedDrawers()
- -- Process each type of item separately
- local sandSuccess = processSand(drawers)
- local redSandSuccess = processRedSand(drawers)
- local gravelSuccess = processGravel(drawers)
- local soulSandSuccess = processSoulSand(drawers)
- os.sleep(10) -- washing happens in bulk, program can be lazy
- print("Moving washed items to drawers...")
- moveWashedItemsToDrawer(drawers)
- end
- end
- -- Start the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement