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:
package me.olsyboy.spleef; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.configuration.file.YamlConfiguration; import java.util.Arrays; import java.util.List; public class Main extends JavaPlugin { public void onEnable(int amountOfPlayers) { amountOfPlayers = 0; loadConfiguration(); reloadConfig(); } public void onDisable() { saveDefaultConfig(); } public void loadConfiguration() { //See "Creating you're defaults" getConfig().options().copyDefaults(true); // NOTE: You do not have to use "plugin." if the class extends the java plugin //Save the config whenever you manipulate it saveDefaultConfig(); } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { Player player = (Player) sender; if (cmd.getName().equalsIgnoreCase("spleef")) { if (args[0].equalsIgnoreCase("setgame")) { if (args.length == 2) { String gameName = args[1]; //initialize the gameName variable here getConfig().set("Game Locations." + gameName + ".Location", Arrays.asList(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getLocation().getPitch(), player.getLocation().getYaw(), player.getLocation().getWorld().getName())); getConfig().options().copyDefaults(true); saveConfig(); player.sendMessage(ChatColor.AQUA + "[Spleef] " + ChatColor.YELLOW + "Spleef Game Location Set"); } } if (args[0].equalsIgnoreCase("join")) { String gameName = args[1]; //initialize the gameName variable here List<String> joinGameLocation = this.getConfig().getStringList("Game Locations." + gameName + ".Location"); String xPos = joinGameLocation.get(0); double xPos2 = Double.parseDouble(xPos); String yPos = joinGameLocation.get(1); double yPos2 = Double.parseDouble(yPos); String zPos = joinGameLocation.get(2); double zPos2 = Double.parseDouble(zPos); String pitch = joinGameLocation.get(3); float pitch2 = Float.parseFloat(pitch); String Yaw = joinGameLocation.get(4); float Yaw2 = Float.parseFloat(Yaw); World actualWorld = Bukkit.getWorld(joinGameLocation.get(5)); Location spleefGameLocation = new Location(actualWorld, xPos2, yPos2, zPos2); spleefGameLocation.setPitch(pitch2); spleefGameLocation.setYaw(Yaw2); player.teleport(spleefGameLocation); } else if (!(args[0].equalsIgnoreCase("setgame"))) { if (!args[0].equalsIgnoreCase("join")) { player.sendMessage("/spleef join {GameName}"); } } } return true; } }
playerJoinedGame.java:
package me.olsyboy.spleef; public class playerJoinedGame extends Main { public void onPlayerJoin(int amountOfPlayers) { amountOfPlayers = amountOfPlayers + 1; } }
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.
Location center = new Location(world, x, y, z);
Then, have a double
value with the needed distance.
double distance = 10D;
First of all, you should make a loop of all players on the server:
for (Player player : Bukkit.getOnlinePlayers()) { }
Then, get the location of player
:
for (Player player : Bukkit.getOnlinePlayers()) { Location location = player.getLocation(); }
Now we can do a check of the distance between the two locations (center
and location
):
for (Player player : Bukkit.getOnlinePlayers()) { Location location = player.getLocation(); if (location.distanceSquared(center) <= distance * distance) { // Do something } }
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:
double distance = 10D; Location center = new Location(Bukkit.getWorld("world"), x, y, z); for (Player player : Bukkit.getOnlinePlayers()) { Location location = player.getLocation(); if (location.distanceSquared(center) <= distance * distance) { // Do something } }