Solved Quick question about ProjectileHitEvent

Discussion in 'Plugin Development' started by Prothean_IGS, Dec 9, 2013.

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

    Prothean_IGS

    Alright so I am trying to do code for when a player gets hit by a snowball, they get healed x amount of hearts. The two ways I figured I could do this would be with either a ProjectileHitEvent or EntityDamageByEntityEvent. My first thought was to go with the projectile event since it seemed to make more sense, but I have a few questions. So for the event.getEntity who that be the projectile or the Entity that was hit by the Projectile? Basically I just want to know how I would get the project that was used in the event and the player that was hit by said projectile.

    So after that I tried going with the EntityDamageByEntityEvent but ran into a few questions there too. I am able to get the player damaged, the player who damaged the player, but I can not seem to get the method of how they damaged them to check if it was by a snowball or not. Would it count as the getDamager since the snowball was thrown? or would that still count as the player that throw the snowball?
     
  2. Offline

    Compressions

    Prothean_IGS The Snowball would represent getDamager(), and the player who launched the Snowball would be the Snowball's getShooter().
     
  3. Offline

    The_Doctor_123

    ProjectileHitEvent is not specific to another entity getting hit. If you throw a snowball at the wall, the event will be called. I recommend you use EntityDamageByEntityEvent instead. You can tell if it was a snowball if you test if the entity involved in the event was an instance of a Snowball.
     
  4. Offline

    Prothean_IGS

    The_Doctor_123 Compressions So if I use the EntityDamageByEntityEvent there is no option for getShooter, meaning the person hit would be the getEntity? The snowball would be the getDamager? Or would the getDamager be the person who threw the snowball since the is no getShooter?

    Sorry, I'm just a little confused with this event
     
  5. Offline

    Compressions

    Prothean_IGS The Snowball would be the damager, but if you casted the damager to a snowball, you can call getShooter() on it and get the entity that launched the snowball.
     
  6. Offline

    Wolfey

    Prothean_IGS Basically, the event.getDamager() will be the Snowball, and the event.getEntity() will be the one that got hit by the snowball. Once you cast the damager as a snowball, then you will have an option to run snowball.getShooter(), returning the shooter of the snowball.
     
  7. Offline

    Prothean_IGS

    Oh, alright that makes sense now, thanks a lot guys!
     
  8. Offline

    Prothean_IGS

    Wolfey another quick question. What should I cast the snowball to? Like when I go to get a player involved I cast it to (Player), should I use like (Entity) or is there something else I should cast it to?

    Alright so this is what I have so far, and when I test it, absolutely nothing happens.

    here the code:
    Code:java
    1. @EventHandler
    2. public boolean onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    3. Player targetPlayer = (Player) event.getEntity();
    4. Projectile object = (Projectile) event.getDamager();
    5. Player player = (Player) object.getShooter();
    6. // Checking to see if the projectile was a snowball
    7. if (object.getEntityId() == 11 && targetPlayer instanceof Player) {
    8. player.sendMessage("Working, check check check");
    9. }
    10.  
    11. return false;
    12. }


    If I did it right, it should check to see if the projectile is a snowball and if the player hit is a player, if both are true then it should send the debug message, but it does not.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  9. Offline

    Wolfey

    Prothean_IGS I don't think that's how you use entity ids. Use this:
    Code:java
    1.  
    2. @EventHandler
    3. public void onDamage(EntityDamageByEntityEvent e) {
    4. if(e.getEntity() instanceof Player && e.getDamager() instanceof Snowball) {
    5. Player p = (Player) e.getEntity();
    6. Snowball snowball = (Snowball) e.getDamager();
    7. // do stuff
    8. }
    9. }
    10.  

    Also why do you have a return false?
     
  10. Offline

    Prothean_IGS

    Ah yes thank you, that did work. And I have return false because I used boolean instead of void. For some reason when I was using void on an event with another plugin I was making it would work no matter what, even if the if statement was false.
     
  11. Offline

    MoeMix

  12. Offline

    Prothean_IGS

    MoeMix it would just be:
    Code:java
    1. Projectile object = (Projectile) event.getDamager();
    2. Player player = (Player) object.getShooter();
    3. String playername = player.getName();
     
  13. Offline

    MoeMix

    Ah yes, simple enough :)
     
Thread Status:
Not open for further replies.

Share This Page