/
BracedCommand.java
174 lines (150 loc) · 8.32 KB
/
BracedCommand.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
package net.aufdemrand.denizen.scripts.commands;
import java.util.*;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.utilities.debugging.dB;
public abstract class BracedCommand extends AbstractCommand {
public static final boolean hyperdebug = false;
/**
* Gets the commands inside the braces of this ScriptEntry.
*
* @param scriptEntry
* The ScriptEntry to get the braced commands from.
*
* @return
* The list of ScriptEntries to be executed in the command.
*/
public static LinkedHashMap<String, ArrayList<ScriptEntry>> getBracedCommands(ScriptEntry scriptEntry) {
// And a place to store all the final braces...
LinkedHashMap<String, ArrayList<ScriptEntry>> bracedSections = new LinkedHashMap<String, ArrayList<ScriptEntry>>();
LinkedHashMap<String, ArrayList<ScriptEntry>> entryBracedSet = scriptEntry.getBracedSet();
if (entryBracedSet != null) {
try {
for (Map.Entry<String, ArrayList<ScriptEntry>> entry: entryBracedSet.entrySet()) {
ArrayList array = new ArrayList(entry.getValue().size());
for (ScriptEntry sEntry: entry.getValue()) {
array.add(sEntry.clone().setPlayer(((BukkitScriptEntryData)scriptEntry.entryData).getPlayer()).setNPC(((BukkitScriptEntryData)scriptEntry.entryData).getNPC()));
}
bracedSections.put(entry.getKey(), array);
}
return bracedSections;
}
catch (CloneNotSupportedException e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
}
// We need a place to store the commands being built at...
TreeMap<Integer, ArrayList<String>> commandList = new TreeMap<Integer, ArrayList<String>>();
int bracesEntered = 0;
boolean newCommand = true;
boolean waitingForDash = false;
// Inject the scriptEntry into the front of the queue, otherwise it doesn't exist
//scriptEntry.getResidingQueue().injectEntry(scriptEntry, 0);
// Send info to debug
if (hyperdebug) dB.echoDebug(scriptEntry, "Starting getBracedCommands...");
// If the specified amount of possible entries is less than the queue size, print that instead
//if (hyperdebug) dB.echoDebug(scriptEntry, "...with queue size: " + scriptEntry.getResidingQueue().getQueueSize());
if (hyperdebug) dB.echoDebug(scriptEntry, "...with first command name: " + scriptEntry.getCommandName());
if (hyperdebug) dB.echoDebug(scriptEntry, "...with first command arguments: " + scriptEntry.getArguments());
ScriptEntry entry = scriptEntry;//scriptEntry.getResidingQueue().getEntry(0);
if (hyperdebug) dB.echoDebug(scriptEntry, "Entry found: " + entry.getCommandName());
// Loop through the arguments of each entry
List<aH.Argument> argList = aH.interpret(entry.getArguments());
// Set the variable to use for naming braced command lists; the first should be the command name
String bracesName = scriptEntry.getCommandName();
int startArg = 0;
for (int i = 0; i < argList.size(); i++) {
aH.Argument arg = argList.get(i);
if (arg.asElement().asString().equals("{")) {
startArg = i;
break;
}
}
for (int i = startArg; i < argList.size(); i++) {
aH.Argument arg = argList.get(i);
if (hyperdebug) dB.echoDebug(scriptEntry, "Arg found: " + arg.raw_value);
// Listen for opened braces
if (arg.matches("{")) {
bracesEntered++;
newCommand = false;
waitingForDash = bracesEntered == 1;
if (hyperdebug) dB.echoDebug(scriptEntry, "Opened brace; " + String.valueOf(bracesEntered) + " now");
if (bracesEntered > 1) {
commandList.get(commandList.lastKey()).add(arg.raw_value);
}
}
// Listen for closed braces
else if (arg.matches("}")) {
bracesEntered--;
newCommand = false;
if (hyperdebug) dB.echoDebug(scriptEntry, "Closed brace; " + String.valueOf(bracesEntered) + " now");
if (bracesEntered > 0) {
commandList.get(commandList.lastKey()).add(arg.raw_value);
}
else {
if (bracedSections.containsKey(bracesName)) {
dB.echoError(scriptEntry.getResidingQueue(), "You may not have braced commands with the same arguments.");
break;
}
ArrayList<ScriptEntry> bracesSection = new ArrayList<ScriptEntry>();
for (ArrayList<String> command : commandList.values()) {
try {
if (command.isEmpty()) {
if (hyperdebug) dB.echoError(scriptEntry.getResidingQueue(), "Empty command?");
continue;
}
String cmd = command.get(0);
if (hyperdebug) dB.echoDebug(scriptEntry, "Calculating " + cmd);
command.remove(0);
String[] args = new String[command.size()];
args = command.toArray(args);
bracesSection.add(new ScriptEntry(cmd,
args,
scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
bracesSection.get(bracesSection.size() - 1).setPlayer(((BukkitScriptEntryData)scriptEntry.entryData).getPlayer());
bracesSection.get(bracesSection.size() - 1).setNPC(((BukkitScriptEntryData)scriptEntry.entryData).getNPC());
if (hyperdebug) dB.echoDebug(scriptEntry, "Command added: " + cmd + ", with " + String.valueOf(args.length) + " arguments");
} catch (ScriptEntryCreationException e) {
if (hyperdebug) dB.echoError(scriptEntry.getResidingQueue(), e.getMessage());
}
}
if (hyperdebug) dB.echoDebug(scriptEntry, "Adding section " + bracesName);
bracedSections.put(bracesName.toUpperCase(), bracesSection);
bracesName = "";
}
}
// Finish building a command
else if (newCommand && bracesEntered == 1) {
commandList.put(commandList.size(), new ArrayList<String>());
commandList.get(commandList.lastKey()).add(arg.raw_value);
newCommand = false;
if (hyperdebug) dB.echoDebug(scriptEntry, "Treating as new command");
}
// Start building a command
else if (arg.matches("-") && bracesEntered == 1) {
newCommand = true;
waitingForDash = false;
if (hyperdebug) dB.echoDebug(scriptEntry, "Assuming following is a new command");
}
// Add to the name of the braced command list
else if (bracesEntered == 0) {
bracesName += arg.raw_value;
}
// Continue building the current command
else {
if (waitingForDash) {
dB.echoError(scriptEntry.getResidingQueue(), "Malformed braced section! Missing a - symbol!");
break;
}
newCommand = false;
commandList.get(commandList.lastKey()).add(arg.raw_value);
if (hyperdebug) dB.echoDebug(scriptEntry, "Adding to the command");
}
}
//scriptEntry.getResidingQueue().removeEntry(0);
scriptEntry.setBracedSet(bracedSections);
return bracedSections;
}
}