Advertisement
jig487

inv Manager

Aug 19th, 2021
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. local whiteList = {
  2.     "minecraft:skull",     3,
  3.     "minecraft:soul_sand", 4,
  4. }
  5.  
  6. while true do
  7.     local bool,data = turtle.inspect()
  8.     if bool then
  9.         print("Redstone level is "..data.metadata)
  10.     end
  11.     if data.metadata > 9 then
  12.         print("Redstone acceptable. Doing the thing")
  13.         turtle.turnLeft()
  14.  
  15.         for i = 1, 16 do --Dump inventory
  16.             local data = turtle.getItemDetail(i)
  17.             if data then
  18.                 turtle.select(i)
  19.                 turtle.drop()
  20.             end
  21.         end
  22.  
  23.         --Grab everything in 16 slot chest and make a list of each item, slot, and count
  24.         local itemList = {}
  25.         turtle.select(1)
  26.         for i = 1, 16 do
  27.             turtle.select(i)
  28.             turtle.suck()
  29.             local data = turtle.getItemDetail(i)
  30.             if not itemList[data.name] then --If new item then make a table for it
  31.                 itemList[data.name] = {}
  32.                 itemList[data.name][1] = { count = data.count, slot = i }
  33.             else --Item has already been seen. Insert an entry
  34.                 itemList[data.name][ #itemList[data.name]+1 ] = {count = data.count, slot = i}
  35.             end
  36.         end
  37.  
  38.         --Clear inventory of everything and all amounts not specified in whiteList
  39.         for i,name in pairs(itemList) do --Iterate through every name in the item list
  40.             for k = 1, #whiteList, 2 do --Iterate through whitelist and check name against it
  41.  
  42.                 if name ~= whiteList[k] and k == #whiteList-1 do --Name is not on whitelist. Dump it
  43.                     for j = 1, #itemList[name] do
  44.                         turtle.select(itemList[name][j].slot)
  45.                         turtle.drop()
  46.                     end
  47.                     break
  48.                 elseif name == whiteList[k] then --Name is on the whitelist. Dump extra items
  49.                     --Get target amount for whitelisted item
  50.                     local tA
  51.                     for j = 1, #whiteList, 2 do
  52.                         if whiteList[j] == name then
  53.                             tA = whitelist[j+1]
  54.                             break
  55.                         end
  56.                     end
  57.                     --Dump extra
  58.                     for l = 1, #itemList[name] do
  59.                         local keepAmount = 0
  60.                         if itemList[name][l].count + keepAmount < tA then --We don't have enough saved yet
  61.                             if itemList[l].count > tA + keepAmount then --Too much in this slot
  62.                                 turtle.drop(itemList[name][l].count-tA)
  63.                                 keepAmount = tA
  64.                             else
  65.                                 keepAmount = keepAmount + itemList[name][l].count --Not enough in just this slot
  66.                             end
  67.                         else --We've saved enough. Dump the extra
  68.                             turtle.select(itemList[name][l].slot)
  69.                             turtle.drop()
  70.                         end
  71.                     end
  72.                 end
  73.             end
  74.         end
  75.     else
  76.         print("Not doing the thing")
  77.     end
  78.     turtle.turnRight()
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement