recipe nbts + item get/add helper

This commit is contained in:
kepler155c@gmail.com
2019-01-04 00:29:03 -05:00
parent d0a7a6e488
commit 6606975f5b
7 changed files with 62 additions and 41 deletions

View File

@@ -51,7 +51,26 @@ function itemDB:splitKey(key, item)
return item
end
function itemDB:get(key)
function itemDB:get(key, populateFn)
if not key then error('itemDB:get: key is required', 2) end
if type(key) == 'string' then
key = self:splitKey(key)
else
key = Util.shallowCopy(key)
end
local item = self:_get(key)
if not item and populateFn then
item = populateFn()
if item then
item = self:add(item)
end
end
return item and Util.merge(key, item)
end
function itemDB:_get(key)
if not key then error('itemDB:get: key is required', 2) end
if type(key) == 'string' then
key = self:splitKey(key)

View File

@@ -131,6 +131,7 @@ local function turtleCraft(recipe, storage, request, count)
if storage:export(storage.turtleInventory, k, count, item) ~= count then
request.status = 'unknown error'
request.statusCode = Craft.STATUS_ERROR
_debug('failed to export: ' .. item.name)
return
end
@@ -203,18 +204,11 @@ local function adjustCounts(recipe, count, ingredients, storage)
-- increment crafted
local result = ingredients[recipe.result]
result.count = result.count + (count * recipe.count)
end
function Craft.craftRecipeInternal(recipe, count, storage, origItem)
local request = origItem.ingredients[recipe.result]
if request.aborted then
_debug('aborted')
return 0
end
if origItem.pending[recipe.result] then
request.status = 'processing'
request.statusCode = Craft.STATUS_INFO
@@ -281,6 +275,12 @@ _G._p = origItem.ingredients
canCraft = canCraft - maxCount
end
if request.aborted then
origItem.aborted = true
_debug('aborted')
return 0
end
return crafted * recipe.count
end

View File

@@ -75,7 +75,7 @@ function Milo:resetCraftingStatus()
for _,key in pairs(Util.keys(self.context.craftingQueue)) do
local item = self.context.craftingQueue[key]
if item.crafted >= item.requested then
if item.crafted >= item.requested or item.aborted then
self.context.craftingQueue[key] = nil
end
end
@@ -135,12 +135,10 @@ end
function Milo:getTurtleInventory()
local list = { }
for i in pairs(self.context.turtleInventory.adapter.list()) do
local item = self.context.turtleInventory.adapter.getItemMeta(i)
if item and not itemDB:get(item) then
itemDB:add(item)
end
list[i] = item
for i, v in pairs(self.context.turtleInventory.adapter.list()) do
list[i] = itemDB:get(v, function()
return self.context.turtleInventory.adapter.getItemMeta(i)
end)
end
itemDB:flush()

View File

@@ -25,15 +25,7 @@ function Adapter:listItems(throttle)
local entry = cache[key]
if not entry then
local cached = itemDB:get(v)
if cached then
cached = Util.shallowCopy(cached)
else
cached = self.getItemMeta(k)
if cached then
cached = Util.shallowCopy(itemDB:add(cached))
end
end
local cached = itemDB:get(v, function() return self.getItemMeta(k) end)
if cached then
entry = cached
entry.count = 0

View File

@@ -18,6 +18,10 @@ local recipeTab = UI.Window {
},
sortColumn = 'slot',
},
ignoreResultNBT = UI.Button {
x = 2, y = -2,
text = 'Ignore Result NBT', event = 'ignore_result_nbt',
},
ignoreNBT = UI.Button {
x = -13, y = -2,
text = 'Ignore NBT', event = 'ignore_nbt',
@@ -38,12 +42,33 @@ function recipeTab:setItem(item)
key = v,
})
end
local key = itemDB:splitKey(self.recipe.result)
self.ignoreResultNBT.inactive = not key.nbtHash
end
self.grid:setValues(t)
end
function recipeTab:eventHandler(event)
if event.type == 'ignore_nbt' then
if event.type == 'ignore_result_nbt' then
-- remove old entry
Milo:updateRecipe(self.recipe.result)
local item = itemDB:splitKey(self.recipe.result)
item.nbtHash = nil
self.recipe.result = itemDB:makeKey(item)
-- add updated entry
Milo:updateRecipe(self.recipe.result, self.recipe)
self.ignoreResultNBT.inactive = true
self:emit({ type = 'info_message', message = 'Recipe updated' })
elseif event.type == 'grid_focus_row' then
local key = itemDB:splitKey(event.selected.key)
self.ignoreNBT.inactive = not key.nbtHash
self.ignoreNBT:draw()
elseif event.type == 'ignore_nbt' then
local selected = self.grid:getSelected()
local item = itemDB:splitKey(selected.key)
item.nbtHash = nil

View File

@@ -1,7 +1,6 @@
local Craft = require('craft2')
local itemDB = require('itemDB')
local Milo = require('milo')
local Util = require('util')
local BLAZE_POWDER = "minecraft:blaze_powder:0"
@@ -26,12 +25,7 @@ function PotionImportTask:cycle(context)
if blazePowder then
context.storage:export(bs, 5, 1, blazePowder)
else
local item = itemDB:get(BLAZE_POWDER)
if item then
item = Util.shallowCopy(item)
else
item = Milo:splitKey(BLAZE_POWDER)
end
local item = itemDB:get(BLAZE_POWDER) or Milo:splitKey(BLAZE_POWDER)
item.requested = 1
Milo:requestCrafting(item)
end

View File

@@ -57,12 +57,8 @@ function page.tabs.inventory:enable()
for k, item in pairs(inv) do
local key = itemDB:makeKey(item)
if not list[key] then
local cItem = itemDB:get(item)
if not cItem then
cItem = itemDB:add(ni.getInventory().getItemMeta(k))
end
local cItem = itemDB:get(item, function() return ni.getInventory().getItemMeta(k) end)
if cItem then
cItem = Util.shallowCopy(cItem)
cItem.key = makeKey(cItem)
list[key] = cItem
end
@@ -138,10 +134,7 @@ Event.onInterval(5, function()
if empty then
for k,v in pairs(inv) do
local item = itemDB:get(v)
if not item then
item = itemDB:add(ni.getInventory().getItemMeta(k))
end
local item = itemDB:get(v, function() ni.getInventory().getItemMeta(k) end)
if item then
if context.state.autostore[makeKey(item)] then
ni.getInventory().pushItems(target, k, v.count, slot)