package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import java.util.*; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; public class Commandlist extends EssentialsCommand { public Commandlist() { super("list"); } @Override public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception { boolean showHidden = true; if (sender instanceof Player) { showHidden = ess.getUser(sender).isAuthorized("essentials.list.hidden"); } sender.sendMessage(listSummary(server, showHidden)); Map> playerList = getPlayerLists(server, showHidden); if (args.length > 0) { sender.sendMessage(listGroupUsers(playerList, args[0].toLowerCase())); } else { sendGroupedList(sender, commandLabel, playerList); } } // Produce a user summary: There are 5 out of maximum 10 players online. private String listSummary(final Server server, final boolean showHidden) { int playerHidden = 0; for (Player onlinePlayer : server.getOnlinePlayers()) { if (ess.getUser(onlinePlayer).isHidden()) { playerHidden++; } } String online; if (showHidden && playerHidden > 0) { online = _("listAmountHidden", server.getOnlinePlayers().length - playerHidden, playerHidden, server.getMaxPlayers()); } else { online = _("listAmount", server.getOnlinePlayers().length - playerHidden, server.getMaxPlayers()); } return online; } // Build the basic player list, divided by groups. private Map> getPlayerLists(final Server server, final boolean showHidden) { Map> playerList = new HashMap>(); for (Player onlinePlayer : server.getOnlinePlayers()) { final User onlineUser = ess.getUser(onlinePlayer); if (onlineUser.isHidden() && !showHidden) { continue; } final String group = Util.stripFormat(onlineUser.getGroup().toLowerCase()); List list = playerList.get(group); if (list == null) { list = new ArrayList(); playerList.put(group, list); } list.add(onlineUser); } return playerList; } // Output a playerlist of just a single group, /list private String listGroupUsers(final Map> playerList, final String groupName) throws Exception { final List users = getMergedList(playerList, groupName); List groupUsers = playerList.get(groupName); if (groupUsers != null && !groupUsers.isEmpty()) { users.addAll(groupUsers); } if (users == null || users.isEmpty()) { throw new Exception(_("groupDoesNotExist")); } return outputFormat(groupName, listUsers(users)); } // Handle the merging of groups private List getMergedList(final Map> playerList, final String groupName) { Set configGroups = ess.getSettings().getListGroupConfig().keySet(); final List users = new ArrayList(); for (String configGroup : configGroups) { if (configGroup.equalsIgnoreCase(groupName)) { String[] groupValues = ess.getSettings().getListGroupConfig().get(configGroup).toString().trim().split(" "); for (String groupValue : groupValues) { if (groupValue == null || groupValue.equals("")) { continue; } List u = playerList.get(groupValue.trim()); if (u == null || u.isEmpty()) { continue; } playerList.remove(groupValue); users.addAll(u); } } } return users; } // Output the standard /list output, when no group is specified private void sendGroupedList(CommandSender sender, String commandLabel, Map> playerList) { Set configGroups = ess.getSettings().getListGroupConfig().keySet(); List asterisk = new ArrayList(); // Loop through the custom defined groups and display them for (String configGroup : configGroups) { String groupValue = ess.getSettings().getListGroupConfig().get(configGroup).toString().trim(); configGroup = configGroup.toLowerCase(); // If the group value is an asterisk, then skip it, and handle it later if (groupValue.equals("*")) { asterisk.add(configGroup); continue; } // If the group value is hidden, we don't need to display it if (groupValue.equalsIgnoreCase("hidden")) { playerList.remove(groupValue); continue; } List outputUserList = new ArrayList(); List matchedList = playerList.get(configGroup); // If the group value is an int, then we might need to truncate it if (Util.isInt(groupValue)) { if (matchedList != null && !matchedList.isEmpty()) { playerList.remove(configGroup); outputUserList.addAll(matchedList); int limit = Integer.parseInt(groupValue); if (matchedList.size() > limit) { sender.sendMessage(outputFormat(configGroup, _("groupNumber", matchedList.size(), commandLabel, configGroup))); } else { sender.sendMessage(outputFormat(configGroup, listUsers(outputUserList))); } continue; } } outputUserList = getMergedList(playerList, configGroup); // If we have no users, than we don't need to continue parsing this group if (outputUserList == null || outputUserList.isEmpty()) { continue; } sender.sendMessage(outputFormat(configGroup, listUsers(outputUserList))); } String[] onlineGroups = playerList.keySet().toArray(new String[0]); Arrays.sort(onlineGroups, String.CASE_INSENSITIVE_ORDER); // If we have an asterisk group, then merge all remaining groups if (!asterisk.isEmpty()) { List asteriskUsers = new ArrayList(); for (String onlineGroup : onlineGroups) { asteriskUsers.addAll(playerList.get(onlineGroup)); } for (String key : asterisk) { playerList.put(key, asteriskUsers); } onlineGroups = asterisk.toArray(new String[0]); } // If we have any groups remaining after the custom groups loop through and display them for (String onlineGroup : onlineGroups) { List users = playerList.get(onlineGroup); if (ess.getPermissionsHandler().getName().equals("ConfigPermissions")) { onlineGroup = _("connectedPlayers"); } if (users == null || users.isEmpty()) { continue; } String groupName = users.get(0).getGroup(); sender.sendMessage(outputFormat(groupName, listUsers(users))); } } // Cosmetic list formatting private String listUsers(List users) { final StringBuilder groupString = new StringBuilder(); Collections.sort(users); boolean needComma = false; for (User user : users) { if (needComma) { groupString.append(", "); } needComma = true; if (user.isAfk()) { groupString.append(_("listAfkTag")); } if (user.isHidden()) { groupString.append(_("listHiddenTag")); } user.setDisplayNick(); groupString.append(user.getDisplayName()); groupString.append("§f"); } return groupString.toString(); } // Build the output string private String outputFormat(String group, String message) { final StringBuilder outputString = new StringBuilder(); outputString.append(_("listGroupTag", Util.replaceFormat(group))); outputString.append(message); outputString.setCharAt(0, Character.toTitleCase(outputString.charAt(0))); return outputString.toString(); } }