Essentials/Essentials/src/main/java/com/earth2me/essentials/PlayerList.java

129 lines
5.3 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
import com.earth2me.essentials.utils.FormatUtil;
import org.bukkit.ChatColor;
import org.bukkit.Server;
2015-04-15 06:06:16 +02:00
2020-10-03 19:46:05 +02:00
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;
2015-04-15 06:06:16 +02:00
import static com.earth2me.essentials.I18n.tl;
2020-10-03 19:46:05 +02:00
public final class PlayerList {
private PlayerList() {
}
2015-04-15 06:06:16 +02:00
// Cosmetic list formatting
public static String listUsers(final IEssentials ess, final List<User> users, final String seperator) {
final StringBuilder groupString = new StringBuilder();
Collections.sort(users);
boolean needComma = false;
2020-10-03 19:46:05 +02:00
for (final User user : users) {
2015-04-15 06:06:16 +02:00
if (needComma) {
groupString.append(seperator);
}
needComma = true;
if (user.isAfk()) {
groupString.append(tl("listAfkTag"));
}
if (user.isHidden()) {
groupString.append(tl("listHiddenTag"));
}
user.setDisplayNick();
groupString.append(user.getDisplayName());
if (ess.getSettings().realNamesOnList() && !ChatColor.stripColor(user.getDisplayName()).equals(user.getName())) {
groupString.append(" (").append(user.getName()).append(")");
}
groupString.append(ChatColor.WHITE.toString());
2015-04-15 06:06:16 +02:00
}
return groupString.toString();
}
2015-04-15 06:06:16 +02:00
// Produce a user summary: There are 5 out of maximum 10 players online.
public static String listSummary(final IEssentials ess, final User user, final boolean showHidden) {
2020-10-03 19:46:05 +02:00
final Server server = ess.getServer();
2015-04-15 06:06:16 +02:00
int playerHidden = 0;
int hiddenCount = 0;
2020-10-03 19:46:05 +02:00
for (final User onlinePlayer : ess.getOnlineUsers()) {
2015-04-15 06:06:16 +02:00
if (onlinePlayer.isHidden() || (user != null && !user.getBase().canSee(onlinePlayer.getBase()))) {
playerHidden++;
2019-12-31 17:49:46 +01:00
if (showHidden || user != null && user.getBase().canSee(onlinePlayer.getBase())) {
2015-04-15 06:06:16 +02:00
hiddenCount++;
}
}
}
2020-10-03 19:46:05 +02:00
final String online;
2015-04-15 06:06:16 +02:00
if (hiddenCount > 0) {
online = tl("listAmountHidden", ess.getOnlinePlayers().size() - playerHidden, hiddenCount, server.getMaxPlayers());
} else {
online = tl("listAmount", ess.getOnlinePlayers().size() - playerHidden, server.getMaxPlayers());
}
return online;
}
2015-04-15 06:06:16 +02:00
// Build the basic player list, divided by groups.
public static Map<String, List<User>> getPlayerLists(final IEssentials ess, final IUser sender, final boolean showHidden) {
2019-12-31 17:49:46 +01:00
final Map<String, List<User>> playerList = new HashMap<>();
2020-10-03 19:46:05 +02:00
for (final User onlineUser : ess.getOnlineUsers()) {
2015-04-15 06:06:16 +02:00
if ((sender == null && !showHidden && onlineUser.isHidden()) || (sender != null && !showHidden && !sender.getBase().canSee(onlineUser.getBase()))) {
continue;
}
final String group = FormatUtil.stripFormat(FormatUtil.stripEssentialsFormat(onlineUser.getGroup().toLowerCase()));
2020-10-03 19:46:05 +02:00
final List<User> list = playerList.computeIfAbsent(group, k -> new ArrayList<>());
2015-04-15 06:06:16 +02:00
list.add(onlineUser);
}
return playerList;
}
2015-04-15 06:06:16 +02:00
// Handle the merging of groups
public static List<User> getMergedList(final IEssentials ess, final Map<String, List<User>> playerList, final String groupName) {
final Set<String> configGroups = ess.getSettings().getListGroupConfig().keySet();
2019-12-31 17:49:46 +01:00
final List<User> users = new ArrayList<>();
2020-10-03 19:46:05 +02:00
for (final String configGroup : configGroups) {
2015-04-15 06:06:16 +02:00
if (configGroup.equalsIgnoreCase(groupName)) {
2020-10-03 19:46:05 +02:00
final String[] groupValues = ess.getSettings().getListGroupConfig().get(configGroup).toString().trim().split(" ");
2015-04-15 06:06:16 +02:00
for (String groupValue : groupValues) {
groupValue = groupValue.toLowerCase(Locale.ENGLISH);
if (groupValue.isEmpty()) {
2015-04-15 06:06:16 +02:00
continue;
}
2020-10-03 19:46:05 +02:00
final List<User> u = playerList.get(groupValue.trim());
2015-04-15 06:06:16 +02:00
if (u == null || u.isEmpty()) {
continue;
}
playerList.remove(groupValue);
users.addAll(u);
}
}
}
return users;
}
2015-04-15 06:06:16 +02:00
// Output a playerlist of just a single group, /list <groupname>
public static String listGroupUsers(final IEssentials ess, final Map<String, List<User>> playerList, final String groupName) throws Exception {
final List<User> users = getMergedList(ess, playerList, groupName);
final List<User> groupUsers = playerList.get(groupName);
if (groupUsers != null && !groupUsers.isEmpty()) {
users.addAll(groupUsers);
}
if (users.isEmpty()) {
2015-04-15 06:06:16 +02:00
throw new Exception(tl("groupDoesNotExist"));
}
2020-10-03 19:46:05 +02:00
final String displayGroupName = Character.toTitleCase(groupName.charAt(0)) +
groupName.substring(1);
return outputFormat(displayGroupName, listUsers(ess, users, ", "));
2015-04-15 06:06:16 +02:00
}
2015-04-15 06:06:16 +02:00
// Build the output string
public static String outputFormat(final String group, final String message) {
return tl("listGroupTag", FormatUtil.replaceFormat(group)) +
message;
2015-04-15 06:06:16 +02:00
}
}