Skip to content

Commit

Permalink
fix output chests
Browse files Browse the repository at this point in the history
  • Loading branch information
emmachase committed May 4, 2020
1 parent e20e298 commit 120b367
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 59 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

### 2.0.3
### Fixed
- Output Chests (`config.outChest`) work properly now

### 2.0.2
### Added
- Ability to use more escape codes in xml (  &krist; {)
Expand Down
2 changes: 1 addition & 1 deletion src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

--#include "src/macros.lua"

local versionTag = "v2.0.2"
local versionTag = "v2.0.3"

local args = {...}
local layoutMode = args[1] == "--layout" or args[1] == "-l"
Expand Down
138 changes: 80 additions & 58 deletions src/sections/inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,69 @@ local function anyFree()
return c > 0
end

local function getFreeSlot()
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
return i
end
end
end

--== Inventory Management Functions ==--

local drawRefresh

local function processChest(chestPeriph, list, slotList, hasPredCache)
local cTable = chestPeriph.list()
if not cTable then
logger.error("Unable to list chest '" .. chestPeriph .. "'")
else
for k, v in pairs(cTable) do -- For each item..
local bName = util.toListName(v.name, v.damage, 0) -- Simplified name to check if deep predicate matching is required

local predicateID = 0
if hasPredCache[bName] then
-- This item has known predicates, find which one

-- First see if we can match the predicate without making expensive meta calls
for chkPredicateID = 1, #predicateCache do
if util.matchPredicate(predicateCache[chkPredicateID], v) then
predicateID = chkPredicateID
break
end
end

-- Check detailed metadata
if predicateID == 0 then
-- This may take a while, so make sure to alert potential customers while shop is unavaliable
-- TODO: ^^^^^ but only when sleep is required

local cachedMeta = chestPeriph.getItemMeta(k)
for chkPredicateID = 1, #predicateCache do
if util.matchPredicate(predicateCache[chkPredicateID], cachedMeta) then
predicateID = chkPredicateID
break
end
end
end
end


local lName = util.toListName(v.name, v.damage, predicateID)

if transformedItems[lName] then
if not list[lName] then
list[lName] = v.count
slotList[lName] = { { k, v.count, chestPeriph } }
else
list[lName] = list[lName] + v.count
slotList[lName][#slotList[lName] + 1] = { k, v.count, chestPeriph }
end
end
end
end
end

local list -- Item count list
local slotList -- Keep track of which slots (in chests) items are located
local hasPredCache -- Keep track of which items have predicates
Expand Down Expand Up @@ -42,54 +101,7 @@ local function countItems()
-- Iterate over all known chests
for ck = 1, #chestPeriphs do
local chestPeriph = chestPeriphs[ck]
local cTable = chestPeriph.list()
if not cTable then
logger.error("Unable to list chest '" .. ck .. "'")
else
for k, v in pairs(cTable) do -- For each item..
local bName = util.toListName(v.name, v.damage, 0) -- Simplified name to check if deep predicate matching is required

local predicateID = 0
if hasPredCache[bName] then
-- This item has known predicates, find which one

-- First see if we can match the predicate without making expensive meta calls
for chkPredicateID = 1, #predicateCache do
if util.matchPredicate(predicateCache[chkPredicateID], v) then
predicateID = chkPredicateID
break
end
end

-- Check detailed metadata
if predicateID == 0 then
-- This may take a while, so make sure to alert potential customers while shop is unavaliable
-- TODO: ^^^^^ but only when sleep is required

local cachedMeta = chestPeriph.getItemMeta(k)
for chkPredicateID = 1, #predicateCache do
if util.matchPredicate(predicateCache[chkPredicateID], cachedMeta) then
predicateID = chkPredicateID
break
end
end
end
end


local lName = util.toListName(v.name, v.damage, predicateID)

if transformedItems[lName] then
if not list[lName] then
list[lName] = v.count
slotList[lName] = { { k, v.count, ck } }
else
list[lName] = list[lName] + v.count
slotList[lName][#slotList[lName] + 1] = { k, v.count, ck }
end
end
end
end
processChest(chestPeriph, list, slotList, hasPredCache)
end

if not util.equals(lastList, slotList) then
Expand All @@ -103,20 +115,25 @@ local function countItems()
end

local function dispense(mcname, count)
while count > 0 do
local toMoveCount = count
while toMoveCount > 0 do
-- We don't need to check for item availability here because
-- we already did that in processPayment()

for i = #slotList[mcname], 1, -1 do
local chestPeriph = chestPeriphs[slotList[mcname][i][3]]
local chestPeriph = slotList[mcname][i][3]
local amountPushed = 0
if not (config.outChest and targetChest == config.outChest) then
amountPushed = chestPeriph.pushItems(config.outChest or config.self, slotList[mcname][i][1], count)
if config.outChest then
local tempSlot = getFreeSlot()
amountPushed = chestPeriph.pushItems(config.self, slotList[mcname][i][1], toMoveCount, tempSlot)
outChest.pullItems(config.self, tempSlot)
else
amountPushed = chestPeriph.pushItems(config.self, slotList[mcname][i][1], toMoveCount)
end

count = count - amountPushed
toMoveCount = toMoveCount - amountPushed

if count <= 0 then
if toMoveCount <= 0 then
break
end

Expand All @@ -133,13 +150,18 @@ local function dispense(mcname, count)

if config.outChest then
local toBeDispensed = count
for k, v in pairs(outChest.list()) do
local iList, iSlotList = {}, {}
processChest(outChest, iList, iSlotList, hasPredCache)
for i = #iSlotList[mcname], 1, -1 do
toBeDispensed = toBeDispensed -
outChest.drop(
iSlotList[mcname][i][1],
math.min(iSlotList[mcname][i][2], toBeDispensed),
config.outChestDir or "up")

if toBeDispensed <= 0 then
break
end
if v.name == mcname then
toBeDispensed = toBeDispensed - outChest.drop(k, math.min(v.count, toBeDispensed), config.outChestDir or "up")
end
end
else
for i = 1, 16 do
Expand Down

0 comments on commit 120b367

Please sign in to comment.