Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- delete inventory-ii.lua
- -- pastebin get CCMarV8X inventory-ii.lua
- -- List of chests
- cobblestone_chests = {"minecraft:chest_11", "minecraft:chest_10", "minecraft:chest_9"}
- utility_chests = {"minecraft:chest_0"}
- -- Define a function to check a chest's inventory for cobblestone
- local function checkChestForCobblestone(chest)
- local cobblestoneCount = 0
- for slot, item in pairs(chest.list()) do
- if item and item.name == "minecraft:cobblestone" then
- cobblestoneCount = cobblestoneCount + item.count
- end
- end
- return cobblestoneCount
- end
- -- Function to transfer cobblestone to a cobblestone chest
- local function transferCobblestoneToChest(fromChest, cobblestoneChest)
- for slot, item in pairs(fromChest.list()) do
- if item and item.name == "minecraft:cobblestone" then
- local excessAmount = item.count
- -- Find a free spot in the cobblestone chest
- for i = 1, 27 do -- Assuming chest has 27 slots
- local targetItem = cobblestoneChest.getStackInSlot(i)
- if not targetItem or targetItem.name == "minecraft:cobblestone" then
- local transferAmount = math.min(excessAmount, 64)
- cobblestoneChest.pushItems(fromChest, slot, transferAmount, i)
- excessAmount = excessAmount - transferAmount
- if excessAmount == 0 then break end
- end
- end
- end
- end
- end
- -- Function to manage cobblestone in utility chests and transfer to cobblestone chests
- local function manageCobblestoneInChests()
- local peripheralNames = peripheral.getNames()
- for _, name in ipairs(peripheralNames) do
- local peripheralType = peripheral.getType(name)
- if peripheralType == "minecraft:chest" then
- local chest = peripheral.wrap(name)
- local cobblestoneCount = checkChestForCobblestone(chest)
- -- If it's a utility chest and has more than 64 cobblestone, transfer excess
- if table.contains(utility_chests, name) then
- if cobblestoneCount > 64 then
- print("Utility chest " .. name .. " has more than 64 cobblestone, transferring excess.")
- transferCobblestoneToChest(chest, peripheral.wrap(cobblestone_chests[1])) -- Transfer to first cobblestone chest
- elseif cobblestoneCount < 64 then
- print("Utility chest " .. name .. " has less than 64 cobblestone, no action needed.")
- end
- -- For non-utility chests, transfer all cobblestone to a cobblestone chest
- else
- if cobblestoneCount > 0 then
- print("Transferring cobblestone from chest " .. name .. " to cobblestone chest.")
- transferCobblestoneToChest(chest, peripheral.wrap(cobblestone_chests[1])) -- Transfer to first cobblestone chest
- end
- end
- else
- print("Skipping " .. name .. " (not a chest)")
- end
- end
- end
- -- Helper function to check if an element is in a table
- function table.contains(table, element)
- for _, value in ipairs(table) do
- if value == element then
- return true
- end
- end
- return false
- end
- -- Run the cobblestone management
- manageCobblestoneInChests()
Add Comment
Please, Sign In to add comment