t8rt0t

turtAPI.lua

Mar 13th, 2024 (edited)
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.15 KB | None | 0 0
  1. os.loadAPI("apis/refuel.lua")
  2. os.loadAPI("apis/stats.lua")
  3.  
  4. --Improved turtle.forward()
  5. --@OptionalArg number amount: The amount it should move forward, default 1
  6. function forward(amount)
  7.     amount = tonumber(amount,10) or 1
  8.     for i=1,amount do
  9.         turtle.forward()
  10.     end
  11.     stats.add("Times Moved",amount)
  12. end
  13. --Improved turtle.back()
  14. --@OptionalArg number amount: The amount it should move back, default 1
  15. function back(amount)
  16.     amount = tonumber(amount,10) or 1
  17.     for i=1,amount do
  18.         turtle.back()
  19.     end
  20.     stats.add("Times Moved",amount)
  21. end
  22. --Improved turtle.turnRight()
  23. --@OptionalArg number amount: The amount it should turn, default 1
  24. function turnRight(amount)
  25.     amount = tonumber(amount,10) or 1
  26.     amount = amount%4
  27.     for i=1,amount do
  28.         turtle.turnRight()
  29.     end
  30.     stats.add("Times Turned Right",amount)
  31. end
  32. --Improved turtle.turnLeft()
  33. --@OptionalArg number amount: The amount it should turn, default 1
  34. function turnLeft(amount)
  35.     amount = tonumber(amount,10) or 1
  36.     amount = amount%4
  37.     for i=1,amount do
  38.         turtle.turnLeft()
  39.     end
  40.     stats.add("Times Turned Left",amount)
  41. end
  42. --Improved turtle.dig(). Will be able to tolerate gravel falling, and won't waste time if theres nothing to dig
  43. function dig()
  44.     while turtle.detect() do
  45.         turtle.dig()
  46.         stats.add("Amount Dug",1)
  47.     end
  48. end
  49. --Improved turtle.digUp(). Will be able to tolerate gravel falling, and won't waste time if theres nothing to dig
  50. function digUp()
  51.     while turtle.detectUp() do
  52.         turtle.digUp()
  53.         stats.add("Amount Dug",1)
  54.     end
  55. end
  56. --Improved turtle.digDown(). Will be able to tolerate gravel falling, and won't waste time if theres nothing to dig
  57. function digDown()
  58.     while turtle.detectDown() do
  59.         turtle.digDown()
  60.         stats.add("Amount Dug",1)
  61.     end
  62. end
  63. --Improved turtle.down()
  64. --@OptionalArg number amount: The amount it should move down, default 1
  65. function down(amount)
  66.     amount = tonumber(amount) or 1
  67.     for i=1,amount do
  68.         digDown()
  69.         turtle.down()
  70.     end
  71.     stats.add("Times Moved",amount)
  72.     stats.add("Vertical Distance",amount)
  73. end
  74. --Improved turtle.down()
  75. --@OptionalArg number amount: The amount it should move down, default 1
  76. function up(amount)
  77.     amount = tonumber(amount) or 1
  78.     for i=1,amount do
  79.         digUp()
  80.         turtle.up()
  81.     end
  82.     stats.add("Times Moved",amount)
  83.     stats.add("Vertical Distance",amount)
  84. end
  85. --Improved turtle.place()
  86. --Places the item in a specified slot
  87. --@OptionalArg number slot: The slot to select before placing
  88. --@OptionalArg string signText: The sign text to write
  89. function place(slot,signText)
  90.     if slot then turtle.select(slot) end
  91.     --dig()
  92.     turtle.place(signText)
  93.     stats.add("Blocks Placed",1)
  94. end
  95. --Improved turtle.placeDown()
  96. --Places the item in a specified slot
  97. --@OptionalArg number slot: The slot to select before placing
  98. --@OptionalArg string signText: The sign text to write
  99. function placeDown(slot,signText)
  100.     if slot then turtle.select(slot) end
  101.     turtle.placeDown(signText)
  102.     stats.add("Blocks Placed",1)
  103. end
  104. --Improved turtle.placeUp()
  105. --Places the item in a specified slot
  106. --@OptionalArg number slot: The slot to select before placing
  107. --@OptionalArg string signText: The sign text to write
  108. function placeUp(slot,signText)
  109.     if slot then turtle.select(slot) end
  110.     turtle.placeUp(signText)
  111.     stats.add("Blocks Placed",1)
  112. end
  113. --Improved turtle.drop()
  114. --Drops the item in the selected slot
  115. --@OptionalArg number slot: The slot to select before dropping
  116. --@Returns boolean success: Whether the item was dropped
  117. function drop(slot)
  118.     if slot then turtle.select(slot) end
  119.     local count = turtle.getItemCount()
  120.     if turtle.drop() then
  121.         stats.add("Items Dropped",count or 0)
  122.         return true
  123.     else
  124.         return false
  125.     end
  126. end
  127. --Improved turtle.dropDown()
  128. --Drops the item in the selected slot
  129. --@OptionalArg number slot: The slot to select before dropping
  130. --@Returns boolean success: Whether the item was dropped
  131. function dropDown(slot)
  132.     if slot then turtle.select(slot) end
  133.     local count = turtle.getItemCount()
  134.     if turtle.dropDown() then
  135.         stats.add("Items Dropped",count or 0)
  136.         return true
  137.     else
  138.         return false
  139.     end
  140. end
  141. --Improved turtle.dropUp()
  142. --Drops the item in the selected slot
  143. --@OptionalArg number slot: The slot to select before dropping
  144. --@Returns boolean success: Whether the item was dropped
  145. function dropUp(slot)
  146.     if slot then turtle.select(slot) end
  147.     local count = turtle.getItemCount()
  148.     if turtle.dropUp() then
  149.         stats.add("Items Dropped",count or 0)
  150.         return true
  151.     else
  152.         return false
  153.     end
  154. end
  155. --Detects whether the dispenser below the turtle is facing up or not
  156. --@Returns boolean isDispFacingUp: Whether the block below the turtle is a dispenser that is facing up
  157. function isDispFacingUp()
  158.     if not turtle.detectDown() then return false end
  159.     local success,data = turtle.inspectDown()
  160.     if not success then return false end
  161.     if not data.name == "minecraft:dispenser" then return false end
  162.     if not data.state.facing == "up" then return false end
  163.     return data.state.facing == "up"
  164.     --return true
  165. end
  166. --Runs a routine to store the items, using a dispenser and a backpack
  167. --MAKE SURE QUARK IS INSTALLED
  168. --@OptionalArg string backpack: "sophisticatedbackpacks:<tier>_backpack"
  169. --@Returns boolean success, string errMsg: If it was successful
  170. function storeItems(backpack)
  171.     local slot = nil
  172.     local dispSlot = refuel.findItem("minecraft:dispenser")
  173.     if not dispSlot then return false, "no dispenser found" end
  174.     if not backpack then
  175.         local tiers = {"","copper_","iron_","gold_","diamond_","netherite_"}
  176.         for i=1,#tiers do
  177.             slot = refuel.findItem("sophisticatedbackpacks:"..tiers[i].."backpack")
  178.             if slot then return storeItems("sophisticatedbackpacks:"..tiers[i].."backpack") end
  179.         end
  180.         return false, "could not find backpack"
  181.     else
  182.         slot = refuel.findItem(backpack)
  183.     end
  184.     if not slot then return false, "no backpack found" end
  185.     digDown()
  186.     placeDown(dispSlot)
  187.     local count = 0
  188.     while not isDispFacingUp() do
  189.         digDown()
  190.         down()
  191.         local slot = refuel.findItem("minecraft:cobblestone") or refuel.findItem("minecraft:cobbled_deepslate")
  192.         if slot then placeDown(slot)
  193.         elseif count >= 10 then
  194.             up()
  195.             error("trouble storing items")
  196.         end
  197.         count = count + 1
  198.         up()
  199.         placeDown(refuel.findItem("minecraft:dispenser"))
  200.     end
  201.     dropDown(slot)
  202.     dig()
  203.     forward()
  204.     digDown()
  205.     down()
  206.     redstone.setOutput("back",true)
  207.     up()
  208.     turnLeft(2)
  209.     for i=1,16 do
  210.         if turtle.getItemCount(i) >0 and not drop(i) then
  211.             dig()
  212.             forward()
  213.             digDown()
  214.             turnLeft(2)
  215.             return false, "inventory full"
  216.         end
  217.     end
  218.     dig()
  219.     forward()
  220.     digDown()
  221.     if count > 0 then
  222.         down()
  223.         digDown()
  224.         up()
  225.     end
  226.     turnLeft(2)
  227.     return true
  228. end
  229. --Detects if there is an empty slot in the turtle's inventory
  230. --@Returns number slot: The slot number, or -1 if there isn't a free slot
  231. function getFreeSlot()
  232.     for i=1,16 do
  233.         if turtle.getItemCount(i) <= 0 then return i end
  234.     end
  235.     return -1
  236. end
  237. --Counts the number of empty slots in the turtle's inventory
  238. --@Returns number count: The number of free slots in the turtle's inventory
  239. function countFreeSlots()
  240.     local count = 0
  241.     for i=1,16 do
  242.         if turtle.getItemCount(i) <= 0 then count = count + 1 end
  243.     end
  244.     return count
  245. end
  246. --Counts the amount of items of a type can fit in a turtles inventory
  247. --@OptionalArg string id: The id of the item to be added
  248. --@Returns number space: The amount of items that can be added
  249. function getFreeSpace(id)
  250.     local count = 0
  251.     for i=1,16 do
  252.         if not id or not turtle.getItemDetail(i) or turtle.getItemDetail(i).name == id then
  253.             count = count + turtle.getItemSpace(i)
  254.         end
  255.     end
  256.     return count
  257. end
Add Comment
Please, Sign In to add comment