milo cleanup + shop

This commit is contained in:
kepler155c@gmail.com
2019-01-11 10:01:37 -05:00
parent 42e72cf3c8
commit bfa528756e
23 changed files with 1141 additions and 101 deletions

View File

@@ -5,7 +5,9 @@
title = 'Milo: Advanced inventory management',
repository = 'kepler155c/opus-apps/{{OPUS_BRANCH}}/milo',
description = [[Provides AE style crafting in computercraft.
Includes Importing, exporting, autocrafting, replenish and limits, and much more.
Includes over 200 standard Minecraft recipes.]],
Includes: importing, exporting, autocrafting, replenish, limits and much more.
Includes over 200 standard Minecraft recipes.]],
licence = 'MIT',
}

View File

@@ -47,29 +47,19 @@ end
function Milo:getState(key)
if not self.state then
self.state = { }
Config.load('milo.state', self.state)
self.state = Config.load('milo.state')
end
return self.state[key]
end
function Milo:setState(key, value)
if not self.state then
self.state = { }
Config.load('milo.state', self.state)
self.state = Config.load('milo.state')
end
self.state[key] = value
Config.update('milo.state', self.state)
end
function Milo:uniqueKey(item)
return table.concat({ item.name, item.damage, item.nbtHash }, ':')
end
function Milo:splitKey(key)
return itemDB:splitKey(key)
end
function Milo:resetCraftingStatus()
self.context.storage.activity = { }
@@ -85,18 +75,8 @@ function Milo:registerTask(task)
table.insert(self.context.tasks, task)
end
function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash)
if not ignoreDamage and not ignoreNbtHash then
return items[inItem.key or self:uniqueKey(inItem)]
end
for _,item in pairs(items) do
if item.name == inItem.name and
(ignoreDamage or item.damage == inItem.damage) and
(ignoreNbtHash or item.nbtHash == inItem.nbtHash) then
return item
end
end
function Milo:getItem(inItem)
return self:listItems()[inItem.key or itemDB:makeKey(inItem)]
end
-- returns a list of items that matches along with a total count
@@ -106,7 +86,7 @@ function Milo:getMatches(item, flags)
local items = self:listItems()
if not flags.ignoreDamage and not flags.ignoreNbtHash then
local key = item.key or Milo:uniqueKey(item)
local key = item.key or itemDB:makeKey(item)
local v = items[key]
if v then
t[key] = Util.shallowCopy(v)
@@ -146,7 +126,7 @@ function Milo:getTurtleInventory()
end
function Milo:requestCrafting(item)
local key = Milo:uniqueKey(item)
local key = itemDB:makeKey(item)
if not self.context.craftingQueue[key] then
item.crafted = 0
@@ -157,7 +137,7 @@ function Milo:requestCrafting(item)
end
end
-- queue up an action that reliees on the crafting grid
-- queue up an action that relies on the crafting grid
function Milo:queueRequest(request, callback)
if Util.empty(self.context.queue) then
os.queueEvent('milo_queue')
@@ -178,7 +158,7 @@ function Milo:craftAndEject(item, count)
end
function Milo:makeRequest(item, count, callback)
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
local current = self:getItem(item) or { count = 0 }
if count <= 0 then
return {
@@ -187,18 +167,18 @@ function Milo:makeRequest(item, count, callback)
count = 0,
current = current.count,
item = item,
key = item.key or Milo:uniqueKey(item),
key = item.key or itemDB:makeKey(item),
}
end
local toCraft = count - math.min(current.count, count)
if toCraft > 0 then
local recipe = Craft.findRecipe(self:uniqueKey(item))
local recipe = Craft.findRecipe(itemDB:makeKey(item))
if not recipe then
toCraft = 0
else
-- if you ask for 1 stick, getCraftableAmount will return 4 (obviously)
toCraft = math.min(toCraft, Craft.getCraftableAmount(recipe, toCraft, Milo:listItems(), { }))
toCraft = math.min(toCraft, Craft.getCraftableAmount(recipe, toCraft, self:listItems(), { }))
end
end
@@ -208,11 +188,11 @@ function Milo:makeRequest(item, count, callback)
count = math.min(count, current.count),
current = current.count,
item = item,
key = item.key or Milo:uniqueKey(item),
key = item.key or itemDB:makeKey(item),
}
if request.count > 0 then
Milo:queueRequest(request, callback)
self:queueRequest(request, callback)
end
if request.craft > 0 then
@@ -244,7 +224,7 @@ function Milo:updateRecipe(result, recipe)
end
function Milo:saveMachineRecipe(recipe, result, machine)
local key = Milo:uniqueKey(result)
local key = itemDB:makeKey(result)
-- save the recipe
self.context.userRecipes[key] = recipe
@@ -261,32 +241,29 @@ function Milo:mergeResources(t)
t = Util.shallowCopy(t)
for k,v in pairs(self.context.resources) do
local key = itemDB:splitKey(k)
local item = self:getItem(t, key)
local item = t[k]
if item then
item = Util.shallowCopy(item)
else
item = key
item = itemDB:splitKey(k)
item.count = 0
item.key = k
end
Util.merge(item, v)
item.resource = v
t[item.key] = item
t[k] = item
end
for k in pairs(Craft.recipes) do
local v = itemDB:splitKey(k)
local item = self:getItem(t, v)
local item = t[k]
if not item then
item = v
item = itemDB:splitKey(k)
item.count = 0
item.key = k
else
item = Util.shallowCopy(item)
end
t[item.key] = item
item.has_recipe = true
t[k] = item
end
for key in pairs(Craft.machineLookup) do
@@ -294,7 +271,7 @@ function Milo:mergeResources(t)
if item then
item = Util.shallowCopy(item)
item.is_craftable = true
t[item.key] = item
t[key] = item
end
end

