-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ScriptBuilder.java
93 lines (78 loc) · 3.1 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
83
84
85
86
87
88
89
90
91
92
93
package net.aufdemrand.denizencore.scripts;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class ScriptBuilder {
/**
* Adds an object to a list of ScriptEntries. Can later be retrieved from the ScriptEntry
* by using getObject(String key)
*
* @param scriptEntryList the list of ScriptEntries
* @param key the key (name) of the object being added
* @param obj the object
* @return the List of ScriptEntries, with the object added in each member
*/
public static List<ScriptEntry> addObjectToEntries(List<ScriptEntry> scriptEntryList, String key, Object obj) {
for (ScriptEntry entry : scriptEntryList) {
entry.addObject(key, obj);
entry.trackObject(key);
}
return scriptEntryList;
}
/*
* Builds ScriptEntry(ies) of items read from a script
*/
public static List<ScriptEntry> buildScriptEntries(List<Object> contents, ScriptContainer parent, ScriptEntryData data) {
List<ScriptEntry> scriptCommands = new ArrayList<ScriptEntry>();
if (contents == null || contents.isEmpty()) {
if (dB.showScriptBuilder) {
dB.echoError("Building script entries... no entries to build!");
}
return null;
}
if (dB.showScriptBuilder) {
dB.echoDebug(parent, "Building script entries:");
}
for (Object ientry : contents) {
String entry;
List<Object> inside;
if (ientry instanceof Map) {
Object key = ((Map) ientry).keySet().toArray()[0];
entry = key.toString();
inside = (List<Object>) ((Map) ientry).get(key);
}
else {
entry = ientry.toString();
inside = null;
}
String[] scriptEntry;
String[] splitEntry = entry.split(" ", 2);
if (splitEntry.length == 1) {
scriptEntry = new String[2];
scriptEntry[0] = entry;
scriptEntry[1] = null;
}
else {
scriptEntry = splitEntry;
}
try {
/* Build new script commands */
String[] args = aH.buildArgs(scriptEntry[1]);
if (dB.showScriptBuilder) {
dB.echoDebug(parent, "Adding '" + scriptEntry[0] + "' Args: " + Arrays.toString(args));
}
ScriptEntry newEntry = new ScriptEntry(scriptEntry[0], args, parent, inside);
newEntry.entryData.transferDataFrom(data);
scriptCommands.add(newEntry);
}
catch (Exception e) {
dB.echoError(e);
}
}
return scriptCommands;
}
}