Example Enchantments

Examples
Life Steal
// All enchantments extend CustomEnchantment
public class LifestealEnchantment extends CustomEnchantment {

    // List of items to enchant onto through an enchanting table
    static final String[] LIFESTEAL_ITEMS = new Material[] {
        Material.WOOD_HOE, Material.STONE_HOE, Material.IRON_HOE, Material.GOLD_HOE, Material.DIAMOND_HOE
    };

    // Fields for a cooldown effect so players don't heal constantly
    static final int COOLDOWN = 5000;
    long timer;

    public LifestealEnchantment() {
        // Pass in the enchantment name and the designated items
        super("Lifesteal", LIFESTEAL_ITEMS);

        // Gives your enchantment a description for the detailed list and detail book
        description = "Steals health on hit";

        // Maximum level of your enchantment
        setMaxLevel(5);

        // Modifies how early you can get your enchantment
        setBase(10);

        // Modifies how easily higher levels are obtained relative to the base value
        setInterval(8);
    }

    // Applies the enchantment effect on hit
    @Override
    public void applyEffect(LivingEntity user, LivingEntity target, int enchantLevel, EntityDamageByEntityEvent event) {
        if (System.currentTimeMillis() - timer < COOLDOWN) return;
        int health = user.getHealth() + enchantLevel;
        if (health > user.getMaxHealth()) health = user.getMaxHealth();
        user.setHealth(health);
        timer = System.currentTimeMillis();
    }
}
Lightning
public class LightningEnchantment extends CustomEnchantment {

    static final String[] LIGHTNING_ITEMS = new Material[] {
        Material.WOOD_AXE, Material.STONE_AXE, Material.IRON_AXE, Material.GOLD_AXE, Material.DIAMOND_AXE
    };

    public LightningEnchantment() {
        // The '2' is an enchantment weight (2 is fairly rare, larger numbers are more common)
        super("Lightning", LIGHTNING_ITEMS, 2);
        description = "Has a change to strike lightning on hit";
        setMaxLevel(5);
        setInterval(8);
    }

    @Override
    public void applyEffect(LivingEntity user, LivingEntity target, int level, EntityDamageByEntityEvent event) {
        // Give a chance to prock the effect instead of all the time
        if (Math.random() < 0.04 * level) target.getWorld().strikeLightning(target.getLocation());
    }
}
Damage Reflection
public class ReflectionEnchantment extends CustomEnchantment {

    static final String[] REFLECT_ITEMS = new Material[] {
        Material.IRON_CHESTPLATE, Material.DIAMOND_CHESTPLATE
    }
    
    public ReflectionEnchantment() {
        super("Reflection", REFLECT_ITEMS);
        description = "Reflects damage back at attackers";
        setMaxLevel(10);
        setBase(8);
        setInterval(5);
    }

    // Apply the enchantment effect when taking damage
    @Override
    public void applyDefenseEffect(LivingEntity user, LivingEntity target, int enchantLevel, EntityDamageEvent event) {
        if (target != null) target.damage((int)(event.getDamage() * 0.1 * enchantLevel), user);
    }
}

Comments

Posts Quoted:
Reply
Clear All Quotes