Advertisement
FakoTheGreat

ME Item Display

Jul 26th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. --This script is intended to be used with one
  2. --connected ME terminal (crafting or normal)
  3. --and one ComputerCraft monitor (either type.
  4.  
  5. --Items to display go below. The system will
  6. --only show as many items as there are lines
  7. --on your monitor, based on text size. Items
  8. --need to be entered as "Name:ID:Metadata".
  9. --Lines can be entered as "line".
  10. local itemList = {"Steel Ingot:7798:0","Stainless Steel Ingot:9630:44"}
  11.  
  12. --Character to use for line separators.
  13. local lineChar = "-"
  14.  
  15. --If using an advanced monitor, these
  16. --set the text colors for names/values.
  17. local itemNameColor = colors.white
  18. local itemNumberColor = colors.green
  19. local lineColor = colors.yellow
  20.  
  21. --Other necessary information
  22. local textSize = 1
  23. local monitorName = "monitor_26"
  24. local terminalName = "right"
  25. local connectDelay = 1
  26. local updateDelay = 10
  27.  
  28. --Do not touch, used internally
  29. local monitor = nil
  30. local terminal = nil
  31. local screenX = 1
  32. local screenY = 1
  33. local nameMax = 1
  34. local curItem = {}
  35. local iName
  36. local iID
  37. local iMeta
  38. local qty
  39. local qStr
  40.  
  41. local function getSetup()
  42.   print("Finding terminal.")
  43.   while terminal == nil do
  44.     terminal = peripheral.wrap(terminalName)
  45.     if reactor == nil then
  46.       print("Waiting for terminal peripheral.")
  47.       os.sleep(connectDelay)
  48.     end
  49.   end
  50.   print("Terminal found. Looking for monitor.")
  51.  
  52.   while monitor == nil do
  53.     monitor = peripheral.wrap(monitorName)
  54.     if monitor == nil then
  55.       print("Waiting for monitor.")
  56.       os.sleep(connectDelay)
  57.     end
  58.   end
  59.   print("Monitor found.  Determining sizes.")
  60.  
  61.   monitor.clear()
  62.   monitor.setTextScale(textSize)
  63.   screenX, screenY = monitor.getSize()
  64.   nameMax = screenX - 6
  65.   print("Monitor can display "..screenY.." items.")
  66.   print("Display names can be "..nameMax.." characters or less.")
  67. end
  68.  
  69. function split(inputstr, sep)
  70.         if sep == nil then
  71.                 sep = "%s"
  72.         end
  73.         local t={} ; i=1
  74.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  75.                 t[i] = str
  76.                 i = i + 1
  77.         end
  78.         return t
  79. end
  80.  
  81. local function formatNumber(theNumber)
  82.   local tempNum = 0
  83.   local tempStr = ""
  84.   if theNumber > 1000000000 then
  85.     tempNum = math.floor(theNumber / 1000000)
  86.     tempStr = tempNum + "M"
  87.   elseif theNumber > 1000000 then
  88.     tempNum = math.floor(theNumber / 1000)
  89.     tempStr = tempNum + "K"
  90.   else
  91.     tempStr = tostring(theNumber)
  92.   end
  93.  
  94.   return tempStr
  95. end
  96.  
  97. local function formatName(toFormat)
  98.   if string.len(toFormat) > nameMax then
  99.     return string.sub(toFormat, 1, nameMax)
  100.   else
  101.     return toFormat
  102.   end
  103. end
  104.    
  105.  
  106. local function writeItems()
  107.   for k,v in ipairs(itemList) do
  108.     if k <= screenY then
  109.       if v == "line" then
  110.         qStr = ""
  111.         for x=1,screenX,1 do
  112.           qStr = qStr..lineChar
  113.         end
  114.         monitor.setCursorPos(1,k)
  115.         if monitor.isColor() then
  116.           monitor.setTextColor(lineColor)
  117.         end
  118.         monitor.write(qStr)
  119.       else
  120.         curItem = split(v, "\:")
  121.  
  122.         if #curItem ~= 3 then
  123.           print(curItem[1].." entry is malformed.")
  124.         else
  125.           iName = curItem[1]
  126.           iID = tonumber(curItem[2])
  127.           iMeta = tonumber(curItem[3])
  128.           monitor.setCursorPos(1,k)
  129.           if monitor.isColor() then
  130.            monitor.setTextColor(itemNameColor)
  131.           end
  132.           monitor.write(formatName(iName))
  133.    
  134.           if terminal.containsItemType(iID, iMeta) then
  135.             qty = terminal.countOfItemType(iID, iMeta)
  136.           else
  137.             qty = 0
  138.           end
  139.    
  140.           qStr = formatNumber(qty)
  141.           monitor.setCursorPos(screenX - string.len(qStr), k)
  142.           if monitor.isColor() then
  143.             monitor.setTextColor(itemNumberColor)
  144.           end
  145.           monitor.write(qStr)
  146.         end
  147.       end
  148.     end
  149.   end
  150. end
  151.  
  152. getSetup()
  153.  
  154. print("Executing main display loop.")
  155. while true do
  156.   monitor.clear()
  157.   writeItems()
  158.   os.sleep(updateDelay)
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement