How do I get the mob name that killed a player?

Discussion in 'Bukkit Help' started by parat26, Jun 30, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    parat26

    Hello.

    I am kinda new to plugin development, and I am currently developing my first "professional" plugin called Utils.

    I am trying to make a Listener class that will replace almost every possible death message, from a mob, to a natural cause.

    When I build my project and letting a zombie kill me I get a NullPointerException Line 46. Please bare with me, I am sure there is mistake that I am missing!

    Code:
    package me.parat26.listeners;
     
    import me.parat26.utils.SettingsManager;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    public class DeathCauses implements Listener
    {
        SettingsManager config = SettingsManager.getInstance();
     
        @SuppressWarnings("deprecation")
        @EventHandler
        /**
        * To be rebuild soon.
        *
        * @param e
        */
        public void checkDeathCauses(EntityDeathEvent e)
        {
            if (e.getEntity() instanceof Player)
            {
                Player p = (Player) e.getEntity();
                String victim = p.getDisplayName();
     
                EntityDamageEvent damage = p.getLastDamageCause();
                DamageCause damageCause = damage.getCause();
             
                if (damageCause != null && damage != null)
                {
                    // Here, how do I get the correct name?!
                    String mob = damage.getEntityType().getName();
     
                    if (damageCause == DamageCause.DROWNING)
                    {
                        String death = config.parse("death.drowned").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.ENTITY_ATTACK)
                    {
                        String death = config.parse("death.mob").replace("<player>", victim).replace("<mob>", mob);
                        ((PlayerDeathEvent) e).setDeathMessage(death); // Line 46
                    }
                    else if (damageCause == DamageCause.ENTITY_EXPLOSION)
                    {
                        String death = config.parse("death.mob-explosion").replace("<player>", victim).replace("<mob>", mob);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.FALL)
                    {
                        String death = config.parse("death.fall").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.FALLING_BLOCK)
                    {
                        String death = config.parse("death.falling-block").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.FIRE)
                    {
                        String death = config.parse("death.fire").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.LAVA)
                    {
                        String death = config.parse("death.lava").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.LIGHTNING)
                    {
                        String death = config.parse("death.lightning").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.MAGIC)
                    {
                        String death = config.parse("death.magic").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.POISON)
                    {
                        String death = config.parse("death.poison").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.PROJECTILE)
                    {
                        String death = config.parse("death.projectile").replace("<player>", victim).replace("<mob>", mob);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.STARVATION)
                    {
                        String death = config.parse("death.starved").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.SUFFOCATION)
                    {
                        String death = config.parse("death.suffocated").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.SUICIDE)
                    {
                        String death = config.parse("death.suicide").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.VOID)
                    {
                        String death = config.parse("death.void").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                    else if (damageCause == DamageCause.WITHER)
                    {
                        String death = config.parse("death.wither").replace("<player>", victim);
                        ((PlayerDeathEvent) e).setDeathMessage(death);
                    }
                }
            }
        }
    }
     
  2. Offline

    DerpyMan

    parat26 Wrong Forum! Go to the forum " Plugin Development"
     
  3. Offline

    MaliciousMan

Thread Status:
Not open for further replies.

Share This Page