Creating Enchantments
The first thing you need to do in order to create a new enchantment is create a new class that extends CustomEnchantment
.
public class LifestealEnchantment extends CustomEnchantment
The constructor takes in two arguments
- String: The name of the enchantment (must be unique to other custom enchantments else it won’t register)
- String: The description for your enchantment in the detailed list and the book
Extra Settings
CustomEnchantment
provides a number of setters to configure your enchantment. You can call any of the following in your constructor:
Enchantment Effects
Next, you can create whatever effects for the enchantment you want by overriding one or more of a few methods.
// Applies when hitting something
applyOnHit(LivingEntity, LivingEntity, int, EntityDamageByEntityEvent)
// Applies when being hit
applyDefense(LivingEntity, LivingEntity, int, EntityDamageEvent)
// Applies when breaking a block
applyBreak(Player, Block, int BlockEvent)
// Applies when left/right clicking or stepping on pressure plates
applyInteractBlock(Player, int, PlayerInteractEvent)
// Applies when an item with the enchantment is equipped or held
applyEquip(Player, int)
// Applies when an item with the enchantment is unequipped or no longer held
applyUnequip(Player, int)
// Applies when clicking on an entity
applyInteractEntity(Player, int, PlayerInteractEntityEvent)
// Applies when launching a projectile
Each of these methods is optional, but you can use as many as you want (e.g. a chestplate that conjures lightning when you attack, reflects damage when you are attacked, and causes diamonds to drop from every block you break with only one enchantment is possible). In order to use one of these, simply override it and throw in whatever effect you want like this:
// An on-hit enchantment example
@Override
public void applyOnHit(LivingEntity user, LivingEntity target, int enchantLevel, EntityDamageByEntityEvent, event) {
// Gain health depending on enchantment level
user.setHealth(Math.max(user.getMaxHealth(), user.getHealth() + enchantLevel));
}