diff --git a/src/main.lua b/src/main.lua index 12d3ced..4547aa3 100644 --- a/src/main.lua +++ b/src/main.lua @@ -23,13 +23,7 @@ local config = require("../config") ---@type Config -local inv; - -if config.inventories == nil and config.remote.connection then - inv = require("modules.inv_ra") -else - inv = require("modules.inv") -end +local inv = require("modules.inv") local ui = require("modules.ui") local chatbox = require("modules.chatbox") local ra = require("modules.ra") diff --git a/src/modules/chatbox.lua b/src/modules/chatbox.lua index 2f458ee..4f2d21b 100644 --- a/src/modules/chatbox.lua +++ b/src/modules/chatbox.lua @@ -1,11 +1,6 @@ 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 n = #s local m = #t diff --git a/src/modules/inv.lua b/src/modules/inv.lua index 99ffe41..0048938 100644 --- a/src/modules/inv.lua +++ b/src/modules/inv.lua @@ -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 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."); +if config.inventories == nil and config.remote.connection then + return require("modules.inventory_layers.inv_ra") +else + return require("modules.inventory_layers.inv_ail") 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 -} diff --git a/src/modules/inventory_layers/inv_ail.lua b/src/modules/inventory_layers/inv_ail.lua new file mode 100644 index 0000000..99ffe41 --- /dev/null +++ b/src/modules/inventory_layers/inv_ail.lua @@ -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 +} diff --git a/src/modules/inv_ra.lua b/src/modules/inventory_layers/inv_ra.lua similarity index 100% rename from src/modules/inv_ra.lua rename to src/modules/inventory_layers/inv_ra.lua diff --git a/src/modules/ra.lua b/src/modules/ra.lua index a54fd4f..847abe5 100644 --- a/src/modules/ra.lua +++ b/src/modules/ra.lua @@ -1,9 +1,5 @@ local config = require("../../config") ---@type Config -if config.inventories == nil and config.remote.connection then - inv = require("modules.inv_ra") -else - inv = require("modules.inv") -end +local inv = require("modules.inv") local encryption = require("lib.encryption") diff --git a/src/modules/ui.lua b/src/modules/ui.lua index 453019e..12ec7cb 100644 --- a/src/modules/ui.lua +++ b/src/modules/ui.lua @@ -1,10 +1,4 @@ -local config = require("../../config") ---@type Config -local inv; -if config.inventories == nil and config.remote.connection then - inv = require("modules.inv_ra") -else - inv = require("modules.inv") -end +local inv = require("modules.inv") local PrimeUI = require("lib.primeui").PrimeUI; local function is_beta(ver)