Minecraft 1.12 modding with forge – 14 – Custom Enchantments

Hello everyone! In this part I’ll show you how to add a custom enchantment as requested by mortem5282.


I’ll create 2 enchantments, thunder which will create a lightning strike when you hit an entity and grassness (I know very original) which will make grass grow when you walk on dirt.

To start we need to create a new class in the init package, I’ll call it ModEnchantments. In the ModEnchantments class add 2 variables, called THUNDER and GRASSNESS, they are both final and static and of type Enchantment.

An enchantment needs 3 things when it is created:

  • The rarity: how rare the enchantment is.
  • The enchantment type: what type the enchantment is, ex: weapons, armor, feet etc. Used to determine if the item can be enchanted.
  • On which slots it works

For the thunder enchantment we are gonna make a custom enchantment type because I want it to work using the bow and the sword but no other tools.
So in the ModEnchantments create another final static variable but this time of type EnumEnchantmentType, I’ll call it WEAPONS because it will be used for weapons. We can create a new type by calling EnumHelper.addEnchantmentType. This method expects a name and in the second argument we can say which items we want this type to work on. We can do that by using a lambda like this:

 public static final EnumEnchantmentType WEAPONS = EnumHelper.addEnchantmentType("weapons", (item)->(item instanceof ItemSword || item instanceof ItemBow)); 

Now we need to create 2 classes in a new package. I’ll call the package enchantment and the classes EnchantmentGrassness and EnchantmentThunder. They will both extend Enchantment. Now we need to add a constructor to both, the constructor won’t take any arguments, we will hardcode the rarity, type, equipmentslots and name in the constructor because we probably have to make an unique class for every enchantment.
In this constructor call the super constructor, here you define the rarity, enchantment type and equipment slots. For example this line will make it uncommon, armor_feet enchantable, and work on the feet.

 super(Rarity.UNCOMMON, EnumEnchantmentType.ARMOR_FEET, new EntityEquipmentSlot[] {EntityEquipmentSlot.FEET}); 

Now we also need to set the name, so call this.setRegistryName and this.setName and pass a name.

If you want the enchantment to have different levels you have to override getMaxLevel (and maybe getMinLevel) and return the values you want. There are a lot of other methods like these and you should probably check out the Enchantment class. I’ll make the grassness enchantment have a max level of 5.

Now we have the basics setup, the enchantments won’t do anything yet but first we will register them.
Go to the ModEnchantments class and now you can instantiate the THUNDER and GRASSNESS variables with the classes we just created.
We need to register to an event so we need to add @Mod.EventBusSubscriber(modid=Reference.MODID) above the class. Now we can create a registerEnchantments method, this method has 1 argument, Register<Enchantment>, and it needs the @SubscribeEvent annotation. In this method we call event.getRegistry().registerAll(….) with the enchantment variables we made.

Now the enchantments are registered and can be put on the corresponding items!

There are multiple ways to add custom behaviour, I’ll show 2 ways but that doesn’t mean those are the only 2!
The thunder one is the simplest, in the EnchantmentThunder class we will override onEntityDamaged this will get run if you hit an entity when you have the enchantment.
In this method we will just spawn a lightning bolt at the location of the target like this:

user.world.addWeatherEffect(new EntityLightningBolt(user.world, target.posX, target.posY, target.posZ, false)); 

The grassness enchantment is more difficult, we have to subscribe to the LivingUpdateEvent this event is fired for every entity everytime it updates, because of this we need to add @Mod.EventBusSubscriber(modid=Reference.MODID) above the class. In this method we will check whether the entity has the enchantment and is standing and then replace the dirt around it with grass. The radius of the replacement will be dependent on the level of the enchantment.

The way we check for the enchantment is done with the EnchantmentHelper. We use the getMaxEnchantmentLevel method and if the level is more than 0 we have the enchantment.

Now we just need to add the names to the lang file like this:

 enchantment.NAME=NAME 

And that is it! I hope it helped! Don’t forget to ask if you need help or leave a comment if you see an error.
Till next time!

~suppergerrie2

Posted in Forge tutorial.

4 Comments

  1. I’m very confused. Could you maybe put the finished code of all files? You’re not expanding on many things you’re saying

  2. You should do a tutorial on making chests, or custom armor. Either one of the two would be AMAZING!

Leave a Reply

Your email address will not be published. Required fields are marked *