get player near explosion

Discussion in 'Plugin Development' started by mcgamer99, Jul 29, 2012.

  1. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    How I can get player near an explosion(like max 5 block distance of explosion)?
  2. Offline

    sd5

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
     
        Location explosionLocation = event.getLocation();
         
        for(Player p : Bukkit.getOnlinePlayers()) {
             
            Location playerLocation = p.getLocation();
             
            if(explosionLocation.distance(playerLocation) <= 5) {
                 
                //Player p is near the explosion.
                 
            } else {
                 
                //Player p is not near the explosion.
                 
            }
             
        }
         
    }

    This post has been edited 1 time. It was last edited by sd5 Jul 29, 2012.
    russjr08, theplayerjooo and mcgamer99 like this.
  3. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I would think it would be more effecient to loop through getNearbyEntities() for the location of the explosion, rather than loop through every player that's online and check their distance.
  4. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    getNearbyEntities requires an Entity object though. So unless you get the TNT/Creeper/Whatever entity, you're out of luck.
  5. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Well, likely he'll be listening to the EntityExplodeEvent, in which case he would have an entity to work with.
  6. Offline

    Cirno

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    This is a stupid idea, but why not try to cast Entity to the explosion?
    I have 99% doubt that this idea will fail.
  7. Offline

    sd5

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Normally only entites explode (TNT, creeper). Beds in the nether are not included...
  8. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Never mind all my edits, so hard to link to something in javadocs.

    This post has been edited 5 times. It was last edited by Firefly Jul 29, 2012.
  9. Offline

    bob9

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Doesn't getnearbyentities just do that anyway though, just behind the scenes?

Share This Page