Skip to content

Instantly share code, notes, and snippets.

@NelsonMinar
Created April 23, 2013 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NelsonMinar/5440251 to your computer and use it in GitHub Desktop.
Save NelsonMinar/5440251 to your computer and use it in GitHub Desktop.
Minecraft ComputerCraft turtle function that enchants books
-- Minecraft turtle function to enchant books. Requires MiscPeripherals
-- Place books in lower right slot; this will enchant them and drop them
-- in a chest below
-- Plug this routine into a melee XP bot's script to automatically generate books
-- Get a reference to the XP Peripheral API
local xp = peripheral.wrap("right")
-- enchantBook takes what level you want, returns true if successful
function enchantBook(level)
-- Check we can do the enchantment
if xp.getLevels() < level then
return false
end
-- Clear slot 15 just in case
turtle.select(15)
turtle.dropDown()
-- Move a single book from 16 to 15. Must do this; can't enchant a stack of books
turtle.select(16)
if not turtle.transferTo(15, 1) then
return false
end
-- Enchant the book and put it in the chest
turtle.select(15)
if not xp.enchant(level) then
return false
end
turtle.dropDown()
return true
end
-- Demo program
print("Level " .. xp.getLevels())
args = {...}
if #args < 1 then
print("Usage: enchant <level>")
return
end
local desiredLevel = tonumber(args[1])
if enchantBook(desiredLevel) then
print "Success!"
else
print "Failed to enchant book"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment