Advertisement
HeiligerJanssen

Untitled

Apr 8th, 2020
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.56 KB | None | 0 0
  1.  
  2. -- Define the peripherals connected to the computer
  3.  
  4. -- Monitor
  5. monitor  = peripheral.wrap("right")
  6.  
  7. -- Item Selector to display and edit the items to be stocked
  8. selector = peripheral.wrap("top")
  9.  
  10. -- ME interface for crafting requests
  11. interface = peripheral.wrap("back")
  12.  
  13.  
  14. --Some Parameters
  15. local w,h = monitor.getSize()
  16. local stockBuffer = 0.05
  17. local craftingInterval = 10
  18. local stockItems = {}
  19. local stockItemsCount = 0
  20.  
  21. -- Meta class
  22. StockItem = {
  23.    item     = 0,
  24.    stockQty = 0,
  25.    status   = "",
  26. }
  27.  
  28. function StockItem:new(o,item,stockQty)
  29.    o = o or {}
  30.    setmetatable(o, self)
  31.    self.__index = self
  32.    self.item = item or nil
  33.    self.stockQty = stockQty or 0
  34.    self.status = "Not Sure"
  35.    return o
  36. end
  37.  
  38. function StockItem:setQty(stockQty)
  39.    self.stockQty = stockQty
  40. end
  41.  
  42.  
  43.  
  44. function checkQuantities()
  45.    local details
  46.    while true do
  47.       if stockItemsCount > 0 then
  48.          for iStock = 1, stockItemsCount, 1 do
  49.             local item =  stockItems[iStock].item
  50.             local stockQty =  stockItems[iStock].stockQty
  51.             -- get current quantity in the ME system
  52.             if interface.getItemDetail(item) ~= nil then
  53.                details = interface.getItemDetail(item).basic()
  54.  
  55.                if details.qty < stockQty then
  56.  
  57.                   if requestCrafting(item,details.qty,stockQty,status) then
  58.                      stockItems[iStock].status = "Crafting"
  59.                   else
  60.                      stockItems[iStock].status = "Error"
  61.                   end
  62.                else
  63.                   stockItems[iStock].status = "Fullfilled"
  64.                end
  65.                print(string.format("%s: %d  %s",details.display_name,stockQty,stockItems[iStock].status))
  66.             end
  67.          end
  68.       end
  69.       os.sleep(craftingInterval)
  70.    end
  71.    return true
  72. end
  73.  
  74.  
  75. function requestCrafting(item,currentQty,stockQty,status)
  76.    local craftingQty = stockQty * (1 + stockBuffer) - currentQty
  77.    local success
  78.    interface.requestCrafting(item,craftingQty)
  79.    return true
  80. end
  81.  
  82. function optionsMenu()
  83.    local key_pressed = false
  84.    while not key_pressed do
  85.       local event, key = os.pullEvent( "key" )
  86.       if key == keys.r then -- if the key pressed was 'strg'
  87.          addItem()
  88.       end
  89.    end
  90. end
  91.  
  92.  
  93. function addItem()
  94.    print( "You pressed [E]. Please place the Item to craft in the upper left slot of the Item Selector" )
  95.    while true do
  96.       local item = selector.getSlot(1)
  97.       if item ~= nil then
  98.          io.write("Enter the desired Quantity: ")
  99.          stockQty = io.read()
  100.          stockItemsCount = stockItemsCount + 1
  101.          stockItems[stockItemsCount] = StockItem:new(nil,item,tonumber(stockQty))
  102.          break
  103.       end
  104.       os.sleep(1)
  105.    end
  106. end
  107.  
  108.  
  109.  
  110. --display text text on monitor, "monitor" peripheral
  111. function drawText(x, y, text, text_color, bg_color)
  112.   monitor.setBackgroundColor(bg_color)
  113.   monitor.setTextColor(text_color)
  114.   monitor.setCursorPos(x,y)
  115.   monitor.write(text)
  116. end
  117.  
  118. --draw line on monitor
  119. function drawLine(x, y, length, color)
  120.     monitor.setBackgroundColor(color)
  121.     monitor.setCursorPos(x,y)
  122.     monitor.write(string.rep(" ", length))
  123. end
  124.  
  125. --clear lines on monitor
  126. function clearLine(...)
  127.     for _,y in ipairs(arg) do
  128.        monitor.setCursorPos(1,y)
  129.       monitor.clearLine()
  130.    end
  131. end
  132.  
  133. function itemKey(item)
  134.   local key = item.id .. "/" .. item.dmg
  135.  
  136.   if item.nbt_hash ~= nil then
  137.     key = key .. "/" .. item.nbt_hash
  138.   end
  139.  
  140.   return key
  141. end
  142.  
  143.  
  144. while true do
  145.    parallel.waitForAny(checkQuantities,optionsMenu)
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement