From 910368346f059585d4ebd2e8d865ce1ad70c92e1 Mon Sep 17 00:00:00 2001 From: osmarks Date: Thu, 26 Jul 2018 16:32:48 +0100 Subject: [PATCH] Migrate identifier logic to lib --- backend-chests.lua | 17 +++++------------ lib.lua | 11 +++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/backend-chests.lua b/backend-chests.lua index e3af46e..c75a620 100644 --- a/backend-chests.lua +++ b/backend-chests.lua @@ -17,26 +17,19 @@ local inventories = w.find_peripherals(function(type, name, wrapped) return string.find(name, "chest") or string.find(name, "shulker") end) -local nameCache = {} - --- Get the name for an item in the cache -local function get_cache_name(item) - local n = item.name .. ":" .. item.damage - if item.nbtHash then n = n .. "#" .. item.nbtHash end - return n -end +local display_name_cache = {} -- Gets the display name of the given item (in the given chest peripheral & slot) -- If its name is not cached, cache it. -- If it is, just return the cached name local function cache(item, chest, slot) - local idx = get_cache_name(item) + local idx = w.get_internal_identifier(item) - if nameCache[idx] then - return nameCache[idx] + if display_name_cache[idx] then + return display_name_cache[idx] else local n = chest.getItemMeta(slot).display_name - nameCache[idx] = n + display_name_cache[idx] = n return n end end diff --git a/lib.lua b/lib.lua index 4570d4d..208fad8 100644 --- a/lib.lua +++ b/lib.lua @@ -4,6 +4,8 @@ Contains: error handling (a set of usable errors and human-readable printing for them), networking (a simple node-based system on top of rednet) configuration (basically just loading serialized tables from a file) +general functions of utility +Plethora helpers ]] local d = require "luadash" @@ -110,6 +112,15 @@ local function query_by_type(type, request, tries) return query_by_ID(ID, request, tries) end +-- PLETHORA HELPERS + +-- Gets the internal identifier of an item - unique (hopefully) per type of item, as defined by NBT, metadata/damage and ID/name +local function get_internal_identifier(item) + local n = item.name .. ":" .. item.damage + if item.nbtHash then n = n .. "#" .. item.nbtHash end + return n +end + -- GENERAL STUFF -- Loads a config file (in serialized-table format) from "filename" or wyvern_config.tbl