154 lines
3.4 KiB
Lua
154 lines
3.4 KiB
Lua
local OUTPUT_NAME = "minecraft:barrel_15"
|
|
local LESS_COUNT = 20
|
|
|
|
local output = peripheral.wrap(OUTPUT_NAME)
|
|
|
|
local containers = { peripheral.find("minecraft:barrel") }
|
|
|
|
local args = {...}
|
|
|
|
function searchInv(inv, targetName)
|
|
for slot, item in pairs(inv.list()) do
|
|
if item.name == targetName then
|
|
return slot
|
|
end
|
|
end
|
|
end
|
|
|
|
function findItem(targetName, count)
|
|
if count == nil then count = 64 end
|
|
|
|
for _, container in pairs(containers) do
|
|
if peripheral.getName(container) ~= OUTPUT_NAME then
|
|
local slot = searchInv(container ,targetName)
|
|
if slot ~= nil then
|
|
local moveCount = output.pullItems(peripheral.getName(container), slot, count)
|
|
count = count - moveCount
|
|
print("Took " .. moveCount .. " from " .. peripheral.getName(container))
|
|
if count <= 0 then return end
|
|
end
|
|
end
|
|
end
|
|
|
|
print("Could not find the remaining " .. count)
|
|
end
|
|
|
|
function store()
|
|
for slot, item in pairs(output.list()) do
|
|
print("Storing " .. item.name)
|
|
for _, container in pairs(containers) do
|
|
if peripheral.getName(container) ~= OUTPUT_NAME then
|
|
output.pushItems(peripheral.getName(container), slot)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function listStorage()
|
|
local lineCount = 0
|
|
|
|
for _, storage in pairs(containers) do
|
|
print(peripheral.getName(storage))
|
|
lineCount = lineCount + 1
|
|
|
|
if lineCount >= LESS_COUNT then
|
|
print("Press ENTER to continue...")
|
|
read()
|
|
lineCount = 0
|
|
end
|
|
end
|
|
end
|
|
|
|
function listItems()
|
|
local lineCount = 0
|
|
|
|
for _, container in pairs(containers) do
|
|
|
|
for _, item in pairs(container.list()) do
|
|
print(item.name .. " x " .. item.count)
|
|
lineCount = lineCount + 1
|
|
end
|
|
|
|
if lineCount >= LESS_COUNT then
|
|
print("Press ENTER to continue...")
|
|
read()
|
|
lineCount = 0
|
|
end
|
|
end
|
|
end
|
|
|
|
function listFull()
|
|
for _, container in pairs(containers) do
|
|
print(peripheral.getName(container))
|
|
print("---------------------------------------------")
|
|
local lineCount = 0
|
|
for _, item in pairs(container.list()) do
|
|
print("\t" .. item.name .. " x " .. item.count)
|
|
lineCount = lineCount + 1
|
|
if lineCount >= LESS_COUNT then
|
|
print("Press ENTER to continue...")
|
|
read()
|
|
lineCount = 0
|
|
end
|
|
end
|
|
print("Press ENTER to continue...")
|
|
read()
|
|
end
|
|
end
|
|
|
|
function list(args)
|
|
local target = args[2]
|
|
|
|
if target == nil then
|
|
listFull()
|
|
return
|
|
end
|
|
|
|
local targets = {
|
|
storage = function()
|
|
listStorage()
|
|
end,
|
|
|
|
items = function()
|
|
listItems()
|
|
end
|
|
}
|
|
|
|
targets[target]()
|
|
end
|
|
|
|
-- HANDLERS
|
|
local handlers = {
|
|
get = function(args)
|
|
local targetName = args[2]
|
|
local count = tonumber(args[3])
|
|
findItem(targetName, count)
|
|
end,
|
|
|
|
store = function(args)
|
|
store(args)
|
|
end,
|
|
|
|
list = function(args)
|
|
list(args)
|
|
end
|
|
}
|
|
|
|
function argParser(args)
|
|
local op = args[1]
|
|
local handler = handlers[op]
|
|
|
|
if handler == nil then
|
|
print("Invalid operation.")
|
|
return
|
|
end
|
|
|
|
handler(args)
|
|
|
|
end
|
|
|
|
function main()
|
|
argParser(args)
|
|
end
|
|
|
|
main() |