View File

@@ -116,6 +116,10 @@ function Storage:initStorage()
end
v.adapter.online = true
v.adapter.dirty = true
if v.adapter.isOn and not v.adapter.isOn() then -- turtle
v.adapter.turnOn()
end
elseif device[k] then
v.adapter = device[k]
v.adapter.online = true

View File

@@ -169,7 +169,7 @@ function page:eject(amount)
if amount == 'stack' then
amount = item.maxCount or 64
elseif amount == 'all' then
item = Milo:getItem(Milo:listItems(), item)
item = Milo:getItem(item)
if item then
amount = item.count
end

View File

@@ -340,7 +340,7 @@ function nodeWizard.filter:eventHandler(event)
elseif event.type == 'scan_turtle' then
local inventory = Milo:getTurtleInventory()
for _,item in pairs(inventory) do
self.entry.filter[Milo:uniqueKey(item)] = true
self.entry.filter[itemDB:makeKey(item)] = true
end
self:resetGrid()
self.grid:update()
@@ -359,7 +359,7 @@ function nodeWizard.filter:eventHandler(event)
self.form:save()
self.entry.filter = { }
for _,v in pairs(self.grid.values) do
self.entry.filter[Milo:uniqueKey(v)] = true
self.entry.filter[itemDB:makeKey(v)] = true
end
self:hide()
self.callback()

View File

@@ -31,7 +31,7 @@ function ExportTask:cycle(context)
if slot then
-- something is in the slot, find what we can export
for key in pairs(entry.filter) do
local filterItem = Milo:splitKey(key)
local filterItem = itemDB:splitKey(key)
if (slot.name == filterItem.name and
(entry.ignoreDamage or slot.damage == filterItem.damage) and
(entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash)) then
@@ -50,7 +50,7 @@ function ExportTask:cycle(context)
-- slot is empty - export first matching item we have in storage
for key in pairs(entry.filter) do
local items = Milo:getMatches(Milo:splitKey(key), entry)
local items = Milo:getMatches(itemDB:splitKey(key), entry)
local _, item = next(items)
if item then
local count = math.min(item.count, itemDB:getMaxCount(item))

