Advertisement
m3Zz

Chest Checker

Apr 30th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. chestSide = "bottom"
  2. signalSide = "top"
  3. updateDelay = 5 --in seconds
  4. fill = 0.98 --in % --> 0-1
  5. x,y = term.getSize()
  6.  
  7. function clear() --GUI stuff.
  8.  term.clear()
  9.  term.setCursorPos(1,1)
  10. end
  11.  
  12. function drawLine() --GUI stuff.
  13.  for i=1,x-1 do
  14.   write("-")
  15.  end
  16. end
  17.  
  18. function valueExists(tbl,value) --Checks if a value exists in a table.
  19.  for k,v in ipairs(tbl) do
  20.   if value == v then
  21.    return true
  22.   end
  23.  end
  24.  return false
  25. end
  26.  
  27. function setup()
  28.  clear()
  29.  drawLine()
  30.  print("Monitoring on side: "..chestSide)
  31.  print("Sending signal to side: "..signalSide.." every "..updateDelay.." seconds.")
  32.  print("Outputting if above "..(fill*100).."%")
  33.  drawLine()
  34.  chest = peripheral.wrap(chestSide)
  35.  if chest == nil then
  36.   clear()
  37.   drawLine()
  38.   print("No chest found!")
  39.   drawLine()
  40.   error()
  41.  end
  42.  sleep(2)
  43. end
  44.  
  45. function display()
  46.  clear()
  47.  drawLine()
  48.  print("Status:")
  49.  print("Chest: "..currentContent.."/"..maxContent*64)
  50.  percentage = (currentContent *100) / (maxContent*64)
  51.  print("Chest is "..math.floor(percentage).."% full.")
  52.  print("Threshold is: "..(fill*100).."%")
  53.  signal = currentContent > ((maxContent*64)*fill)
  54.  if signal then
  55.   print("Above Threshold!")
  56.  else
  57.   print("Below Threshold.")
  58.  end
  59.  print("")
  60.  print("Update Interval: "..updateDelay.."s")
  61.  drawLine()
  62. end
  63.  
  64. function main()
  65.  while true do
  66.   maxContent = chest.getInventorySize()
  67.   currentContent = 0
  68.   for i = 1,maxContent do
  69.    data = chest.getStackInSlot(i)
  70.    if data == nil then
  71.     data = 0
  72.    else
  73.     currentContent = currentContent + data.qty
  74.    end
  75.   end
  76.   display()
  77.   rs.setOutput(signalSide,signal)
  78.   sleep(updateDelay)
  79.  end
  80. end
  81.  
  82. setup()
  83. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement