Search code examples
javapluginsserverminecraftbukkit

How to add an enchantment that 1 shots baby mobs?


I have tried many events such as onAttack,EntityDeathEvent,EntityDamageByEntityEvent. None of these seems to work. It is probably because i wrote the code wrong, here's what i want. I added a command which gives you a diamond sword with the enchantment I tried to add which was going to 1 shot baby mobs already applied. Code:

@EventHandler
public void onAttack(EntityDamageByEntityEvent event) {
    Player player = (Player) event.getDamager();
    Ageable ageableEntity = (Ageable) event.getEntity();
    if (event.getEntity() instanceof Ageable) {
        if (ageableEntity.isAdult() == false) {
            if(player.getInventory().getItemInMainHand().containsEnchantment(OrphanEnchants.ORPHAN_OBLITERATOR)) {
                ageableEntity.setLastDamage(1000);
                ageableEntity.setHealth(0);
            }
        }
    }
    
}

Tell me if i; used to event wrong, should choose another event or registered the enchantment wrong.(i know i didn't give the code that was registering)


Solution

  • Your code seems solid and at first sight I don't see what could possibly be wrong with it (Unless you just didn't register the event). Though there is a flaw. You should probably check if event.getDamager() is an instance of Player before casting it. I also recommend using ageableEntity.damage(damage, player) so that the game processes it as if the player actually killed it, and it doesn't just suddenly die.

    I've used your own code and made some changes to it, and added comments which should explain what everything is there for. This is just an alternative assuming the code you have really didn't work.

        @EventHandler
        public void onAttack(EntityDamageByEntityEvent event) {
            // check if damager is a Player and the entity is Ageable
            // otherwise there's no point of continuing
            if(event.getDamager() instanceof Player && event.getEntity() instanceof Ageable) {
                Player player = (Player) event.getDamager();
                Ageable ageableEntity = (Ageable) event.getEntity();
                // check if they're not an adult and the item has the enchantment required
                // otherwise there's no point of continuing
                if (!ageableEntity.isAdult() && player.getInventory().getItemInMainHand().containsEnchantment(OrphanEnchants.ORPHAN_OBLITERATOR)) {
                    // cancel the event to handle it yourself
                    event.setCancelled(true);
                    
                    // remove all potion effects to prevent absorbtion/regeneration/resistance
                    // or other potions from keeping the entity alive
                    ageableEntity.getActivePotionEffects().stream().map(PotionEffect::getType).forEach(ageableEntity::removePotionEffect);
                    
                    // set their health very low, but not zero, because then the
                    // player doesn't get credit for killing it
                    ageableEntity.setHealth(0.5D);
                    
                    // make the player damage the entity, set damage to MAX_VALUE
                    // so the entity MUST die
                    ageableEntity.damage(Double.MAX_VALUE, player);
                }
            }
        }