Skip to content

Commit

Permalink
checkin-squashmepls
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Becar committed May 22, 2018
1 parent f98d044 commit 77ac3ad
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 34 deletions.
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ 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).

## [Unreleased]
### Added
- Major version mismatch safety warning

### Fixed
- Fixed compatibility for multiple items with the same modid

## 0.0.7 - 2018-05-20
### Added
- Added sanity checks for peripheral wraps
Expand Down
6 changes: 4 additions & 2 deletions examples/.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@

showBlanks = false, -- Whether or not to show items that are not in stock on the list, defaults to false
items = { -- An array object of all the items you wish to sell
["minecraft:dye"] = { -- The key of each entry should be the full modname:itemname combo
{ -- The key of each entry should be the full modname:itemname combo
modid = "minecraft:dye",
damage = 4, -- * For items that are distinguished by their damage value (*Required if the item is a super-item)
price = 1, -- * The price in kst each unit of this item should cost (*Required)
disp = "Lapis Lazuli", -- The name to display in the table, not technically required, but highly recommended
Expand All @@ -51,7 +52,8 @@
-- will be the first item, 2 would be the second, and so on.. (if not
-- present items will be sorted alphabetically)
},
["minecraft:obsidian"] = {
{
modid = "minecraft:obsidian",
price = 2,
disp = "Obsidian",
addy = "obs",
Expand Down
1 change: 1 addition & 0 deletions src/macros.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--#define foreach(_val_, _tab_) for i = 1, #_tab_ do local _val_ = _tab_[i] --
2 changes: 2 additions & 0 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local layoutMode = args[1] == "--layout" or args[1] == "-l"
local successTools = {}

local function xenon()
--#require "src/util.lua" as util

if not (turtle or layoutMode) then
error("Xenon must run on a turtle")
end
Expand Down
63 changes: 33 additions & 30 deletions src/sections/inventory.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
--== Various Helper Functions ==--

local function toListName(name, damage)
return name .. "::" .. damage
end

local function fromListName(lName)
return lName:match("(.+)%:%:")
end

local function anyFree()
local c = 0
for i = 1, 16 do
Expand All @@ -21,15 +13,22 @@ 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
local function countItems()
list = {}
hasPredCache = {}
slotList = {}

if config.showBlanks then
for k, v in pairs(config.items) do
local lName = toListName(k, v.damage or 0)
foreach(item, config.items) do
local bName = util.toListName(item.modid, item.damage or 0, 0)
local lName = util.toListName(item.modid, item.damage or 0, item.predicateID or 0)
list[lName] = 0
slotList[lName] = {}

if not hasPredCache[bName] then
hasPredCache[bName] = item.predicateID ~= nil
end
end
end

Expand All @@ -40,28 +39,32 @@ local function countItems()
logger.error("Unable to list chest '" .. ck .. "'")
else
for k, v in pairs(cTable) do
local lName = toListName(v.name, v.damage)

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 }
local bName = util.toListName(v.name, v.damage, 0)

local predicateID = 0
if hasPredCache[bName] then
-- This item has known predicates, find which one
for predicateID = 1, #predicateCache do
-- TODO transform v with getItemMeta if initial match fails
if util.matchPredicate(predicateCache[predicateID], v) then
predicateID = predicateID
end
end
end
end
end
end

local rm = {}
for k, _ in pairs(list) do
if not config.items[fromListName(k)] then
rm[#rm + 1] = k
end
end
local lName = util.toListName(v.name, v.damage, predicateID)

for i = 1, #rm do
list[rm[i]] = nil
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
end

local els = renderer.querySelector("table")
Expand Down Expand Up @@ -111,7 +114,7 @@ end
local function findItem(name)
for k, item in pairs(config.items) do
if item.addy == name then
return item, toListName(k, item.damage or 0)
return item, util.toListName(k, item.damage or 0)
end
end

Expand Down
2 changes: 0 additions & 2 deletions src/sections/requires.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@ local util = require("util.lua")
logger.init(true, config.title)
successTools.logger = logger

--#require "src/util.lua" as util

--#require "vendor/json.lua" as json
24 changes: 24 additions & 0 deletions src/sections/transformations.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

local transformedItems = {}

local predicateCache = {}
local predicateIDCounter = 0
foreach(item, config.items) do
if item.predicate then
for predicateID = 1, #predicateCache do
local predicate = predicateCache[predicateID]
if util.equals(predicate, item.predicate) then
item.predicateID = predicateID
end
end

if not item.predicateID then
predicateIDCounter = predicateIDCounter + 1

item.predicateID = predicateIDCounter
predicateCache[predicateID] = item.predicate
end
end

transformedItems[util.toListName(item.modid, item.damage or 0, item.predicateID or 0)] = item
end
4 changes: 4 additions & 0 deletions src/sections/updates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ if config.checkForUpdates ~= false then
if release.tag_name ~= versionTag then
logger.warn("Version mismatch, latest release is "
.. release.tag_name .. ", but running version is " .. versionTag)

if release.tag_name:match("v(%d+)") ~= versionTag:match("v(%d+)") then
logger.warn("Latest version has a major version seperation gap, it may not be safe to update. Review the changelog for more details.")
end
end
end
else
Expand Down
54 changes: 54 additions & 0 deletions src/util.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
local util = {}

function util.toListName(modid, damage, pred)
return name .. "::" .. damage .. "::" .. pred
end

function util.fromListName(lName)
return lName:match("^(.-)::")
end

function util.wrappedWrite(surf, text, x, y, width, color, align, lineHeight)
lineHeight = lineHeight or 1

Expand Down Expand Up @@ -63,4 +71,50 @@ function util.round(num, numDecimalPlaces)
return math.floor(num * mult + 0.5) / mult
end

function util.matchPredicate(predicate, tab)
for k, v in pairs(predicate) do
if not tab[k] then
return false
end

if type(v) == "table" then
return util.matchPredicate(v, tab[k])
else
if tab[k] ~= v then
return false
end
end
end

return true
end

function util.equals(val1, val2)
local typeV = type(val1)

if typeV ~= type(val2) then
return false
end

if typeV ~= "table" then
return val1 == val2
end

local lengthV1 = 0
for k, v in pairs(val1) do
lengthV1 = lengthV1 + 1

if not util.equals(v, val2[k]) then
return false
end
end

local lengthV2 = 0
for _ in pairs(val2) do
lengthV2 = lengthV2 + 1
end

return lengthV1 == lengthV2
end

return util

0 comments on commit 77ac3ad

Please sign in to comment.