/
GotoCommand.java
97 lines (82 loc) · 3.11 KB
/
GotoCommand.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
package net.aufdemrand.denizencore.scripts.commands.queue;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.util.List;
public class GotoCommand extends AbstractCommand {
// <--[command]
// @Name Goto
// @Syntax goto [<name>]
// @Required 1
// @Short Jump forward to a location marked by <@link command mark>.
// @Group queue
//
// @Description
// Jumps forward to a marked location in the script.
// For example:
// <code>
// - goto potato
// - narrate "This will never show"
// - mark potato
// </code>
//
// @Tags
//
// None
//
// @Usage
// Use to jump forward to a location.
// - goto potato
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Interpret arguments
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("m_name")) {
scriptEntry.addObject("m_name", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
// Check for required information
if (!scriptEntry.hasObject("m_name")) {
throw new InvalidArgumentsException("Must have a mark name!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
// Fetch required objects
Element mName = scriptEntry.getElement("m_name");
// Debug the execution
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), mName.debug());
}
// Jump forth
boolean hasmark = false;
for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("mark") && args.size() > 0 && args.get(0).equalsIgnoreCase(mName.asString())) {
hasmark = true;
break;
}
}
if (hasmark) {
while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("mark") && args.size() > 0 && args.get(0).equalsIgnoreCase(mName.asString())) {
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot go to that location - doesn't seem to exist!");
}
}
}