-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ScriptBuilder.java
82 lines (76 loc) · 3.63 KB
/
ScriptBuilder.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
package com.denizenscript.denizencore.scripts;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class ScriptBuilder {
public static char LINE_PREFIX_CHAR = '^'; // This would be an invisible special character... if SnakeYAML allowed them!
public static String stripLinePrefix(String rawLine) {
if (!rawLine.startsWith(String.valueOf(LINE_PREFIX_CHAR))) {
return rawLine;
}
int infoEnd = rawLine.indexOf(LINE_PREFIX_CHAR, 1);
return rawLine.substring(infoEnd + 2); // Skip the symbol and the space after.
}
public static List<ScriptEntry> buildScriptEntries(List<Object> contents, ScriptContainer parent, ScriptEntryData data) {
if (contents == null || contents.isEmpty()) {
if (CoreConfiguration.debugScriptBuilder) {
Debug.echoError("Building script entries... no entries to build!");
}
return null;
}
if (CoreConfiguration.debugScriptBuilder) {
Debug.echoDebug(parent, "Building script entries:");
}
List<ScriptEntry> scriptCommands = new ArrayList<>(contents.size());
for (Object ientry : contents) {
if (ientry == null) {
ientry = "null";
}
String entry;
Object inside;
if (ientry instanceof Map) {
Object key = ((Map) ientry).keySet().toArray()[0];
entry = key.toString();
Object rawValue = ((Map) ientry).get(key);
if (!(rawValue instanceof List) && !(rawValue instanceof Map)) {
Debug.echoError("Script '" + parent.getName() + "' has invalid line " + ientry + ": line ends with ':' but no script body inside.");
return null;
}
inside = rawValue;
}
else {
entry = ientry.toString();
inside = null;
}
int lineNum = 1;
if (entry.startsWith(String.valueOf(LINE_PREFIX_CHAR))) {
int infoEnd = entry.indexOf(LINE_PREFIX_CHAR, 1);
String lineNumStr = entry.substring(1, infoEnd);
entry = entry.substring(infoEnd + 2); // Skip the symbol and the space after.
lineNum = Integer.parseInt(lineNumStr);
}
String[] scriptEntry = entry.split(" ", 2);
try {
/* Build new script commands */
String[] args = scriptEntry.length > 1 ? ArgumentHelper.buildArgs(scriptEntry[1]) : null;
if (CoreConfiguration.debugScriptBuilder) {
Debug.echoDebug(parent, "Adding '" + scriptEntry[0] + "' Args: " + Arrays.toString(args));
}
ScriptEntry newEntry = new ScriptEntry(scriptEntry[0], args, parent, inside, lineNum);
newEntry.internal.originalLine = entry;
newEntry.entryData.transferDataFrom(data);
scriptCommands.add(newEntry);
}
catch (Exception e) {
Debug.echoError("Exception while building script '" + (parent == null ? "(null)" : parent.getName()) + "'...");
Debug.echoError(e);
}
}
return scriptCommands;
}
}