Determine whom a fireball was meant for

Discussion in 'Plugin Development' started by Mini-Me, Jan 27, 2012.

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

    Mini-Me

    Hello Bukkit community,

    I'm trying to write a plugin and in an event handler for an EntityExplodeEvent I need to get for which player a creeper explosion and a fireball was meant for. What I got so far is this:
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. void onCreeperOrFireballExplodes(EntityExplodeEvent event) {
    3. Entity Exploded = event.getEntity();
    4. if(Exploded instanceof Creeper) {
    5. Creeper Attacker = (Creeper) Exploded;
    6. if (Attacker.getTarget() instanceof Player) {
    7. Player player = (Player) Attacker.getTarget();
    8. //And here we have our result and can go on...
    9. }
    10. }
    11. }
    12. else if(Exploded instanceof Fireball) {
    13. //Now I don't know how to get a target here
    14. }
    15. }

    I know that I can get the shooter of the fireball, but a ghast is unlike the creeper not a creature and thus doesn't have the method to get the target. Is there at all a way to determine whom a fireball was meant for?

    Thanks in advance,

    Mini-Me
     
  2. Offline

    TheFieldZy

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL)
    3. public void onCreeperOrFireballExplodes(EntityExplodeEvent event) {
    4. Entity Exploded = event.getEntity();
    5. if(Exploded instanceof Creeper) {
    6. Creeper Attacker = (Creeper) Exploded;
    7. if (Attacker.getTarget() instanceof Player) {
    8. Player player = (Player) Attacker.getTarget();
    9. //And here we have our result and can go on...
    10. }
    11. } else if (Exploded instanceof Fireball) {
    12. Projectile proj = (Projectile) Exploded;
    13. Creature Attacker = (Creature)proj.getShooter();
    14. if (Attacker.getTarget() instanceof Player) {
    15. Player player = (Player) Attacker.getTarget();
    16. }
    17. }
    18. }
    19.  
     
  3. Offline

    Bruno Lanevik

    try use getTargetBlock() and then calculate the distance to the closest player maybe?
     
  4. Offline

    Mini-Me

    The problem I see here is that the shooter of the projectile fireball is a ghast, which is not a subclass of creature and thus cannot be casted:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.entity.CraftGhast cannot be cast to org.bukkit.entity.Creature
    So it doesn't know the method "getTarget".

    Wouldn't that give me the location of where the ghast looks at during the explosion?
    => just checked that and seeing that I get water all over the place but not near me that confirms my theorie.

    I think I'll just have to expect that the palyer that is the closest to the fireball during the explosion is the target...

    Thanks for your answers, though.
     
Thread Status:
Not open for further replies.

Share This Page