Jameelo

storageLib

Apr 26th, 2025 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.22 KB | Gaming | 0 0
  1. --[[
  2.     This library deals with interacting with inventories, both internal and external
  3. ]]
  4.  
  5. os.loadAPI("common/systemLib.lua")
  6.  
  7. CHESTS = {"minecraft:chest", "minecraft:barrel", "minecraft:trapped_chest", "aether:treasure_chest", "twilightforest:twilight_oak_chest",
  8.           "twilightforest:canopy_chest", "twilightforest:mangrove_chest", "twilightforest:dark_chest", "twilightforest:time_chest", "twilightforest:transformation_chest",
  9.           "twilightforest:mining_chest", "twilightforest:sorting_chest"}
  10. ECHESTS = {"enderstorage:ender_chest", "minecraft:ender_chest", "enderchests:ender_chest"}
  11.  
  12. ALLCHESTS = {table.unpack(CHESTS), table.unpack(ECHESTS)}
  13.  
  14. function emptyInv(direction, EChestPlaced) -- Dump everything except chests.
  15.     local directions = {up    = turtle.dropUp,
  16.                         down  = turtle.dropDown,
  17.                         front = turtle.drop}
  18.     local drop = true
  19.  
  20.     for n = 1,16,1 do -- for all inventory cells
  21.         if turtle.getItemCount(n) ~= 0 then -- if the item count in this cell is more than zero
  22.             if systemLib.contains(CHESTS,turtle.getItemDetail(n).name) and not EChestPlaced then -- If the item is a chest, don't dump it unless we've placed an echest
  23.                 drop = false
  24.             end
  25.             if drop then
  26.                 turtle.select(n)
  27.                 directions[direction]()
  28.             end
  29.         end
  30.         drop = true
  31.     end
  32. end
  33.  
  34. function dumpItems() -- Empty the inventory, prioritising ender chest usage
  35.     local chestIndex = findChest(ECHESTS) -- Get the index of the ender chest in the inventory, if it exists.
  36.     local eChest = false
  37.     local leftCount = 0
  38.  
  39.     if chestIndex > 0 then -- Echest Present
  40.         eChest = true
  41.     else
  42.         chestIndex = findChest(CHESTS)
  43.     end
  44.  
  45.     -- Direction determination
  46.     if chestIndex > 0 then
  47.         turtle.select(chestIndex)
  48.         if not turtle.detectUp() then
  49.             -- We can place the chest above us
  50.             turtle.placeUp()
  51.             emptyInv("up",eChest)
  52.             if eChest then
  53.                 turtle.digUp()
  54.             end
  55.             return
  56.         else
  57.             -- If you cannot place the chest above you
  58.             for _ = 1,4,1 do
  59.                 turtle.turnLeft()
  60.                 leftCount = leftCount + 1
  61.                 if not turtle.detect() then
  62.                     -- Can place the chest in this direction
  63.                     turtle.place()
  64.                     emptyInv("front",eChest)
  65.                     if eChest then
  66.                         turtle.dig()
  67.                     end
  68.                     for _ = 1,leftCount,1 do
  69.                         turtle.turnRight()
  70.                     end
  71.                     return
  72.                 end
  73.             end
  74.             shell.run("os.shutdown()")
  75.         end
  76.     else
  77.         -- If there are no chests whatsoever
  78.         printError("No chests available!",0)
  79.     end
  80. end
  81.  
  82. function findChest(chestArray) -- loops through
  83.     local currChest
  84.     for _,chestID in pairs(chestArray) do
  85.         currChest = findItemBF(chestID)
  86.         if currChest > 0 then
  87.             return currChest
  88.         end
  89.     end
  90.     return 0
  91. end
  92.  
  93. function findItemBF(ID) -- brute force finds any item passed to it, otherwise returns 0
  94.     for n = 1,16,1 do
  95.         if turtle.getItemCount(n) ~= 0 then
  96.             if turtle.getItemDetail(n).name == ID then
  97.                 return n
  98.             end
  99.         end
  100.     end
  101.     return 0
  102. end
  103.  
  104. function refuelChestSafe() -- Refuel without comsuming any chests
  105.     local isRefueled
  106.     for index = 1,16,1 do
  107.         turtle.select(index)
  108.         local isFuel, _ = turtle.refuel(0) -- See if there's any fuel in this slot
  109.         if isFuel then
  110.             if not systemLib.contains(CHESTS,turtle.getItemDetail(index).name) then
  111.                 isRefueled = turtle.refuel() -- Om nom nom
  112.             end
  113.         end
  114.     end
  115.     return isRefueled
  116. end
  117.  
  118. function everySlotTaken()
  119.     --Cycle through all the slots and get the inventory size, if every cell has at least 1 item in it then there's no space left for new items
  120.     for n = 1,16,1 do
  121.         if turtle.getItemCount(n) == 0 then
  122.             return false
  123.         end
  124.     end
  125.     return true
  126. end
Add Comment
Please, Sign In to add comment