/
ScriptBuilder.java
76 lines (63 loc) · 2.59 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
package net.aufdemrand.denizencore.scripts;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.debugging.dB;
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
*/
// TODO: use a context object
public static List<ScriptEntry> buildScriptEntries(List<String> 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 (String entry : contents) {
String[] scriptEntry = new String[2];
String[] splitEntry = entry.split(" ", 2);
if (splitEntry.length == 1) {
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);
newEntry.entryData.transferDataFrom(data);
scriptCommands.add(newEntry);
} catch (Exception e) {
dB.echoError(e);
}
}
return scriptCommands;
}
}