diff --git a/views/main.lua b/views/main.lua index da287a6..d55a389 100644 --- a/views/main.lua +++ b/views/main.lua @@ -3,6 +3,20 @@ local ensure_width = require("cc.strings").ensure_width local ui = require("ui") local main_view = {} +local nbt_sensitive_names = { + "minecraft:potion", "minecraft:enchanted_book" +} + +local display_name_transformers = { + ["minecraft:enchanted_book"] = function(detail) + local enchantments = {} + for _, enchant in ipairs(detail.enchantments) do + table.insert(enchantments, enchant.displayName) + end + return table.concat(enchantments, ", ") .. " Book" + end +} + local function clamp(x, min, max) return math.min(math.max(x, min), max) end @@ -23,12 +37,43 @@ local function is_integer(num) return num % 1 == 0 end -local function list_items(inventories) +local function does_array_contain(array, value) + for _, v in ipairs(array) do + if v == value then + return true + end + end + return false +end + +local function add_item_detail(item_details, name, detail) + if item_details[name] then return end + + local display_name + if not display_name_transformers[detail.name] then + display_name = detail.displayName + else + display_name = display_name_transformers[detail.name](detail) + end + + item_details[name] = { + display_name = display_name, + stack_size = detail.maxCount + } +end + +local function list_items(inventories, item_details) local item_registry = {} for _, name in ipairs(inventories) do for slot, item in pairs(peripheral.call(name, "list")) do - item_registry[item.name] = item_registry[item.name] or {} - table.insert(item_registry[item.name], { + local item_name = item.name + if does_array_contain(nbt_sensitive_names, item_name) then + local detail = peripheral.call(name, "getItemDetail", slot) + item_name = item_name .. "#" .. item.nbt + add_item_detail(item_details, item_name, detail) + end + item_registry[item_name] = item_registry[item_name] or {} + table.insert(item_registry[item_name], { count = item.count, slot = slot, peripheral = name @@ -51,10 +96,7 @@ local function populate_item_details(item_details, item_registry) 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 - } + add_item_detail(item_details, name, detail) end end end @@ -209,8 +251,8 @@ function main_view:prepare(inventories, bundles, result_inventory) self.inventories = inventories remove_value(self.inventories, result_inventory) - self.item_registry = list_items(self.inventories) self.item_details = {} + self.item_registry = list_items(self.inventories, self.item_details) populate_item_details(self.item_details, self.item_registry) self.bundles = bundles or {} @@ -501,7 +543,7 @@ function main_view:has_selected_items() end function main_view:refresh_items() - self.item_registry = list_items(self.inventories) + self.item_registry = list_items(self.inventories, self.item_details) populate_item_details(self.item_details, self.item_registry) self.right_store.items = {}