Help With EntityDeathEvent!

Discussion in 'Spigot Plugin Development' started by MrEminent42, May 9, 2015.

Thread Status:
Not open for further replies.
  1. Hello, I'm trying to make a plugin that runs commands when a player dies. The part I'm having trouble with is getting the killer. Here is my code:
    Code (Text):
    @EventHandler
        public void onEntityDeath(EntityDeathEvent e) {
            LivingEntity v = e.getEntity();
            Player k = v.getKiller();
           
            if (e instanceof PlayerDeathEvent) {
                if (e.getEntity().getKiller() != null && e.getEntity().getKiller() instanceof Player) {
                    for (String c : getConfig().getStringList("commands")) {
                        c.replaceAll("%player%", k.getName());
                        c.replaceAll("%tokens%", String.valueOf(3 * getTokens((Player) e.getEntity())));
                        getServer().dispatchCommand(getServer().getConsoleSender(), c);
                    }
                    return;
                }
            }
           
            for (String c : getConfig().getStringList("commands")) {
                c.replaceAll("%player%", k.getName());
                c.replaceAll("%tokens%", String.valueOf(getTokens(k)));
                getServer().dispatchCommand(getServer().getConsoleSender(), c);
            }
           
        }
    When I kill an Entity, I get the message "That player can not be found"
    If pinpointed it to the k variable, because thats the only variable that is a Player.

    How can I get the killer of an entity?
     
    • Why don't you use directly PlayerDeathEvent
    • You assign the Killer Entity to a Player object without knowing that the Killer is indeed a player (instance check and then casting to avoid issues)
     
  2. Killer is always a player.
     
    • Agree Agree x 1
  3. Well I need to run commands on both the EntityDeathEvent and PlayerDeathEvent.

    Both requiring the killer.
     
  4. Ok my bad. Thought it will return just the entity type as you can get killed by mobs aswell and thus the killer isn't a player

    Edit: Either something with the command is wrong then. Or with the command syntax. Print it out before doing the command as maybe it messes up the player name or forgets a space so the command will be like

    /command playernametokens
     
  5. No idea what you said mean
    The command is fine, its just replacing the %player% variable that is the problem, because it cant find the killer.
     
    • Useful Useful x 1
  6. If it cant find the Player you would get a NPE on k.getname and Not that the command tells you that the player cannot be found
     
  7. Oh yeah.

    I figured out the problem. Instead of
    Code (Text):
    c.replaceAll("%player%", k.getName());
    c.replaceAll("%tokens%", String.valueOf(getTokens(k)));
    I have to do
    Code (Text):
    c = c.replaceAll("%player%", k.getName());
    c = c.replaceAll("%tokens%", String.valueOf(getTokens(k)));
     
  8. What if a creeper explodes killing a skeleton or something or if a skeleton kills a creeper?
     
  9. getKiller() would return null with an NPE.
     
    • Useful Useful x 1
  10. @MrEminent42 you should use replace rather than replaceAll, as replaceAll is meant for regular expressions and replace is meant for literal Strings.
     
  11. Okay thanks.
     
Thread Status:
Not open for further replies.