Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Enchanting Turtle V0.2 by QQII
- --Declerations
- local enchantLevel = 0 --The level the turtle initially attemps to perform an enchant at
- local enchants = 0 --The number of enchants performed by the turtle
- local slot = 1 --The slot number that the items to be enchanted are in
- --Functions
- function wrapPeripheral(thing)
- local Sides = {
- "left","right",
- "top","bottom",
- "front","back",
- }
- for i,v in pairs(Sides) do
- if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
- print("'"..thing.."' found on side '"..v.."'.")
- return peripheral.wrap(v), v
- end
- end
- print("ERROR: '"..thing.."' not found!")
- return nil, nil
- end
- local function toLevel(experience)
- if experience <= 272 then
- return experience / 17
- elseif experience > 272 and experience <= 825 then
- return 9.84 + 0.17 * math.sqrt(24 * experience - 5159)
- else
- return 21.6 + 0.075 * math.sqrt(56 * experience - 32511)
- end
- end
- local function clearSlot(slotNumber) --Attemps to clear the slot slotNumber
- if turtle.getItemCount(slotNumber) ~= 0 then
- turtle.select(slotNumber)
- if not turtle.drop() then
- print("ERROR: failure to clear slot number " .. slotNumber)
- end
- end
- end
- local function clearSlots() --Places all the items into the chest under the turtle
- for i = 1,16 do
- if turtle.getItemCount(i) ~= 0 then
- clearSlot(i)
- end
- end
- end
- local function getItem() --Attempts to take something from the chest above the turtle
- clearSlots()
- turtle.select(slot)
- if not turtle.suck() then
- print("ERROR: failure to obtain item to enchant")
- end
- if turtle.getItemCount(slot) ~= 1 then
- if not turtle.drop(turtle.getItemCount(slot) - 1) then
- print("ERROR: failure to reduce slot number " .. slot .. " to one item")
- end
- end
- end
- local function enchant() --Attemps to enchant the item
- turtle.select(slot)
- if xp.enchant(enchantLevel) then
- enchants = enchants + 1
- print("Enchants Performed: " .. enchants)
- end
- clearSlots()
- end
- --Wrap peripherals
- local xp = wrapPeripheral("xp")
- if (not xp) then
- print("ERROR: xp peripheral is necessary")
- os.shutdown()
- end
- xp.setAutoCollect(true) --Sets the turtle to automatically collect xp
- --User Inputs
- print("The turtle will attack mobs above it.")
- print("It will look for items to enchant in the inventory infront of it.")
- print("It will place enchanted items in an inventory below it.")
- io.write("Level to Enchant: ")
- if enchantLevel <= 0 then
- while enchantLevel <= 0 do
- enchantLevel = tonumber(read())
- end
- else
- print(enchantLevel)
- end
- --Main Script
- while true do
- while not turtle.attackUp() do
- xp.collect()
- end
- if xp.getLevels() >= enchantLevel then
- getItem()
- enchant()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement