Jump to content

Return # of players in a certain area


Recommended Posts

Posted

Right now I am using 
 

getPlayers().getAll().size();

But this only works for the whole loaded map. 
I scoured through the API but cannot find a method to return the number of players within a certain area. 


I am looking for a method that can do something similar to this:  getPlayers().getAll(area).size();

Appreciate any help I can get :)

Posted

	public boolean checkforPlayers(Area area) {
        List<Player> playerss = players.getAll();
        for (Player p : playerss) {
            if (area.contains(p) && !p.getName().equals(myPlayer().getName())) {
                return true;
            }
        }
        return false;
    }
	

Posted (edited)
2 hours ago, TheWind said:

if you want it in one line I think this might work:

getPlayers().filter(player -> area.contains(player)).size();

this^


Remember that NPC and players only get loaded when they are in range. (+-16 tiles)
This is different then gameobjects that get loaded together with the map chunks.
 

Edited by Khaleesi
  • Like 1
Posted (edited)
9 hours ago, Juggles said:

looking for a method that can do something similar to this:  getPlayers().getAll(area).size();
 

 

9 hours ago, TheWind said:

if you want it in one line I think this might work: 


getPlayers().filter(player -> area.contains(player)).size();

 

 

I believe this would also work:

getPlayers().filter(new AreaFilter(area)).size();

 

Edited by Explv
  • Like 1
Posted
16 hours ago, TheWind said:

if you want it in one line I think this might work:

getPlayers().filter(player -> area.contains(player)).size();

If I'm going to do Lambda, I'll stay away from OSBot's filter and just use theirs:

public final int countPlayers(Area area) {
	return (int) players.getAll().stream().filter(area::contains).count();
}

:)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...