How Enchantments Are Chosen

When you click the button for a level 17 enchantment, how does
Minecraft decide to apply Sharpness II? Here's an explanation. But
first, some terminology:

"Enchantment level" is the cost of the enchantment in experience
levels (the green number on the button).

"Enchantment power" is the strength of the particular enchantment.
For example, "Sharpness IV" has power 4.

The enchantment algorithm uses a three-step process.

Step One

Apply modifiers to the enchantment level

The first thing that Minecraft does is apply two modifiers to the base
enchantment level (the green number, and the number of experience
levels you actually pay). Each modifier is restricted to a certain
range, with numbers close to the middle of the range more common than
those near the ends.

The first modifier is based on the item's "enchantability," which
depends on the material and the type of the item (see the table below).
Minecraft picks a number between 0 and half of the enchantability (with enchantability / 4 the most probable
result), then adds that number plus one to the enchantment level.

Armor

Swordor Tool

Wood

15

Leather

15

Stone

5

Iron

9

14

Chain

12

Diamond

10

10

Gold

25

22

Bows and books have enchantability 1.

Next, Minecraft picks a value between 0.85 and 1.15, with values closer
to 1.0 being more likely. The enchantment level is multiplied by this
value (so it increases or decreases by up to 15%) and then rounded to
the nearest integer.

Step Two

Find possible enchantments

Now, based on the modified level, Minecraft makes a list of all
enchantment types that can be applied to the target item along with the
power that each enchantment will have.

The power of each enchantment type is determined by the level and the
values in the table below. For each power value of an enchantment
type, there is a minimum and maximum level that can produce the
enchantment at that power. If the modified enchantment level is within
the range, then the enchantment will be assigned that power. If the
level is within two overlapping ranges for the same enchantment type,
the higher power value is used.

I

II

III

IV

V

Protection

1

21

12

32

23

43

34

54

--

Fire Protection

10

22

18

30

26

38

34

46

--

Feather Fall

5

15

11

21

17

27

23

33

--

Blast Protection

5

17

13

25

21

33

29

41

--

Projectile Protection

3

18

9

24

15

30

21

36

--

Respiration

10

40

20

50

30

60

--

--

Aqua Affinity

1

41

--

--

--

--

Thorns

10

60

30

80

50

100

--

--

Sharpness

1

21

12

32

23

43

34

54

45

65

Smite

5

25

13

33

21

41

29

49

37

57

Bane of Arthropods

5

25

13

33

21

41

29

49

37

57

Knockback

5

55

25

75

--

--

--

Fire Aspect

10

60

30

80

--

--

--

Looting

15

65

24

74

33

83

--

--

Efficiency

1

51

11

61

21

71

31

81

41

91

Silk Touch

15

65

--

--

--

--

Unbreaking

5

55

13

63

21

71

--

--

Fortune

15

65

24

74

33

83

--

--

Power

1

16

11

26

21

36

31

46

41

56

Punch

12

37

32

57

--

--

--

Flame

20

50

--

--

--

--

Infinity

20

50

--

--

--

--

An example: Suppose we are enchanting a sword with a modified enchantment level of
21. If the sword gets the Sharpness enchantment, it will get Sharpness
II because 21 is in the power-II range for Sharpness (12-32). 21 is
also in the power-I range (1-21), but the higher power value will win
over the lower. If the sword gets Smite, then it will be Smite III,
because 21 is in the power-III range for Smite (21-41).

Note that extremely high modified enchantment levels can fall outside
the ranges of all valid enchantments for the item. If this happens,
the item is left unenchanted, but you are not charged any experience
levels and can try enchanting it again.

Step Three

Select a set of enchantments from the list

Now that it's got a list of the possible enchantments for the item,
Minecraft has to pick some of them that will actually be applied.

You always get at least one enchantment. The first enchantment is
picked from the list based on the weights below.

Weight

Protection

10

Fire Protection

5

Feather Fall

5

Blast Protection

2

Projectile Protection

5

Respiration

2

Aqua Affinity

2

Thorns

1

Sharpness

10

Smite

5

Bane of Arthropods

5

Knockback

5

Fire Aspect

2

Looting

2

Efficiency

10

Silk Touch

1

Unbreaking

5

Fortune

2

Power

10

Punch

2

Flame

2

Infinity

1

For example, suppose the item is a piece of armor with both Protection
and Respiration available. Protection has 10 chances to be selected
compared to 2 for Respiration, so the probability of getting Protection
is about 5 times higher than the probability of Respiration.

After the first element is selected, there is a chance of receiving
more, based on this algorithm:

Remove from the list of possible enchantments anything that
conflicts with previously-chosen enchantments. For example, if
Smite was already picked, Sharpness and Bane of Arthropods will be
removed at this step. See below for more info on conflicts.

Pick one enchantment from the remaining possible enchantments
(based on the weights, as before) and apply it to the item.

Divide the modified level in half (rounded down) and
repeat from step 1.

When enchanting a book, if this process selects more than one
enchantment, the game chooses one enchantment at random to keep and
discards the rest.

Here are the rules for enchantment conflicts:

Every enchantment conflicts with itself. (So you can't get a weapon
with two copies of the Efficiency enchantment.)

All protection enchantments conflict with each other, so an item can
only have one at a time. (In the code, Feather Fall is implemented
as a protection enchantment, but it doesn't conflict with the
others.)

All damage enchantments (Sharpness, Smite, and Bane of Arthropods)
conflict with each other.