Skip to content

Commit

Permalink
Updated CommandBook for the changes to the getOnlinePlayers method
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkArc committed Aug 29, 2014
1 parent fb97946 commit 5ecfeec
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -53,7 +53,7 @@
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId> <artifactId>bukkit</artifactId>
<version>1.7.8-R0.1-SNAPSHOT</version> <version>1.7.10-R0.1-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/sk89q/commandbook/MessagingComponent.java
Expand Up @@ -44,7 +44,6 @@
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;


import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;


import static com.sk89q.commandbook.util.ChatUtil.replaceColorMacros; import static com.sk89q.commandbook.util.ChatUtil.replaceColorMacros;
Expand Down Expand Up @@ -197,7 +196,7 @@ public void say(CommandContext args, CommandSender sender) throws CommandExcepti
if (sender instanceof Player) { if (sender instanceof Player) {
if (BasePlugin.callEvent( if (BasePlugin.callEvent(
new AsyncPlayerChatEvent(false, (Player) sender, msg, new AsyncPlayerChatEvent(false, (Player) sender, msg,
new HashSet<Player>(Arrays.asList(BasePlugin.server().getOnlinePlayers())))).isCancelled()) { new HashSet<Player>(BasePlugin.server().getOnlinePlayers()))).isCancelled()) {
return; return;
} }
} }
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/com/sk89q/commandbook/OnlineListComponent.java
Expand Up @@ -35,10 +35,7 @@
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;


