Migrate identifier logic to lib
This commit is contained in:
parent
d1207f728e
commit
910368346f
@ -17,26 +17,19 @@ local inventories = w.find_peripherals(function(type, name, wrapped)
|
|||||||
return string.find(name, "chest") or string.find(name, "shulker")
|
return string.find(name, "chest") or string.find(name, "shulker")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local nameCache = {}
|
local display_name_cache = {}
|
||||||
|
|
||||||
-- 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
|
|
||||||
|
|
||||||
-- Gets the display name of the given item (in the given chest peripheral & slot)
|
-- Gets the display name of the given item (in the given chest peripheral & slot)
|
||||||
-- If its name is not cached, cache it.
|
-- If its name is not cached, cache it.
|
||||||
-- If it is, just return the cached name
|
-- If it is, just return the cached name
|
||||||
local function cache(item, chest, slot)
|
local function cache(item, chest, slot)
|
||||||
local idx = get_cache_name(item)
|
local idx = w.get_internal_identifier(item)
|
||||||
|
|
||||||
if nameCache[idx] then
|
if display_name_cache[idx] then
|
||||||
return nameCache[idx]
|
return display_name_cache[idx]
|
||||||
else
|
else
|
||||||
local n = chest.getItemMeta(slot).display_name
|
local n = chest.getItemMeta(slot).display_name
|
||||||
nameCache[idx] = n
|
display_name_cache[idx] = n
|
||||||
return n
|
return n
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
11
lib.lua
11
lib.lua
@ -4,6 +4,8 @@ Contains:
|
|||||||
error handling (a set of usable errors and human-readable printing for them),
|
error handling (a set of usable errors and human-readable printing for them),
|
||||||
networking (a simple node-based system on top of rednet)
|
networking (a simple node-based system on top of rednet)
|
||||||
configuration (basically just loading serialized tables from a file)
|
configuration (basically just loading serialized tables from a file)
|
||||||
|
general functions of utility
|
||||||
|
Plethora helpers
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local d = require "luadash"
|
local d = require "luadash"
|
||||||
@ -110,6 +112,15 @@ local function query_by_type(type, request, tries)
|
|||||||
return query_by_ID(ID, request, tries)
|
return query_by_ID(ID, request, tries)
|
||||||
end
|
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
|
-- GENERAL STUFF
|
||||||
|
|
||||||
-- Loads a config file (in serialized-table format) from "filename" or wyvern_config.tbl
|
-- Loads a config file (in serialized-table format) from "filename" or wyvern_config.tbl
|
||||||
|
Loading…
Reference in New Issue
Block a user