Skip to content
Advertisement

Check how many people are within a certain distance from the center

I’m making a Spleef plugin. I need to count the amount of people in a lobby.

I thought that I could count how many people are within a certain distance from the center of the lobby. I think that this may work better than recording when someone types the command.

Main.java:

JavaScript

playerJoinedGame.java:

JavaScript

I have not called the onPlayerJoin method from the main class yet.

I’m open to anyone having any better ways of counting the amount of people in a lobby.

Advertisement

Answer

Make sure you have a Location object with the center of which you want to get the nearby players.

JavaScript

Then, have a double value with the needed distance.

JavaScript

First of all, you should make a loop of all players on the server:

JavaScript

Then, get the location of player:

JavaScript

Now we can do a check of the distance between the two locations (center and location):

JavaScript

Notice: You should use distanceSquared(Location) which is equivalent to the result square of distance(Location), because distance(Location) uses Java’s square-root method, which is very resource-heavy.

Final result:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement