Migrate identifier logic to lib

This commit is contained in:
osmarks 2018-07-26 16:32:48 +01:00
parent d1207f728e
commit 910368346f
2 changed files with 16 additions and 12 deletions

View File

@ -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

11
lib.lua
View File

@ -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