View difference between Paste ID: CCMarV8X and wKqtBNmq
SHOW: | | - or go back to the newest paste.
1-
-- delete inventory-i.lua
1+
-- delete inventory-ii.lua
2-
-- pastebin get wKqtBNmq inventory-i.lua
2+
-- pastebin get CCMarV8X inventory-ii.lua
3
-- List of chests
4
cobblestone_chests = {"minecraft:chest_11", "minecraft:chest_10", "minecraft:chest_9"}
5-
    if not chest then
5+
utility_chests = {"minecraft:chest_0"}
6-
        print("Error: Invalid chest peripheral!")
6+
7-
        return 0
7+
8
local function checkChestForCobblestone(chest)
9-
    
9+
10
    for slot, item in pairs(chest.list()) do
11
        if item and item.name == "minecraft:cobblestone" then
12
            cobblestoneCount = cobblestoneCount + item.count
13
        end
14
    end
15
    return cobblestoneCount
16
end
17
18
-- Function to transfer cobblestone to a cobblestone chest
19-
-- Function to find all chests on the network and check for cobblestone
19+
local function transferCobblestoneToChest(fromChest, cobblestoneChest)
20-
local function reportCobblestoneOnNetwork()
20+
    for slot, item in pairs(fromChest.list()) do
21
        if item and item.name == "minecraft:cobblestone" then
22
            local excessAmount = item.count
23
            -- Find a free spot in the cobblestone chest
24
            for i = 1, 27 do -- Assuming chest has 27 slots
25-
            print("Found chest: " .. name)
25+
                local targetItem = cobblestoneChest.getStackInSlot(i)
26-
            chest = peripheral.wrap(name)
26+
                if not targetItem or targetItem.name == "minecraft:cobblestone" then
27-
            -- Checking if we successfully wrapped the chest	
27+
                    local transferAmount = math.min(excessAmount, 64)
28
                    cobblestoneChest.pushItems(fromChest, slot, transferAmount, i)
29-
            if cobblestoneCount > 0 then
29+
                    excessAmount = excessAmount - transferAmount
30-
                print("Cobblestone in chest at " .. name .. ": " .. cobblestoneCount)
30+
                    if excessAmount == 0 then break end
31
                end
32
            end
33
        end
34
    end
35
end
36
37
-- Function to manage cobblestone in utility chests and transfer to cobblestone chests
38-
-- Run the report
38+
local function manageCobblestoneInChests()
39-
reportCobblestoneOnNetwork()
39+
40
    for _, name in ipairs(peripheralNames) do
41
        local peripheralType = peripheral.getType(name)
42
        if peripheralType == "minecraft:chest" then
43
            local chest = peripheral.wrap(name)
44
            local cobblestoneCount = checkChestForCobblestone(chest)
45
46
            -- If it's a utility chest and has more than 64 cobblestone, transfer excess
47
            if table.contains(utility_chests, name) then
48
                if cobblestoneCount > 64 then
49
                    print("Utility chest " .. name .. " has more than 64 cobblestone, transferring excess.")
50
                    transferCobblestoneToChest(chest, peripheral.wrap(cobblestone_chests[1]))  -- Transfer to first cobblestone chest
51
                elseif cobblestoneCount < 64 then
52
                    print("Utility chest " .. name .. " has less than 64 cobblestone, no action needed.")
53
                end
54
            -- For non-utility chests, transfer all cobblestone to a cobblestone chest
55
            else
56
                if cobblestoneCount > 0 then
57
                    print("Transferring cobblestone from chest " .. name .. " to cobblestone chest.")
58
                    transferCobblestoneToChest(chest, peripheral.wrap(cobblestone_chests[1]))  -- Transfer to first cobblestone chest
59
                end
60
            end
61
        else
62
            print("Skipping " .. name .. " (not a chest)")
63
        end
64
    end
65
end
66
67
-- Helper function to check if an element is in a table
68
function table.contains(table, element)
69
    for _, value in ipairs(table) do
70
        if value == element then
71
            return true
72
        end
73
    end
74
    return false
75
end
76
77
-- Run the cobblestone management
78
manageCobblestoneInChests()
79