Advertisement
RasmusStagsted

ComputerCraft Inv API

Jun 21st, 2018
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. --[[
  2.  
  3. local functions:
  4.  
  5.     split(inputstr, sep)
  6.  
  7. global functions:
  8.  
  9.     compareItems(itemStr1, itemStr2)
  10.     select(slot)
  11.     getItemDetail(slot)
  12.     selectItem(item, wait)
  13.     getItemCount(item)
  14.     waitForItems(item, count, timeout)
  15.     emptySlots()
  16.     emptyItemFunc(item, count, wait, func)
  17.     emptyItem(item, count, wait)
  18.     emptyItemUp(item, count, wait)
  19.     emptyItemDown(item, count, wait)
  20.     stack()
  21.  
  22. --]]
  23.  
  24. local function split(inputstr, sep)
  25.         if sep == nil then
  26.                 sep = "%s"
  27.         end
  28.         local table = {}
  29.         local i = 1
  30.         for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  31.                 table [i] = str
  32.                 i = i + 1
  33.         end
  34.         return table
  35. end
  36.  
  37. function compareItems(itemStr1, itemStr2)
  38.     if itemStr1 and itemStr2 then
  39.         local mod1, item1, damage1 = unpack(split(itemStr1, ":"))
  40.         local mod2, item2, damage2 = unpack(split(itemStr2, ":"))
  41.         if (mod1 == mod2) and (item1 == item2) then
  42.             if damage1 and damage2 then
  43.                 return damage1 == damage2
  44.             else
  45.                 return true
  46.             end
  47.         else
  48.             return false
  49.         end
  50.     else
  51.         return false
  52.     end
  53. end
  54.  
  55. function select(slot)
  56.     assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
  57.     turtle.select(slot)
  58. end
  59.  
  60. function getItemDetail(slot)
  61.     if slot == nil then
  62.         slot = turtle.getSelectedSlot()
  63.     end
  64.     assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
  65.  
  66.     local data = turtle.getItemDetail(slot)
  67.     if data then
  68.         return data.name .. ":" .. data.damage
  69.     else
  70.         return nil
  71.     end
  72. end
  73.  
  74. function selectItem(item, wait)
  75.     local trial = 0
  76.     while true do
  77.         for i = 1, 16, 1 do
  78.             if  compareItems(item, getItemDetail(i)) then
  79.                 turtle.select(i)
  80.                 return true
  81.             end
  82.         end
  83.         if not wait then
  84.             return false
  85.         else
  86.             print("Can't select " .. item .. " - " .. trial)
  87.             os.sleep(1)
  88.             trial = trial + 1
  89.         end
  90.     end
  91. end
  92.  
  93. function getItemCount(item)
  94.     local count = 0
  95.     for i = 1, 16, 1 do
  96.         if compareItems(item, getItemDetail(i)) then
  97.             count = count + turtle.getItemCount(i)
  98.         end
  99.     end
  100.     return count
  101. end
  102.  
  103. function waitForItems(item, count, timeout)
  104.     local trial = 0
  105.     while inv.getItemCount(item) < count do
  106.         print("Missing " .. item .. " - " .. count - inv.getItemCount(item) .. " (" .. inv.getItemCount(item) .. " of " .. count .. ") - " .. trial)
  107.         if not timeout == nil and timeout == trial then
  108.             return false
  109.         end
  110.         os.sleep(1)
  111.     end
  112.     return true
  113. end
  114.  
  115. function emptySlots()
  116.     local count = 0
  117.     for i = 1, 16, 1 do
  118.         if (turtle.getItemCount(i) == 0) then
  119.             count = count + 1
  120.         end
  121.     end
  122.     return count
  123. end
  124.  
  125. function emptyItemFunc(item, count, wait, func)
  126.     if count == nil then
  127.         count = getItemCount(item)
  128.     end
  129.     local i = 0
  130.     local trial = 0
  131.     while i < count do
  132.         if not selectItem(item) then
  133.             return true
  134.         else
  135.             local pre_count = getItemCount(item)
  136.             if not func(math.min(count - i, 64)) then
  137.                 if wait then
  138.                     print("Can't drop item - " .. trial)
  139.                     os.sleep(1)
  140.                     trial = trial + 1
  141.                 else
  142.                     return false
  143.                 end
  144.             end
  145.             local post_count = getItemCount(item)
  146.             i = i + pre_count - post_count
  147.         end
  148.     end
  149.     return true
  150. end
  151.  
  152. function emptyItem(item, count, wait)
  153.     emptyItemFunc(item, count, wait, turtle.drop)
  154. end
  155.  
  156. function emptyItemUp(item, count, wait)
  157.     emptyItemFunc(item, count, wait, turtle.dropUp)
  158. end
  159.  
  160. function emptyItemDown(item, count, wait)
  161.     emptyItemFunc(item, count, wait, turtle.dropDown)
  162. end
  163.  
  164. function stack()
  165.     for i = 1, 16, 1 do
  166.         for j = 1, 16 - i do
  167.             if compareItems(getItemDetail(i), getItemDetail(i + j)) then
  168.                 select(i + j)
  169.                 turtle.transferTo(i, 64 - turtle.getItemCount(i))
  170.             end
  171.         end
  172.     end
  173. end
  174.  
  175. --[[
  176. function export(slot, resourceChest, count)
  177.     assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
  178.     assert(tonumber(resourceChest) and resourceChest >= 1 and resourceChest <= 16, "Expected slot number in range 1-16, but got: " .. resourceChest)
  179.     if (count == nil) then
  180.         count = 64
  181.     else
  182.         assert(tonumber(count) and count <= turtle.getItemCount(slot) and count >= 0, "Expected count in range 0-" .. turtle.getItemCount() .. ", but got: " .. count)
  183.     end
  184.     select(resourceChest)
  185.     while (not turtle.placeUp()) do
  186.        
  187.     end
  188.     select(slot)
  189.     local temp = turtle.getItemCount(slot)
  190.     while (turtle.getItemCount(slot) > 0) do
  191.         turtle.dropUp(turtle.getItemCount(slot) - (temp - count))
  192.     end
  193.     select(resourceChest)
  194.     turtle.digUp()
  195. end
  196.  
  197. function import(slot, resourceChest, count)
  198.     assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
  199.     assert(tonumber(resourceChest) and resourceChest >= 1 and resourceChest <= 16, "Expected slot number in range 1-16, but got: " .. resourceChest)
  200.     if (count == nil) then
  201.         count = 64
  202.     else
  203.         assert(tonumber(count) and count <= 64 and count >= 0, "Expected count in range 0-64, but got: " .. count)
  204.     end
  205.     select(resourceChest)
  206.     while (not turtle.placeUp()) do
  207.        
  208.     end
  209.     select(slot)
  210.     while (turtle.getItemCount(slot) < count) do
  211.         turtle.suckUp(count - turtle.getItemCount(slot))
  212.     end
  213.     select(resourceChest)
  214.     turtle.digUp()
  215. end
  216.  
  217. function emptySlot(slot, trashChest)
  218.     assert(slot, "Expected slot number or array of slot numbers in range 1-16, but got: " .. textutils.serialize(slot))
  219.     if (type(slot) == "table") then
  220.         select(trashChest)
  221.         while (not turtle.placeUp()) do
  222.        
  223.         end
  224.         for i = 1, table.getn(slot), 1 do
  225.             select(slot[i])
  226.             while (turtle.getItemCount(slot[i]) > 0) do
  227.                 turtle.dropUp(turtle.getItemCount(slot[i]))
  228.             end
  229.         end
  230.         select(trashChest)
  231.         turtle.digUp()
  232.    
  233.     else
  234.         select(trashChest)
  235.         while (not turtle.placeUp()) do
  236.        
  237.         end
  238.         select(slot)
  239.         while (turtle.getItemCount(slot) > 0) do
  240.             turtle.dropUp(turtle.getItemCount(slot))
  241.         end
  242.         select(trashChest)
  243.         turtle.digUp()
  244.     end
  245. end
  246.  
  247. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement