Solved find number of players in area

Discussion in 'Plugin Development' started by vhbob, Dec 10, 2015.

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

    vhbob

    hey, im trying to figure out a way to see the amount of players in a certain area, i cannot figure this out please help :)
     
  2. Offline

    mcdorli

    loop trough them
    if (player's location in the certain area)
    do your stuff

    if that area is only one chunk, you could do Chunk#getEntities()
     
  3. Offline

    vhbob

    @mcdorli thanks ill try that

    @mcdorli how does one use loops for this 3:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 10, 2015
  4. Offline

    mcdorli

    for (Player player : Bukkit.getOnlinePlayers())
    Please learn java, foreach loops are very basic part of it.
     
  5. Offline

    vhbob

    @mcdorli ah okay, thanks i haven't coded in a while and forgot some things xD. i still dont get how to see if there with in a 50 block radius of a location
     
  6. Offline

    mcdorli

    pythagoras theorem (extended) if (x*x + y*y + z*z <= r*r), where x, y and z are the x, y and z distances from the location, and r is the radius
     
  7. Offline

    vhbob

    @mcdorli where do i put the location??
     
  8. Offline

    mcdorli

    You have a location? then just #Location.distance(Location location) or something like that
     
  9. Offline

    vhbob

    @mcdorli so this will work if " l " is my location
    Code:
    if (p.getLocation().distance(l) < 50)
     
  10. Offline

    mcdorli

  11. Offline

    vhbob

    @mcdorli ahh ok, but if i created a int and set it to " 0 " and added 1 to it every time if found someone in that radius wouldnt it just stay 0?
     
  12. Offline

    mcdorli

    If there's someone in that area, then probably not. I think i don't understand the question
     
  13. Offline

    vhbob

    @mcdorli i need an int to equal the number of players within 50 blocks of the loaction
     
  14. Offline

    mcdorli

    As you mentioned, set it to 0 first, then every time it finds someone, add one to it.
     
  15. Offline

    vhbob

    @mcdorli ahh thanks i thought it through and figured it out, thanks again :) so this should work:
    Code:
    for (Player player : Bukkit.getOnlinePlayers()) {
                                if (p.getLocation().distance(l) < 50) {
                                    players = players + 1;
                                }
                            }
     
Thread Status:
Not open for further replies.

Share This Page