Skip to content
Permalink
Browse files

Updated CommandBook for the changes to the getOnlinePlayers method

  • Loading branch information...
DarkArc committed Aug 29, 2014
1 parent fb97946 commit 5ecfeece32751f0e744cd503586715621a1cc364
@@ -53,7 +53,7 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.7.8-R0.1-SNAPSHOT</version>
<version>1.7.10-R0.1-SNAPSHOT</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
@@ -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<Player>(Arrays.asList(BasePlugin.server().getOnlinePlayers())))).isCancelled()) {
new HashSet<Player>(BasePlugin.server().getOnlinePlayers()))).isCancelled()) {
return;
}
}
@@ -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 @@ public void reload() {
* @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<? extends Player> 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 void onPlayerJoin(PlayerJoinEvent event) {
min = 0, max = 1)
@CommandPermissions({"commandbook.who"})
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
// 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;
}

@@ -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<? extends Player> 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%")) {
@@ -165,7 +165,7 @@ public static int matchMCWorldTime(String timeStr) throws CommandException {
*/
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;

filter = filter.toLowerCase();
@@ -242,13 +242,13 @@ public static int matchMCWorldTime(String timeStr) 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.");
}

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 int matchMCWorldTime(String timeStr) throws CommandException {
* @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) {
if (player.getName().equalsIgnoreCase(filter)
|| (CommandBook.inst().lookupWithDisplayNames
@@ -381,6 +381,7 @@ public static CommandSender matchPlayerOrConsole(CommandSender sender, String fi
* @return
* @throws CommandException
*/
@Deprecated
public static List<Player> checkPlayerMatch(List<Player> players) throws CommandException {
// Check to see if there were any matches
if (players.isEmpty()) {
@@ -389,6 +390,22 @@ public static CommandSender matchPlayerOrConsole(CommandSender sender, String fi
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
* throw an exception.
@@ -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<? extends Player> online, ChatColor color) {
StringBuilder out = new StringBuilder();

// To keep track of commas

0 comments on commit 5ecfeec

Please sign in to comment.
You can’t perform that action at this time.