|
@@ -46,37 +46,33 @@ import java.util.Map;
|
46
|
46
|
* The command manager creates and manages a single instance of all commands,
|
47
|
47
|
* and provides methods to load each group of commands into a parser instance.
|
48
|
48
|
*/
|
49
|
|
-public final class CommandManager {
|
|
49
|
+public class CommandManager {
|
|
50
|
+
|
|
51
|
+ /** A singleton instance of the command manager. */
|
|
52
|
+ private static final CommandManager INSTANCE = new CommandManager();
|
50
|
53
|
|
51
|
54
|
/** A list of commands that have been instantiated. */
|
52
|
|
- private static final Map<CommandInfo, Command> COMMANDS
|
|
55
|
+ private final Map<CommandInfo, Command> commands
|
53
|
56
|
= new HashMap<CommandInfo, Command>();
|
54
|
57
|
|
55
|
58
|
/** A list of command parsers that have been instantiated. */
|
56
|
|
- private static final MapList<CommandType, CommandParser> PARSERS
|
|
59
|
+ private final MapList<CommandType, CommandParser> parsers
|
57
|
60
|
= new MapList<CommandType, CommandParser>();
|
58
|
61
|
|
59
|
62
|
/** The command char we're using. */
|
60
|
|
- private static char commandChar = IdentityManager.getGlobalConfig()
|
|
63
|
+ private char commandChar = IdentityManager.getGlobalConfig()
|
61
|
64
|
.getOptionChar("general", "commandchar");
|
62
|
65
|
|
63
|
66
|
/** The silence char we're using. */
|
64
|
|
- private static char silenceChar = IdentityManager.getGlobalConfig()
|
|
67
|
+ private char silenceChar = IdentityManager.getGlobalConfig()
|
65
|
68
|
.getOptionChar("general", "silencechar");
|
66
|
69
|
|
67
|
|
- /**
|
68
|
|
- * Prevents creation of a new command manager.
|
69
|
|
- */
|
70
|
|
- private CommandManager() {
|
71
|
|
- // Do nothing
|
72
|
|
- }
|
73
|
|
-
|
74
|
70
|
/**
|
75
|
71
|
* Returns the current command character.
|
76
|
72
|
*
|
77
|
73
|
* @return the current command char
|
78
|
74
|
*/
|
79
|
|
- public static char getCommandChar() {
|
|
75
|
+ public char getCommandChar() {
|
80
|
76
|
return commandChar;
|
81
|
77
|
}
|
82
|
78
|
|
|
@@ -85,7 +81,7 @@ public final class CommandManager {
|
85
|
81
|
*
|
86
|
82
|
* @return the current silence char
|
87
|
83
|
*/
|
88
|
|
- public static char getSilenceChar() {
|
|
84
|
+ public char getSilenceChar() {
|
89
|
85
|
return silenceChar;
|
90
|
86
|
}
|
91
|
87
|
|
|
@@ -96,7 +92,7 @@ public final class CommandManager {
|
96
|
92
|
* @param info The information about the command
|
97
|
93
|
* @since 0.6.3m1
|
98
|
94
|
*/
|
99
|
|
- public static void registerCommand(final Command command, final CommandInfo info) {
|
|
95
|
+ public void registerCommand(final Command command, final CommandInfo info) {
|
100
|
96
|
registerCommand(info, command, true);
|
101
|
97
|
}
|
102
|
98
|
|
|
@@ -109,7 +105,7 @@ public final class CommandManager {
|
109
|
105
|
* {@link CommandInfo} to be registered.
|
110
|
106
|
* @since 0.6.3m1
|
111
|
107
|
*/
|
112
|
|
- public static <T extends Command & CommandInfo> void registerCommand(final T command) {
|
|
108
|
+ public <T extends Command & CommandInfo> void registerCommand(final T command) {
|
113
|
109
|
registerCommand(command, command);
|
114
|
110
|
}
|
115
|
111
|
|
|
@@ -119,8 +115,8 @@ public final class CommandManager {
|
119
|
115
|
* @param info The information object for the command that should be unregistered
|
120
|
116
|
* @since 0.6.3m1
|
121
|
117
|
*/
|
122
|
|
- public static void unregisterCommand(final CommandInfo info) {
|
123
|
|
- registerCommand(info, COMMANDS.get(info), false);
|
|
118
|
+ public void unregisterCommand(final CommandInfo info) {
|
|
119
|
+ registerCommand(info, commands.get(info), false);
|
124
|
120
|
}
|
125
|
121
|
|
126
|
122
|
/**
|
|
@@ -132,16 +128,16 @@ public final class CommandManager {
|
132
|
128
|
* should be unregistered.
|
133
|
129
|
* @since 0.6.3m1
|
134
|
130
|
*/
|
135
|
|
- private static void registerCommand(final CommandInfo info, final Command command,
|
|
131
|
+ private void registerCommand(final CommandInfo info, final Command command,
|
136
|
132
|
final boolean register) {
|
137
|
|
- if (PARSERS.containsKey(info.getType())) {
|
138
|
|
- registerCommand(info, command, PARSERS.get(info.getType()), register);
|
|
133
|
+ if (parsers.containsKey(info.getType())) {
|
|
134
|
+ registerCommand(info, command, parsers.get(info.getType()), register);
|
139
|
135
|
}
|
140
|
136
|
|
141
|
137
|
if (register) {
|
142
|
|
- COMMANDS.put(info, command);
|
|
138
|
+ commands.put(info, command);
|
143
|
139
|
} else {
|
144
|
|
- COMMANDS.remove(info);
|
|
140
|
+ commands.remove(info);
|
145
|
141
|
}
|
146
|
142
|
|
147
|
143
|
registerCommandName(info, register);
|
|
@@ -156,7 +152,7 @@ public final class CommandManager {
|
156
|
152
|
* @param register Whether to register or unregister the commands
|
157
|
153
|
* @since 0.6.3m1
|
158
|
154
|
*/
|
159
|
|
- private static void registerCommand(final CommandInfo info, final Command command,
|
|
155
|
+ private void registerCommand(final CommandInfo info, final Command command,
|
160
|
156
|
final List<? extends CommandParser> myParsers, final boolean register) {
|
161
|
157
|
for (CommandParser parser : myParsers) {
|
162
|
158
|
if (register) {
|
|
@@ -176,7 +172,7 @@ public final class CommandManager {
|
176
|
172
|
* should be unregistered.
|
177
|
173
|
* @since 0.6.3m1
|
178
|
174
|
*/
|
179
|
|
- private static void registerCommandName(final CommandInfo command,
|
|
175
|
+ private void registerCommandName(final CommandInfo command,
|
180
|
176
|
final boolean register) {
|
181
|
177
|
// Do tab completion
|
182
|
178
|
final String commandName = getCommandChar() + command.getName();
|
|
@@ -215,7 +211,7 @@ public final class CommandManager {
|
215
|
211
|
* @param register True if the command should be registered, false if it
|
216
|
212
|
* should be unregistered.
|
217
|
213
|
*/
|
218
|
|
- private static void registerCommandName(final TabCompleter completer,
|
|
214
|
+ private void registerCommandName(final TabCompleter completer,
|
219
|
215
|
final String name, final boolean register) {
|
220
|
216
|
if (register) {
|
221
|
217
|
completer.addEntry(TabCompletionType.COMMAND, name);
|
|
@@ -227,7 +223,7 @@ public final class CommandManager {
|
227
|
223
|
/**
|
228
|
224
|
* Instansiates the default commands.
|
229
|
225
|
*/
|
230
|
|
- public static void initCommands() {
|
|
226
|
+ public void initCommands() {
|
231
|
227
|
// Chat commands
|
232
|
228
|
registerCommand(new Me());
|
233
|
229
|
|
|
@@ -313,7 +309,7 @@ public final class CommandManager {
|
313
|
309
|
* @param parser The {@link CommandParser} to load commands in to
|
314
|
310
|
* @param supertypes The types of commands that should be loaded
|
315
|
311
|
*/
|
316
|
|
- public static void loadCommands(final CommandParser parser,
|
|
312
|
+ public void loadCommands(final CommandParser parser,
|
317
|
313
|
final CommandType ... supertypes) {
|
318
|
314
|
for (CommandType supertype : supertypes) {
|
319
|
315
|
for (CommandType type : supertype.getComponentTypes()) {
|
|
@@ -321,7 +317,7 @@ public final class CommandManager {
|
321
|
317
|
parser.registerCommand(pair.getValue(), pair.getKey());
|
322
|
318
|
}
|
323
|
319
|
|
324
|
|
- PARSERS.add(type, parser);
|
|
320
|
+ parsers.add(type, parser);
|
325
|
321
|
}
|
326
|
322
|
}
|
327
|
323
|
}
|
|
@@ -333,7 +329,7 @@ public final class CommandManager {
|
333
|
329
|
* @param name The name to look for
|
334
|
330
|
* @return A command with a matching signature, or null if none were found
|
335
|
331
|
*/
|
336
|
|
- public static Map.Entry<CommandInfo, Command> getCommand(final String name) {
|
|
332
|
+ public Map.Entry<CommandInfo, Command> getCommand(final String name) {
|
337
|
333
|
return getCommand(null, name);
|
338
|
334
|
}
|
339
|
335
|
|
|
@@ -344,7 +340,7 @@ public final class CommandManager {
|
344
|
340
|
* @param name The name to look for
|
345
|
341
|
* @return A command with a matching signature, or null if none were found
|
346
|
342
|
*/
|
347
|
|
- public static Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
|
|
343
|
+ public Map.Entry<CommandInfo, Command> getCommand(final CommandType type,
|
348
|
344
|
final String name) {
|
349
|
345
|
final Map<CommandInfo, Command> res = getCommands(type, name);
|
350
|
346
|
|
|
@@ -357,7 +353,7 @@ public final class CommandManager {
|
357
|
353
|
* @param command The name of the command to test
|
358
|
354
|
* @return True iff the command is a channel command, false otherwise
|
359
|
355
|
*/
|
360
|
|
- public static boolean isChannelCommand(final String command) {
|
|
356
|
+ public boolean isChannelCommand(final String command) {
|
361
|
357
|
return getCommand(CommandType.TYPE_CHANNEL, command) != null
|
362
|
358
|
|| getCommand(CommandType.TYPE_CHAT, command) != null;
|
363
|
359
|
}
|
|
@@ -368,7 +364,7 @@ public final class CommandManager {
|
368
|
364
|
* @param type The type of command to list
|
369
|
365
|
* @return A list of command names
|
370
|
366
|
*/
|
371
|
|
- public static List<String> getCommandNames(final CommandType type) {
|
|
367
|
+ public List<String> getCommandNames(final CommandType type) {
|
372
|
368
|
final List<String> res = new ArrayList<String>();
|
373
|
369
|
|
374
|
370
|
for (CommandInfo command : getCommands(type).keySet()) {
|
|
@@ -386,7 +382,7 @@ public final class CommandManager {
|
386
|
382
|
* @return A map of commands
|
387
|
383
|
* @since 0.6.3m1
|
388
|
384
|
*/
|
389
|
|
- public static Map<CommandInfo, Command> getCommands(final CommandType type) {
|
|
385
|
+ public Map<CommandInfo, Command> getCommands(final CommandType type) {
|
390
|
386
|
return getCommands(type, null);
|
391
|
387
|
}
|
392
|
388
|
|
|
@@ -399,11 +395,11 @@ public final class CommandManager {
|
399
|
395
|
* @return A map of {@link CommandInfo}s and their associated {@link Command}.
|
400
|
396
|
* @since 0.6.3m1
|
401
|
397
|
*/
|
402
|
|
- private static Map<CommandInfo, Command> getCommands(final CommandType type,
|
|
398
|
+ private Map<CommandInfo, Command> getCommands(final CommandType type,
|
403
|
399
|
final String name) {
|
404
|
400
|
final Map<CommandInfo, Command> res = new HashMap<CommandInfo, Command>();
|
405
|
401
|
|
406
|
|
- for (Map.Entry<CommandInfo, Command> entry : COMMANDS.entrySet()) {
|
|
402
|
+ for (Map.Entry<CommandInfo, Command> entry : commands.entrySet()) {
|
407
|
403
|
if ((type == null || type.equals(entry.getKey().getType()))
|
408
|
404
|
&& (name == null || name.equals(entry.getKey().getName()))) {
|
409
|
405
|
res.put(entry.getKey(), entry.getValue());
|
|
@@ -413,4 +409,13 @@ public final class CommandManager {
|
413
|
409
|
return res;
|
414
|
410
|
}
|
415
|
411
|
|
|
412
|
+ /**
|
|
413
|
+ * Retrieves a singleton instance of the CommandManager.
|
|
414
|
+ *
|
|
415
|
+ * @return A singleton instance of the CommandManager.
|
|
416
|
+ */
|
|
417
|
+ public static CommandManager getCommandManager() {
|
|
418
|
+ return INSTANCE;
|
|
419
|
+ }
|
|
420
|
+
|
416
|
421
|
}
|