properly abstract (Kinda?) inv_ail and inv_ra

This commit is contained in:
Soph :3 2026-01-30 16:48:53 +02:00
parent 6825a05778
commit d86ab173e1
7 changed files with 224 additions and 239 deletions

View file

@ -23,13 +23,7 @@
local config = require("../config") ---@type Config local config = require("../config") ---@type Config
local inv; local inv = require("modules.inv")
if config.inventories == nil and config.remote.connection then
inv = require("modules.inv_ra")
else
inv = require("modules.inv")
end
local ui = require("modules.ui") local ui = require("modules.ui")
local chatbox = require("modules.chatbox") local chatbox = require("modules.chatbox")
local ra = require("modules.ra") local ra = require("modules.ra")

View file

@ -1,11 +1,6 @@
local config = require("../../config") ---@type Config local config = require("../../config") ---@type Config
local inv; local inv = require("modules.inv")
if config.inventories == nil and config.remote.connection then
inv = require("modules.inv_ra")
else
inv = require("modules.inv")
end
local function levDist(s, t) local function levDist(s, t)
local n = #s local n = #s
local m = #t local m = #t

View file

@ -1,216 +1,6 @@
local previousInventory = {}
---@type ccTweaked.peripheral.WiredModem
local modem = peripheral.find("modem");
local turtleId = modem.getNameLocal()
local turtleMoveAllowed = true
local config = require("../../config") ---@type Config local config = require("../../config") ---@type Config
if config.inventories == nil and config.remote.connection then
local abstractInventoryLib = require("lib.abstractInventoryLib") return require("modules.inventory_layers.inv_ra")
else
local peripherals = peripheral.getNames(); return require("modules.inventory_layers.inv_ail")
---@type AbstractInventory
local ail = nil
local function sync()
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
end
ail = abstractInventoryLib(foundInventories, true, {})
print("Synced.");
end 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
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
turtleMoveAllowed = true
return total
end
---@param slots ccTweaked.turtle.turtleSlot[]
---@param perip ccTweaked.peripheral.Inventory|nil
---@param id string|nil
---@param maxAmount number|nil
local function sendItemAwayMultiple(slots, perip, id, maxAmount)
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
end
local totalMoved = 0
local remaining = maxAmount or math.huge
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 numItems = 0
for _, _ in pairs(itemsInSlots) do
numItems = numItems + 1
end
if numItems > 0 then
os.queueEvent("update_ui")
end
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
end
local function detectPlayerInsert()
if turtle then
previousInventory = getTurtleInventory()
while true do
os.pullEvent("turtle_inventory")
local currentInventory = getTurtleInventory()
if turtleMoveAllowed then
local newlyAddedSlots = {}
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
end
previousInventory = currentInventory
end
end
end
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,
listItemAmounts = listItemAmounts,
listNames = listNames,
getItem = getItem,
sync = sync,
run = run
}

View file

@ -0,0 +1,216 @@
local previousInventory = {}
---@type ccTweaked.peripheral.WiredModem
local modem = peripheral.find("modem");
local turtleId = modem.getNameLocal()
local turtleMoveAllowed = true
local config = require("../../config") ---@type Config
local abstractInventoryLib = require("lib.abstractInventoryLib")
local peripherals = peripheral.getNames();
---@type AbstractInventory
local ail = nil
local function sync()
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
end
ail = abstractInventoryLib(foundInventories, true, {})
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
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
turtleMoveAllowed = true
return total
end
---@param slots ccTweaked.turtle.turtleSlot[]
---@param perip ccTweaked.peripheral.Inventory|nil
---@param id string|nil
---@param maxAmount number|nil
local function sendItemAwayMultiple(slots, perip, id, maxAmount)
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
end
local totalMoved = 0
local remaining = maxAmount or math.huge
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 numItems = 0
for _, _ in pairs(itemsInSlots) do
numItems = numItems + 1
end
if numItems > 0 then
os.queueEvent("update_ui")
end
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
end
local function detectPlayerInsert()
if turtle then
previousInventory = getTurtleInventory()
while true do
os.pullEvent("turtle_inventory")
local currentInventory = getTurtleInventory()
if turtleMoveAllowed then
local newlyAddedSlots = {}
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
end
previousInventory = currentInventory
end
end
end
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,
listItemAmounts = listItemAmounts,
listNames = listNames,
getItem = getItem,
sync = sync,
run = run
}

View file

@ -1,9 +1,5 @@
local config = require("../../config") ---@type Config local config = require("../../config") ---@type Config
if config.inventories == nil and config.remote.connection then local inv = require("modules.inv")
inv = require("modules.inv_ra")
else
inv = require("modules.inv")
end
local encryption = require("lib.encryption") local encryption = require("lib.encryption")

View file

@ -1,10 +1,4 @@
local config = require("../../config") ---@type Config local inv = require("modules.inv")
local inv;
if config.inventories == nil and config.remote.connection then
inv = require("modules.inv_ra")
else
inv = require("modules.inv")
end
local PrimeUI = require("lib.primeui").PrimeUI; local PrimeUI = require("lib.primeui").PrimeUI;
local function is_beta(ver) local function is_beta(ver)