Skip to content

Instantly share code, notes, and snippets.

@isochronous
Last active June 1, 2025 22:46
Show Gist options
  • Save isochronous/c2b9c5512203010be2f98f66177d35c5 to your computer and use it in GitHub Desktop.
Save isochronous/c2b9c5512203010be2f98f66177d35c5 to your computer and use it in GitHub Desktop.
ComputerCraft program to maintain a certain stock of items in an ender chest based on the contents of a template chest
-- Peripheral names
local sourceName = "storagedrawers:controllerslave_0"
local templateContainerName = "minecraft:ironchest_crystal_0"
local outputName = "minecraft:ender chest_4"
-- Wrap peripherals
local source = peripheral.wrap(sourceName)
local templateContainer = peripheral.wrap(templateContainerName)
local output = peripheral.wrap(outputName)
-- Item limit for the output chest
local itemLimit = 64
-- Main loop
while true do
-- Fetch the template container inventory dynamically
local templateList = templateContainer.list()
-- Iterate over each slot in the template container
for templateSlot, templateItem in pairs(templateList) do
-- Fetch metadata for the template item
local templateName = templateItem.name
local templateMeta = templateContainer.getItemMeta(templateSlot)
local templateRawName = templateMeta.rawName
-- Count how many of this item are already in the output chest
local outputList = output.list()
local outputCount = 0
for outputSlot, outputItem in pairs(outputList) do
if outputItem.name == templateName then
-- Fetch metadata to confirm rawName matches
local outputMeta = output.getItemMeta(outputSlot)
if outputMeta and outputMeta.rawName == templateRawName then
outputCount = outputCount + outputMeta.count
end
end
end
-- Calculate how many more items are needed to reach the item limit
local neededCount = itemLimit - outputCount
if neededCount > 0 then
-- Fetch the source inventory dynamically
local sourceList = source.list()
-- Find the item in the source and transfer the required amount
for sourceSlot, sourceItem in pairs(sourceList) do
if sourceItem.name == templateName then
-- Fetch metadata to confirm rawName matches
local sourceMeta = source.getItemMeta(sourceSlot)
if sourceMeta and sourceMeta.rawName == templateRawName then
local transferCount = math.min(neededCount, sourceMeta.count)
source.pushItems(outputName, sourceSlot, transferCount)
end
end
end
end
end
-- Add a small delay to prevent excessive CPU usage
sleep(0.1)
end
@isochronous
Copy link
Author

I have an ender chest that I use to provide a single stack of arbitrary items to various different automation setups. This program simply maintains that ender chests's stock from a storage drawer network. The list of items to maintain is defined by the contents of the "template" chest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment