Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --riewestLIB
- --Version "1.1.1"
- --MCVersion "1.10.2"
- --Writer: Riewest (AKA Riewest14)
- function test()
- --slotsToKeep.chestSlot = 15
- --slotsToKeep.fuelSlot = 16
- --emptyInvExcept("top")
- end
- --Slots to keep
- --set with setSlots()
- slotsToKeep = {}
- Settings = {}
- --initializes values for how you
- --want the functions to behave
- --keepSlots: table containing the slots to keep
- -- slot naming scheme ex fuelSlot
- --Slots keys should be named as such as well
- function addSlots(keepSlots)
- if not keepSlots then
- Settings.keepSlotsActive = false
- else
- Settings.keepSlotsActive = true
- end
- if Settings.keepSlotsActive then
- for k, v in pairs(keepSlots) do
- slotsToKeep[k] = v
- end
- end
- end
- function addSettings(newSettings)
- if newSettings then
- for k, v in pairs(newSettings) do
- Settings[k] = v
- end
- end
- end
- ---------------
- --ITEM HANDLING
- ---------------
- --Places item in slot passed
- --on the side passed
- function placeSide(slot, side)
- if not slot then
- return
- end
- if not side then
- side = "front"
- end
- local oldSlot = turtle.getSelectedSlot()
- turtle.select(slot)
- if side == "front" then
- turtle.place()
- elseif side == "top" then
- turtle.placeUp()
- elseif side == "bottom" then
- turtle.placeDown()
- else
- rotate(side)
- turtle.place()
- reverseRotate(side)
- end
- turtle.select(oldSlot)
- end
- --Drops all items containing the
- --substring passed in. Keeps the
- --slots passed in.
- --side: side to drop items
- function dropItem(name, side)
- if not name then
- return
- end
- if not side then
- side = "front"
- end
- local slotsToDrop = {}
- local i = 1
- while i <= 16 do
- found, fslot = findSlot(name, i)
- if found then
- i = fslot + 1
- table.insert(slotsToDrop, fslot)
- else
- i = i + 1
- end
- end
- remove = {}
- if Settings.keepSlotsActive then
- for k, v in pairs(slotsToDrop) do
- for ik, jk in pairs(slotsToKeep) do
- if v == jk then
- table.insert(remove, k)
- end
- end
- end
- end
- for k, v in pairs(remove) do
- table.remove(slotsToDrop, v)
- end
- emptyInv(slotsToDrop, side)
- end
- --Attempts to get fuel from a
- --inventory on side passed and fill
- --the slot passed. Works best if
- --The item is still in slot or
- --Chest only has fuel items
- function getFuel(side)
- if not slotsToKeep.fuelSlot then
- print("Set Fuel Slot")
- return
- end
- if not side then
- side = "front"
- end
- oldSlot = turtle.getSelectedSlot()
- turtle.select(slotsToKeep.fuelSlot)
- side = string.lower(side)
- rotate(side)
- toGet = turtle.getItemSpace()
- if side == "front" then
- turtle.suck(toGet)
- elseif side == "top" then
- turtle.suckUp(toGet)
- elseif side == "bottom" then
- turtle.suckDown(toGet)
- else
- turtle.suck(toGet)
- end
- reverseRotate(side)
- turtle.select(oldSlot)
- end
- --Tries to put all of the items from
- --the all other slots into the
- --passed slot
- function sortItem(slot)
- if not slot then
- return
- end
- oldSlot = turtle.getSelectedSlot()
- turtle.select(slot)
- if turtle.getItemCount() > 0 then
- itemDetail = turtle.getItemDetail()
- for i = 1, 16 do
- turtle.select(i)
- if not (i == slot) and (turtle.getItemCount() > 0) then
- tempDetail = turtle.getItemDetail()
- if (itemDetail.name == tempDetail.name) then
- turtle.transferTo(slot)
- end
- end
- end
- end
- turtle.select(oldSlot)
- end
- --Empty's the entire inventory
- -- to the passed side
- function emptyAllInv(side)
- slots = {}
- for i = 1, 16 do
- table.insert(slots, i)
- end
- emptyInv(slots, side)
- end
- --Empty's the slots passed in through
- --the array.
- function emptyInv(slotsToEmpty, side)
- oldSlot = turtle.getSelectedSlot()
- if not side then
- side = "back"
- end
- if not slotsToEmpty then
- count = 16
- else
- count = table.getn(slotsToEmpty)
- end
- rotate(side)
- for i = 1, count do
- dropSide(slotsToEmpty[i], side)
- end
- reverseRotate(side)
- turtle.select(oldSlot)
- end
- --Empty's slots except the passed
- --in values
- function emptyInvExcept(side)
- local slotsToEmpty = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
- --table.sort(slotsToKeep)
- --for i = table.getn(slotsToKeep), 1, -1 do
- --table.remove(slotsToEmpty, slotsToKeep[i])
- --end
- local rem = {}
- for k, v in pairs(slotsToKeep) do
- table.insert(rem, v)
- end
- table.sort(rem)
- for i = table.getn(rem), 1, -1 do
- table.remove(slotsToEmpty, rem[i])
- end
- emptyInv(slotsToEmpty, side)
- end
- --Drops all the items in slotNum
- --to the indicated side
- function dropSide(slotNum, side)
- turtle.select(slotNum)
- side = string.lower(side)
- success = false
- if side == "front" then
- success = turtle.drop()
- elseif side == "top" then
- success = turtle.dropUp()
- elseif side == "bottom" then
- success = turtle.dropDown()
- else
- success = turtle.drop()
- end
- return success
- end
- --Method to tell the turtle to suck
- --in front of it and the left and right
- function tSuck(distinctItems)
- for i = 1, distinctItems do
- turtle.suck()
- end
- turtle.turnLeft()
- for i = 1, distinctItems do
- turtle.suck()
- end
- turnAround()
- for i = 1, distinctItems do
- turtle.suck()
- end
- turtle.turnLeft()
- end
- --Method to refuel the turtle
- --if its fuel level is below a
- --critical amount. Fuel slot is
- --passed in, as well as threshold
- function tRefuel(thresh)
- local fSlot = nil
- if not thresh then
- thresh = 100
- end
- if not slotsToKeep.fuelSlot then
- fSlot = 16
- else
- fSlot = slotsToKeep.fuelSlot
- end
- success = false
- if turtle.getFuelLevel() <= thresh then
- oldSlot = turtle.getSelectedSlot()
- turtle.select(fSlot)
- success = turtle.refuel(turtle.getItemCount() - 1)
- turtle.select(oldSlot)
- end
- return success
- end
- ------------------
- --MOVEMENT METHODS
- ------------------
- --Just runs turtle.forward()
- --until the specified amount toMov
- --is met. Digs block if unable to move
- function tMoveF(toMov)
- if not toMov then
- toMov = 1
- end
- for i = 1, toMov do
- while not (turtle.forward()) do
- turtle.dig()
- end
- end
- end
- --Runst turtle.back() toMov times
- function tMoveB(toMov)
- if not toMov then
- toMov = 1
- end
- for i = 1, toMov do
- while not turtle.back() do
- sleep(.1)
- end
- end
- end
- --Runs turtle.up() toMov times
- --and digs up if unable to move up.
- function tMoveU(toMov)
- if not toMov then
- toMov = 1
- end
- for i = 1, toMov do
- while not (turtle.up()) do
- turtle.digUp()
- end
- end
- end
- --Runs turtle.down() toMov times
- -- and digs down if unable to move down
- function tMoveD(toMov)
- if not toMov then
- toMov = 1
- end
- for i = 1, toMov do
- while not (turtle.down()) do
- turtle.digDown()
- end
- end
- end
- --Turns the turtle 180 degrees
- function turnAround()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- --Turns the turtle to the passed side
- function rotate(side)
- if not side then
- return
- elseif side == "back" then
- turnAround()
- elseif side == "left" then
- turtle.turnLeft()
- elseif side == "right" then
- turtle.turnRight()
- else
- end
- end
- --Turns the turtle opposite the
- -- passed side
- function reverseRotate(side)
- if not side then
- return
- elseif side == "back" then
- turnAround()
- elseif side == "left" then
- turtle.turnRight()
- elseif side == "right" then
- turtle.turnLeft()
- else
- end
- end
- --------------
- --DATA METHODS
- --------------
- --Splits string into table on the delimiter :
- function split(str)
- local fields = {}
- for word in string.gmatch(str, '([^:]+)') do
- table.insert(fields, word)
- end
- return fields
- end
- --removes the value passed from the
- -- table for all instances
- function removeTableVal(tab, val)
- if not tab then
- return
- end
- for i = table.getn(tab), 1, -1 do
- if tab[i] == val then
- table.remove(tab, i)
- end
- end
- return tab
- end
- --returns the slots that can be emptied
- function getSlotsToEmpty()
- local slotsToEmpty = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
- for k, v in pairs(slotsToKeep) do
- slotsToEmpty = removeTableVal(slotsToEmpty, v)
- end
- return slotsToEmpty
- end
- --Displays fuel level in the upper
- --right corner of the screen
- function displayFuel()
- fuelLevel = turtle.getFuelLevel()
- oldX, oldY = term.getCursorPos()
- displayY = 1
- displayX = 34
- term.setCursorPos(displayX, displayY)
- print("Fuel:")
- term.setCursorPos(displayX, displayY + 1)
- print(fuelLevel)
- term.setCursorPos(oldX, oldY)
- end
- --Function to find the slot number
- --Of the item containing passed in
- --String, Return bool, slot
- function findSlot(name, startSlot)
- if not startSlot then
- startSlot = 1
- end
- found = false
- slot = 0
- for i = startSlot, 16 do
- if turtle.getItemCount(i) > 0 then
- itemDetail = turtle.getItemDetail(i)
- if string.match(itemDetail.name, string.lower(name)) then
- slot = i
- found = true
- break
- end
- end
- end
- return found, slot
- end
- test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement