Guest User

startup

a guest
Dec 25th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. monitor = peripheral.wrap("left")
  2.  
  3. chest1 = peripheral.wrap("chest_1")
  4. chest2 = peripheral.wrap("chest_2")
  5.  
  6. letters = "abcdefghijklmnopqrstuvwxyz"
  7.  
  8. items = {}
  9. sortedItems = {}
  10. tableLength = 0
  11.  
  12. function addChest(chest)
  13.   for i = 1, chest.getInventorySize(), 1 do
  14.     item = chest.getStackInSlot(i)
  15.     if item ~= null then
  16.       if items[item["display_name"]] == null then
  17.         itemEntry = {}
  18.         itemEntry["name"] = item["display_name"]
  19.         itemEntry["qty"] = item["qty"]
  20.         items[item["display_name"]] = itemEntry
  21.         tableLength = tableLength + 1
  22.         sortedItems[tableLength] = itemEntry
  23.       else
  24.         items[item["display_name"]]["qty"] = items[item["display_name"]]["qty"] + item["qty"]
  25.       end
  26.     end
  27.   end
  28. end
  29.  
  30. function addChests()
  31.   tableLength = 0
  32.   items = {}
  33.   addChest(chest1)
  34.   addChest(chest2)
  35. end
  36.  
  37. function sort()
  38.   sortedItems = {}
  39.   for i = tableLength - 1, 1, -1 do
  40.     for j = 1, i, 1 do
  41.       print(j)
  42.       if string.find(letters, string.sub(sortedItems[j]["name"], 1, 1), 0) > string.find(letters, string.sub(sortedItems[j]["name"], 1, 1), 0) then
  43.         temp = sortedItems[j]
  44.         sortedItems[j] = sortedItems[j+1]
  45.         sortedItems[j+1] = temp
  46.       end
  47.     end
  48.   end  
  49. end
  50.  
  51. function refreshMonitor()
  52.   monitor.setTextScale(0.5)
  53.   monitor.setCursorPos(1, 1)
  54.   monitor.clear()
  55.   for k, v in pairs(sortedItems) do
  56.     monitor.write(v["name"] .. ": " .. v["qty"])
  57.     x, y = monitor.getCursorPos()
  58.     monitor.setCursorPos(1, y + 1)
  59.   end
  60. end
  61.  
  62. while true do
  63.   addChests()
  64.   sort()
  65.   refreshMonitor()
  66.   sleep(5)
  67. end
Advertisement
Add Comment
Please, Sign In to add comment