[SOLVED] Getting player killer

Hello guys, i searched how get the killer of a player with PlayerDeathEvent, i didn’t find any topic talking about this.
So I wonder if it is possible at the moment to get the killer of a player or an entity.
I searched with “event.getCause()” but I didn’t find anything about this :confused:

Thanks!

@MrAzErTy31

Have you tried seeing what event.getCause() returns when a player is killed by another player?

I’m on my local server btw xD So nope, i didn’t tried to see what event.getCause() returns when a player is killed by another player :confused:

@MrAzErTy31

I have a feeling event.getCause() wont show the killer. Maybe you could use the death message somehow to find the killer?

1 Like

Hmm very interesting idea ! Spliting killer name from deathMessage and get it with getServer().getPlayer(killerName) Thanks you !

Or, you could use Cause#getCause(), like was originally suggested.

@Subscribe public void onPlayerDeath(PlayerDeathEvent event) {
    Cause cause = event.getCause();
    if (cause instanceof Player) {
        // Killer was player.
    }
}

Excuse the pseudo-code.

4 Likes

Oh ! I didn’t know Cause Object could be instance of Player Object. I’m sorry ^^ Thanks a lot guys ! :smiley:

Problem solved in my opinion :smile:

1 Like

Don’t forget to change the topic title to solved :thumbsup:

Reading the javadocs is the number one way to get the answers you need.

https://github.com/SpongePowered/SpongeAPI/blob/master/src/main/java/org/spongepowered/api/event/cause/Cause.java#L81

2 Likes

I’m so stupid x) I just readed @return The cause I should sleep ^^ Anyway Thanks a lot @FerusGrim and @Flibio ! :smile: Problem Solved !

2 Likes

Er, it’s probably closer to

@Subscribe public void onPlayerDeath(PlayerDeathEvent event) {
    Cause cause = event.getCause();
    if(cause.getCause() instanceof Player){
        //Killer was player
    }
}

event.getCause gets you the Cause container. Cause.getCause() gets you the object that caused this event.

1 Like

Keep in mind that you also have to do factoring to see if the Cause was an arrow and, if it was, who it was shot by, etc.

This is starting to get unwieldly, Getting the player who killed another player in a death event would be a decent helper method to have.

Having getCause(Player.class) automatically scan the cause hierarchy for a responsible player would be extremely useful.

Would be the owner of players, detenator of tnt if available, placer of lava if available, and just all around be useful to have in the API.

OvercastNetworks Tracker previously did a lot of work in order to track the cause of player deaths it would be useful if similar trackers were in Sponge.

2 Likes

Please be aware that causes are going to change
https://github.com/SpongePowered/SpongeAPI/pull/712