Advertisement
Guest User

startup

a guest
Oct 19th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1. local modem = peripheral.wrap("bottom")
  2. local me = "turtle_29"
  3. local chests = modem.getNamesRemote()
  4. local itemNames = {}
  5. local mon = peripheral.wrap("monitor_21")
  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.displayName] == nil then
  40.                 inv[data.displayName] = data
  41.                 table.insert(itemNames,data.displayName)
  42.             end
  43.             inv[data.displayName].count = inv[data.displayName].count + data.count
  44.         end
  45.     end
  46. end
  47.  
  48. function storeItem()
  49.     local myItem = turtle.getItemDetail(1)
  50.     if myItem == nil then
  51.         return
  52.     end
  53.     local count = myItem.count
  54.     local myinv = peripheral.wrap("right")
  55.     for i = 1,#chests do
  56.         for j = 1,chestSize[i] do
  57.             local temp = chests[i].getItem(j)
  58.             if temp == nil then
  59.                 chests[i].pullItems(me,1,nil,j)
  60.                 return
  61.             else
  62.                 local data = temp.getMetadata()
  63.                 if data.displayName == myItem.displayName then
  64.                     chests[i].pullItems(me,1,nil,j)
  65.                     count = count - (data.maxCount-data.count)
  66.                     if count <= 0 then
  67.                         return
  68.                     end
  69.                 end
  70.             end
  71.         end
  72.     end    
  73. end
  74.  
  75. function takeItem(name,count)
  76.     for i = 1,#chests do
  77.         for j =1,chestSize[i] do
  78.             local temp = chests[i].getItem(j)
  79.             if temp == nil then
  80.            
  81.             else
  82.                 local data = temp.getMetadata()
  83.                 if data.displayName == name then
  84.                     if data.count < count then
  85.                         chests[i].pushItems(me,j,data.count)
  86.                         count = count - data.count
  87.                         inv[data.displayName].count = inv[data.displayName].count - data.count
  88.                     else
  89.                         chests[i].pushItems(me,j,count)
  90.                         inv[data.displayName].count = inv[data.displayName].count - count
  91.                         return
  92.                     end
  93.                 end
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. for k,v in pairs(inv) do
  100.     print(v.displayName..": "..v.count)
  101. end
  102.  
  103. function display()
  104.     local w,h = mon.getSize()
  105.     local col = math.floor(w/7)
  106.     local row = math.floor((h-5)/1)
  107.     local x = 0
  108.     local y = 1
  109.     for k,v in pairs(inv) do
  110.         local name = string.sub(v.displayName,1,4)
  111.         mon.setCursorPos((x*7)+1,y*1)
  112.         if (x+y)%2==0 then
  113.             mon.setBackgroundColor(colors.lightGray)
  114.             mon.setTextColor(colors.black)
  115.         else
  116.             mon.setBackgroundColor(colors.black)
  117.             mon.setTextColor(colors.white)
  118.         end
  119.         if v.count <= 99 then
  120.             mon.write(name.."-"..tostring(v.count))
  121.         else
  122.             mon.write(name.."-"..tostring(math.floor(v.count/64)).."S")
  123.         end
  124.         x=x+1
  125.         if x >= col then
  126.             x = 0
  127.             y = y  + 1
  128.         end
  129.     end
  130. end
  131.  
  132.  
  133. while true do
  134.     display()
  135.     evt,a,x,y = os.pullEvent()
  136.     if evt == "monitor_touch" then
  137.         local w,h = mon.getSize()
  138.         local col = math.floor(w/7)
  139.         local row = h
  140.         local num = (y-1)*5+(math.floor(x/7))
  141.         print(x..","..y.."    "..num)
  142.         for k,v in pairs(inv) do
  143.             if num <= 0 then
  144.                 takeItem(v.displayName,1)
  145.                 turtle.drop()
  146.                 break
  147.             end
  148.             num = num -1
  149.         end
  150.     elseif evt == "turtle_inventory" then
  151.         storeItem()
  152.     end
  153. end
  154.  
  155. while true do
  156.     display()
  157.     write("take, list or drop? (t/l/d)")
  158.     input = read()
  159.     if input == "t" then
  160.         write("obtain item:")
  161.         local name = read()
  162.         write("how many:")
  163.         local count = tonumber(read())
  164.         takeItem(name,count)
  165.         print("item taken!")
  166.     elseif input == "l" then
  167.         term.clear()
  168.         term.setCursorPos(1,1)
  169.         for i,v in pairs(inv) do
  170.             print(inv[i].displayName..": "..tostring(inv[i].count))
  171.         end
  172.     elseif input == "d" then
  173.         if turtle.getItemCount(1) > 0 then
  174.             storeItem()
  175.             print("item gone!")
  176.         end
  177.     end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement