feat: add find_item functionality to locate items in chests

This commit is contained in:
MayaTheShy
2026-03-22 21:42:58 -04:00
parent d25f25ee52
commit 72c978ee75

View File

@@ -705,6 +705,43 @@ local function main()
state.bumpStateVersion()
end
pcall(broadcastState)
elseif message.type == "find_item" and message.items then
-- Return chest+slot locations for the first matching item
-- message.items = list of item names to search (in priority order)
-- message.limit = max items to return info for (default 64)
local limit = message.limit or 64
local results = {}
for _, itemName in ipairs(message.items) do
if cache.catalogue[itemName] then
for _, source in ipairs(cache.catalogue[itemName]) do
local chest = ops.wrapCached(source.chest)
if chest then
for slot, slotItem in pairs(chest.list()) do
if slotItem.name == itemName then
table.insert(results, {
chest = source.chest,
slot = slot,
name = itemName,
count = slotItem.count,
})
if #results >= limit then break end
end
end
end
if #results >= limit then break end
end
end
if #results > 0 then break end -- found fuel, stop searching
end
log.info("NET", "find_item: found %d source(s)", #results)
pcall(function()
ctx.networkModem.transmit(replyChannel, cfg.ORDER_CHANNEL, {
type = "find_item_result",
commandId = message.commandId,
results = results,
})
end)
end
end) -- pcall handler