-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CommandRegistry.java
159 lines (144 loc) · 7.14 KB
/
CommandRegistry.java
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
package com.denizenscript.denizencore.scripts.commands;
import com.denizenscript.denizencore.scripts.commands.core.*;
import com.denizenscript.denizencore.scripts.commands.file.LogCommand;
import com.denizenscript.denizencore.scripts.commands.queue.*;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.scripts.commands.file.FileCopyCommand;
import com.denizenscript.denizencore.scripts.commands.file.YamlCommand;
import java.util.HashMap;
import java.util.Map;
public abstract class CommandRegistry {
public CommandRegistry() {
}
public static final DebugInvalidCommand debugInvalidCommand = new DebugInvalidCommand();
public final Map<String, AbstractCommand> instances = new HashMap<>();
public final Map<Class<? extends AbstractCommand>, String> classes = new HashMap<>();
public void register(String commandName, AbstractCommand commandInstance) {
this.instances.put(CoreUtilities.toLowerCase(commandName), commandInstance);
this.classes.put(((AbstractCommand) commandInstance).getClass(), CoreUtilities.toLowerCase(commandName));
}
public Map<String, AbstractCommand> list() {
return instances;
}
public AbstractCommand get(String commandName) {
return instances.get(CoreUtilities.toLowerCase(commandName));
}
public <T extends AbstractCommand> T get(Class<T> clazz) {
String command = classes.get(clazz);
if (command != null) {
return clazz.cast(instances.get(command));
}
else {
return null;
}
}
// <--[language]
// @Name Command Syntax
// @group Script Command System
// @Description
// Almost every Denizen command and requirement has arguments after the command itself.
// These arguments are just snippets of text showing what exactly the command should do,
// like what the chat command should say, or where the look command should point.
// But how do you know what to put in the arguments?
//
// You merely need to look at the command's usage/syntax info.
// Let's take for example:
// <code>
// - animatechest [<location>] ({open}/close) (sound:{true}/false)
// </code>
// Obviously, the command is 'animatechest'... but what does the rest of it mean?
//
// Anything in [brackets] is required... you MUST put it there.
// Anything in (parenthesis) is optional... you only need to put it there if you want to.
// Anything in {braces} is default... the command will just assume this if no argument is actually typed.
// Anything in <> is non-literal... you must change what is inside of it.
// Anything outside of <> is literal... you must put it exactly as-is.
// <#> represents a number without a decimal, and <#.#> represents a number with a decimal
// Lastly, input that ends with "|..." (EG, [<entity>|...] ) can take a list of the input indicated before it (In that example, a list of entities)
// An argument that contains a ":" (like "duration:<value>") is a prefix:value pair. The prefix is usually literal and the value dynamic. The prefix and the colon should be kept directly in the final command.
//
// A few examples:
// [<location>] is required and non-literal... you might fill it with a notable location, or a tag that returns one like '<player.location>'.
// (sound:{true}/false) is optional and has a default value of true... you can put sound:false to prevent sound, or leave it blank to allow sound.
// (repeats:<#>) is optional, has no clear default, and is a number. You can put repeats:3 to repeat three times, or leave it blank to not repeat.
// Note: Optional arguments without a default usually have a secret default... EG, the (repeats:<#>) above has a secret default of '0'.
//
// Also, you should never directly type in [], (), {}, or <> even though they are in the syntax info.
// The only exception is in a replaceable tag (EG: <npc.has_trait[<traitname>]> will take <npc.has_trait[mytrait]> as a valid actual usage)
//
// Highly specific note: <commands> means a block of commands wrapped in braces or as a sub-block... EG:
// <code>
// - repeat 3:
// - narrate "<[value]>"
// - narrate "everything spaced out as a sub-block (these two narrates) following a ":" ended command (that repeat) is for the <commands> input!"
// </code>
//
// -->
public void registerCoreCommands() {
// core
registerCommand(AdjustCommand.class);
registerCommand(DebugCommand.class);
// Intentionally do not register the DebugInvalidCommand
registerCommand(EventCommand.class);
registerCommand(FlagCommand.class);
registerCommand(ReloadCommand.class);
registerCommand(SQLCommand.class);
registerCommand(WebGetCommand.class);
// file
registerCommand(FileCopyCommand.class);
registerCommand(LogCommand.class);
registerCommand(YamlCommand.class);
// queue
registerCommand(ChooseCommand.class);
registerCommand(DefineCommand.class);
registerCommand(DefineMapCommand.class);
registerCommand(DetermineCommand.class);
registerCommand(ElseCommand.class);
registerCommand(ForeachCommand.class);
registerCommand(GotoCommand.class);
registerCommand(IfCommand.class);
registerCommand(InjectCommand.class);
registerCommand(MarkCommand.class);
registerCommand(QueueCommand.class);
registerCommand(RandomCommand.class);
registerCommand(RateLimitCommand.class);
registerCommand(RepeatCommand.class);
registerCommand(RunCommand.class);
registerCommand(StopCommand.class);
registerCommand(WaitCommand.class);
registerCommand(WaitUntilCommand.class);
registerCommand(WhileCommand.class);
}
public <T extends AbstractCommand> void registerCommand(Class<T> cmd) {
try {
AbstractCommand command = cmd.newInstance();
register(command.getName(), command);
}
catch (Throwable e) {
Debug.echoError("Could not register command " + cmd.getName() + ", exception follows...");
Debug.echoError(e);
}
}
@Deprecated
public <T extends AbstractCommand> void registerCoreMember(Class<T> cmd, String name, String hint, int args) {
try {
cmd.newInstance().as(name).withOptions(hint, args);
}
catch (Throwable e) {
Debug.echoError("Could not register command " + name + ": " + e.getMessage());
Debug.echoError(e);
}
}
public void disableCoreMembers() {
for (AbstractCommand member : instances.values()) {
try {
member.onDisable();
}
catch (Exception e) {
Debug.echoError("Unable to disable '" + member.getClass().getName() + "'!");
Debug.echoError(e);
}
}
}
}