Detecting suicide with PlayerDeathEvent

Discussion in 'Plugin Development' started by sniddunc, Dec 21, 2015.

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

    sniddunc

    Hello,

    I am wondering how I would detect whether or not the player killed them self or they were killed by another player. Currently, when a player kills themself, they still get XP. I tried comparing the names of each player and if they're the same it sends a message, and if it isn't it proceeds with the normal action.

    This is my code:
    PHP:
        @EventHandler
        
    public void playerKillPlayer(PlayerDeathEvent event) {
           
            if (
    event.getEntity().getKiller() instanceof Player) {
               
                
    Player victim = (Playerevent.getEntity();
                
    Player killer = (Playervictim.getKiller();

                
    String current PlayerData.getData().getString("players." killer.getUniqueId() + "." killer.getName() + ".rep");
               
                
    int repset Integer.parseInt(current);
               
                if (
    killer.getName() == victim.getName()) {
                    
    victim.sendMessage(ChatColor.GREEN "What are you even trying to do?");
                }
                else {
                    
    PlayerData.getData().set("players." killer.getUniqueId() + "." killer.getName() + ".rep", (repset 10));
                    
    killer.sendMessage(ChatColor.GREEN "You received " ChatColor.RED "10 REP" ChatColor.GREEN " for killing " ChatColor.DARK_GREEN victim.getName());
                    
    PlayerData.saveData();
                    
    current null;
                    
    repset 0;
                }
               
            }
           
        }
    It keeps giving the points regardless of whether or not it's from another player or themselves.
    Could someone explain to me why this isn't working or give me an alternate method?

    Thank you.
     
    Luseres likes this.
  2. Offline

    567legodude

    @sniddunc Compare strings with .equals()
     
  3. Offline

    sniddunc

    That's the second thing I tried. It still does not work.
     
  4. Offline

    Zombie_Striker

    @sniddunc
    You should make sure .getKiller is not null (in case the player dies of a cactus or fall damage. We don't want NPEs do we?)

    Did you debug? What line does it get to before it stops?
     
  5. Offline

    sniddunc

    Everything works fine other than the fact that it won't detect suicide. I have debugged it.
    I don't need to check getKiller because I'm checking if the instance of killer is player.
    Is there a different event I should be using?
     
  6. You say suicide... Do you mean with a /kill command or by jumping in lava/drowning/fall damage/cactus?
     
  7. Offline

    sniddunc

    Sorry, I should have been more specific.
    What I mean exactly is shooting themselves to death.
     
  8. @sniddunc Okay. https://jd.bukkit.org/org/bukkit/event/entity/PlayerDeathEvent.html
    getEntity on this event returns a Player. No need to cast.
    DO NOT CHECK STRING EQUALITY WITH ==. As with all objects, use .equals
    You should be able to do
    if (victim.equals(victim.getKiller()) {} try that?
    And, reason you shouldn't use == is because it checks reference equality, not if the strings are the same. Mostly it works, some cases it doesn't. It is "wrong" to use it as an equality check on anything other than primitives, unless you want to check references.
    LivingEntity#getKiller also returns a Player. No reason to cast that either.
    Or you could check the death message.

    Written from my phone, excuse errors, if any.
     
  9. Offline

    sniddunc

    This is the second thing I tried, and it still does not work.
    This is the segment of the code that won't work.

    Code:
                if (victim.getName().equals(killer.getName())) {
                    victim.sendMessage(ChatColor.GREEN + "What are you even trying to do?");
                }
                else {
                    PlayerData.getData().set("players." + killer.getUniqueId() + "." + killer.getName() + ".rep", (repset + 10));
                    killer.sendMessage(ChatColor.GREEN + "You received " + ChatColor.RED + "10 REP" + ChatColor.GREEN + " for killing " + ChatColor.DARK_GREEN + victim.getName());
                    PlayerData.saveData();
                    current = null;
                    repset = 0;
                }
    I have no idea why this isn't working, no errors or anything. No listeners are conflicting with it, I tried making a completely empty plugin other than the necessities and that listener and it still does not work.
     
  10. What part of it doesn't work? You probably shouldn't use the players name in your data file, the UUID should be enough. I'm guessing that victim and killer isn't the same? Did you try just comparing the player objects?
     
Thread Status:
Not open for further replies.

Share This Page