Fabled Enchants: Coding Enchantments

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:

  • Name: Setter
    • Description:
  • Natural Items: addNaturalItems(Material…)
    • Description: What items the enchantment can be obtained on
  • Weight: setWeight(double), setWeight(Material, double)
    • Description: How common the enchantment is. You can specify weights for specific materials as well.
  • Group: setGroup(String)
    • Description: Specifies a conflict group. Enchantments in the same group can’t be obtained on the same item
  • Stackable: setCanStack(Boolean)
    • Description: Whether or not multiple items having the enchantment stack their effects
  • Table Enabled: setTableEnabled(Boolean)
    • Description: Whether or not the enchantment is obtainable via enchantment tables
  • Max Level: setMaxLevel(int), setMaxLevel(int, int)
    • Description: The maximum obtainable level of the enchantment from either anvils or enchantment tables
  • Combine Cost: setCombineCostPerLevel(int)
    • Description: How many levels it costs to merge this enchantment onto another item
  • Min Enchanting Level: setMinEnchantingLevel(double)
    • Description: The minimum modified enchantment level needed to receive this enchantment
  • Enchant Level Scale Factor: setEnchantLevelScaleFactor(double)
    • Description: Modified enchantment levels between each additional enchantment rank
  • Enchantment Level Buffer: setEnchantLevelBuffer(double)
    • Description: How many levels after the max scaled amount until the enchantment is no longer obtainable

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));
}