View difference between Paste ID: 2gKTZwfn and Q7bNJZV2
SHOW: | | - or go back to the newest paste.
1-
--Enchanting Turtle V0.1 by QQII
1+
--Enchanting Turtle V0.2 by QQII 
2-
--Badly attempt to find an optimum times to attack before enchanting 
2+
3
local enchantLevel = 0				--The level the turtle initially attemps to perform an enchant at 
4
local enchants = 0					--The number of enchants performed by the turtle
5-
local xp = peripheral.wrap("right")
5+
local slot = 1						--The slot number that the items to be enchanted are in
6
	
7
--Functions
8-
local attacks = 165			--The number of attacks it performs before attempting to enchant
8+
function wrapPeripheral(thing)
9-
local enchantLevel = 0			--The level the turtle initially attemps to perform an enchant at 
9+
	local Sides = {
10-
local enchants = 0			--The number of enchants performed by the turtle
10+
					"left","right",
11-
local levels = 0			--The amount of levels collected for the number of attacks performed
11+
					"top","bottom",
12-
local slot = 1				--The slot number that the items to be enchanted are in
12+
					"front","back",
13-
xp.setAutoCollect(true)			--Sets the turtle to automatically collect xp 
13+
					}
14
 
15
	for i,v in pairs(Sides) do
16-
print("Make sure that the mobs fall ontop of the turtle.")
16+
		if (peripheral.isPresent(v) and peripheral.getType(v) == thing) then
17-
print("Make sure that there is an inventory infront containing books or other items for enchating.")
17+
			print("'"..thing.."' found on side '"..v.."'.")
18-
print("Make sure that there is an inventory below the turtle for it to place the items it has encanted.")
18+
			return peripheral.wrap(v), v
19-
print("Enter the level you want the turtle to enchant at:")
19+
20-
	while enchantLevel <= 0
20+
21-
		enchantLevel = tonumber(read())
21+
 
22
	print("ERROR: '"..thing.."' not found!")
23
	return nil, nil
24
end
25-
local function clearSlot(slotNumber)			--Attemps to clear the slot slotNumber
25+
26-
	turtle.select(slotNumber)
26+
local function toLevel(experience)
27
	if experience <= 272 then
28-
		if not turtle.dropDown() then 
28+
		return experience / 17
29
	elseif experience > 272 and experience <= 825 then
30
		return 9.84 + 0.17 * math.sqrt(24 * experience - 5159)
31
	else
32
		return 21.6 + 0.075 * math.sqrt(56 * experience - 32511)
33
	end
34
end
35
36-
		clearSlot(i)
36+
local function clearSlot(slotNumber)	--Attemps to clear the slot slotNumber
37
	if turtle.getItemCount(slotNumber) ~= 0 then
38
		turtle.select(slotNumber)
39
		if not turtle.drop() then 
40-
local function getItem()				--Attemps to take something from the chest above the turtle
40+
41
		end
42
	end
43
end
44
45
local function clearSlots()				--Places all the items into the chest under the turtle
46
	for i = 1,16 do
47
		if turtle.getItemCount(i) ~= 0 then
48
			clearSlot(i)
49
		end
50
	end
51
end
52
53
local function getItem()				--Attempts to take something from the chest above the turtle
54
	clearSlots()
55
	turtle.select(slot)
56
	if not turtle.suck() then
57-
		print("Number of enchants performed by the turtle: " .. enchants)
57+
58
	end
59-
		turtle.drop()				--Replaces the items if it doesn't have enough levels
59+
60
		if not turtle.drop(turtle.getItemCount(slot) - 1) then
61
			print("ERROR: failure to reduce slot number " .. slot .. " to one item")
62
		end
63
	end
64
end
65
66-
	levels = xp.getLevels()
66+
67
	turtle.select(slot)
68-
	for i = 1,attacks do
68+
69-
		while not turtle.attackUp() do end
69+
70
		print("Enchants Performed: " .. enchants)
71
	end
72-
	xp.collect()
72+
73-
	levels = xp.getLevels() - levels
73+
74-
	print("Gained " .. levels .. " for " .. attacks .. " attacks.")
74+
75-
	if levels >= enchantLevel then			--Assumes all mobs that drop xp (for fine tuning)
75+
76-
		attacks = attacks - 1
76+
local xp = wrapPeripheral("xp")
77-
	elseif levels < enchantLevel then		--Assumes a constant flow of 1 hit kill mobs that drop 5 xp each
77+
	if (not xp) then
78-
		attacks = attacks + 5 * (enchantLevel - levels)
78+
		print("ERROR: xp peripheral is necessary")
79
		os.shutdown()
80-
	print("Turtle will now attack " .. attacks .. " times before attempting to enchant")
80+
81
xp.setAutoCollect(true)				--Sets the turtle to automatically collect xp 
82-
	getItem()
82+
83-
	enchant()
83+
84
print("The turtle will attack mobs above it.")
85
print("It will look for items to enchant in the inventory infront of it.")
86
print("It will place enchanted items in an inventory below it.")
87
io.write("Level to Enchant: ")
88
	if enchantLevel <= 0 then
89
		while enchantLevel <= 0 do
90
			enchantLevel = tonumber(read())
91
		end
92
	else
93
		print(enchantLevel)
94
	end
95
	
96
--Main Script
97
while true do 
98
	while not turtle.attackUp() do 
99
		xp.collect()
100
	end
101
	if xp.getLevels() >= enchantLevel then
102
		getItem()
103
		enchant()
104
	end
105
end