View File

@@ -17,12 +17,12 @@ function ImportTask:cycle(context)
local function itemMatchesFilter(item)
if not entry.ignoreDamage and not entry.ignoreNbtHash then
local key = Milo:uniqueKey(item)
local key = itemDB:makeKey(item)
return entry.filter[key]
end
for key in pairs(entry.filter) do
local v = Milo:splitKey(key)
local v = itemDB:splitKey(key)
if item.name == v.name and
(entry.ignoreDamage or item.damage == v.damage) and
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then

View File

@@ -39,11 +39,12 @@ local manageTab = UI.Window {
}
function manageTab:setItem(item)
self.origItem = item
self.item = Util.shallowCopy(item)
self.res = item.resource or { }
self.item = item
self.res = Util.shallowCopy(context.resources[item.key] or { })
self.res.displayName = self.item.displayName
self.form:setValues(self.res)
-- TODO: ignore damage should not be active if there is not a maxDamage value
end
function manageTab:eventHandler(event)
@@ -51,19 +52,17 @@ function manageTab:eventHandler(event)
UI:setPreviousPage()
elseif event.type == 'form_complete' then
local item = self.item
if self.form:save() then
if self.res.displayName ~= self.origItem.displayName then
self.origItem.displayName = self.res.displayName
itemDB:add(self.origItem)
if self.res.displayName ~= self.item.displayName then
self.item.displayName = self.res.displayName
itemDB:add(self.item)
itemDB:flush()
-- TODO: ugh
if context.storage.cache[self.origItem.key] then
context.storage.cache[self.origItem.key].displayName = self.res.displayName
if context.storage.cache[self.item.key] then
context.storage.cache[self.item.key].displayName = self.res.displayName
end
--context.storage:setDirty()
end
self.res.displayName = nil
Util.prune(self.res, function(v)
if type(v) == 'boolean' then
@@ -75,20 +74,14 @@ function manageTab:eventHandler(event)
end)
local newKey = {
name = item.name,
damage = self.res.ignoreDamage and 0 or item.damage,
nbtHash = not self.res.ignoreNbtHash and item.nbtHash or nil,
name = self.item.name,
damage = self.res.ignoreDamage and 0 or self.item.damage,
nbtHash = not self.res.ignoreNbtHash and self.item.nbtHash or nil,
}
for k,v in pairs(context.resources) do
if v == self.res then
context.resources[k] = nil
break
end
end
context.resources[self.item.key] = nil
if not Util.empty(self.res) then
context.resources[Milo:uniqueKey(newKey)] = self.res
context.resources[itemDB:makeKey(newKey)] = self.res
end
Milo:saveResources()

View File

@@ -1,4 +1,5 @@
local Milo = require('milo')
local itemDB = require('itemDB')
local Milo = require('milo')
local LimitTask = {
name = 'limiter',
@@ -11,7 +12,7 @@ function LimitTask:cycle(context)
if trashcan then
for key,res in pairs(context.resources) do
if res.limit then
local items, count = Milo:getMatches(Milo:splitKey(key), res)
local items, count = Milo:getMatches(itemDB:splitKey(key), res)
if count > res.limit then
local amount = count - res.limit
for _, item in pairs(items) do

View File

@@ -111,7 +111,7 @@ function pages.confirmation:validate()
}
for k,v in pairs(inventory) do
recipe.ingredients[k] = Milo:uniqueKey(v)
recipe.ingredients[k] = itemDB:makeKey(v)
end
Milo:saveMachineRecipe(recipe, result, machine.name)

View File

