Advertisement
Guest User

startup

a guest
Oct 18th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. local modem = peripheral.wrap("left")
  2. local me = "turtle_30"
  3. local chests = modem.getNamesRemote()
  4. local itemNames = {}
  5. local mon = peripheral.wrap("monitor_22")
  6. mon.clear()
  7. local tempChests = {}
  8. for i = 1,#chests do
  9.     if string.find(chests[i],"chest") then
  10.         table.insert(tempChests,chests[i])
  11.     end
  12. end
  13. chests = tempChests
  14.  
  15. --for i = 1,#chests do
  16.     --print(chests[i])
  17.     --local meth = peripheral.getMethods(chests[i])
  18.     --for j = 1,#meth do
  19.         --print("    "..meth[j])
  20.     --end
  21. --end
  22.  
  23. local chestSize = {}
  24. local inv = {}
  25. local chestNames = {}
  26.  
  27. for i = 1,#chests do
  28.     chestSize[i] = peripheral.call(chests[i],"size")
  29.     chestNames[i] = chests[i]
  30.     chests[i] = peripheral.wrap(chests[i])
  31.     for j = 1,chestSize[i] do
  32.         local temp = chests[i].getItem(j)
  33.        
  34.         if temp ~= nil then
  35.             local data = temp.getMetadata()
  36.             --for k,v in pairs(data) do
  37.                 --print(k..":"..tostring(v))
  38.             --end
  39.             if inv[data.name] == nil then
  40.                 inv[data.name] = data
  41.                 table.insert(itemNames,data.displayName)
  42.             end
  43.             inv[data.name].count = inv[data.name].count + data.count
  44.         end
  45.     end
  46. end
  47.  
  48. function storeItem()
  49.     local myItem = turtle.getItemDetail(1)
  50.     local count = myItem.count
  51.     local myinv = peripheral.wrap("right")
  52.     for i = 1,#chests do
  53.         for j = 1,chestSize[i] do
  54.             local temp = chests[i].getItem(j)
  55.             if temp == nil then
  56.                 chests[i].pushItems(me,j,count)
  57.                 return
  58.             else
  59.                 local data = temp.getMetadata()
  60.                 if data.name == myItem.name then
  61.                     chests[i].pushItems(me,j,count)
  62.                     count = count - (data.maxCount-data.count)
  63.                     if count <= 0 then
  64.                         return
  65.                     end
  66.                 end
  67.             end
  68.         end
  69.     end    
  70. end
  71.  
  72. function takeItem(name,count)
  73.     for i = 1,#chests do
  74.         for j =1,chestSize[i] do
  75.             local temp = chests[i].getItem(j)
  76.             if temp == nil then
  77.            
  78.             else
  79.                 local data = temp.getMetadata()
  80.                 if data.displayName == name then
  81.                     if data.count < count then
  82.                         chests[i].pushItems(me,j,data.count)
  83.                         count = count - data.count
  84.                     else
  85.                         chests[i].pushItems(me,j,count)
  86.                         return
  87.                     end
  88.                 end
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. for k,v in pairs(inv) do
  95.     print(v.displayName..": "..v.count)
  96. end
  97.  
  98. function display()
  99.     local w,h = mon.getSize()
  100.     local col = math.floor(w/7)
  101.     local row = math.floor((h-5)/1)
  102.     local x = 0
  103.     local y = 1
  104.     for k,v in pairs(inv) do
  105.         local name = string.sub(v.displayName,1,4)
  106.         mon.setCursorPos((x*7)+1,y*1)
  107.         if (x+y)%2==0 then
  108.             mon.setBackgroundColor(colors.lightGray)
  109.             mon.setTextColor(colors.black)
  110.         else
  111.             mon.setBackgroundColor(colors.black)
  112.             mon.setTextColor(colors.white)
  113.         end
  114.         if v.count <= 99 then
  115.             mon.write(name.."-"..tostring(v.count))
  116.         else
  117.             mon.write(name.."-"..tostring(math.floor(v.count/64)).."S")
  118.         end
  119.         x=x+1
  120.         if x >= col then
  121.             x = 0
  122.             y = y  + 1
  123.         end
  124.     end
  125. end
  126.  
  127.  
  128. while true do
  129.     display()
  130.     write("take, list or drop? (t/l/d)")
  131.     input = read()
  132.     if input == "t" then
  133.         write("obtain item:")
  134.         local name = read()
  135.         write("how many:")
  136.         local count = tonumber(read())
  137.         takeItem(name,count)
  138.         print("item taken!")
  139.     elseif input == "l" then
  140.         term.clear()
  141.         term.setCursorPos(1,1)
  142.         for i,v in pairs(inv) do
  143.             print(inv[i].displayName..": "..tostring(inv[i].count))
  144.         end
  145.     elseif input == "d" then
  146.         if turtle.getItemCount(1) > 0 then
  147.             storeItem()
  148.             print("item gone!")
  149.         end
  150.     end
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement