-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DefineCommand.java
138 lines (128 loc) · 5.9 KB
/
DefineCommand.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
package com.denizenscript.denizencore.scripts.commands.queue;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.QueueTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.data.ActionableDataProvider;
import com.denizenscript.denizencore.utilities.data.DataAction;
import com.denizenscript.denizencore.utilities.data.DataActionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
/**
* Creates a queue/script-level variable.
*/
public class DefineCommand extends AbstractCommand {
public DefineCommand() {
setName("define");
setSyntax("define [<id>](:<action>)[:<value>]");
setRequiredArguments(1, 2);
isProcedural = true;
}
// <--[command]
// @Name Define
// @Syntax define [<id>](:<action>)[:<value>]
// @Required 1
// @Maximum 2
// @Short Creates a temporary variable inside a script queue.
// @Synonyms Definition
// @Group queue
// @Guide https://guide.denizenscript.com/guides/basics/definitions.html
//
// @Description
// Definitions are queue-level 'variables' that can be used throughout a script, once defined, by using the <[<id>]> tag.
// Definitions are only valid on the current queue and are not transferred to any new queues constructed within the script,
// such as by a 'run' command, without explicitly specifying to do so.
//
// Definitions are lighter and faster than creating a temporary flag.
// Definitions are also automatically removed when the queue is completed, so there is no worry for leaving unused data hanging around.
//
// This command supports data actions, see <@link language data actions>.
//
// @Tags
// <[<id>]> to get the value assigned to an ID
// <QueueTag.definition[<definition>]>
// <QueueTag.definitions>
//
// @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[<[blocks]>].within[<[range]>].size> blocks in the area that may be of interest.'
//
// @Usage
// Use to validate a player input to a command script, and then output the found player's name.
// - define target <server.match_player[<context.args.get[1]>]||null>
// - if <[target]> == null:
// - narrate '<red>Unknown player target.'
// - stop
// - narrate 'You targeted <[target].name>!'
//
// @Usage
// Use to keep the value of a replaceable tag that you might use many times within a single script.
// - define arg1 <context.args.get[1]>
// - if <[arg1]> == hello:
// - narrate 'Hello!'
// - else if <[arg1]> == goodbye:
// - narrate 'Goodbye!'
//
// @Usage
// Use to remove a definition.
// - define myDef:!
//
// -->
public static class DefinitionActionProvider extends ActionableDataProvider {
public ScriptQueue queue;
@Override
public ObjectTag getValueAt(String keyName) {
return queue.getDefinitionObject(keyName);
}
@Override
public void setValueAt(String keyName, ObjectTag value) {
queue.addDefinition(keyName, value);
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("definition")) {
if (CoreUtilities.contains(arg.getRawValue(), ':')) {
DefinitionActionProvider provider = new DefinitionActionProvider();
provider.queue = scriptEntry.getResidingQueue();
scriptEntry.addObject("action", DataActionHelper.parse(provider, arg, scriptEntry.context));
}
else {
scriptEntry.addObject("definition", new ElementTag(CoreUtilities.toLowerCase(arg.getValue())));
}
}
else if (!scriptEntry.hasObject("value")) {
scriptEntry.addObject("value", arg.object instanceof ElementTag ? new ElementTag(arg.getRawValue()) : arg.object);
}
else {
arg.reportUnhandled();
}
}
if ((!scriptEntry.hasObject("definition") || !scriptEntry.hasObject("value")) && !scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify a definition and value!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag definition = scriptEntry.getElement("definition");
ObjectTag value = scriptEntry.getObjectTag("value");
ElementTag remove = scriptEntry.getElement("remove");
DataAction action = (DataAction) scriptEntry.getObject("action");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), new QueueTag(scriptEntry.getResidingQueue()), definition, value, action == null ? null : action.debug(), remove);
}
if (action != null) {
action.execute(scriptEntry.getContext());
return;
}
scriptEntry.getResidingQueue().addDefinition(definition.asString(), value.duplicate());
}
}