Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Enchanting Turtle V0.3 by QQII
- --Declerations
- local Arguments = { ... } --Program arguments
- local enchantLevel = tonumber(Arguments[1]) or 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
- local level = 0 --Requested by Perry
- --Wrap peripherals
- 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 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
- --Functions
- local function clearSlot(slotNumber) --Attemps to clear the slot slotNumber
- if turtle.getItemCount(slotNumber) ~= 0 then
- turtle.select(slotNumber)
- if turtle.dropDown() then
- return true
- else
- print("ERROR: failure to clear slot number " .. slotNumber)
- return false
- end
- else
- return true
- 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()
- if clearSlot(slot) then
- turtle.select(slot)
- if turtle.suck() then
- if turtle.getItemCount(slot) ~= 1 and turtle.getItemCount(slot) ~= 0 then
- if turtle.drop(turtle.getItemCount(slot) - 1) then
- return true
- else
- print("ERROR: failure to reduce slot number " .. slot .. " to one item")
- return false
- end
- end
- else
- print("ERROR: failure to obtain item to enchant")
- return false
- 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
- --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 xp.getLevels() < enchantLevel do
- turtle.attack()
- if level ~= xp.getLevels() then
- print("Level: " .. xp.getLevels())
- end
- level = xp.getLevels()
- os.sleep(0.1)
- end
- if xp.getLevels() >= enchantLevel then
- print("Enchanting...")
- getItem()
- enchant()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement