1
0

seperate item counts and their details

This commit is contained in:
Rokas Puzonas 2022-08-28 20:26:33 +00:00
parent a75607f890
commit 3fe0246d57

View File

@ -12,11 +12,11 @@ local function rect(x, y, w, h)
end
local function list_items(inventories)
local items = {}
local item_registry = {}
for _, name in ipairs(inventories) do
for slot, item in pairs(peripheral.call(name, "list")) do
items[item.name] = items[item.name] or {}
table.insert(items[item.name], {
item_registry[item.name] = item_registry[item.name] or {}
table.insert(item_registry[item.name], {
count = item.count,
slot = slot,
peripheral = name
@ -24,19 +24,27 @@ local function list_items(inventories)
end
end
for name, item_collection in pairs(items) do
local item = item_collection[1]
local detail = peripheral.call(item.peripheral, "getItemDetail", item.slot)
items[name].displayName = detail.displayName
items[name].stack_size = detail.maxCount
item_collection.count = 0
for _, slot in ipairs(item_collection) do
item_collection.count = item_collection.count + slot.count
for _, items in pairs(item_registry) do
items.count = 0
for _, slot in ipairs(items) do
items.count = items.count + slot.count
end
end
return items
return item_registry
end
local function populate_item_details(item_details, item_registry)
for name, items in pairs(item_registry) do
local item = items[1]
if item and not item_details[name] then
local detail = peripheral.call(item.peripheral, "getItemDetail", item.slot)
item_details[name] = {
display_name = detail.displayName,
stack_size = detail.maxCount
}
end
end
end
local function remove_value(array, value)
@ -81,9 +89,12 @@ function main_view:prepare(inventories, result_inventory)
self.result_inventory = result_inventory
self.inventories = inventories
remove_value(self.inventories, result_inventory)
self.items = list_items(self.inventories)
local all_item_counts = get_item_counts(self.items)
self.item_registry = list_items(self.inventories)
self.item_details = {}
populate_item_details(self.item_details, self.item_registry)
local all_item_counts = get_item_counts(self.item_registry)
self.left_store = {
all_items = all_item_counts,
filtered_names = self:list_filtered_names(all_item_counts, ""),
@ -105,13 +116,19 @@ function main_view:list_filtered_names(all_items, name_filter)
local names = {}
for name, count in pairs(all_items) do
local displayName = self.items[name].displayName:lower()
if displayName:find(name_filter) and count > 0 then
local display_name = self.item_details[name].display_name:lower()
if display_name:find(name_filter) and count > 0 then
table.insert(names, name)
end
end
table.sort(names, function(a, b) return all_items[a] > all_items[b] end)
table.sort(names, function(a, b)
if all_items[a] == all_items[b] then
return a < b
else
return all_items[a] > all_items[b]
end
end)
return names
end
@ -134,7 +151,7 @@ function main_view:display_item_list(store, area, active)
local i = row + store.scroll
local name = filtered_names[i]
local count = store.all_items[name]
local item_info = self.items[name]
local item_info = self.item_details[name]
local count_width = math.floor(math.log(count, 10) + 1)
if i == store.selected_idx and active then
term.setTextColor(colors.yellow)
@ -144,7 +161,7 @@ function main_view:display_item_list(store, area, active)
term.setCursorPos((max_count_width+1 - count_width), row)
term.write(count)
term.setCursorPos(max_count_width+2, row)
term.write(ensure_width(item_info.displayName, area.w-max_count_width-1))
term.write(ensure_width(item_info.display_name, area.w-max_count_width-1))
end
if shown_count > area.h then
@ -157,7 +174,7 @@ function main_view:display_item_list(store, area, active)
end
function main_view:move_item(item_name, amount, destination)
local item_collection = self.items[item_name]
local item_collection = self.item_registry[item_name]
local moved_amount = 0
while moved_amount < amount and item_collection.count > 0 do
@ -287,8 +304,10 @@ function main_view:on_key(key)
elseif key == keys.tab then
self.is_left_active = not self.is_left_active
elseif key == keys.f5 then
self.items = list_items(self.inventories)
self.left_store.all_items = get_item_counts(self.items)
self.item_registry = list_items(self.inventories)
populate_item_details(self.item_details, self.item_registry)
self.left_store.all_items = get_item_counts(self.item_registry)
self.right_store.all_items = {}
self:refresh_filtered_names(self.left_store, self.left_area)
self:refresh_filtered_names(self.right_store, self.right_area)
@ -308,19 +327,20 @@ function main_view:on_key(key)
else
if left_selected then
local item = left_selected
local stack_size = self.items[item].stack_size
local stack_size = self.item_details[item].stack_size
local transferred_count = self:move_item(item, stack_size, self.result_inventory)
self.left_store.all_items[item] = self.left_store.all_items[item] - transferred_count
self:refresh_filtered_names(self.left_store, self.left_area)
self:select_item(self.left_store, self.left_area, item)
end
end
elseif key == keys.d then
end
if self.is_left_active then
if key == keys.l and left_selected then
local item = left_selected
local stack_size = self.items[item].stack_size
local stack_size = self.item_details[item].stack_size
local transferred = math.min(self.left_store.all_items[item], stack_size)
self.left_store.all_items[item] = self.left_store.all_items[item] - transferred
self.right_store.all_items[item] = (self.right_store.all_items[item] or 0) + transferred
@ -331,7 +351,7 @@ function main_view:on_key(key)
else
if key == keys.h and right_selected then
local item = right_selected
local stack_size = self.items[item].stack_size
local stack_size = self.item_details[item].stack_size
local transferred = math.min(self.right_store.all_items[item], stack_size)
self.left_store.all_items[item] = self.left_store.all_items[item] + transferred
self.right_store.all_items[item] = self.right_store.all_items[item] - transferred