Advertisement
BasketMC

ComputerCraft: Chest Management API

Dec 4th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. -- apis/ChestManagement
  2.  
  3. -- Print what is inside the chest in front of the turtle
  4. function whatIsInChest()
  5.    
  6.     if turtle.detectDown() then
  7.         turtle.digDown()
  8.     end
  9.    
  10.     turtle.select(16)
  11.     turtle.placeDown()
  12.     turtle.select(1)
  13.    
  14.     -- Move everything from the chest in front to the swap chest below
  15.     while turtle.suck() do
  16.         if not turtle.dropDown() then
  17.             -- The swap chest is smaller than the source chest :(
  18.             print("Bottom chest is full??  Reverting")
  19.             while turtle.suckDown() do
  20.                 turtle.drop()
  21.             end
  22.             return false
  23.         end
  24.     end
  25.    
  26.     local contents = {}
  27.    
  28.     -- Move everything from the swap chest back into the chest in front
  29.     while turtle.suckDown() do
  30.         local info = turtle.getItemDetail(1)
  31.  
  32.         -- Look through existing content to see if it matches
  33.         for k, tItem1 in pairs(contents) do
  34.             if itemsAreTheSame(tItem1, info) then              
  35.                 contents[k].count = contents[k].count + info.count
  36.                 info = nil
  37.                 break
  38.             end
  39.         end
  40.  
  41.         if info ~= nil then
  42.             table.insert(contents, info)
  43.         end
  44.    
  45.         turtle.drop()
  46.     end
  47.    
  48.     -- Put the swap chest back into the inventory
  49.     turtle.select(16)
  50.     turtle.digDown()
  51.     turtle.select(1)
  52.    
  53.     for k, v in pairs(contents) do
  54.         for k2, v2 in pairs(v) do
  55.             print(k2 .. ": " .. v2)
  56.         end
  57.         print(" ")
  58.     end
  59.    
  60.     return contents
  61.  
  62. end
  63.  
  64. function itemsAreTheSame(tItem1, tItem2)
  65.  
  66.     if tItem1 == nil then
  67.         return false
  68.     end
  69.  
  70.     if tItem2 == nil then
  71.         return false
  72.     end
  73.  
  74.     for k, v in pairs(tItem1) do
  75.         if k ~= "count" and tItem1[k] ~= tItem2[k] then
  76.             return false
  77.         end
  78.     end
  79.  
  80.     -- Check it based on the keys for tItem2 just in case it has some extra important key
  81.     for k, v in pairs(tItem2) do
  82.         if k ~= "count" and tItem1[k] ~= tItem2[k] then
  83.             return false
  84.         end
  85.     end
  86.  
  87.     return true
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement