From cf79daddde6a081092edaf1b31a937f7376c252d Mon Sep 17 00:00:00 2001 From: KHobbits Date: Tue, 20 Aug 2013 23:39:34 +0100 Subject: Add support for {PLAYERLIST:GROUP} AND {PLAYERLIST:GROUP:} Replace with what you want to show if there are no players from that group online. --- .../src/com/earth2me/essentials/PlayerList.java | 151 ++++++++++++++++++++ .../earth2me/essentials/commands/Commandlist.java | 154 ++------------------- .../essentials/textreader/KeywordReplacer.java | 21 +++ 3 files changed, 181 insertions(+), 145 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/PlayerList.java diff --git a/Essentials/src/com/earth2me/essentials/PlayerList.java b/Essentials/src/com/earth2me/essentials/PlayerList.java new file mode 100644 index 000000000..0b6c3dc49 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/PlayerList.java @@ -0,0 +1,151 @@ +package com.earth2me.essentials; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.utils.FormatUtil; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import org.bukkit.Server; +import org.bukkit.entity.Player; + + +public class PlayerList +{ + // Cosmetic list formatting + static public String listUsers(final IEssentials ess, final List users, final String seperator) + { + final StringBuilder groupString = new StringBuilder(); + Collections.sort(users); + boolean needComma = false; + for (User user : users) + { + if (needComma) + { + groupString.append(seperator); + } + needComma = true; + if (user.isAfk()) + { + groupString.append(_("listAfkTag")); + } + if (user.isHidden()) + { + groupString.append(_("listHiddenTag")); + } + user.setDisplayNick(); + groupString.append(user.getDisplayName()); + groupString.append("\u00a7f"); + } + return groupString.toString(); + } + + // Produce a user summary: There are 5 out of maximum 10 players online. + static public String listSummary(final IEssentials ess, final boolean showHidden) + { + Server server = ess.getServer(); + 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. + static public Map> getPlayerLists(final IEssentials ess, final boolean showHidden) + { + Server server = ess.getServer(); + final Map> playerList = new HashMap>(); + for (Player onlinePlayer : server.getOnlinePlayers()) + { + final User onlineUser = ess.getUser(onlinePlayer); + if (onlineUser.isHidden() && !showHidden) + { + continue; + } + final String group = FormatUtil.stripFormat(FormatUtil.stripEssentialsFormat(onlineUser.getGroup().toLowerCase())); + List list = playerList.get(group); + if (list == null) + { + list = new ArrayList(); + playerList.put(group, list); + } + list.add(onlineUser); + } + return playerList; + } + + // Handle the merging of groups + static public List getMergedList(final IEssentials ess, final Map> playerList, final String groupName) + { + final 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) + { + groupValue = groupValue.toLowerCase(Locale.ENGLISH); + 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 a playerlist of just a single group, /list + static public String listGroupUsers(final IEssentials ess, final Map> playerList, final String groupName) throws Exception + { + final List users = getMergedList(ess, playerList, groupName); + final List groupUsers = playerList.get(groupName); + if (groupUsers != null && !groupUsers.isEmpty()) + { + users.addAll(groupUsers); + } + if (users == null || users.isEmpty()) + { + throw new Exception(_("groupDoesNotExist")); + } + final StringBuilder displayGroupName = new StringBuilder(); + displayGroupName.append(Character.toTitleCase(groupName.charAt(0))); + displayGroupName.append(groupName.substring(1)); + return outputFormat(displayGroupName.toString(), listUsers(ess, users, ", ")); + } + + // Build the output string + static public String outputFormat(final String group, final String message) + { + final StringBuilder outputString = new StringBuilder(); + outputString.append(_("listGroupTag", FormatUtil.replaceFormat(group))); + outputString.append(message); + return outputString.toString(); + } +} diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandlist.java b/Essentials/src/com/earth2me/essentials/commands/Commandlist.java index 18fa0a673..ef7911d12 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandlist.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandlist.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.PlayerList; import com.earth2me.essentials.User; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.NumberUtil; @@ -25,13 +26,12 @@ public class Commandlist extends EssentialsCommand { showHidden = ess.getUser(sender).isAuthorized("essentials.list.hidden") || ess.getUser(sender).isAuthorized("essentials.vanish.interact"); } - - sender.sendMessage(listSummary(server, showHidden)); - final Map> playerList = getPlayerLists(server, showHidden); + sender.sendMessage(PlayerList.listSummary(ess, showHidden)); + final Map> playerList = PlayerList.getPlayerLists(ess, showHidden); if (args.length > 0) { - sender.sendMessage(listGroupUsers(playerList, args[0].toLowerCase())); + sender.sendMessage(PlayerList.listGroupUsers(ess, playerList, args[0].toLowerCase())); } else { @@ -39,105 +39,6 @@ public class Commandlist extends EssentialsCommand } } - // 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) - { - final Map> playerList = new HashMap>(); - for (Player onlinePlayer : server.getOnlinePlayers()) - { - final User onlineUser = ess.getUser(onlinePlayer); - if (onlineUser.isHidden() && !showHidden) - { - continue; - } - final String group = FormatUtil.stripFormat(FormatUtil.stripEssentialsFormat(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); - - final List groupUsers = playerList.get(groupName); - if (groupUsers != null && !groupUsers.isEmpty()) - { - users.addAll(groupUsers); - } - if (users == null || users.isEmpty()) - { - throw new Exception(_("groupDoesNotExist")); - } - - final StringBuilder displayGroupName = new StringBuilder(); - displayGroupName.append(Character.toTitleCase(groupName.charAt(0))); - displayGroupName.append(groupName.substring(1)); - return outputFormat(displayGroupName.toString(), listUsers(users)); - } - - // Handle the merging of groups - private List getMergedList(final Map> playerList, final String groupName) - { - final 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) - { - groupValue = groupValue.toLowerCase(Locale.ENGLISH); - 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) { @@ -177,17 +78,17 @@ public class Commandlist extends EssentialsCommand int limit = Integer.parseInt(groupValue); if (matchedList.size() > limit) { - sender.sendMessage(outputFormat(oConfigGroup, _("groupNumber", matchedList.size(), commandLabel, FormatUtil.stripFormat(configGroup)))); + sender.sendMessage(PlayerList.outputFormat(oConfigGroup, _("groupNumber", matchedList.size(), commandLabel, FormatUtil.stripFormat(configGroup)))); } else { - sender.sendMessage(outputFormat(oConfigGroup, listUsers(outputUserList))); + sender.sendMessage(PlayerList.outputFormat(oConfigGroup, PlayerList.listUsers(ess, outputUserList, ", "))); } continue; } } - outputUserList = getMergedList(playerList, configGroup); + outputUserList = PlayerList.getMergedList(ess, playerList, configGroup); // If we have no users, than we don't need to continue parsing this group if (outputUserList == null || outputUserList.isEmpty()) @@ -195,7 +96,7 @@ public class Commandlist extends EssentialsCommand continue; } - sender.sendMessage(outputFormat(oConfigGroup, listUsers(outputUserList))); + sender.sendMessage(PlayerList.outputFormat(oConfigGroup, PlayerList.listUsers(ess, outputUserList, ", "))); } String[] onlineGroups = playerList.keySet().toArray(new String[0]); @@ -231,44 +132,7 @@ public class Commandlist extends EssentialsCommand continue; } - sender.sendMessage(outputFormat(groupName, listUsers(users))); + sender.sendMessage(PlayerList.outputFormat(groupName, PlayerList.listUsers(ess, 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", FormatUtil.replaceFormat(group))); - outputString.append(message); - return outputString.toString(); - } } diff --git a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java b/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java index 0fb8ddeb0..b1c45a2a1 100644 --- a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java +++ b/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java @@ -18,6 +18,8 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.PlayerList; +import static com.earth2me.essentials.PlayerList.getMergedList; public class KeywordReplacer implements IText @@ -72,6 +74,7 @@ public class KeywordReplacer implements IText { displayName = address = ipAddress = balance = mails = world = worldTime12 = worldTime24 = worldDate = coords = ""; } + Map> playerList = PlayerList.getPlayerLists(ess, extended); userName = sender.getName(); int playerHidden = 0; @@ -160,6 +163,24 @@ public class KeywordReplacer implements IText line = line.replace("{PLUGINS}", plugins); line = line.replace("{VERSION}", version); } + + for (String groupName : playerList.keySet()) { + final List groupUsers = playerList.get(groupName); + if (groupUsers != null && !groupUsers.isEmpty()) + { + line = line.replaceAll("\\{PLAYERLIST\\:" + groupName.toUpperCase() + "(?:\\:([^\\{\\}]*))?\\}", + PlayerList.listUsers(ess, groupUsers, " ")); + } + } + + boolean doReplace = true; + while (doReplace) { + final String newLine = line.replaceAll("\\{PLAYERLIST\\:\\w*(?:\\:([^\\{\\}]*))?\\}", "$1"); + if (newLine.equals(line)) { + doReplace = false; + } + line = newLine; + } replaced.add(line); } -- cgit v1.2.3