cleanup
This commit is contained in:
@@ -42,7 +42,7 @@ local blockDB = TableDB()
|
||||
|
||||
function blockDB:load()
|
||||
|
||||
local blocks = JSON.decodeFromFile('usr/etc/blocks.json'))
|
||||
local blocks = JSON.decodeFromFile('usr/etc/blocks.json')
|
||||
|
||||
if not blocks then
|
||||
error('Unable to read blocks.json')
|
||||
|
||||
@@ -3,6 +3,20 @@ local TableDB = require('tableDB')
|
||||
|
||||
local itemDB = TableDB({ fileName = 'usr/config/items.db' })
|
||||
|
||||
local function splitKey(key, item)
|
||||
|
||||
item = item or { }
|
||||
|
||||
local t = Util.split(key, '(.-):')
|
||||
if #t[#t] > 8 then
|
||||
item.nbtHash = table.remove(t)
|
||||
end
|
||||
item.damage = tonumber(table.remove(t))
|
||||
item.name = table.concat(t, ':')
|
||||
|
||||
return item
|
||||
end
|
||||
|
||||
function itemDB:get(key)
|
||||
|
||||
local item = TableDB.get(self, key)
|
||||
@@ -34,6 +48,54 @@ function itemDB:makeKey(item)
|
||||
return { item.name, item.damage, item.nbtHash }
|
||||
end
|
||||
|
||||
-- Accepts: "minecraft:stick:0" or { name = 'minecraft:stick', damage = 0 }
|
||||
function itemDB:getName(item)
|
||||
|
||||
if type(item) == 'string' then
|
||||
item = splitKey(item)
|
||||
end
|
||||
|
||||
local detail = self:get(self:makeKey(item))
|
||||
if detail then
|
||||
return detail.displayName
|
||||
end
|
||||
return item.name .. ':' .. item.damage
|
||||
end
|
||||
|
||||
function itemDB:load()
|
||||
|
||||
TableDB.load(self)
|
||||
|
||||
for key,item in pairs(self.data) do
|
||||
splitKey(key, item)
|
||||
item.maxDamage = item.maxDamage or 0
|
||||
item.maxCount = item.maxCount or 64
|
||||
end
|
||||
end
|
||||
|
||||
function itemDB:flush()
|
||||
if self.dirty then
|
||||
|
||||
local t = { }
|
||||
for k,v in pairs(self.data) do
|
||||
v = Util.shallowCopy(v)
|
||||
v.name = nil
|
||||
v.damage = nil
|
||||
v.nbtHash = nil
|
||||
if v.maxDamage == 0 then
|
||||
v.maxDamage = nil
|
||||
end
|
||||
if v.maxCount == 64 then
|
||||
v.maxCount = nil
|
||||
end
|
||||
t[k] = v
|
||||
end
|
||||
|
||||
Util.writeTable(self.fileName, t)
|
||||
self.dirty = false
|
||||
end
|
||||
end
|
||||
|
||||
itemDB:load()
|
||||
|
||||
return itemDB
|
||||
|
||||
@@ -27,7 +27,7 @@ function RefinedAdapter:init(args)
|
||||
Util.merge(self, controller)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function RefinedAdapter:isValid()
|
||||
return not not self.listAvailableItems
|
||||
end
|
||||
|
||||
@@ -7,17 +7,15 @@ function TableDB:init(args)
|
||||
fileName = '',
|
||||
dirty = false,
|
||||
data = { },
|
||||
tabledef = { },
|
||||
}
|
||||
Util.merge(defaults, args)
|
||||
Util.merge(self, defaults)
|
||||
end
|
||||
|
||||
function TableDB:load()
|
||||
local table = Util.readTable(self.fileName)
|
||||
if table then
|
||||
self.data = table.data
|
||||
self.tabledef = table.tabledef
|
||||
local t = Util.readTable(self.fileName)
|
||||
if t then
|
||||
self.data = t.data or t
|
||||
end
|
||||
end
|
||||
|
||||
@@ -43,10 +41,7 @@ end
|
||||
|
||||
function TableDB:flush()
|
||||
if self.dirty then
|
||||
Util.writeTable(self.fileName, {
|
||||
-- tabledef = self.tabledef,
|
||||
data = self.data,
|
||||
})
|
||||
Util.writeTable(self.fileName, self.data)
|
||||
self.dirty = false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,11 +3,11 @@ local Util = require('util')
|
||||
|
||||
local Craft = { }
|
||||
|
||||
local function clearGrid(chestAdapter)
|
||||
local function clearGrid(inventoryAdapter)
|
||||
for i = 1, 16 do
|
||||
local count = turtle.getItemCount(i)
|
||||
if count > 0 then
|
||||
chestAdapter:insert(i, count)
|
||||
inventoryAdapter:insert(i, count)
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
return false
|
||||
end
|
||||
@@ -19,7 +19,7 @@ end
|
||||
local function splitKey(key)
|
||||
local t = Util.split(key, '(.-):')
|
||||
local item = { }
|
||||
if #t[#t] > 2 then
|
||||
if #t[#t] > 8 then
|
||||
item.nbtHash = table.remove(t)
|
||||
end
|
||||
item.damage = tonumber(table.remove(t))
|
||||
@@ -39,13 +39,13 @@ local function getItemCount(items, key)
|
||||
return 0
|
||||
end
|
||||
|
||||
local function turtleCraft(recipe, qty, chestAdapter)
|
||||
local function turtleCraft(recipe, qty, inventoryAdapter)
|
||||
|
||||
clearGrid(chestAdapter)
|
||||
clearGrid(inventoryAdapter)
|
||||
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
local item = splitKey(v)
|
||||
chestAdapter:provide(item, qty, k)
|
||||
inventoryAdapter:provide(item, qty, k)
|
||||
if turtle.getItemCount(k) == 0 then -- ~= qty then
|
||||
-- FIX: ingredients cannot be stacked
|
||||
return false
|
||||
@@ -55,9 +55,9 @@ local function turtleCraft(recipe, qty, chestAdapter)
|
||||
return turtle.craft()
|
||||
end
|
||||
|
||||
function Craft.craftRecipe(recipe, count, chestAdapter)
|
||||
function Craft.craftRecipe(recipe, count, inventoryAdapter)
|
||||
|
||||
local items = chestAdapter:listItems()
|
||||
local items = inventoryAdapter:listItems()
|
||||
|
||||
local function sumItems(items)
|
||||
-- produces { ['minecraft:planks:0'] = 8 }
|
||||
@@ -78,10 +78,10 @@ function Craft.craftRecipe(recipe, count, chestAdapter)
|
||||
if itemCount < icount * count then
|
||||
local irecipe = Craft.recipes[key]
|
||||
if irecipe then
|
||||
Util.print('Crafting %d %s', icount * count - itemCount, key)
|
||||
--Util.print('Crafting %d %s', icount * count - itemCount, key)
|
||||
if not Craft.craftRecipe(irecipe,
|
||||
icount * count - itemCount,
|
||||
chestAdapter) then
|
||||
inventoryAdapter) then
|
||||
turtle.select(1)
|
||||
return
|
||||
end
|
||||
@@ -89,7 +89,7 @@ Util.print('Crafting %d %s', icount * count - itemCount, key)
|
||||
end
|
||||
end
|
||||
repeat
|
||||
if not turtleCraft(recipe, math.min(count, maxCount), chestAdapter) then
|
||||
if not turtleCraft(recipe, math.min(count, maxCount), inventoryAdapter) then
|
||||
turtle.select(1)
|
||||
return false
|
||||
end
|
||||
@@ -101,7 +101,7 @@ Util.print('Crafting %d %s', icount * count - itemCount, key)
|
||||
end
|
||||
|
||||
-- given a certain quantity, return how many of those can be crafted
|
||||
function Craft.getCraftableAmount(recipe, count, items)
|
||||
function Craft.getCraftableAmount(recipe, count, items, missing)
|
||||
|
||||
local function sumItems(recipe, items, summedItems, count)
|
||||
|
||||
@@ -116,6 +116,9 @@ function Craft.getCraftableAmount(recipe, count, items)
|
||||
summedItem = summedItem + sumItems(irecipe, items, summedItems, 1)
|
||||
end
|
||||
if summedItem <= 0 then
|
||||
if missing then
|
||||
missing.name = item
|
||||
end
|
||||
return canCraft
|
||||
end
|
||||
summedItems[item] = summedItem - 1
|
||||
@@ -126,7 +129,7 @@ function Craft.getCraftableAmount(recipe, count, items)
|
||||
return canCraft
|
||||
end
|
||||
|
||||
return sumItems(recipe, items, { }, math.ceil(count / recipe.count))
|
||||
return sumItems(recipe, items, { }, math.ceil(count / recipe.count), missing)
|
||||
end
|
||||
|
||||
function Craft.canCraft(item, count, items)
|
||||
|
||||
@@ -2,8 +2,8 @@ local Point = require('point')
|
||||
local Util = require('util')
|
||||
|
||||
local checkedNodes = { }
|
||||
local nodes = { }
|
||||
local box = { }
|
||||
local nodes = { }
|
||||
local box = { }
|
||||
local oldCallback
|
||||
|
||||
local function toKey(pt)
|
||||
@@ -149,7 +149,7 @@ return function(startPt, endPt, firstPt, verbose)
|
||||
print(string.format('%d nodes remaining', Util.size(nodes)))
|
||||
end
|
||||
|
||||
if Util.size(nodes) == 0 then
|
||||
if not next(nodes) then
|
||||
break
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user