Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define the peripherals connected to the computer
- -- Monitor
- monitor = peripheral.wrap("right")
- -- Item Selector to display and edit the items to be stocked
- selector = peripheral.wrap("top")
- -- ME interface for crafting requests
- interface = peripheral.wrap("back")
- --Some Parameters
- local w,h = monitor.getSize()
- local stockBuffer = 0.05
- local craftingInterval = 10
- local stockItems = {}
- local stockItemsCount = 0
- -- Meta class
- StockItem = {
- item = 0,
- stockQty = 0,
- status = "",
- }
- function StockItem:new(o,item,stockQty)
- o = o or {}
- setmetatable(o, self)
- self.__index = self
- self.item = item or nil
- self.stockQty = stockQty or 0
- self.status = "Not Sure"
- return o
- end
- function StockItem:setQty(stockQty)
- self.stockQty = stockQty
- end
- function checkQuantities()
- local details
- while true do
- if stockItemsCount > 0 then
- for iStock = 1, stockItemsCount, 1 do
- local item = stockItems[iStock].item
- local stockQty = stockItems[iStock].stockQty
- -- get current quantity in the ME system
- if interface.getItemDetail(item) ~= nil then
- details = interface.getItemDetail(item).basic()
- if details.qty < stockQty then
- if requestCrafting(item,details.qty,stockQty,status) then
- stockItems[iStock].status = "Crafting"
- else
- stockItems[iStock].status = "Error"
- end
- else
- stockItems[iStock].status = "Fullfilled"
- end
- print(string.format("%s: %d %s",details.display_name,stockQty,stockItems[iStock].status))
- end
- end
- end
- os.sleep(craftingInterval)
- end
- return true
- end
- function requestCrafting(item,currentQty,stockQty,status)
- local craftingQty = stockQty * (1 + stockBuffer) - currentQty
- local success
- interface.requestCrafting(item,craftingQty)
- return true
- end
- function optionsMenu()
- local key_pressed = false
- while not key_pressed do
- local event, key = os.pullEvent( "key" )
- if key == keys.r then -- if the key pressed was 'strg'
- addItem()
- end
- end
- end
- function addItem()
- print( "You pressed [E]. Please place the Item to craft in the upper left slot of the Item Selector" )
- while true do
- local item = selector.getSlot(1)
- if item ~= nil then
- io.write("Enter the desired Quantity: ")
- stockQty = io.read()
- stockItemsCount = stockItemsCount + 1
- stockItems[stockItemsCount] = StockItem:new(nil,item,tonumber(stockQty))
- break
- end
- os.sleep(1)
- end
- end
- --display text text on monitor, "monitor" peripheral
- function drawText(x, y, text, text_color, bg_color)
- monitor.setBackgroundColor(bg_color)
- monitor.setTextColor(text_color)
- monitor.setCursorPos(x,y)
- monitor.write(text)
- end
- --draw line on monitor
- function drawLine(x, y, length, color)
- monitor.setBackgroundColor(color)
- monitor.setCursorPos(x,y)
- monitor.write(string.rep(" ", length))
- end
- --clear lines on monitor
- function clearLine(...)
- for _,y in ipairs(arg) do
- monitor.setCursorPos(1,y)
- monitor.clearLine()
- end
- end
- function itemKey(item)
- local key = item.id .. "/" .. item.dmg
- if item.nbt_hash ~= nil then
- key = key .. "/" .. item.nbt_hash
- end
- return key
- end
- while true do
- parallel.waitForAny(checkQuantities,optionsMenu)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement