Jump to content

How to know if other players are nearby


Recommended Posts

Posted (edited)

Idk how MethodProvider works so i took out the "methods.". The code was also picking up my character and going into the if statement so I changed the if condition to 

if ((p.getPosition().distance(myPlayer().getPosition()) > 1) && (p.getPosition().distance(myPlayer().getPosition()) < 20)  )

Thanks!

Edited by TheGreatPani
Posted

Idk how MethodProvider works so i took out the "methods.". The code was also picking up my character and going into the if statement so I changed the if condition to 

if ((p.getPosition().distance(myPlayer().getPosition()) > 1) && (p.getPosition().distance(myPlayer().getPosition()) < 20)  )

Thanks!

//why not

!p.getname#equals(your acc)
Posted
for (Player p : methods.getPlayers().getAll()) {
            if (p.isVisible() || p.getPosition().distance(methods.myPlayer().getPosition()) < 10) {
                // OH NOES
            }
        }
}

where methods is an instance of MethodProvider

note: i did not test this

 

accully u cant use this method for do, yuo must use recurse method to loop through for player

Posted (edited)


// Set maximum distance.

private final int maximumDistance = 12;

/*

Create filter.

Player must:

- Not be null.

- Exist.

- Be within the maximum distance from my player.

*/

private final Filter<Player> filter = player -> player != null && player.exists() && (player.getPosition().distance(myPlayer.getPosition()) < maximumDistance );

// ...

/*

To check if one or more players are nearby:

Apply filter to Players instance.

Check if the result set is greater than 1.

(myPlayer itself will always be part of the result set).

*/

if(getPlayers().filter(filter).size() > 1) {

// There are players nearby.

}

// ...

/*

To get the amount of players nearby:

Apply filter to Players instance.

Subtract 1 from the result set.

(myPlayer itself will always be part of the result set).

*/

int nearby = getPlayers().filter(filter).size() - 1;

Edited by Botre
Posted (edited)

I'm trying to make my script log out when any player is in my vicinity. I think I have to use the "getAll()" method in the Players class in the API to do this, but I can't figure out how to write it. Also, will the list be true, full, or something else if there is another player nearby? Thanks. 

 

If you want to check if a player is less than a certain distance away, you can use:

private boolean isOtherPlayerWithinDistance(final int distance) {
    for(final Player player : getPlayers().getAll()) {
        if(player != myPlayer() && player.getPosition().distance(myPosition()) < distance) {
            return true;
        }
    }
    return false;
}

Or you can use a filter:

private boolean isOtherPlayerWithinDistance(final int distance) {
    return !getPlayers().filter(player -> player != myPlayer() && 
                                player.getPosition().distance(myPosition()) < distance).isEmpty();
}

Otherwise you can just check the size of getPlayers().getAll()

private boolean isOtherPlayerNearby() {
    return getPlayers().getAll().size() > 1;
}
Edited by Explv
  • Like 2

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...