Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vos.loadApi('core.api')
- vos.loadApi('ui.api')
- local ME = vos.loadApi('me.api')
- ME.setDevice(peripheral.wrap('back'))
- local turtleInventory = peripheral.wrap('left')
- local ME_direction = 'north'
- local autocraft = true
- local jobListGrid
- if device.wireless_modem then
- vos.loadApi('relay.api')
- Relay.find()
- end
- multishell.setTitle(TAB_ID, 'Storage Manager')
- Logger.disable()
- function getItem(items, id, dmg)
- for _,item in pairs(items) do
- if item.id == id and item.dmg == dmg then
- return item
- end
- end
- end
- function mergeResources(t)
- local resources = Util.readTable('resource.limits')
- resources = resources or { }
- for _,v in pairs(resources) do
- local item = getItem(t, v.id, v.dmg)
- if item then
- item.limit = tonumber(v.limit)
- item.low = tonumber(v.low)
- end
- end
- end
- function filterItems(t, filter)
- local r = {}
- if filter then
- filter = filter:lower()
- for k,v in pairs(t) do
- if string.find(v.lname, filter) then
- table.insert(r, v)
- end
- end
- else
- return t
- end
- return r
- end
- function updatePanel()
- pcall(function()
- local pStorage = 100 -
- math.floor(ME.getFreeBytes() * 100 /
- ME.getTotalBytes())
- local pItemsFree = ME.getRemainingItemTypes()
- Relay.send("status",
- string.format("Storage: %d%% (%d free slots)",
- pStorage, pItemsFree))
- local percent = math.min(64, pItemsFree)
- percent = math.floor(percent / 64 * 100)
- local color = colors.green
- if percent < 50 then
- color = colors.orange
- elseif percent < 25 then
- color = colors.red
- end
- Relay.send('panelUpdate', {
- uid = 'storage',
- type = 'meter',
- text = 'Storage',
- value = percent,
- color = color,
- })
- end)
- end
- function craftItems(itemList)
- for _,item in pairs(itemList) do
- if not craft(item.id, item.dmg, item.qty) then
- ME.craft(item.id, item.dmg, item.qty)
- end
- end
- end
- function isCrafting(jobList, id, dmg)
- for _, job in pairs(jobList) do
- if job.id == id and job.dmg == dmg then
- return job
- end
- end
- end
- local function jobMonitor(jobList)
- local mon = UI.Device({
- deviceType = 'monitor',
- textScale = .5,
- })
- jobListGrid = UI.Grid({
- parent = mon,
- sortColumn = 'name',
- columns = {
- { 'Amount', 'qty', 6 },
- { 'Crafting', 'name', mon.width - 10 },
- },
- })
- end
- function craft(id, dmg, qty)
- local recipes = Util.readTable('recipes') or { }
- local key = id .. ':' .. dmg
- local recipe = recipes[key]
- if not recipe then
- return
- end
- local function selectOpenSlot()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- return i
- end
- end
- end
- local function clearGrid()
- for i = 1, 16 do
- local count = turtle.getItemCount(i)
- if count > 0 then
- ME.insert(i, count, ME_direction)
- if turtle.getItemCount(i) ~= 0 then
- return true
- end
- end
- end
- end
- clearGrid()
- for i = 1, math.ceil(qty / recipe.qty) do
- local failed = false
- for k,v in pairs(recipe.ingredients) do
- if not ME.extract(v.id, v.dmg, v.qty, ME_direction, k) then
- failed = true
- break
- end
- end
- if failed then
- break
- end
- turtle.craft()
- for i = 1, 16 do
- local count = turtle.getItemCount(i)
- if count > 0 then
- ME.insert(i, count, ME_direction)
- end
- end
- end
- return true
- end
- function watchResources()
- local items = ME.getAvailableItems()
- local jobList = ME.getJobList()
- local messages = { }
- local itemList = { }
- if Util.size(items) == 0 then
- return
- end
- local t = Util.readTable('resource.limits') or { }
- for k, res in pairs(t) do
- local item = getItem(items, res.id, res.dmg)
- res.limit = tonumber(res.limit)
- res.low = tonumber(res.low)
- if not item then
- item = {
- id = res.id,
- dmg = res.dmg,
- name = res.name,
- qty = 0
- }
- end
- if res.limit and item.qty > res.limit then
- Logger.debug("Purging " .. item.qty-res.limit .. " " .. res.name)
- if not ME.extract(item.id, item.dmg, item.qty - res.limit, 'west') then
- table.insert(messages, 'Failed to purge ' .. res.name)
- end
- elseif res.low and item.qty < res.low then
- table.insert(itemList, {
- id = item.id,
- dmg = item.dmg,
- qty = res.low - item.qty,
- name = item.name
- })
- end
- end
- if #messages and Relay then
- Relay.send('panelUpdate', {
- type = 'message',
- message = messages,
- uid = 'Crafting'
- })
- end
- return itemList
- end
- itemPage = UI.Page({
- x = 4,
- y = math.floor((UI.term.height - 9) / 2) + 1,
- height = 9,
- width = UI.term.width - 6,
- backgroundColor = colors.lightGray,
- titleBar = UI.TitleBar({
- title = 'Limit Resource',
- previousPage = true,
- }),
- form = UI.Form({
- fields = {
- { label = 'ID', key = 'iddmg', width = 12, display = UI.Form.D.static },
- -- { label = 'Name', key = 'name', width = 25, display = UI.Form.D.static },
- -- { label = 'Quantity', key = 'qty', display = UI.Form.D.static },
- { label = 'Low', key = 'low', width = 8, display = UI.Form.D.entry },
- { label = 'Limit', key = 'limit', width = 8, display = UI.Form.D.entry,
- validation = UI.Form.V.number, dataType = UI.Form.T.number },
- { text = 'Accept', event = 'accept', display = UI.Form.D.button,
- x = 5, y = 5, width = 10 },
- { text = 'Cancel', event = 'cancel', display = UI.Form.D.button,
- x = 19, y = 5, width = 10 },
- },
- labelWidth = 12,
- x = 5,
- y = 3,
- height = 6
- }),
- statusBar = UI.StatusBar({
- columns = {
- { '', 'msg', 25 },
- { '', 'slots', 12 },
- { '', 'space', 12 }
- },
- status = statusBarValues
- })
- })
- function itemPage:enable()
- self:focusFirst()
- end
- function itemPage:eventHandler(event)
- if event.type == 'cancel' then
- UI.pager:setPreviousPage()
- elseif event.type == 'accept' then
- --resourceDB.update(self.item)
- --resourceDB.flush()
- local values = self.form.values
- local t = Util.readTable('resource.limits') or { }
- for k,v in pairs(t) do
- if v.id == values.id and v.dmg == values.dmg then
- table.remove(t, k)
- break
- end
- end
- table.insert(t, values)
- Util.writeTable('resource.limits', t)
- UI.pager:setPreviousPage()
- else
- return UI.Page.eventHandler(self, event)
- end
- return true
- end
- listingPage = UI.Page({
- grid = UI.Grid({
- columns = {
- --{ 'ID', 'iddmg', 8 },
- { 'Name', 'name', 24 },
- { 'Qty', 'qty', 5 },
- { 'Low', 'low', 5 },
- { 'High', 'limit', 6 },
- },
- sortColumn = 'name',
- width = UI.term.width,
- autoSpace = true,
- pageSize = UI.term.height-2
- }),
- statusBar = UI.StatusBar({
- backgroundColor = colors.gray,
- width = UI.term.width,
- --y = UI.term.height,
- filterText = UI.Text({
- value = 'Filter',
- x = 2,
- width = 6,
- }),
- filter = UI.TextEntry({
- width = 10,
- limit = 50,
- x = 9,
- }),
- learn = UI.Button({
- text = 'Learn',
- event = 'learn',
- x = 22,
- width = 7
- }),
- refresh = UI.Button({
- text = 'Refresh',
- event = 'refresh',
- x = 30,
- width = 8
- }),
- }),
- accelerators = {
- r = 'refresh',
- q = 'quit',
- }
- })
- function listingPage.statusBar:draw()
- return UI.Window.draw(self)
- end
- function listingPage.statusBar.filter:eventHandler(event)
- if event.type == 'mouse_rightclick' then
- self.value = ''
- self:draw()
- local page = UI.pager:getCurrentPage()
- page.filter = nil
- page:applyFilter()
- page.grid:draw()
- page:setFocus(self)
- end
- return UI.TextEntry.eventHandler(self, event)
- end
- function listingPage:eventHandler(event)
- if event.type == 'quit' then
- Event.exitPullEvents()
- elseif event.type == 'grid_select' then
- itemPage.form:setValues(self.grid:getSelected())
- itemPage.titleBar.title = self.grid:getSelected().name
- UI.pager:setPage('item')
- elseif event.type == 'refresh' then
- self:refresh()
- self.grid:draw()
- elseif event.type == 'learn' then
- local t = Util.readTable('recipes') or { }
- local recipe = { }
- local ingredients = turtleInventory.getAllStacks(false)
- if ingredients then
- if turtle.craft() then
- recipe = turtleInventory.getAllStacks(false)
- if recipe and recipe[1] then
- recipe = recipe[1]
- local key = recipe.id .. ':' .. recipe.dmg
- recipe.ingredients = ingredients
- t[key] = recipe
- Util.writeTable('recipes', t)
- self.statusBar:timedStatus('learned ' .. recipe.name, 3)
- end
- else
- self.statusBar:timedStatus('Failed to craft', 3)
- end
- else
- self.statusBar:timedStatus('No recipe defined', 3)
- end
- elseif event.type == 'text_change' then
- self.filter = event.text
- if #self.filter == 0 then
- self.filter = nil
- end
- self:applyFilter()
- self.grid:draw()
- self.statusBar.filter:focus()
- else
- UI.Page.eventHandler(self, event)
- end
- end
- function listingPage:enable()
- self:refresh()
- end
- function listingPage:refresh()
- self.allItems = ME.getAvailableItems('all')
- Util.each(self.allItems, function(item)
- item.lname = item.name:lower()
- end)
- mergeResources(self.allItems)
- self:applyFilter()
- end
- function listingPage:applyFilter()
- local t = filterItems(self.allItems, self.filter)
- for _,v in pairs(t) do
- v.iddmg = v.id .. ':' .. v.dmg
- end
- self.grid:setTable(t)
- end
- UI.pager:setPages({
- listing = listingPage,
- item = itemPage
- })
- UI.pager:setPage(listingPage)
- listingPage:setFocus(listingPage.statusBar.filter)
- jobMonitor()
- Event.heartbeat(5,
- function()
- local itemList = watchResources()
- jobListGrid.t = itemList -- ME.getJobList()
- jobListGrid:draw()
- craftItems(itemList)
- end
- )
- Event.pullEvents()
- UI.term:reset()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement