Skip to content

Instantly share code, notes, and snippets.

@kane-thornwyrd
Created May 14, 2013 21:38
Show Gist options
  • Save kane-thornwyrd/5579804 to your computer and use it in GitHub Desktop.
Save kane-thornwyrd/5579804 to your computer and use it in GitHub Desktop.
-- number of seconds before rebooting the machine, use -1 to disable
local timeToReboot = 300
--time in seconds between updates
local sleepTime = 1
-- what level to enchant items at
local enchantAtLevel = 30
t = peripheral.wrap("right")
t.setAutoCollect(true)
-- enchant the book in slot one and drop it into the chest underneath the turtle
function enchantItem(nLvl)
if t.enchant(nLvl) then
turtle.dropDown()
else
term.setCursorPos(3,2)
io.write("Unable to enchant item")
end
end
function findBook()
if turtle.getItemCount(16)>=1 then
turtle.select(16)
turtle.transferTo(1,1)
turtle.select(1)
return true
else
return false
end
end
-- Look for items to enchant in slots 2 through 15
-- if there are none it looks for books in slot 16
function getItems()
if turtle.getItemCount(1) > 0 then
turtle.select(1)
return true
end
for i=2,15 do
if turtle.getItemCount(i)>0 then
turtle.select(i)
turtle.transferTo(1,1)
turtle.select(1)
return true
end
end
if findBook() then
return true
else
return false
end
end
local seconds = 0
function SecondsToClock(sSeconds)
local nSeconds = tonumber(sSeconds)
if nSeconds == 0 then
return "00:00:00";
else
nHours = string.format("%02d", math.floor(nSeconds/3600));
nMins = string.format("%02d", math.floor(nSeconds/60 - (nHours*60)));
nSecs = string.format("%02d", math.floor(nSeconds - nHours*3600 - nMins *60));
return nHours..":"..nMins..":"..nSecs
end
end
local w,h = term.getSize()
function printStatus()
term.clear()
term.setCursorPos(1,1)
term.write(string.format("Current Level: %s/%s", t.getLevels(), enchantAtLevel))
local clockTime = "Time: "..SecondsToClock(seconds)
term.setCursorPos(w - #clockTime+1, 1)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.write(clockTime)
if timeToReboot > 0 then
local rebootDisplay = "Reboot in: "..string.format("%d",(timeToReboot - seconds))
term.setCursorPos(w - #rebootDisplay+1, 2)
term.write(rebootDisplay)
end
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
if not getItems() then
term.setCursorPos(2,2)
term.write("Nothing to enchant")
else
term.setCursorPos(2,2)
term.write("Waiting to enchant")
end
end
local isRunning = true
local reboot = false
while isRunning do
printStatus()
if t.getLevels() >= enchantAtLevel then
if getItems() then
enchantItem(enchantAtLevel)
end
else
if timeToReboot > 0 and seconds >= timeToReboot then
term.clear()
term.setCursorPos(1,1)
term.write("Rebooting...")
reboot = true
isRunning = false
sleep(2)
else
sleep(sleepTime)
seconds = seconds + sleepTime
end
end
end
if reboot then os.reboot() end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment