brensen11

blib

May 4th, 2022 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.13 KB | None | 0 0
  1. blib = {}
  2. blib.coords = {x = 1, y = 0, z = 1, xf = 0, yf = 1}
  3. ------------------------------------------------------------------------------------------------------------------------
  4.  
  5. function blib.MapTurnRight()
  6.     if(turtle.turnRight()) then
  7.         if blib.coords.xf == 1 then
  8.             blib.coords.xf = 0
  9.             blib.coords.yf = -1
  10.         elseif blib.coords.xf == -1 then
  11.             blib.coords.xf = 0
  12.             blib.coords.yf = 1
  13.         elseif blib.coords.yf == 1 then
  14.             blib.coords.yf = 0
  15.             blib.coords.xf = 1
  16.         elseif blib.coords.yf == -1 then
  17.             blib.coords.yf = 0
  18.             blib.coords.xf = -1
  19.         end
  20.         return true
  21.     end
  22.     return false
  23. end
  24.  
  25. function blib.MapTurnLeft()
  26.     if(turtle.turnLeft()) then
  27.         if blib.coords.xf == 1 then
  28.             blib.coords.xf = 0
  29.             blib.coords.yf = 1
  30.         elseif blib.coords.xf == -1 then
  31.             blib.coords.xf = 0
  32.             blib.coords.yf = -1
  33.         elseif blib.coords.yf == 1 then
  34.             blib.coords.yf = 0
  35.             blib.coords.xf = -1
  36.         elseif blib.coords.yf == -1 then
  37.             blib.coords.yf = 0
  38.             blib.coords.xf = 1
  39.         end
  40.         return true
  41.     end
  42.     return false
  43. end
  44.  
  45. function blib.MapForward()
  46.     if(turtle.forward()) then
  47.         if blib.coords.xf == 1 then
  48.             blib.coords.x = blib.coords.x + 1
  49.         elseif blib.coords.xf == -1 then
  50.             blib.coords.x = blib.coords.x - 1
  51.         elseif blib.coords.yf == 1 then
  52.             blib.coords.y = blib.coords.y + 1
  53.         elseif blib.coords.yf == -1 then
  54.             blib.coords.y = blib.coords.y - 1
  55.         end
  56.         return true
  57.     end
  58.     return false
  59. end
  60.  
  61. function blib.MapBackward()
  62.     if(turtle.back()) then
  63.         if blib.coords.xf == 1 then
  64.             blib.coords.x = blib.coords.x - 1
  65.         elseif blib.coords.xf == -1 then
  66.             blib.coords.x = blib.coords.x + 1
  67.         elseif blib.coords.yf == 1 then
  68.             blib.coords.y = blib.coords.y - 1
  69.         elseif blib.coords.yf == -1 then
  70.             blib.coords.y = blib.coords.y + 1
  71.         end
  72.         return true
  73.     end
  74.     return false
  75. end
  76.  
  77. function blib.MapUp()
  78.     if(turtle.up()) then
  79.         blib.coords.z = blib.coords.z + 1
  80.         return true
  81.     end
  82.     return false
  83. end
  84.  
  85. function blib.MapDown()
  86.     if(turtle.down()) then
  87.         blib.coords.z = blib.coords.z - 1
  88.         return true
  89.     end
  90.     return false
  91. end
  92.  
  93. function blib.MapTurnRightDig()
  94.     blib.MapTurnRight()
  95.     blib.DetectAndDig()
  96.     blib.MapForward()
  97.     blib.MapTurnRight()
  98. end
  99.  
  100. function blib.MapTurnLeftDig()
  101.     blib.MapTurnLeft()
  102.     blib.DetectAndDig()
  103.     blib.MapForward()
  104.     blib.MapTurnLeft()
  105. end
  106.  
  107. function blib.DumpItemsExistingChest()
  108.     print("Dumping Items")
  109.     for slot = 1, slotCount do
  110.         turtle.select(slot)
  111.         if(turtle.getItemDetail(slot) ~= nil) then
  112.             local item = turtle.getItemDetail(slot)
  113.             if(item.name ~= "minecraft:coal" and item.name ~= "minecraft:chest" and item.name ~= "minecraft:torch" and item.name ~= "minecraft:charcoal") then
  114.                 turtle.drop()
  115.             end
  116.         end
  117.     end
  118.     return true
  119. end
  120.  
  121. -- location requires .x, .y, .z, .xf, .yf
  122. function blib.NavigateTo(loc)
  123.    
  124.  
  125.  
  126. end
  127.  
  128.  
  129.  
  130. -------------------------------------------------------------------------------------------------------------------------
  131.  
  132. slotCount = 16 --This library is meant for turtles, which have 16 slots
  133.  
  134. function blib.Clear()
  135.     term.clear()
  136.     term.setCursorPos(1,1)
  137. end
  138.  
  139.  
  140.  
  141.  
  142. --Checks fuel level and refules if fuel level is less than 10
  143. --refuels by checking each slot for fuel
  144. --returns true or false upon success or failure respectively
  145. function blib.CheckFuel(fuelLevel)
  146.     local returnVal = false
  147.     if(turtle.getFuelLevel() < fuelLevel) then
  148.         for slot = 1, slotCount do
  149.             print("Attempting to refuel on slot " .. slot)
  150.             turtle.select(slot)
  151.             local SlotInfo = turtle.getItemDetail(slot)
  152.             if(SlotInfo ~= nil and (SlotInfo.name == "minecraft:coal" or SlotInfo.name == "minecraft:charcoal")) then
  153.                 while(turtle.getFuelLevel() < fuelLevel) do
  154.                     if(not turtle.refuel(1)) then --turtle.refuel(# of items you want to refuel) e.g. 1 coal, 1 bucket of lava, etc
  155.                         return false
  156.                     end
  157.                 end
  158.                 return true
  159.             end
  160.         end
  161.         return false
  162.     end
  163.     return true
  164. end
  165.  
  166. function blib.PlaceTorch(direction)
  167.     local torchIdx = blib.GetItemIndex("minecraft:torch")
  168.     if(torchIdx == nil) then
  169.         return false
  170.     end
  171.     if(direction == "up") then
  172.         turtle.digUp()
  173.         turtle.select(torchIdx)
  174.         turtle.placeUp()
  175.         return true
  176.     elseif(direction == "left") then
  177.         turtle.turnLeft()
  178.         turtle.dig()
  179.         turtle.select(torchIdx)
  180.         turtle.place()
  181.         turtle.turnRight()
  182.         return true
  183.     end
  184.     turtle.digDown()
  185.     turtle.select(torchIdx)
  186.     turtle.placeDown()
  187.     return true
  188. end
  189.  
  190. ----------------------------- INVENTORY MANAGEMENT -----------------------------------------------------------------
  191.  
  192.  
  193. --Checks if all inventory slots are full
  194. --if full returns true
  195. function blib.InventoryFull()
  196.     local sum = 0
  197.     for slot = 1, slotCount do
  198.         if(turtle.getItemCount(slot) > 0) then
  199.             sum = sum + 1
  200.         end
  201.     end
  202.     if (sum == slotCount) then
  203.         return true
  204.     end
  205.     return false
  206. end
  207.  
  208. --Dumps all items EXCEPT coal and torches into a chest, and leaves it there that hopefully the turtle is carrying
  209. -- returns true upon success, false if otherwise
  210. function blib.DumpItemsChest()
  211.     print("Attempting to dump items")
  212.     local idx = blib.GetItemIndex("minecraft:chest")
  213.     if idx == nil then
  214.         return false
  215.     end
  216.     turtle.select( idx )
  217.     turtle.digDown()
  218.     turtle.placeDown()
  219.     print("Dumping Items")
  220.     for slot = 1, slotCount do
  221.         turtle.select(slot)
  222.         if(turtle.getItemDetail(slot) ~= nil) then
  223.             local item = turtle.getItemDetail(slot)
  224.            
  225.             if(item.name ~= "minecraft:coal" and item.name ~= "minecraft:chest" and item.name ~= "minecraft:torch" and item.name ~= "minecraft:charcoal") then
  226.                 turtle.dropDown()
  227.             end
  228.         end
  229.     end
  230.     return true
  231. end
  232.  
  233. --Dumps all items EXCEPT coal and torches into an enderchest its carrying
  234. function blib.DumpItemsEnderChest()
  235.     print("Attempting to dump items")
  236.     local idx = blib.GetItemIndex("minecraft:ender_chest")
  237.     if idx == nil then
  238.         return false
  239.     end
  240.     turtle.select(idx)
  241.     turtle.digUp()
  242.     turtle.placeUp()
  243.     print("Dumping Items")
  244.     for slot = 1, slotCount do
  245.         turtle.select(slot)
  246.         if(turtle.getItemDetail(slot) ~= nil) then
  247.             local item = turtle.getItemDetail(slot)
  248.            
  249.             if(item.name ~= "minecraft:coal" and itemname ~= "minecraft:torch") then
  250.                 turtle.dropUp()
  251.             end
  252.         end
  253.     end
  254.     turtle.digUp()
  255. end
  256.  
  257. --Checks each inv slot for enderchest
  258. --returns ender chest slot number OR nil if unsuccessful
  259. function blib.GetItemIndex(searchItem)
  260.     assert(type(searchItem) == "string", "expected string defining minecraft item")
  261.     print("Searching for " .. searchItem)
  262.     for slot = 1, slotCount do -- for(int i=0; i<16; i++), but lua uses 1 as start index, cringe.
  263.         local item = turtle.getItemDetail(slot)
  264.         if(item ~= nil) then
  265.             if(item.name == searchItem) then
  266.                 return slot
  267.             end
  268.         end
  269.     end
  270.     return nil
  271. end
  272.  
  273. function blib.SelectItem(searchItem)
  274.     local idx = blib.GetItemIndex(searchItem)
  275.     if(idx ~= nil) then
  276.         turtle.select(idx)
  277.         return true
  278.     end
  279.     return false
  280. end
  281.  
  282. ---------------------------------------------- EZ MINING --------------------------------------------------------------------
  283.  
  284. --For cases of gravel, sand, etc
  285. function blib.DetectAndDig()
  286.     while(turtle.detect()) do
  287.         turtle.dig()
  288.     end
  289. end
  290.  
  291. -- Detects and Digs, then digs Up and Down (obvis)
  292. function blib.Forward3()
  293.     blib.DetectAndDig()
  294.     turtle.forward()
  295.     turtle.digUp()
  296.     turtle.digDown()
  297. end
  298.  
  299. -- for edges, end of block, square, etc
  300. function blib.TurnRightDig()
  301.     turtle.turnRight()
  302.     blib.Forward3()
  303.     turtle.turnRight()
  304. end
  305.  
  306. -- ^^^ same as above (but left!)
  307. function blib.TurnLeftDig()
  308.     turtle.turnLeft()
  309.     blib.Forward3()
  310.     turtle.turnLeft()
  311. end
  312.  
  313. function blib.table_deepcopy(orig)
  314.     local orig_type = type(orig)
  315.     local copy
  316.     if orig_type == 'table' then
  317.         copy = {}
  318.         for orig_key, orig_value in next, orig, nil do
  319.             copy[blib.table_deepcopy(orig_key)] = blib.table_deepcopy(orig_value)
  320.         end
  321.         setmetatable(copy, blib.table_deepcopy(getmetatable(orig)))
  322.     else -- number, string, boolean, etc
  323.         copy = orig
  324.     end
  325.     return copy
  326. end
  327.  
  328. -- os.shutdown is a thing
  329. -- local user_input = io.read() or sum. lit.
  330.  
  331.  
  332. return blib
Add Comment
Please, Sign In to add comment