player.getLastDamageCause().getEntity(); returning the player!

Discussion in 'Plugin Development' started by Tice, Jun 24, 2014.

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

    Tice

    Hello! I've ran into some trouble whilst trying to log the player that previously hit a player typing a command.

    Here is what happens:
    1. Player1 hits player2.
    2. Player2 types /hub.
    3: This code is ran: Bukkit.getServer().broadcastMessage(Player1.getName());
    4. This message is broadcasted: Player2.

    May be badly explained, but I hope you get the general picture. It's not returning what it's supposed to?
    Please help me with this. Thanks in advance!

    P.S: Player1 is a Player variable(Player Player1 = (Player) player.getLastDamaggeCause().getEntity();

    The variable "player", is the sender of the command.
     
  2. Offline

    1Rogue

    so then Player1 is Player2, according to that logic:

    Player2 types /hub -> Player1 is assigned from the executor of command which is Player2 -> Player2's name is broadcast/
     
  3. Offline

    Traks

    1. There's no need to use Bukkit.getServer().broadcastMessage(String), you can just use Bukkit.broadcastMessage(String), see here.
    2. I strongly suggest including source code next time, instead of trying to describe your problem with variable names...

    Now onto your problem, getEntity() returns the subject of the DamageEvent, not the damager. To get the damager, you need to check:
    Code:java
    1. Player.getLastDamageCause() instanceof EntityDamageByEntityEvent

    and then run the following code to get the damager
    Code:java
    1. Player damager = ((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager();

    I haven't tested this, but looking at CraftBukkit's source code, it should work. By the way, if someone knows a 'nicer' way of doing this, please show it to me!
     
  4. Offline

    Tice

    I will. Thank you for your generous help.

    EDIT: There seem to have been an issue on reload. It works great, thanks you for help! I removed all the information in this post since it is unnecessary.
     
Thread Status:
Not open for further replies.

Share This Page