Polish RA, make it usable.

This commit is contained in:
Soph :3 2026-01-29 18:59:52 +02:00
parent 262c0b5408
commit 6825a05778
14 changed files with 771 additions and 289 deletions

View file

@ -13,85 +13,76 @@ local peripherals = peripheral.getNames();
local ail = nil
local function sync()
print("Syncing..")
---@type string[]
local foundInventories = {}
print("Syncing..")
---@type string[]
local foundInventories = {}
for _, invPattern in ipairs(config.inventories) do
for _, inv in ipairs(peripherals) do
if string.find(inv, invPattern) then
table.insert(foundInventories, inv)
end
end
for _, invPattern in ipairs(config.inventories) do
for _, inv in ipairs(peripherals) do
if string.find(inv, invPattern) then
table.insert(foundInventories, inv)
end
end
end
ail = abstractInventoryLib(foundInventories, true, {
--[[cache = true,
optimal = true,
unoptimal = true,
api = true,
defrag = true,
redirect = function (e)
print(e)
end,]]
})
ail = abstractInventoryLib(foundInventories, true, {})
print("Synced.");
print("Synced.");
end
---@param itemName string
---@param perip ccTweaked.peripheral.Inventory|string|nil
---@param maxAmount number|nil
local function sendItemToSelf(itemName, perip, maxAmount)
if perip == nil then
perip = turtleId
end
turtleMoveAllowed = false
if perip == nil then
perip = turtleId
end
turtleMoveAllowed = false
local remaining = maxAmount or 64
local total = 0
local remaining = maxAmount or 64
local total = 0
if remaining <= 0 then
turtleMoveAllowed = true
return
end
if #ail.listNBT(itemName) ~= 0 then
local nbtList = ail.listNBT(itemName)
local chosenNBT = nil
if nbtList and #nbtList > 0 then
chosenNBT = nbtList[math.random(1, #nbtList)]
end
if chosenNBT == "NONE" then
chosenNBT = nil
end
while remaining > 0 do
local toSend = math.min(64, remaining)
local ok, moved = pcall(function()
ail.performTransfer()
local amount = ail.pushItems(perip, itemName, toSend, nil, chosenNBT, {
["allowBadTransfers"] = true,
["optimal"] = false
})
ail.performTransfer()
return amount
end)
total = total + moved
if ok and moved and moved > 0 then
remaining = remaining - moved
else
break
end
end
end
if remaining <= 0 then
turtleMoveAllowed = true
return
end
return total
if #ail.listNBT(itemName) ~= 0 then
local nbtList = ail.listNBT(itemName)
local chosenNBT = nil
if nbtList and #nbtList > 0 then
chosenNBT = nbtList[math.random(1, #nbtList)]
end
if chosenNBT == "NONE" then
chosenNBT = nil
end
while remaining > 0 do
local toSend = math.min(64, remaining)
local ok, moved = pcall(function()
ail.performTransfer()
local amount = ail.pushItems(perip, itemName, toSend, nil, chosenNBT, {
["allowBadTransfers"] = true,
["optimal"] = false
})
ail.performTransfer()
return amount
end)
total = total + moved
if ok and moved and moved > 0 then
remaining = remaining - moved
else
break
end
end
end
turtleMoveAllowed = true
return total
end
---@param slots ccTweaked.turtle.turtleSlot[]
@ -99,111 +90,127 @@ end
---@param id string|nil
---@param maxAmount number|nil
local function sendItemAwayMultiple(slots, perip, id, maxAmount)
if perip == nil then
perip = turtle
if perip == nil then
perip = turtle
end
local srcId = id or turtleId
if srcId == nil then return end
local itemsInSlots = {}
for _, slot in ipairs(slots) do
local item = perip.getItemDetail(slot)
if item then
itemsInSlots[slot] = item
end
local srcId = id or turtleId
end
if srcId == nil then return end
local totalMoved = 0
local remaining = maxAmount or math.huge
local itemsInSlots = {}
for _, slot in ipairs(slots) do
local item = perip.getItemDetail(slot)
if item then
itemsInSlots[slot] = item
end
if remaining <= 0 then
return 0
end
for _, slot in ipairs(slots) do
if remaining <= 0 then break end
local toSend = math.min(64, remaining)
local ok, pulled = pcall(function()
ail.performTransfer()
local moved = ail.pullItems(srcId, slot, toSend, nil, nil, {
["allowBadTransfers"] = true,
["optimal"] = false
})
ail.performTransfer()
return moved
end)
if ok and pulled and pulled > 0 then
totalMoved = totalMoved + pulled
remaining = remaining - pulled
end
end
local totalMoved = 0
local remaining = maxAmount or math.huge
local numItems = 0
for _, _ in pairs(itemsInSlots) do
numItems = numItems + 1
end
if remaining <= 0 then
return 0
end
if numItems > 0 then
os.queueEvent("update_ui")
end
for _, slot in ipairs(slots) do
if remaining <= 0 then break end
local toSend = math.min(64, remaining)
local ok, pulled = pcall(function()
ail.performTransfer()
local moved = ail.pullItems(srcId, slot, toSend, nil, nil, {
["allowBadTransfers"] = true,
["optimal"] = false
})
ail.performTransfer()
return moved
end)
if ok and pulled and pulled > 0 then
totalMoved = totalMoved + pulled
remaining = remaining - pulled
end
end
local numItems = 0
for _, _ in pairs(itemsInSlots) do
numItems = numItems + 1
end
if numItems > 0 then
os.queueEvent("update_ui")
end
return totalMoved
return totalMoved
end
local function getTurtleInventory()
local inventory = {}
for slot = 1, 16 do
local item = turtle.getItemDetail(slot, false)
inventory[slot] = item
end
return inventory
local inventory = {}
for slot = 1, 16 do
local item = turtle.getItemDetail(slot, false)
inventory[slot] = item
end
return inventory
end
local function detectPlayerInsert()
if turtle then
previousInventory = getTurtleInventory()
while true do
os.pullEvent("turtle_inventory")
os.pullEvent("turtle_inventory")
local currentInventory = getTurtleInventory()
local currentInventory = getTurtleInventory()
if turtleMoveAllowed then
local newlyAddedSlots = {}
if turtleMoveAllowed then
local newlyAddedSlots = {}
for slot = 1, 16 do
local prev = previousInventory[slot]
local curr = currentInventory[slot]
for slot = 1, 16 do
local prev = previousInventory[slot]
local curr = currentInventory[slot]
if not prev and curr then
table.insert(newlyAddedSlots, slot)
end
end
if #newlyAddedSlots > 0 then
sendItemAwayMultiple(newlyAddedSlots)
end
if not prev and curr then
table.insert(newlyAddedSlots, slot)
end
end
previousInventory = currentInventory
if #newlyAddedSlots > 0 then
sendItemAwayMultiple(newlyAddedSlots)
end
end
previousInventory = currentInventory
end
end
end
local function getAIL()
return ail
local function run()
return ail.run
end
local function listNames()
return ail.listNames()
end
local function listItemAmounts()
return ail.listItemAmounts()
end
local function getItem(z)
return ail.getItem(z)
end
return {
detectPlayerInsert = detectPlayerInsert,
sendItemAwayMultiple = sendItemAwayMultiple,
sendItemToSelf = sendItemToSelf,
getTurtleInventory = getTurtleInventory,
sync = sync,
getAIL = getAIL,
detectPlayerInsert = detectPlayerInsert,
sendItemAwayMultiple = sendItemAwayMultiple,
sendItemToSelf = sendItemToSelf,
getTurtleInventory = getTurtleInventory,
listItemAmounts = listItemAmounts,
listNames = listNames,
getItem = getItem,
sync = sync,
run = run
}