Creating an Enchantment

Tutorial
Creating a new Enchantment

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

After you have a class, you need to add a Constructor that passes the enchantment name and the applicable items for the enchantment. The parameters to pass to 'super' are:

  • 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
  • Material[] - the types of items that can receive this enchantment through an enchanting table
  • String - the conflict group of the enchantment (same group means they conflict, no group means doesn't conflict with anything)
  • int - the enchantment weight (how likely it is to get the enchantment, generally between 1 and 10 where 1 is rare, 10 is common)
    Note: Enchantment names must be unique to other custom enchantments
    Note: You can mix and match various combinations of those parameters, but the order stays the same
    // Items able to be enchanted with lifesteal
    static final Material[] LIFESTEAL_ITEMS = new Material[] {
            Material.WOOD_SWORD, Material.STONE_SWORD, Material.IRON_SWORD, Material.GOLD_SWORD, Material.DIAMOND_SWORD };

    // Constructor
    public LifestealEnchantment() {
        super("Lifesteal", LIFESTEAL_ITEMS, 2);
    
        // These are optional things you can change

        // This is the maximum level for your enchantment
        // Default is 1
        this.max = 5;

        // This changes the minimum modified level needed to get your enchant
        // On not normally enchanted items like blaze rods or pumpkin seeds, you should keep it as 1
        // Default is 1
        this.base = 10;

        // This one changes how easily you can get higher tiers of the enchantment
        // If you have a high maximum level, you probably want a small interval
        // Default is 10
        this.interval = 8;
    }

For more details on the base and interval values, see the tutorial on them

Next, you can create whatever effects for the enchantment you want by overriding one or more of a few methods.

  • applyEffect(LivingEntity, LivingEntity, int, EntityDamageByEntityEvent) is for on-hit effects (when you attack something)
  • applyDefenseEffect(LivingEntity, LivingEntity, int, EntityDamageEvent) is for defensive effects (when you take damage)
  • applyToolEffect(Player, Block, int BlockEvent) is for tool effects (when breaking blocks)
  • applyMiscEffect(Player, int, PlayerInteractEvent) is for interact events (when left/right clicking)
  • applyEquipEffect(Player, int) is for equipping effects (putting on armor)
  • applyUnequipEffect(Player, int) is for unequipping effects (taking off armor)
  • applyEntityEffect(Player, int, PlayerInteractEntityEvent) is for right clicking on entities
  • applyProjectileEffect(LivingEntity, int, ProjectileLaunchEvent) is for the initial shot of 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 applyEffect(LivingEntity user, LivingEntity target, int enchantLevel, EntityDamageByEntityEvent, event) {

        // Gain health depending on enchantment level
        int health = user.getHealth() + enchantLevel;
        if (health > user.getMaxHealth()) health = user.getMaxHealth();
        user.setHealth(health);
    }

Each of the types of enchantments give you the event that caused it to be activated, so you can change the outcomes of the event such as damage dealt or if a block is instantly broken.


Comments

  • To post a comment, please or register a new account.
Posts Quoted:
Reply
Clear All Quotes