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 return item
end 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 not key then error('itemDB:get: key is required', 2) end
if type(key) == 'string' then if type(key) == 'string' then
key = self:splitKey(key) 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 if storage:export(storage.turtleInventory, k, count, item) ~= count then
request.status = 'unknown error' request.status = 'unknown error'
request.statusCode = Craft.STATUS_ERROR request.statusCode = Craft.STATUS_ERROR
_debug('failed to export: ' .. item.name) _debug('failed to export: ' .. item.name)
return return
end end
@@ -203,18 +204,11 @@ local function adjustCounts(recipe, count, ingredients, storage)
-- increment crafted -- increment crafted
local result = ingredients[recipe.result] local result = ingredients[recipe.result]
result.count = result.count + (count * recipe.count) result.count = result.count + (count * recipe.count)
end end
function Craft.craftRecipeInternal(recipe, count, storage, origItem) function Craft.craftRecipeInternal(recipe, count, storage, origItem)
local request = origItem.ingredients[recipe.result] local request = origItem.ingredients[recipe.result]
if request.aborted then
_debug('aborted')
return 0
end
if origItem.pending[recipe.result] then if origItem.pending[recipe.result] then
request.status = 'processing' request.status = 'processing'
request.statusCode = Craft.STATUS_INFO request.statusCode = Craft.STATUS_INFO
@@ -281,6 +275,12 @@ _G._p = origItem.ingredients
canCraft = canCraft - maxCount canCraft = canCraft - maxCount
end end
if request.aborted then
origItem.aborted = true
_debug('aborted')
return 0
end
return crafted * recipe.count return crafted * recipe.count
end end

View File

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

View File

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

View File

@@ -18,6 +18,10 @@ local recipeTab = UI.Window {
}, },
sortColumn = 'slot', sortColumn = 'slot',
}, },
ignoreResultNBT = UI.Button {
x = 2, y = -2,
text = 'Ignore Result NBT', event = 'ignore_result_nbt',
},
ignoreNBT = UI.Button { ignoreNBT = UI.Button {
x = -13, y = -2, x = -13, y = -2,
text = 'Ignore NBT', event = 'ignore_nbt', text = 'Ignore NBT', event = 'ignore_nbt',
@@ -38,12 +42,33 @@ function recipeTab:setItem(item)
key = v, key = v,
}) })
end end
local key = itemDB:splitKey(self.recipe.result)
self.ignoreResultNBT.inactive = not key.nbtHash
end end
self.grid:setValues(t) self.grid:setValues(t)
end end
function recipeTab:eventHandler(event) 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 selected = self.grid:getSelected()
local item = itemDB:splitKey(selected.key) local item = itemDB:splitKey(selected.key)
item.nbtHash = nil item.nbtHash = nil

View File

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

View File

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