forked from EssentialsX/Essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEssentialsCommand.java
More file actions
339 lines (297 loc) · 14 KB
/
EssentialsCommand.java
File metadata and controls
339 lines (297 loc) · 14 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.IEssentialsModule;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.FormatUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.ess3.api.IEssentials;
import net.ess3.api.TranslatableException;
import net.ess3.provider.KnownCommandsProvider;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.StringUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.earth2me.essentials.I18n.tlLiteral;
public abstract class EssentialsCommand implements IEssentialsCommand {
/**
* Common time durations (in seconds), for use in tab completion.
*/
public static final List<String> COMMON_DURATIONS = ImmutableList.of("1", "60", "600", "3600", "86400");
/**
* Common date diffs, for use in tab completion
*/
protected static final List<String> COMMON_DATE_DIFFS = ImmutableList.of("1m", "15m", "1h", "3h", "12h", "1d", "1w", "1mo", "1y");
private static final Pattern ARGUMENT_PATTERN = Pattern.compile("([ :>])(([\\[<])[A-Za-z |]+[>\\]])");
private final transient String name;
private final transient Map<String, String> usageStrings = new LinkedHashMap<>();
protected transient IEssentials ess;
protected transient IEssentialsModule module;
protected EssentialsCommand(final String name) {
this.name = name;
int i = 1;
try {
// This is not actually infinite, it will throw an unchecked exception if a resource key is missing
//noinspection InfiniteLoopStatement
while (true) {
final String baseKey = name + "CommandUsage" + i;
addUsageString(tlLiteral(baseKey), baseKey + "Description");
i++;
}
} catch (MissingResourceException ignored) {
}
}
private void addUsageString(final String usage, final String description) {
final StringBuffer buffer = new StringBuffer();
final Matcher matcher = ARGUMENT_PATTERN.matcher(usage);
while (matcher.find()) {
final String color = matcher.group(3).equals("<") ? tlLiteral("commandArgumentRequired") : tlLiteral("commandArgumentOptional");
matcher.appendReplacement(buffer, "$1" + color + matcher.group(2).replace("|", tlLiteral("commandArgumentOr") + "|" + color) + "<reset>");
}
matcher.appendTail(buffer);
usageStrings.put(buffer.toString(), description);
}
@Override
public Map<String, String> getUsageStrings() {
return usageStrings;
}
public static String getFinalArg(final String[] args, final int start) {
final StringBuilder bldr = new StringBuilder();
for (int i = start; i < args.length; i++) {
if (i != start) {
bldr.append(" ");
}
bldr.append(args[i]);
}
return bldr.toString();
}
private boolean canInteractWith(final User interactor, final User interactee) {
return ess.canInteractWith(interactor, interactee);
}
@Override
public void setEssentials(final IEssentials ess) {
this.ess = ess;
}
@Override
public void setEssentialsModule(final IEssentialsModule module) {
this.module = module;
}
@Override
public String getName() {
return name;
}
// Get online players - only show vanished if source has permission
protected User getPlayer(final Server server, final CommandSource sender, final String[] args, final int pos) throws PlayerNotFoundException, NotEnoughArgumentsException {
return getPlayer(server, sender, args, pos, false);
}
protected User getPlayer(final Server server, final CommandSource sender, final String[] args, final int pos, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException {
if (sender.isPlayer()) {
final User user = ess.getUser(sender.getPlayer());
return getPlayer(server, user, args, pos, getOffline);
}
return getPlayer(server, args, pos, true, getOffline);
}
// Get online players - only show vanished if source has permission
protected User getPlayer(final Server server, final CommandSource sender, final String searchTerm) throws PlayerNotFoundException, NotEnoughArgumentsException {
if (sender.isPlayer()) {
final User user = ess.getUser(sender.getPlayer());
return getPlayer(server, user, searchTerm, user.canInteractVanished(), false);
}
return getPlayer(server, searchTerm, true, false);
}
// Get online players - only show vanished if source has permission
protected User getPlayer(final Server server, final User user, final String[] args, final int pos) throws PlayerNotFoundException, NotEnoughArgumentsException {
return getPlayer(server, user, args, pos, false);
}
protected User getPlayer(final Server server, final User user, final String[] args, final int pos, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException {
return getPlayer(server, user, args, pos, user.canInteractVanished(), getOffline);
}
// Get online or offline players, this method allows for raw access
protected User getPlayer(final Server server, final String[] args, final int pos, final boolean getHidden, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException {
return getPlayer(server, null, args, pos, getHidden, getOffline);
}
User getPlayer(final Server server, final User sourceUser, final String[] args, final int pos, final boolean getHidden, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException {
if (args.length <= pos) {
throw new NotEnoughArgumentsException();
}
if (args[pos].isEmpty()) {
throw new PlayerNotFoundException();
}
return getPlayer(server, sourceUser, args[pos], getHidden, getOffline);
}
// Get online or offline players, this method allows for raw access
protected User getPlayer(final Server server, final String searchTerm, final boolean getHidden, final boolean getOffline) throws PlayerNotFoundException {
return getPlayer(server, null, searchTerm, getHidden, getOffline);
}
private User getPlayer(final Server server, final User sourceUser, final String searchTerm, final boolean getHidden, final boolean getOffline) throws PlayerNotFoundException {
return ess.matchUser(server, sourceUser, searchTerm, getHidden, getOffline);
}
@Override
public final void run(final Server server, final User user, final String commandLabel, final Command cmd, final String[] args) throws Exception {
final Trade charge = new Trade(this.getName(), ess);
charge.isAffordableFor(user);
run(server, user, commandLabel, args);
charge.charge(user);
}
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
run(server, user.getSource(), commandLabel, args);
}
@Override
public final void run(final Server server, final CommandSource sender, final String commandLabel, final Command cmd, final String[] args) throws Exception {
run(server, sender, commandLabel, args);
}
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
throw new TranslatableException("onlyPlayers", commandLabel);
}
@Override
public final List<String> tabComplete(final Server server, final User user, final String commandLabel, final Command cmd, final String[] args) {
if (args.length == 0) {
// Shouldn't happen, but bail out early if it does so that args[0] can always be used
return Collections.emptyList();
}
final List<String> options = getTabCompleteOptions(server, user, commandLabel, args);
if (options == null) {
return null;
}
return StringUtil.copyPartialMatches(args[args.length - 1], options, Lists.newArrayList());
}
// Doesn't need to do any starts-with checks
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
return getTabCompleteOptions(server, user.getSource(), commandLabel, args);
}
@Override
public final List<String> tabComplete(final Server server, final CommandSource sender, final String commandLabel, final Command cmd, final String[] args) {
if (args.length == 0) {
// Shouldn't happen, but bail out early if it does so that args[0] can always be used
return Collections.emptyList();
}
final List<String> options = getTabCompleteOptions(server, sender, commandLabel, args);
if (options == null) {
return null;
}
return StringUtil.copyPartialMatches(args[args.length - 1], options, Lists.newArrayList());
}
// Doesn't need to do any starts-with checks
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
// No tab completion results
return getPlayers(sender);
}
boolean canInteractWith(final CommandSource interactor, final User interactee) {
return ess.canInteractWith(interactor, interactee);
}
/**
* Gets a list of all player names that can be seen with by the given CommandSource,
* for tab completion.
*/
protected List<String> getPlayers(final CommandSource interactor) {
final List<String> players = Lists.newArrayList();
for (final User user : ess.getOnlineUsers()) {
if (canInteractWith(interactor, user)) {
players.add(ess.getSettings().changeTabCompleteName() ? FormatUtil.stripFormat(user.getDisplayName()) : user.getName());
}
}
return players;
}
/**
* Gets a list of all player names that can be seen with by the given User,
* for tab completion.
*/
protected List<String> getPlayers(final User interactor) {
return getPlayers(interactor.getSource());
}
/**
* Gets a list of tab-completable items that start with the given name.
* Due to the number of items, this may not return the entire list.
*/
protected List<String> getItems() {
return new ArrayList<>(ess.getItemDb().listNames());
}
/**
* Gets a list of tab-completable items usable for "getMatching".
*/
protected List<String> getMatchingItems(final String arg) {
final List<String> items = Lists.newArrayList("hand", "inventory", "blocks");
if (!arg.isEmpty()) {
// Emphasize the other items if they haven't entered anything yet.
items.addAll(getItems());
}
return items;
}
/**
* Lists all commands.
*/
protected final List<String> getCommands(Server server) {
final Map<String, Command> commandMap = Maps.newHashMap(this.ess.provider(KnownCommandsProvider.class).getKnownCommands());
final List<String> commands = Lists.newArrayListWithCapacity(commandMap.size());
for (final Command command : commandMap.values()) {
if (!(command instanceof PluginIdentifiableCommand)) {
continue;
}
commands.add(command.getName());
}
return commands;
}
/**
* Lists all plugin names
*
* @param server Server instance
* @return List of plugin names
*/
protected final List<String> getPlugins(final Server server) {
final List<String> plugins = Lists.newArrayList();
for (final Plugin p : server.getPluginManager().getPlugins()) {
plugins.add(p.getName());
}
return plugins;
}
/**
* Attempts to tab-complete a command or its arguments.
*/
protected final List<String> tabCompleteCommand(final CommandSource sender, final Server server, final String label, final String[] args, final int index) {
// TODO: Pass this to the real commandmap
final Command command = server.getPluginCommand(label);
if (command == null) {
return Collections.emptyList();
}
final int numArgs = args.length - index - 1;
if (ess.getSettings().isDebug()) {
ess.getLogger().info(numArgs + " " + index + " " + Arrays.toString(args));
}
String[] effectiveArgs = new String[numArgs];
System.arraycopy(args, index, effectiveArgs, 0, numArgs);
if (effectiveArgs.length == 0) {
effectiveArgs = new String[] {""};
}
if (ess.getSettings().isDebug()) {
ess.getLogger().info(command + " -- " + Arrays.toString(effectiveArgs));
}
return command.tabComplete(sender.getSender(), label, effectiveArgs);
}
@Override
public void showError(final CommandSender sender, final Throwable throwable, final String commandLabel) {
ess.showError(new CommandSource((Essentials) ess, sender), throwable, commandLabel);
}
public CompletableFuture<Boolean> getNewExceptionFuture(final CommandSource sender, final String commandLabel) {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
future.exceptionally(e -> {
showError(sender.getSender(), e, commandLabel);
return false;
});
return future;
}
}