import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ComponentInformation(friendlyName = "Online List", desc = "Lists online players both on command and on player join.") @ComponentInformation(friendlyName = "Online List", desc = "Lists online players both on command and on player join.")
public class OnlineListComponent extends BukkitComponent implements Listener { public class OnlineListComponent extends BukkitComponent implements Listener {
Expand Down Expand Up @@ -70,14 +67,25 @@ private static class LocalConfiguration extends ConfigurationBase {
* @param online * @param online
* @param sender * @param sender
*/ */
@Deprecated
public void sendOnlineList(Player[] online, CommandSender sender) { 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<? extends Player> online, CommandSender sender) {


StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();
int onlineCount = online.length; int onlineCount = online.size();


// This applies mostly to the console, so there might be 0 players // This applies mostly to the console, so there might be 0 players
// online if that's the case! // online if that's the case!
if (online.length == 0) { if (online.isEmpty()) {
sender.sendMessage("0 players are online."); sender.sendMessage("0 players are online.");
return; return;
} }
Expand Down Expand Up @@ -191,7 +199,7 @@ public class Commands {
min = 0, max = 1) min = 0, max = 1)
@CommandPermissions({"commandbook.who"}) @CommandPermissions({"commandbook.who"})
public void who(CommandContext args, CommandSender sender) throws CommandException { public void who(CommandContext args, CommandSender sender) throws CommandException {
Player[] online = CommandBook.server().getOnlinePlayers(); Collection<? extends Player> online = CommandBook.server().getOnlinePlayers();


// Some crappy wrappers uses this to detect if the server is still // Some crappy wrappers uses this to detect if the server is still
// running, even though this is a very unreliable way to do it // running, even though this is a very unreliable way to do it
Expand Down Expand Up @@ -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 // This applies mostly to the console, so there might be 0 players
// online if that's the case! // online if that's the case!
if (online.length == 0) { if (online.isEmpty()) {
sender.sendMessage("0 players are online."); sender.sendMessage("0 players are online.");
return; return;
} }
Expand All @@ -245,7 +253,7 @@ public void who(CommandContext args, CommandSender sender) throws CommandExcepti
StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();


out.append(ChatColor.GRAY + "Found players (out of "); 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.GRAY + "): ");
out.append(ChatColor.WHITE); out.append(ChatColor.WHITE);


Expand All @@ -272,7 +280,7 @@ public void who(CommandContext args, CommandSender sender) throws CommandExcepti
// This means that no matches were found! // This means that no matches were found!
if (first) { if (first) {
sender.sendMessage(ChatColor.RED + "No players (out of " sender.sendMessage(ChatColor.RED + "No players (out of "
+ online.length + ") matched '" + filter + "'."); + online.size() + ") matched '" + filter + "'.");
return; return;
} }


Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/sk89q/commandbook/util/ChatUtil.java
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
Expand Down Expand Up @@ -94,12 +95,12 @@ public static String toFriendlyString(Location location) {
* @return * @return
*/ */
public static String replaceMacros(CommandSender sender, String message) { public static String replaceMacros(CommandSender sender, String message) {
Player[] online = CommandBook.server().getOnlinePlayers(); Collection<? extends Player> online = CommandBook.server().getOnlinePlayers();


message = message.replace("%name%", toName(sender)); message = message.replace("%name%", toName(sender));
message = message.replace("%cname%", toColoredName(sender, null)); message = message.replace("%cname%", toColoredName(sender, null));
message = message.replace("%id%", toUniqueName(sender)); 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 // Don't want to build the list unless we need to
if (message.contains("%players%")) { if (message.contains("%players%")) {
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/com/sk89q/commandbook/util/InputUtil.java
Expand Up @@ -165,7 +165,7 @@ public static class PlayerParser {
*/ */
public static List<Player> matchPlayerNames(CommandSender source, String filter) { public static List<Player> matchPlayerNames(CommandSender source, String filter) {


Player[] players = CommandBook.server().getOnlinePlayers(); Collection<? extends Player> players = CommandBook.server().getOnlinePlayers();
boolean useDisplayNames = CommandBook.inst().lookupWithDisplayNames; boolean useDisplayNames = CommandBook.inst().lookupWithDisplayNames;


filter = filter.toLowerCase(); filter = filter.toLowerCase();
Expand Down Expand Up @@ -242,13 +242,13 @@ public static List<Player> matchPlayerNames(CommandSender source, String filter)
*/ */
public static List<Player> matchPlayers(CommandSender source, String filter) throws CommandException { public static List<Player> 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."); throw new CommandException("No players matched query.");
} }


if (filter.equals("*")) { if (filter.equals("*")) {
CommandBook.inst().checkPermission(source, "commandbook.targets.everyone"); 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 // Handle special hash tag groups
Expand Down Expand Up @@ -308,7 +308,7 @@ public static List<Player> matchPlayers(CommandSender source, String filter) thr
* @throws CommandException * @throws CommandException
*/ */
public static Player matchPlayerExactly(CommandSender sender, String filter) throws CommandException { public static Player matchPlayerExactly(CommandSender sender, String filter) throws CommandException {
Player[] players = CommandBook.server().getOnlinePlayers(); Collection<? extends Player> players = CommandBook.server().getOnlinePlayers();
for (Player player : players) { for (Player player : players) {
if (player.getName().equalsIgnoreCase(filter) if (player.getName().equalsIgnoreCase(filter)
|| (CommandBook.inst().lookupWithDisplayNames || (CommandBook.inst().lookupWithDisplayNames
Expand Down Expand Up @@ -381,6 +381,7 @@ public static Iterable<Player> detectTargets(CommandSender sender, CommandContex
* @return * @return
* @throws CommandException * @throws CommandException
*/ */
@Deprecated
public static List<Player> checkPlayerMatch(List<Player> players) throws CommandException { public static List<Player> checkPlayerMatch(List<Player> players) throws CommandException {
// Check to see if there were any matches // Check to see if there were any matches
if (players.isEmpty()) { if (players.isEmpty()) {
Expand All @@ -389,6 +390,22 @@ public static List<Player> checkPlayerMatch(List<Player> players) throws Command
return players; 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 extends Collection<? extends Player>> 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 * Checks if the given list of players contains only one player, otherwise
* throw an exception. * throw an exception.
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/sk89q/commandbook/util/ServerUtil.java
Expand Up @@ -5,6 +5,9 @@
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;


import java.util.Arrays;
import java.util.Collection;

public class ServerUtil { public class ServerUtil {


public static String getOnlineList(Player[] online) { public static String getOnlineList(Player[] online) {
Expand All @@ -18,7 +21,19 @@ public static String getOnlineList(Player[] online) {
* @param color * @param color
* @return * @return
*/ */
@Deprecated
public static String getOnlineList(Player[] online, ChatColor color) { 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<? extends Player> online, ChatColor color) {
StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();


// To keep track of commas // To keep track of commas
Expand Down

0 comments on commit 5ecfeec

Please sign in to comment.