TheGreatPani Posted May 31, 2016 Posted May 31, 2016 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.
Pure_ Posted May 31, 2016 Posted May 31, 2016 (edited) 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 Edited May 31, 2016 by Pure_
TheGreatPani Posted May 31, 2016 Author Posted May 31, 2016 (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 May 31, 2016 by TheGreatPani
Chris Posted May 31, 2016 Posted May 31, 2016 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)
FaruqAbdul Posted May 31, 2016 Posted May 31, 2016 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
Explv Posted May 31, 2016 Posted May 31, 2016 (edited) accully u cant use this method for do, yuo must use recurse method to loop through for player That method works fine, he just might want to check that the player != myPlayer Edited May 31, 2016 by Explv
Botre Posted May 31, 2016 Posted May 31, 2016 (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 May 31, 2016 by Botre
Explv Posted May 31, 2016 Posted May 31, 2016 (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 May 31, 2016 by Explv 2