Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- blib = {}
- blib.coords = {x = 1, y = 0, z = 1, xf = 0, yf = 1}
- ------------------------------------------------------------------------------------------------------------------------
- function blib.MapTurnRight()
- if(turtle.turnRight()) then
- if blib.coords.xf == 1 then
- blib.coords.xf = 0
- blib.coords.yf = -1
- elseif blib.coords.xf == -1 then
- blib.coords.xf = 0
- blib.coords.yf = 1
- elseif blib.coords.yf == 1 then
- blib.coords.yf = 0
- blib.coords.xf = 1
- elseif blib.coords.yf == -1 then
- blib.coords.yf = 0
- blib.coords.xf = -1
- end
- return true
- end
- return false
- end
- function blib.MapTurnLeft()
- if(turtle.turnLeft()) then
- if blib.coords.xf == 1 then
- blib.coords.xf = 0
- blib.coords.yf = 1
- elseif blib.coords.xf == -1 then
- blib.coords.xf = 0
- blib.coords.yf = -1
- elseif blib.coords.yf == 1 then
- blib.coords.yf = 0
- blib.coords.xf = -1
- elseif blib.coords.yf == -1 then
- blib.coords.yf = 0
- blib.coords.xf = 1
- end
- return true
- end
- return false
- end
- function blib.MapForward()
- if(turtle.forward()) then
- if blib.coords.xf == 1 then
- blib.coords.x = blib.coords.x + 1
- elseif blib.coords.xf == -1 then
- blib.coords.x = blib.coords.x - 1
- elseif blib.coords.yf == 1 then
- blib.coords.y = blib.coords.y + 1
- elseif blib.coords.yf == -1 then
- blib.coords.y = blib.coords.y - 1
- end
- return true
- end
- return false
- end
- function blib.MapBackward()
- if(turtle.back()) then
- if blib.coords.xf == 1 then
- blib.coords.x = blib.coords.x - 1
- elseif blib.coords.xf == -1 then
- blib.coords.x = blib.coords.x + 1
- elseif blib.coords.yf == 1 then
- blib.coords.y = blib.coords.y - 1
- elseif blib.coords.yf == -1 then
- blib.coords.y = blib.coords.y + 1
- end
- return true
- end
- return false
- end
- function blib.MapUp()
- if(turtle.up()) then
- blib.coords.z = blib.coords.z + 1
- return true
- end
- return false
- end
- function blib.MapDown()
- if(turtle.down()) then
- blib.coords.z = blib.coords.z - 1
- return true
- end
- return false
- end
- function blib.MapTurnRightDig()
- blib.MapTurnRight()
- blib.DetectAndDig()
- blib.MapForward()
- blib.MapTurnRight()
- end
- function blib.MapTurnLeftDig()
- blib.MapTurnLeft()
- blib.DetectAndDig()
- blib.MapForward()
- blib.MapTurnLeft()
- end
- function blib.DumpItemsExistingChest()
- print("Dumping Items")
- for slot = 1, slotCount do
- turtle.select(slot)
- if(turtle.getItemDetail(slot) ~= nil) then
- local item = turtle.getItemDetail(slot)
- if(item.name ~= "minecraft:coal" and item.name ~= "minecraft:chest" and item.name ~= "minecraft:torch" and item.name ~= "minecraft:charcoal") then
- turtle.drop()
- end
- end
- end
- return true
- end
- -- location requires .x, .y, .z, .xf, .yf
- function blib.NavigateTo(loc)
- end
- -------------------------------------------------------------------------------------------------------------------------
- slotCount = 16 --This library is meant for turtles, which have 16 slots
- function blib.Clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- --Checks fuel level and refules if fuel level is less than 10
- --refuels by checking each slot for fuel
- --returns true or false upon success or failure respectively
- function blib.CheckFuel(fuelLevel)
- local returnVal = false
- if(turtle.getFuelLevel() < fuelLevel) then
- for slot = 1, slotCount do
- print("Attempting to refuel on slot " .. slot)
- turtle.select(slot)
- local SlotInfo = turtle.getItemDetail(slot)
- if(SlotInfo ~= nil and (SlotInfo.name == "minecraft:coal" or SlotInfo.name == "minecraft:charcoal")) then
- while(turtle.getFuelLevel() < fuelLevel) do
- if(not turtle.refuel(1)) then --turtle.refuel(# of items you want to refuel) e.g. 1 coal, 1 bucket of lava, etc
- return false
- end
- end
- return true
- end
- end
- return false
- end
- return true
- end
- function blib.PlaceTorch(direction)
- local torchIdx = blib.GetItemIndex("minecraft:torch")
- if(torchIdx == nil) then
- return false
- end
- if(direction == "up") then
- turtle.digUp()
- turtle.select(torchIdx)
- turtle.placeUp()
- return true
- elseif(direction == "left") then
- turtle.turnLeft()
- turtle.dig()
- turtle.select(torchIdx)
- turtle.place()
- turtle.turnRight()
- return true
- end
- turtle.digDown()
- turtle.select(torchIdx)
- turtle.placeDown()
- return true
- end
- ----------------------------- INVENTORY MANAGEMENT -----------------------------------------------------------------
- --Checks if all inventory slots are full
- --if full returns true
- function blib.InventoryFull()
- local sum = 0
- for slot = 1, slotCount do
- if(turtle.getItemCount(slot) > 0) then
- sum = sum + 1
- end
- end
- if (sum == slotCount) then
- return true
- end
- return false
- end
- --Dumps all items EXCEPT coal and torches into a chest, and leaves it there that hopefully the turtle is carrying
- -- returns true upon success, false if otherwise
- function blib.DumpItemsChest()
- print("Attempting to dump items")
- local idx = blib.GetItemIndex("minecraft:chest")
- if idx == nil then
- return false
- end
- turtle.select( idx )
- turtle.digDown()
- turtle.placeDown()
- print("Dumping Items")
- for slot = 1, slotCount do
- turtle.select(slot)
- if(turtle.getItemDetail(slot) ~= nil) then
- local item = turtle.getItemDetail(slot)
- if(item.name ~= "minecraft:coal" and item.name ~= "minecraft:chest" and item.name ~= "minecraft:torch" and item.name ~= "minecraft:charcoal") then
- turtle.dropDown()
- end
- end
- end
- return true
- end
- --Dumps all items EXCEPT coal and torches into an enderchest its carrying
- function blib.DumpItemsEnderChest()
- print("Attempting to dump items")
- local idx = blib.GetItemIndex("minecraft:ender_chest")
- if idx == nil then
- return false
- end
- turtle.select(idx)
- turtle.digUp()
- turtle.placeUp()
- print("Dumping Items")
- for slot = 1, slotCount do
- turtle.select(slot)
- if(turtle.getItemDetail(slot) ~= nil) then
- local item = turtle.getItemDetail(slot)
- if(item.name ~= "minecraft:coal" and itemname ~= "minecraft:torch") then
- turtle.dropUp()
- end
- end
- end
- turtle.digUp()
- end
- --Checks each inv slot for enderchest
- --returns ender chest slot number OR nil if unsuccessful
- function blib.GetItemIndex(searchItem)
- assert(type(searchItem) == "string", "expected string defining minecraft item")
- print("Searching for " .. searchItem)
- for slot = 1, slotCount do -- for(int i=0; i<16; i++), but lua uses 1 as start index, cringe.
- local item = turtle.getItemDetail(slot)
- if(item ~= nil) then
- if(item.name == searchItem) then
- return slot
- end
- end
- end
- return nil
- end
- function blib.SelectItem(searchItem)
- local idx = blib.GetItemIndex(searchItem)
- if(idx ~= nil) then
- turtle.select(idx)
- return true
- end
- return false
- end
- ---------------------------------------------- EZ MINING --------------------------------------------------------------------
- --For cases of gravel, sand, etc
- function blib.DetectAndDig()
- while(turtle.detect()) do
- turtle.dig()
- end
- end
- -- Detects and Digs, then digs Up and Down (obvis)
- function blib.Forward3()
- blib.DetectAndDig()
- turtle.forward()
- turtle.digUp()
- turtle.digDown()
- end
- -- for edges, end of block, square, etc
- function blib.TurnRightDig()
- turtle.turnRight()
- blib.Forward3()
- turtle.turnRight()
- end
- -- ^^^ same as above (but left!)
- function blib.TurnLeftDig()
- turtle.turnLeft()
- blib.Forward3()
- turtle.turnLeft()
- end
- function blib.table_deepcopy(orig)
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in next, orig, nil do
- copy[blib.table_deepcopy(orig_key)] = blib.table_deepcopy(orig_value)
- end
- setmetatable(copy, blib.table_deepcopy(getmetatable(orig)))
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- -- os.shutdown is a thing
- -- local user_input = io.read() or sum. lit.
- return blib
Add Comment
Please, Sign In to add comment