-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ScriptRegistry.java
131 lines (114 loc) · 5.67 KB
/
ScriptRegistry.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
package net.aufdemrand.denizencore.scripts;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.scripts.containers.core.*;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.text.StringHolder;
import java.util.*;
public class ScriptRegistry {
// Currently loaded 'script-containers'
private static Map<String, Object> scriptContainers = new HashMap<String, Object>();
private static Map<String, Class<? extends ScriptContainer>> scriptContainerTypes = new HashMap<String, Class<? extends ScriptContainer>>();
public static void _registerType(String typeName, Class<? extends ScriptContainer> scriptContainerClass) {
scriptContainerTypes.put(typeName.toUpperCase(), scriptContainerClass);
}
public static Set<String> _getScriptNames() {
return scriptContainers.keySet();
}
public static void _registerCoreTypes() {
_registerType("custom", CustomScriptContainer.class);
_registerType("task", TaskScriptContainer.class);
_registerType("procedure", ProcedureScriptContainer.class);
_registerType("world", WorldScriptContainer.class);
_registerType("yaml data", YamlDataScriptContainer.class);
}
public static boolean containsScript(String id) {
return scriptContainers.containsKey(id.toUpperCase());
}
public static boolean containsScript(String id, Class scriptContainerType) {
if (!scriptContainers.containsKey(id.toUpperCase())) return false;
ScriptContainer script = (ScriptContainer) scriptContainers.get(id.toUpperCase());
String type = null;
for (Map.Entry<String, Class<? extends ScriptContainer>> entry : scriptContainerTypes.entrySet()) {
if (entry.getValue() == scriptContainerType)
type = entry.getKey();
}
return type != null && (script.getContainerType().equalsIgnoreCase(type));
}
public static void _buildCoreYamlScriptContainers(YamlConfiguration yamlScripts) {
scriptContainers.clear();
OldEventManager.world_scripts.clear();
OldEventManager.events.clear();
DenizenCore.getImplementation().refreshScriptContainers();
if (yamlScripts == null)
return;
// Get a set of key names in concatenated Denizen Scripts
Set<StringHolder> scripts = yamlScripts.getKeys(false);
// Iterate through set
for (StringHolder scriptName1 : scripts) {
String scriptName = scriptName1.str;
// Make sure the script has a type
if (yamlScripts.contains(scriptName + ".TYPE")) {
String type = yamlScripts.getString(scriptName + ".TYPE");
// Check that types is a registered type
if (!scriptContainerTypes.containsKey(type.toUpperCase())) {
dB.log("<G>Trying to load an invalid script. '<A>" + scriptName + "<Y>(" + type + ")'<G> is an unknown type.");
ScriptHelper.setHadError();
continue;
}
// Instantiate a new scriptContainer of specified type.
Class typeClass = scriptContainerTypes.get(type.toUpperCase());
dB.log("Adding script " + scriptName + " as type " + type.toUpperCase());
try {
scriptContainers.put(scriptName, typeClass.getConstructor(YamlConfiguration.class, String.class)
.newInstance(ScriptHelper._gs().getConfigurationSection(scriptName), scriptName));
}
catch (Exception e) {
dB.echoError(e);
ScriptHelper.setHadError();
}
}
else {
dB.echoError("Found type-less container: '" + scriptName + "'.");
ScriptHelper.setHadError();
}
}
}
public static List<YamlConfiguration> outside_scripts = new ArrayList<net.aufdemrand.denizencore.utilities.YamlConfiguration>();
/**
* Adds a YAML FileConfiguration to the list of scripts to be loaded. Adding a new
* FileConfiguration will reload the scripts automatically.
*
* @param yaml_script the FileConfiguration containing the script
*/
public static void addYamlScriptContainer(YamlConfiguration yaml_script) {
outside_scripts.add(yaml_script);
}
/**
* Removes a YAML FileConfiguration to the list of scripts to be loaded. Removing a
* FileConfiguration will reload the scripts automatically.
*
* @param yaml_script the FileConfiguration containing the script
*/
public static void removeYamlScriptContainer(YamlConfiguration yaml_script) {
outside_scripts.remove(yaml_script);
DenizenCore.reloadScripts();
}
public static <T extends ScriptContainer> T getScriptContainerAs(String name, Class<T> type) {
try {
if (scriptContainers.containsKey(name.toUpperCase()))
return type.cast(scriptContainers.get(name.toUpperCase()));
else return null;
}
catch (Exception e) {
}
return null;
}
public static <T extends ScriptContainer> T getScriptContainer(String name) {
if (scriptContainers.containsKey(name.toUpperCase()))
return (T) scriptContainers.get(name.toUpperCase());
else return null;
}
}