-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CommandRegistry.java
251 lines (219 loc) · 11 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
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
package net.aufdemrand.denizencore.scripts.commands;
import net.aufdemrand.denizencore.interfaces.RegistrationableInstance;
import net.aufdemrand.denizencore.interfaces.dRegistry;
import net.aufdemrand.denizencore.scripts.commands.core.DefineCommand;
import net.aufdemrand.denizencore.scripts.commands.core.DetermineCommand;
import net.aufdemrand.denizencore.scripts.commands.core.ForeachCommand;
import net.aufdemrand.denizencore.scripts.commands.core.IfCommand;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.util.HashMap;
import java.util.Map;
public abstract class CommandRegistry implements dRegistry {
public CommandRegistry() {
}
public final Map<String, AbstractCommand> instances = new HashMap<String, AbstractCommand>();
public final Map<Class<? extends AbstractCommand>, String> classes = new HashMap<Class<? extends AbstractCommand>, String>();
@Override
public boolean register(String commandName, RegistrationableInstance commandInstance) {
this.instances.put(CoreUtilities.toLowerCase(commandName), (AbstractCommand) commandInstance);
this.classes.put(((AbstractCommand) commandInstance).getClass(), commandName.toUpperCase());
return true;
}
@Override
public Map<String, AbstractCommand> list() {
return instances;
}
@Override
public AbstractCommand get(String commandName) {
return instances.get(CoreUtilities.toLowerCase(commandName));
}
@Override
public <T extends RegistrationableInstance> 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)
//
// A few examples:
// [<location>] is required and non-literal... you might fill it with 'l@1,2,3,world' which is a valid location object.
// (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... EG:
// <code>
// - repeat 3 {
// - narrate "%value%"
// - narrate "everything between the {and} symbols (including them) are for the <commands> input!"
// }
// </code>
//
// -->
public void registerCoreCommands() {
// <--[command]
// @Name Define
// @Syntax define [<id>] [<value>]
// @Required 1
// @Stable stable
// @Short Creates a temporary variable inside a script queue.
// @Author aufdemrand
// @Group core
// @Description
// Definitions are queue-level (or script-level) 'variables' that can be used throughout a script, once
// defined, by using %'s around the definition id/name. Definitions are only valid on the current queue and are
// not transferred to any new queues constructed within the script, such as a 'run' command, without explicitly
// specifying to do so.
//
// Definitions are lighter and faster than creating a temporary flag, but unlike flags, are only a single entry,
// that is, you can't add or remove from the definition, but you can re-create it if you wish to specify a new
// value. Definitions are also automatically destroyed when the queue is completed, so there is no worry for
// leaving unused data hanging around.
//
// Definitions are also resolved before replaceable tags, meaning you can use them within tags, even as an
// attribute. ie. <%player%.name>
// @Tags
// %<ID>% to get the value assign to an ID
// <def[<ID>]> to get the value assigned to an ID
// @Usage
// Use to make complex tags look less complex, and scripts more readable.
// - narrate 'You invoke your power of notice...'
// - define range '<player.flag[range_level].mul[3]>'
// - define blocks '<player.flag[noticeable_blocks]>'
// - narrate '[NOTICE] You have noticed <player.location.find.blocks[<def[blocks]>].within[<def[range]>].size>
// blocks in the area that may be of interest.'
// @Usage
// Use to keep the value of a replaceable tag that you might use many times within a single script. Definitions
// can be faster and cleaner than reusing a replaceable tag over and over.
// - define arg1 <c.args.get[1]>
// - if <def[arg1]> == hello narrate 'Hello!'
// - if <def[arg1]> == goodbye narrate 'Goodbye!'
// @Usage
// Use to pass some important information (arguments) on to another queue.
// - run 'new_task' d:hello|world
// 'new_task' now has some definitions, <def[1]> and <def[2]>, that contains the contents specified, 'hello' and 'world'.
// @Usage
// Use to remove a definition
// - define myDef:!
// -->
registerCoreMember(DefineCommand.class,
"DEFINE", "define [<id>] [<value>]", 1);
// <--[command]
// @Name Determine
// @Syntax determine (passively) [<value>]
// @Required 1
// @Stable stable
// @Short Sets the outcome of a world event.
// @Author aufdemrand
// @Group core
// @Description
// TODO: Document Command Details
// @Tags
// TODO: Document Command Details
// @Usage
// Use to modify the result of an event
// - determine <context.message.substring[5]>
// @Usage
// Use to cancel an event, but continue running script commands
// - determine passively cancelled
// -->
registerCoreMember(DetermineCommand.class,
"DETERMINE", "determine (passively) [<value>]", 1);
// <--[command]
// @Name Foreach
// @Syntax foreach [stop/next/<object>|...] [<commands>]
// @Required 1
// @Stable stable
// @Short Loops through a dList, running a set of commands for each item.
// @Author Morphan1, mcmonkey
// @Group core
// @Video /denizen/vids/Loops
// @Description
// Loops through a dList of any type. For each item in the dList, the specified commands will be ran for
// that list entry. To call the value of the entry while in the loop, you can use <def[value]>.
//
// To end a foreach loop, do - foreach stop
//
// To jump immediately to the next entry in the loop, do - foreach next
// @Tags
// <def[value]> to get the current item in the loop
// <def[loop_index]> to get the current loop iteration number
// @Usage
// Use to run commands for 'each entry' in a list of objects/elements.
// - foreach li@e@123|n@424|p@BobBarker {
// - announce "There's something at <def[value].location>!"
// }
// @Usage
// Use to iterate through entries in any tag that returns a list
// - foreach <server.list_online_players> {
// - narrate "Thanks for coming to our server! Here's a bonus $50.00!"
// - give <def[value]> money qty:50
// }
// -->
registerCoreMember(ForeachCommand.class,
"FOREACH", "foreach [stop/next/<object>|...] [<commands>]", 1);
// <--[command]
// @Name If
// @Syntax if [<value>] (!)(<operator> <value>) (&&/|| ...) [<commands>] (else <commands>)
// @Required 1
// @Stable stable
// @Short Compares values, and runs one script if they match, or a different script if they don't match.
// @Author aufdemrand, David Cernat
// @Group core
// @Video /denizen/vids/Alternate/Dynamic%20Actions:%20The%20If%20Command
// @Description
// TODO: Document Command Details
// @Tags
// <el@element.is[<operator>].to[<element>]>
// @Usage
// TODO: Document Command Details
// -->
registerCoreMember(IfCommand.class, "IF", "if [<value>] (!)(<operator> <value>) (&&/|| ...) [<commands>] (else <commands>)", 0);
}
public <T extends AbstractCommand> void registerCoreMember(Class<T> cmd, String names, String hint, int args) {
for (String name : names.split(", ")) {
try {
cmd.newInstance().activate().as(name).withOptions(hint, args);
} catch(Throwable e) {
dB.echoError("Could not register command " + name + ": " + e.getMessage());
dB.echoError(e);
}
}
}
@Override
public void disableCoreMembers() {
for (RegistrationableInstance member : instances.values())
try {
member.onDisable();
} catch (Exception e) {
dB.echoError("Unable to disable '" + member.getClass().getName() + "'!");
dB.echoError(e);
}
}
}