/
InjectCommand.java
133 lines (111 loc) · 5.07 KB
/
InjectCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dScript;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import java.util.List;
public class InjectCommand extends AbstractCommand {
// <--[command]
// @Name Inject
// @Syntax inject (locally) [<script>] (path:<name>) (instantly)
// @Required 1
// @Short Runs a script in the current ScriptQueue.
// @Video /denizen/vids/Run%20And%20Inject
// @Group core
//
// @Description
// Injects a script into the current ScriptQueue.
// This means this task will run with all of the original queue's definitions and tags.
// It will also now be part of the queue, so any delays or definitions used in the injected script will be
// accessible in the original queue.
//
// @Tags
// None
//
// @Usage
// Injects the InjectedTask task into the current queue
// - inject InjectedTask
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (arg.matches("instant", "instantly")) {
scriptEntry.addObject("instant", new Element(true));
}
else if (arg.matches("local", "locally")) {
scriptEntry.addObject("local", new Element(true));
}
else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(dScript.class)
&& !arg.matchesPrefix("p", "path")) {
scriptEntry.addObject("script", arg.asType(dScript.class));
}
else if (!scriptEntry.hasObject("path")) {
String path = arg.asElement().asString();
if (!scriptEntry.hasObject("script")) {
int dotIndex = path.indexOf('.');
if (dotIndex > 0) {
dScript script = new dScript(path.substring(0, dotIndex));
if (script.isValid()) {
scriptEntry.addObject("script", script);
path = path.substring(dotIndex + 1);
}
}
}
scriptEntry.addObject("path", new Element(path));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("script") && !scriptEntry.hasObject("local")) {
throw new InvalidArgumentsException("Must define a SCRIPT to be injected.");
}
if (!scriptEntry.hasObject("path") && scriptEntry.hasObject("local")) {
throw new InvalidArgumentsException("Must specify a PATH.");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
(scriptEntry.hasObject("script") ? scriptEntry.getdObject("script").debug() : scriptEntry.getScript().debug())
+ (scriptEntry.hasObject("instant") ? scriptEntry.getdObject("instant").debug() : "")
+ (scriptEntry.hasObject("path") ? scriptEntry.getElement("path").debug() : "")
+ (scriptEntry.hasObject("local") ? scriptEntry.getElement("local").debug() : ""));
}
// Get the script
dScript script = scriptEntry.getdObject("script");
// Get the entries
List<ScriptEntry> entries;
// If it's local
if (scriptEntry.hasObject("local")) {
entries = scriptEntry.getScript().getContainer().getEntries(scriptEntry.entryData.clone(),
scriptEntry.getElement("path").asString());
}
// If it has a path
else if (scriptEntry.hasObject("path")) {
entries = script.getContainer().getEntries(scriptEntry.entryData.clone(),
scriptEntry.getElement("path").asString());
}
// Else, assume standard path
else {
entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
}
if (entries == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Script inject failed (invalid path or script name)!");
return;
}
// If 'instantly' was specified, run the commands immediately.
if (scriptEntry.hasObject("instant")) {
scriptEntry.getResidingQueue().runNow(entries, "INJECT");
}
else {
// Inject the entries into the current scriptqueue
scriptEntry.getResidingQueue().injectEntries(entries, 0);
}
}
}