diff --git a/pom.xml b/pom.xml index 5a18a3d..14f3416 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,7 @@ org.bukkit bukkit - 1.7.8-R0.1-SNAPSHOT + 1.7.10-R0.1-SNAPSHOT compile jar diff --git a/src/main/java/com/sk89q/commandbook/MessagingComponent.java b/src/main/java/com/sk89q/commandbook/MessagingComponent.java index 5454a8d..0950fb8 100644 --- a/src/main/java/com/sk89q/commandbook/MessagingComponent.java +++ b/src/main/java/com/sk89q/commandbook/MessagingComponent.java @@ -44,7 +44,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; -import java.util.Arrays; import java.util.HashSet; import static com.sk89q.commandbook.util.ChatUtil.replaceColorMacros; @@ -197,7 +196,7 @@ public void say(CommandContext args, CommandSender sender) throws CommandExcepti if (sender instanceof Player) { if (BasePlugin.callEvent( new AsyncPlayerChatEvent(false, (Player) sender, msg, - new HashSet(Arrays.asList(BasePlugin.server().getOnlinePlayers())))).isCancelled()) { + new HashSet(BasePlugin.server().getOnlinePlayers()))).isCancelled()) { return; } } diff --git a/src/main/java/com/sk89q/commandbook/OnlineListComponent.java b/src/main/java/com/sk89q/commandbook/OnlineListComponent.java index e90a0c2..814722a 100644 --- a/src/main/java/com/sk89q/commandbook/OnlineListComponent.java +++ b/src/main/java/com/sk89q/commandbook/OnlineListComponent.java @@ -35,10 +35,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @ComponentInformation(friendlyName = "Online List", desc = "Lists online players both on command and on player join.") public class OnlineListComponent extends BukkitComponent implements Listener { @@ -70,14 +67,25 @@ private static class LocalConfiguration extends ConfigurationBase { * @param online * @param sender */ + @Deprecated public void sendOnlineList(Player[] online, CommandSender sender) { + sendOnlineList(Arrays.asList(online), sender); + } + + /** + * Send the online player list. + * + * @param online + * @param sender + */ + public void sendOnlineList(Collection online, CommandSender sender) { StringBuilder out = new StringBuilder(); - int onlineCount = online.length; + int onlineCount = online.size(); // This applies mostly to the console, so there might be 0 players // online if that's the case! - if (online.length == 0) { + if (online.isEmpty()) { sender.sendMessage("0 players are online."); return; } @@ -191,7 +199,7 @@ public class Commands { min = 0, max = 1) @CommandPermissions({"commandbook.who"}) public void who(CommandContext args, CommandSender sender) throws CommandException { - Player[] online = CommandBook.server().getOnlinePlayers(); + Collection online = CommandBook.server().getOnlinePlayers(); // Some crappy wrappers uses this to detect if the server is still // running, even though this is a very unreliable way to do it @@ -225,7 +233,7 @@ public void who(CommandContext args, CommandSender sender) throws CommandExcepti // This applies mostly to the console, so there might be 0 players // online if that's the case! - if (online.length == 0) { + if (online.isEmpty()) { sender.sendMessage("0 players are online."); return; } @@ -245,7 +253,7 @@ public void who(CommandContext args, CommandSender sender) throws CommandExcepti StringBuilder out = new StringBuilder(); out.append(ChatColor.GRAY + "Found players (out of "); - out.append(ChatColor.GRAY + "" + online.length); + out.append(ChatColor.GRAY + "" + online.size()); out.append(ChatColor.GRAY + "): "); out.append(ChatColor.WHITE); @@ -272,7 +280,7 @@ public void who(CommandContext args, CommandSender sender) throws CommandExcepti // This means that no matches were found! if (first) { sender.sendMessage(ChatColor.RED + "No players (out of " - + online.length + ") matched '" + filter + "'."); + + online.size() + ") matched '" + filter + "'."); return; } diff --git a/src/main/java/com/sk89q/commandbook/util/ChatUtil.java b/src/main/java/com/sk89q/commandbook/util/ChatUtil.java index 1183f4c..e1da26d 100644 --- a/src/main/java/com/sk89q/commandbook/util/ChatUtil.java +++ b/src/main/java/com/sk89q/commandbook/util/ChatUtil.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.Calendar; +import java.util.Collection; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -94,12 +95,12 @@ public static String toFriendlyString(Location location) { * @return */ public static String replaceMacros(CommandSender sender, String message) { - Player[] online = CommandBook.server().getOnlinePlayers(); + Collection online = CommandBook.server().getOnlinePlayers(); message = message.replace("%name%", toName(sender)); message = message.replace("%cname%", toColoredName(sender, null)); message = message.replace("%id%", toUniqueName(sender)); - message = message.replace("%online%", String.valueOf(online.length)); + message = message.replace("%online%", String.valueOf(online.size())); // Don't want to build the list unless we need to if (message.contains("%players%")) { diff --git a/src/main/java/com/sk89q/commandbook/util/InputUtil.java b/src/main/java/com/sk89q/commandbook/util/InputUtil.java index 9e8eabc..b53439e 100644 --- a/src/main/java/com/sk89q/commandbook/util/InputUtil.java +++ b/src/main/java/com/sk89q/commandbook/util/InputUtil.java @@ -165,7 +165,7 @@ public static class PlayerParser { */ public static List matchPlayerNames(CommandSender source, String filter) { - Player[] players = CommandBook.server().getOnlinePlayers(); + Collection players = CommandBook.server().getOnlinePlayers(); boolean useDisplayNames = CommandBook.inst().lookupWithDisplayNames; filter = filter.toLowerCase(); @@ -242,13 +242,13 @@ public static List matchPlayerNames(CommandSender source, String filter) */ public static List matchPlayers(CommandSender source, String filter) throws CommandException { - if (CommandBook.server().getOnlinePlayers().length == 0) { + if (CommandBook.server().getOnlinePlayers().isEmpty()) { throw new CommandException("No players matched query."); } if (filter.equals("*")) { CommandBook.inst().checkPermission(source, "commandbook.targets.everyone"); - return checkPlayerMatch(Arrays.asList(CommandBook.server().getOnlinePlayers())); + return checkPlayerMatch(Lists.newArrayList(CommandBook.server().getOnlinePlayers())); } // Handle special hash tag groups @@ -308,7 +308,7 @@ public static List matchPlayers(CommandSender source, String filter) thr * @throws CommandException */ public static Player matchPlayerExactly(CommandSender sender, String filter) throws CommandException { - Player[] players = CommandBook.server().getOnlinePlayers(); + Collection players = CommandBook.server().getOnlinePlayers(); for (Player player : players) { if (player.getName().equalsIgnoreCase(filter) || (CommandBook.inst().lookupWithDisplayNames @@ -381,6 +381,7 @@ public static Iterable detectTargets(CommandSender sender, CommandContex * @return * @throws CommandException */ + @Deprecated public static List checkPlayerMatch(List players) throws CommandException { // Check to see if there were any matches if (players.isEmpty()) { @@ -389,6 +390,22 @@ public static List checkPlayerMatch(List players) throws Command return players; } + /** + * Checks if the given list of players is greater than size 0, otherwise + * throw an exception. + * + * @param players + * @return + * @throws CommandException + */ + public static > T checkPlayerMatch(T players) throws CommandException { + // Check to see if there were any matches + if (players.isEmpty()) { + throw new CommandException("No players matched query."); + } + return players; + } + /** * Checks if the given list of players contains only one player, otherwise * throw an exception. diff --git a/src/main/java/com/sk89q/commandbook/util/ServerUtil.java b/src/main/java/com/sk89q/commandbook/util/ServerUtil.java index 187a2a2..6957c55 100644 --- a/src/main/java/com/sk89q/commandbook/util/ServerUtil.java +++ b/src/main/java/com/sk89q/commandbook/util/ServerUtil.java @@ -5,6 +5,9 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.Arrays; +import java.util.Collection; + public class ServerUtil { public static String getOnlineList(Player[] online) { @@ -18,7 +21,19 @@ public static String getOnlineList(Player[] online) { * @param color * @return */ + @Deprecated public static String getOnlineList(Player[] online, ChatColor color) { + return getOnlineList(Arrays.asList(online), color); + } + + /** + * Returns a comma-delimited list of players. + * + * @param online + * @param color + * @return + */ + public static String getOnlineList(Collection online, ChatColor color) { StringBuilder out = new StringBuilder(); // To keep track of commas