1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
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<String, List<User>> 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<String, List<User>> getPlayerLists(final Server server, final boolean showHidden)
{
Map<String, List<User>> playerList = new HashMap<String, List<User>>();
for (Player onlinePlayer : server.getOnlinePlayers())
{
final User onlineUser = ess.getUser(onlinePlayer);
if (onlineUser.isHidden() && !showHidden)
{
continue;
}
final String group = onlineUser.getGroup().toLowerCase();
List<User> list = playerList.get(group);
if (list == null)
{
list = new ArrayList<User>();
playerList.put(group, list);
}
list.add(onlineUser);
}
return playerList;
}
// Output a playerlist of just a single group, /list <groupname>
private String listGroupUsers(final Map<String, List<User>> playerList, final String groupName) throws Exception
{
final List<User> users = getMergedList(playerList, groupName);
List<User> 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<User> getMergedList(final Map<String, List<User>> playerList, final String groupName)
{
Set<String> configGroups = ess.getSettings().getListGroupConfig().keySet();
final List<User> users = new ArrayList<User>();
for (String key : configGroups)
{
if (key.equalsIgnoreCase(groupName))
{
String[] groups = ess.getSettings().getListGroupConfig().get(key).toString().trim().split(" ");
for (String g : groups)
{
if (g == null || g.equals(""))
{
continue;
}
List<User> u = playerList.get(g.trim());
if (u == null || u.isEmpty())
{
continue;
}
playerList.remove(g);
users.addAll(u);
}
}
}
return users;
}
// Output the standard /list output, when no group is specififed
private void sendGroupedList(CommandSender sender, String commandLabel, Map<String, List<User>> playerList)
{
final StringBuilder outputString = new StringBuilder();
Set<String> configGroups = ess.getSettings().getListGroupConfig().keySet();
List<String> asterisk = new ArrayList<String>();
// Loop through the custom defined groups and display them
for (String group : configGroups)
{
String groupValue = ess.getSettings().getListGroupConfig().get(group).toString().trim();
group = group.toLowerCase();
// If the group value is an asterisk, then skip it, and handle it later
if (groupValue.equals("*"))
{
asterisk.add(group);
continue;
}
// If the group value is hidden, we don't need to display it
if (groupValue.equalsIgnoreCase("hidden"))
{
playerList.remove(groupValue);
continue;
}
List<User> users = new ArrayList<User>();
List<User> u = playerList.get(group);
// If the group value is an int, then we might need to truncate it
if (Util.isInt(groupValue))
{
if (u != null && !u.isEmpty())
{
playerList.remove(group);
users.addAll(u);
int limit = Integer.parseInt(groupValue);
if (u.size() > limit)
{
sender.sendMessage(outputFormat(group, _("groupNumber", u.size(), commandLabel, group)));
}
else {
sender.sendMessage(outputFormat(group, listUsers(users)));
}
continue;
}
}
users = getMergedList(playerList, group);
// If we have no users, than we don't need to continue parsing this group
if (users == null || users.isEmpty())
{
continue;
}
sender.sendMessage(outputFormat(group, listUsers(users)));
}
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<User> asteriskUsers = new ArrayList<User>();
for (String group : onlineGroups)
{
asteriskUsers.addAll(playerList.get(group));
}
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 group : onlineGroups)
{
List<User> users = playerList.get(group);
if (ess.getPermissionsHandler().getName().equals("ConfigPermissions"))
{
group = _("connectedPlayers");
}
if (users == null || users.isEmpty())
{
continue;
}
sender.sendMessage(outputFormat(group, listUsers(users)));
}
}
// Cosmetic list formatting
private String listUsers(List<User> users)
{
final StringBuilder groupString = new StringBuilder();
Collections.sort(users);
boolean first = true;
for (User user : users)
{
if (!first)
{
groupString.append(", ");
}
else
{
first = false;
}
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();
}
}
|