How to get all players in a 5 block radius

Discussion in 'Plugin Development' started by MooshViolet, Dec 15, 2014.

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

    MooshViolet

    Hi. So i am calling a PlayerDropItemEvent. I was just wondering how i would loop through all of the players in a 5 block radius of the player that dropped. I was thinking like:
    Code:
    for(Player ps:  Bukkit.getonlinePlayers()){
    
    But how do I make a statement to say if ps is within 5 blocks of p (player)?
     
  2. Offline

    mythbusterma

    @MooshViolet

    Erm, what is this for?

    Also, Entity#getNearbyEntities(int,int,int)
     
  3. Offline

    MooshViolet

    @mythbusterma
    Well I am creating a plugin when someone drops an item, a sound occurs (already did that). Just need it to sound to players nearby.
    So I would do p.getNearbyEntities(then what would i put as the ints? would i put the x y and z of the p?)
     
  4. Offline

    Skionz

    @MooshViolet
    if(player.distance(location) <= 5) {
    Is this what your looking for?
     
  5. Offline

    mythbusterma

    @MooshViolet

    You can just play the sound at a location.
     
  6. Offline

    MooshViolet

    @Skionz
    I dont think so because p.distance does not work.
     
  7. Offline

    Skionz

    @MooshViolet My bad. It is Location#distance not Player#distance.
     
  8. Offline

    TheCodingCat

    Try looping through Entity#getNearbyEntities() where the item is the entity, check if it as instance of a Player then cast the entity to player then play the sound. If you are stilled concerned location wise use what
    @Skionz
    said
     
  9. Offline

    MooshViolet

  10. Offline

    TheCodingCat

    World#playSound() will play the sound at the location you specify
    @MooshViolet
     
  11. Offline

    MooshViolet

    @Skionz
    So somehting like this:?
    if(location.distance(p) <=5){
     
  12. Offline

    Skionz

    @MooshViolet The distance method takes another location not a Player. Assuming 'p' is a Player of course.
     
  13. Offline

    MooshViolet

    @Skionz
    its okay I got it figured out. Thanks for your help!
     
    Skionz likes this.
  14. Offline

    RingOfStorms

    Please listen to these two people. You are literally doing way to much for something that can be achieved by world.playSound method
    World.playSound (Location location, Sound sound, float volume, float pitch)
     
Thread Status:
Not open for further replies.

Share This Page