milo: rework potion naming + storage cache updating
This commit is contained in:
@@ -97,6 +97,16 @@ function itemDB:get(key)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function formatTime(t)
|
||||||
|
local m = math.floor(t/60)
|
||||||
|
local s = t % 60
|
||||||
|
if s < 10 then
|
||||||
|
s = '0' .. s
|
||||||
|
end
|
||||||
|
|
||||||
|
return m .. ':' .. s
|
||||||
|
end
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
If the base item contains an NBT hash, then the NBT hash uniquely
|
If the base item contains an NBT hash, then the NBT hash uniquely
|
||||||
identifies this item.
|
identifies this item.
|
||||||
@@ -115,19 +125,33 @@ function itemDB:add(baseItem)
|
|||||||
nItem.maxCount = baseItem.maxCount
|
nItem.maxCount = baseItem.maxCount
|
||||||
nItem.maxDamage = baseItem.maxDamage
|
nItem.maxDamage = baseItem.maxDamage
|
||||||
|
|
||||||
-- potions can have the same damage, diff nbts, but same names
|
-- enchanted items
|
||||||
-- ie. potion of healing, potion of healing II
|
|
||||||
-- both show as "Potion of Healing"
|
|
||||||
|
|
||||||
if baseItem.enchantments then
|
if baseItem.enchantments then
|
||||||
nItem.displayName = nItem.displayName .. ': '
|
if nItem.name == 'minecraft:enchanted_book' then
|
||||||
|
nItem.displayName = 'Book: '
|
||||||
|
else
|
||||||
|
nItem.displayName = nItem.displayName .. ': '
|
||||||
|
end
|
||||||
for k, v in ipairs(baseItem.enchantments) do
|
for k, v in ipairs(baseItem.enchantments) do
|
||||||
if k > 1 then
|
if k > 1 then
|
||||||
nItem.displayName = nItem.displayName .. ', '
|
nItem.displayName = nItem.displayName .. ', '
|
||||||
end
|
end
|
||||||
nItem.displayName = nItem.displayName .. v.fullName
|
nItem.displayName = nItem.displayName .. v.fullName
|
||||||
end
|
end
|
||||||
elseif nItem.name ~= 'minecraft:potion' --[[ HACK ]] then
|
|
||||||
|
-- potions
|
||||||
|
elseif nItem.name == 'minecraft:potion' or nItem.name == 'minecraft:lingering_potion' then
|
||||||
|
if baseItem.effects then
|
||||||
|
local effect = baseItem.effects[1]
|
||||||
|
if effect.amplifier == 1 then
|
||||||
|
nItem.displayName = nItem.displayName .. ' II'
|
||||||
|
end
|
||||||
|
if effect.duration and effect.duration > 0 then
|
||||||
|
nItem.displayName = string.format('%s (%s)', nItem.displayName, formatTime(effect.duration))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
for k,item in pairs(self.data) do
|
for k,item in pairs(self.data) do
|
||||||
if nItem.name == item.name and
|
if nItem.name == item.name and
|
||||||
nItem.displayName == item.displayName then
|
nItem.displayName == item.displayName then
|
||||||
|
|||||||
@@ -200,34 +200,24 @@ _G._debug('STORAGE: refresh in ' .. timer())
|
|||||||
return cache
|
return cache
|
||||||
end
|
end
|
||||||
|
|
||||||
function Storage:updateCache(adapter, key, count)
|
function Storage:updateCache(adapter, item, count)
|
||||||
if not adapter.cache then
|
if not adapter.cache then
|
||||||
adapter.dirty = true
|
adapter.dirty = true
|
||||||
self.dirty = true
|
self.dirty = true
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
||||||
local entry = adapter.cache[key]
|
local entry = adapter.cache[key]
|
||||||
|
|
||||||
if not entry then
|
if not entry then
|
||||||
if count < 0 then
|
if count < 0 then
|
||||||
adapter.dirty = true
|
_G._debug('STORAGE: update cache - count < 0', 4)
|
||||||
self.dirty = true
|
|
||||||
else
|
else
|
||||||
-- TODO: all items imported should be updated in itemdb
|
entry = Util.shallowCopy(item)
|
||||||
-- error here if not
|
entry.count = count
|
||||||
local item = itemDB:get(key)
|
entry.key = key
|
||||||
if item then
|
adapter.cache[key] = entry
|
||||||
entry = Util.shallowCopy(item)
|
|
||||||
entry.count = count
|
|
||||||
entry.key = key
|
|
||||||
adapter.cache[key] = entry
|
|
||||||
else
|
|
||||||
_G._debug('STORAGE: item missing details')
|
|
||||||
-- TODO: somehow update itemdb with this maybe new item
|
|
||||||
adapter.dirty = true
|
|
||||||
self.dirty = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
entry.count = entry.count + count
|
entry.count = entry.count + count
|
||||||
@@ -235,6 +225,26 @@ _G._debug('STORAGE: item missing details')
|
|||||||
adapter.cache[key] = nil
|
adapter.cache[key] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not entry then
|
||||||
|
_G._debug('STORAGE: item missing details')
|
||||||
|
adapter.dirty = true
|
||||||
|
self.dirty = true
|
||||||
|
else
|
||||||
|
local sentry = self.cache[key]
|
||||||
|
if sentry then
|
||||||
|
sentry.count = sentry.count + count
|
||||||
|
if sentry.count <= 0 then
|
||||||
|
self.cache[key] = nil
|
||||||
|
end
|
||||||
|
elseif count > 0 then
|
||||||
|
sentry = Util.shallowCopy(entry)
|
||||||
|
sentry.count = count
|
||||||
|
self.cache[key] = sentry
|
||||||
|
else
|
||||||
|
self.dirty = true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Storage:_sn(name)
|
function Storage:_sn(name)
|
||||||
@@ -261,8 +271,7 @@ function Storage:export(target, slot, count, item)
|
|||||||
item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target),
|
item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target),
|
||||||
slot and string.format('[%d]', slot) or '[*]')
|
slot and string.format('[%d]', slot) or '[*]')
|
||||||
|
|
||||||
self:updateCache(adapter, key, -amount)
|
self:updateCache(adapter, item, -amount)
|
||||||
self:updateCache(self, key, -amount)
|
|
||||||
end
|
end
|
||||||
count = count - amount
|
count = count - amount
|
||||||
total = total + amount
|
total = total + amount
|
||||||
@@ -301,6 +310,19 @@ function Storage:import(source, slot, count, item)
|
|||||||
self:listItems()
|
self:listItems()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local entry = itemDB:get(key)
|
||||||
|
if not entry then
|
||||||
|
if item.displayName then
|
||||||
|
-- this item already has metadata
|
||||||
|
entry = itemDB:add(item)
|
||||||
|
else
|
||||||
|
-- get the metadata from the device and add to db
|
||||||
|
entry = itemDB:add(device[source].getItemMeta(slot))
|
||||||
|
end
|
||||||
|
itemDB:flush()
|
||||||
|
end
|
||||||
|
item = entry
|
||||||
|
|
||||||
local function insert(adapter)
|
local function insert(adapter)
|
||||||
local amount = adapter:insert(slot, count, nil, source)
|
local amount = adapter:insert(slot, count, nil, source)
|
||||||
if amount > 0 then
|
if amount > 0 then
|
||||||
@@ -309,8 +331,7 @@ function Storage:import(source, slot, count, item)
|
|||||||
item.displayName or item.name, amount,
|
item.displayName or item.name, amount,
|
||||||
self:_sn(source), slot, self:_sn(adapter.name))
|
self:_sn(source), slot, self:_sn(adapter.name))
|
||||||
|
|
||||||
self:updateCache(adapter, key, amount)
|
self:updateCache(adapter, item, amount)
|
||||||
self:updateCache(self, key, amount)
|
|
||||||
|
|
||||||
-- record that we have imported this item into storage during this cycle
|
-- record that we have imported this item into storage during this cycle
|
||||||
self.activity[key] = (self.activity[key] or 0) + amount
|
self.activity[key] = (self.activity[key] or 0) + amount
|
||||||
@@ -322,7 +343,7 @@ function Storage:import(source, slot, count, item)
|
|||||||
-- find a chest locked with this item
|
-- find a chest locked with this item
|
||||||
for node in self:onlineAdapters() do
|
for node in self:onlineAdapters() do
|
||||||
if node.lock and node.lock[key] then
|
if node.lock and node.lock[key] then
|
||||||
insert(node.adapter)
|
insert(node.adapter, item)
|
||||||
if count > 0 and node.void then
|
if count > 0 and node.void then
|
||||||
total = total + self:trash(source, slot, count)
|
total = total + self:trash(source, slot, count)
|
||||||
return total
|
return total
|
||||||
@@ -334,20 +355,6 @@ function Storage:import(source, slot, count, item)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not itemDB:get(item) then
|
|
||||||
if item.displayName then
|
|
||||||
-- this item already has metadata
|
|
||||||
itemDB:add(item)
|
|
||||||
elseif not slot then
|
|
||||||
_G._debug("IMPORT: NO SLOT")
|
|
||||||
elseif not device[source] or not device[source].getItemMeta then
|
|
||||||
_G._debug("IMPORT: DEVICE? : " .. source)
|
|
||||||
else
|
|
||||||
-- get the metadata from the device and add to db
|
|
||||||
itemDB:add(device[source].getItemMeta(slot))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- is this item in some chest
|
-- is this item in some chest
|
||||||
if self.cache[key] then
|
if self.cache[key] then
|
||||||
for node, adapter in self:onlineAdapters() do
|
for node, adapter in self:onlineAdapters() do
|
||||||
|
|||||||
Reference in New Issue
Block a user