* @throws CommandException no matches found
*/
public static List<Player> matchPlayers(CommandSender source, String filter) throws CommandException {
if (CommandBook.server().getOnlinePlayers().isEmpty()) {
throw new CommandException("No players matched query.");
}
if (filter.equals("*")) {
CommandBook.inst().checkPermission(source, "commandbook.targets.everyone");
return checkPlayerMatch(Lists.newArrayList(CommandBook.server().getOnlinePlayers()));
}
// Handle special hash tag groups
if (filter.charAt(0) == '#') {
// Handle #world, which matches player of the same world as the
// calling source
if (filter.equalsIgnoreCase("#world")) {
World sourceWorld = checkPlayer(source).getWorld();
CommandBook.inst().checkPermission(source, "commandbook.targets.world." + sourceWorld.getName());
return checkPlayerMatch(sourceWorld.getPlayers());
// Handle #near, which is for nearby players.
} else if (filter.startsWith("#near")) {
CommandBook.inst().checkPermission(source, "commandbook.targets.near");
List<Player> players = new ArrayList<Player>();
Player sourcePlayer = checkPlayer(source);
World sourceWorld = sourcePlayer.getWorld();
Location sourceLocation = sourcePlayer.getLocation();
double targetDistance = 30;
String[] split = filter.split(":");
if (split.length == 2) {
try {
targetDistance = Double.parseDouble(split[1]);
} catch (NumberFormatException ignored) { }
}
// Square the target distance so that we don't have to calculate the
// square root in this distance calculation
targetDistance = Math.pow(targetDistance, 2);
Location k = sourceLocation.clone(); // Included for optimization
for (Player player : CommandBook.server().getOnlinePlayers()) {
if (!player.getWorld().equals(sourceWorld)) continue;
if (player.getLocation(k).distanceSquared(sourceLocation) < targetDistance) {
players.add(player);
}
}
return checkPlayerMatch(players);
} else {
throw new CommandException("Invalid group '" + filter + "'.");
}
}
return checkPlayerMatch(matchPlayerNames(source, filter));
}