Jump to content
Juggles

Return # of players in a certain area

Recommended Posts

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 :)

Link to comment
Share on other sites

	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;
    }
	

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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();
}

:)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing   0 members

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