Solved How to detect attacker and the attacked player

Discussion in 'Plugin Development' started by TheMintyMate, Mar 29, 2014.

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

    TheMintyMate

    Im attempting to develop a plugin which detects which way a player is facing. If a player attacks another players back, I want something to happen. However, im not sure how to determine the attacker and the attached player. Help would be greatly appreciated :)
    Below is the code, with a few annotations in "<>".

    Code Withdrawn.
     
  2. Offline

    Wolfey

    Use an EntityDamageByEntityEvent, the e.getEntity() is the one being attacked, and the e.getDamager() is the one attacking.

    Be sure to check if these two are players first, though.
     
    TheMintyMate likes this.
  3. Offline

    TheMintyMate

    Wolfey
    How do you suggest I check if they are players?
     
  4. Offline

    Wolfey

    TheMintyMate
    Code:java
    1. if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
    2. // do stuff
    3. }
    This will check if both of the entities are players.
     
  5. Offline

    Deleted user


    Code:java
    1.  
    2. @EventHandler
    3. private void onDamageOpponent(EntityDamageByEntityEvent event) {
    4. if (!(event.getDamager() instanceof Player))
    5. return;
    6. if (!(event.getEntity() instanceof Player))
    7. return;
    8.  
    9. Player damagee = (Player) event.getEntity();
    10. Player damager = (Player) event.getDamager();
    11.  


    Show Spoiler

    [​IMG]
    [/spoiler
     
    TheMintyMate likes this.
  6. Offline

    TheMintyMate

    Wolfey
    Eballer48
    Thank-you guys. I earlier implemented your fix for the Event type, and I have now implemented Eballer48's fix. I do not see a reason to implement your fix Wolfey to check if the enitity is a player, as Esballar's fix concerns the player directly (though if you believe I do need to, tell me :) ) though thank-you as well :)
    Here is the end result:

    Code Withdrawn.

    I now only have the issue that "damagee.angle" is described as "Undefined for Player". Any ideas of how to meet this error? Thank-you for all your help,
    Minty :)
     
  7. Offline

    Wolfey

    TheMintyMate We both basically gave you the same code... It was pretty obvious you had to cast the entities after, reason why I didn't put it in...

    Also I think you messed up on your code... lol.
     
  8. Offline

    TheMintyMate

    @wolfrey
    Indeed I have :p Any ideas what went wrong?
     
  9. Offline

    Wolfey

    TheMintyMate The player class does not have an angle() method.
     
    TheMintyMate likes this.
  10. Offline

    TheMintyMate

    Ok. It turned out that I wasn't defining "Vector attackedVector" which is what I needed to define. Thank-you guys for your help.

    This thread is now Closed.
     
Thread Status:
Not open for further replies.

Share This Page