@@ -25,7 +25,7 @@ function PotionImportTask:cycle(context)
if blazePowder then
context.storage:export(bs, 5, 1, blazePowder)
else
local item = itemDB:get(BLAZE_POWDER) or Milo:splitKey(BLAZE_POWDER)
local item = itemDB:get(BLAZE_POWDER) or itemDB:splitKey(BLAZE_POWDER)
item.requested = 1
Milo:requestCrafting(item)
end
@@ -35,7 +35,7 @@ function PotionImportTask:cycle(context)
-- brewing has completd
if self.brewQueue[bs.name] and list[1] then
local key = Milo:uniqueKey(list[1])
local key = itemDB:makeKey(list[1])
if not Craft.findRecipe(key) then
Milo:saveMachineRecipe(self.brewQueue[bs.name], list[1], bs.name)
end
@@ -68,7 +68,7 @@ function PotionImportTask:cycle(context)
if valid() then
for i = 1, 4 do
recipe.ingredients[i] = Milo:uniqueKey(list[i])
recipe.ingredients[i] = itemDB:makeKey(list[i])
end
self.brewQueue[bs.name] = recipe

View File

@@ -106,7 +106,7 @@ local function client(socket)
local slot = node.adapter.getItemMeta(slotNo)
if slot then
if context.storage:import(node, slotNo, slot.count, slot) > 0 then
local item = Milo:getItem(Milo:listItems(), slot)
local item = Milo:getItem(slot)
if item then
socket:write({
type = 'received',
@@ -127,7 +127,7 @@ local function client(socket)
if count == 'stack' then
count = itemDB:getMaxCount(data.item)
elseif count == 'all' then
local item = Milo:getItem(Milo:listItems(), data.item)
local item = Milo:getItem(data.item)
count = item and item.count or 0
end

View File

@@ -1,3 +1,4 @@
local itemDB = require('itemDB')
local Milo = require('milo')
local ReplenishTask = {
@@ -8,7 +9,7 @@ local ReplenishTask = {
function ReplenishTask:cycle(context)
for k,res in pairs(context.resources) do
if res.low then
local item = Milo:splitKey(k)
local item = itemDB:splitKey(k)
item.key = k
local _, count = Milo:getMatches(item, res)
@@ -27,7 +28,7 @@ function ReplenishTask:cycle(context)
replenish = true,
})
else
local request = context.craftingQueue[Milo:uniqueKey(item)]
local request = context.craftingQueue[itemDB:makeKey(item)]
if request and request.replenish then
--request.count = request.crafted
end

View File

@@ -8,7 +8,7 @@ local Util = require('util')
local colors = _G.colors
local os = _G.os
local config = Config.load('store')
local config = Config.load('shop')
--[[ Display ]]--
local function createPage(node)
@@ -123,18 +123,14 @@ local pages = { }
-- called when an item to sell has been changed
Event.on('store_refresh', function()
config = Config.load('store')
config = Config.load('shop')
end)
Event.on('store_provide', function(_, item, quantity)
local count = 0
for k, v in pairs(config) do
if v.name == item then
count = Milo:eject(itemDB:splitKey(k), quantity)
break
end
end
os.queueEvent('store_provided', item, count)
Event.on('store_provide', function(_, item, quantity, uid)
Milo:queueRequest({ }, function()
local count = Milo:eject(item, quantity)
os.queueEvent('store_provided', uid, count)
end)
end)
--[[ Task ]]--

View File

@@ -55,7 +55,7 @@ local function learnRecipe()
]]--
maxCount = 1
newRecipe.craftingTools[Milo:uniqueKey(tool)] = true
newRecipe.craftingTools[itemDB:makeKey(tool)] = true
v1.craftingTool = true
break
end
@@ -80,7 +80,7 @@ local function learnRecipe()
newRecipe.count = recipe.count
local key = Milo:uniqueKey(recipe)
local key = itemDB:makeKey(recipe)
if recipe.maxCount ~= 64 then
newRecipe.maxCount = recipe.maxCount
end
@@ -88,7 +88,7 @@ local function learnRecipe()
if ingredient.maxDamage > 0 then
-- ingredient.damage = '*' -- I don't think this is right
end
ingredients[k] = Milo:uniqueKey(ingredient)
ingredients[k] = itemDB:makeKey(ingredient)
end
Milo:updateRecipe(key, newRecipe)