-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathCommand.java
More file actions
326 lines (254 loc) · 9.94 KB
/
Command.java
File metadata and controls
326 lines (254 loc) · 9.94 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
package cn.nukkit.command;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.command.data.*;
import cn.nukkit.lang.TextContainer;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.permission.Permissible;
import cn.nukkit.utils.TextFormat;
import co.aikar.timings.Timing;
import co.aikar.timings.Timings;
import java.util.*;
/**
* author: MagicDroidX
* Nukkit Project
*/
public abstract class Command {
private static CommandData defaultDataTemplate = null;
protected CommandData commandData;
private final String name;
private String nextLabel;
private String label;
private String[] aliases = new String[0];
private String[] activeAliases = new String[0];
private CommandMap commandMap = null;
protected String description = "";
protected String usageMessage = "";
private String permission = null;
private String permissionMessage = null;
protected Map<String, CommandParameter[]> commandParameters = new HashMap<>();
public Timing timing;
public Command(String name) {
this(name, "", null, new String[0]);
}
public Command(String name, String description) {
this(name, description, null, new String[0]);
}
public Command(String name, String description, String usageMessage) {
this(name, description, usageMessage, new String[0]);
}
public Command(String name, String description, String usageMessage, String[] aliases) {
this.commandData = new CommandData();
this.name = name.toLowerCase(); // Uppercase letters crash the client?!?
this.nextLabel = name;
this.label = name;
this.description = description;
this.usageMessage = usageMessage == null ? "/" + name : usageMessage;
this.aliases = aliases;
this.activeAliases = aliases;
this.timing = Timings.getCommandTiming(this);
this.commandParameters.put("default", new CommandParameter[]{CommandParameter.newType("args", true, CommandParamType.RAWTEXT)});
}
/**
* Returns an CommandData containing command data
*
* @return CommandData
*/
public CommandData getDefaultCommandData() {
return this.commandData;
}
public CommandParameter[] getCommandParameters(String key) {
return commandParameters.get(key);
}
public Map<String, CommandParameter[]> getCommandParameters() {
return commandParameters;
}
public void setCommandParameters(Map<String, CommandParameter[]> commandParameters) {
this.commandParameters = commandParameters;
}
public void addCommandParameters(String key, CommandParameter[] parameters) {
this.commandParameters.put(key, parameters);
}
/**
* Generates modified command data for the specified player
* for AvailableCommandsPacket.
*
* @param player player
* @return CommandData|null
*/
public CommandDataVersions generateCustomCommandData(Player player) {
if (!this.testPermission(player)) {
return null;
}
CommandData customData = this.commandData.clone();
if (getAliases().length > 0) {
List<String> aliases = new ArrayList<>(Arrays.asList(getAliases()));
if (!aliases.contains(this.name)) {
aliases.add(this.name);
}
customData.aliases = new CommandEnum(this.name + "Aliases", aliases);
}
customData.description = player.getServer().getLanguage().translateString(this.getDescription());
this.commandParameters.forEach((key, par) -> {
CommandOverload overload = new CommandOverload();
overload.input.parameters = par;
customData.overloads.put(key, overload);
});
if (customData.overloads.size() == 0) customData.overloads.put("default", new CommandOverload());
CommandDataVersions versions = new CommandDataVersions();
versions.versions.add(customData);
return versions;
}
public Map<String, CommandOverload> getOverloads() {
return this.commandData.overloads;
}
public abstract boolean execute(CommandSender sender, String commandLabel, String[] args);
public String getName() {
return name;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public boolean testPermission(CommandSender target) {
if (this.testPermissionSilent(target)) {
return true;
}
if (this.permissionMessage == null) {
target.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.unknown", this.name));
} else if (!this.permissionMessage.equals("")) {
target.sendMessage(this.permissionMessage.replace("<permission>", this.permission));
}
return false;
}
public boolean testPermissionSilent(CommandSender target) {
if (this.permission == null || this.permission.equals("")) {
return true;
}
String[] permissions = this.permission.split(";");
for (String permission : permissions) {
if (target.hasPermission(permission)) {
return true;
}
}
return false;
}
public String getLabel() {
return label;
}
public boolean setLabel(String name) {
this.nextLabel = name;
if (!this.isRegistered()) {
this.label = name;
this.timing = Timings.getCommandTiming(this);
return true;
}
return false;
}
public boolean register(CommandMap commandMap) {
if (this.allowChangesFrom(commandMap)) {
this.commandMap = commandMap;
return true;
}
return false;
}
public boolean unregister(CommandMap commandMap) {
if (this.allowChangesFrom(commandMap)) {
this.commandMap = null;
this.activeAliases = this.aliases;
this.label = this.nextLabel;
return true;
}
return false;
}
public boolean allowChangesFrom(CommandMap commandMap) {
return this.commandMap == null || this.commandMap.equals(commandMap);
}
public boolean isRegistered() {
return this.commandMap != null;
}
public String[] getAliases() {
return this.activeAliases;
}
public String getPermissionMessage() {
return permissionMessage;
}
public String getDescription() {
return description;
}
public String getUsage() {
return usageMessage;
}
public void setAliases(String[] aliases) {
this.aliases = aliases;
if (!this.isRegistered()) {
this.activeAliases = aliases;
}
}
public void setDescription(String description) {
this.description = description;
}
public void setPermissionMessage(String permissionMessage) {
this.permissionMessage = permissionMessage;
}
public void setUsage(String usageMessage) {
this.usageMessage = usageMessage;
}
public static CommandData generateDefaultData() {
if (defaultDataTemplate == null) {
//defaultDataTemplate = new Gson().fromJson(new InputStreamReader(Server.class.getClassLoader().getResourceAsStream("command_default.json")));
}
return defaultDataTemplate.clone();
}
public static void broadcastCommandMessage(CommandSender source, String message) {
broadcastCommandMessage(source, message, true);
}
public static void broadcastCommandMessage(CommandSender source, String message, boolean sendToSource) {
Set<Permissible> users = source.getServer().getPluginManager().getPermissionSubscriptions(Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
TranslationContainer result = new TranslationContainer("chat.type.admin", source.getName(), message);
TranslationContainer colored = new TranslationContainer(TextFormat.GRAY + "" + TextFormat.ITALIC + "%chat.type.admin", source.getName(), message);
if (sendToSource && !(source instanceof ConsoleCommandSender)) {
source.sendMessage(message);
}
for (Permissible user : users) {
if (user instanceof CommandSender) {
if (user instanceof ConsoleCommandSender) {
((ConsoleCommandSender) user).sendMessage(result);
} else if (!user.equals(source)) {
((CommandSender) user).sendMessage(colored);
}
}
}
}
public static void broadcastCommandMessage(CommandSender source, TextContainer message) {
broadcastCommandMessage(source, message, true);
}
public static void broadcastCommandMessage(CommandSender source, TextContainer message, boolean sendToSource) {
TextContainer m = message.clone();
String resultStr = "[" + source.getName() + ": " + (!m.getText().equals(source.getServer().getLanguage().get(m.getText())) ? "%" : "") + m.getText() + "]";
Set<Permissible> users = source.getServer().getPluginManager().getPermissionSubscriptions(Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
String coloredStr = TextFormat.GRAY + "" + TextFormat.ITALIC + resultStr;
m.setText(resultStr);
TextContainer result = m.clone();
m.setText(coloredStr);
TextContainer colored = m.clone();
if (sendToSource && !(source instanceof ConsoleCommandSender)) {
source.sendMessage(message);
}
for (Permissible user : users) {
if (user instanceof CommandSender) {
if (user instanceof ConsoleCommandSender) {
((ConsoleCommandSender) user).sendMessage(result);
} else if (!user.equals(source)) {
((CommandSender) user).sendMessage(colored);
}
}
}
}
@Override
public String toString() {
return this.name;
}
}