/
ScriptRegistry.java
123 lines (105 loc) · 5.17 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
package net.aufdemrand.denizen.scripts;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.*;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
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("interact", InteractScriptContainer.class);
_registerType("book", BookScriptContainer.class);
_registerType("item", ItemScriptContainer.class);
_registerType("entity", EntityScriptContainer.class);
_registerType("task", TaskScriptContainer.class);
_registerType("assignment", AssignmentScriptContainer.class);
_registerType("procedure", ProcedureScriptContainer.class);
_registerType("world", WorldScriptContainer.class);
_registerType("format", FormatScriptContainer.class);
_registerType("inventory", InventoryScriptContainer.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.getType().equalsIgnoreCase(type));
}
public static void _buildCoreYamlScriptContainers(FileConfiguration yamlScripts) {
scriptContainers.clear();
WorldScriptHelper.world_scripts.clear();
ItemScriptHelper.item_scripts.clear();
// Get a set of key names in concantenated Denizen Scripts
Set<String> scripts = yamlScripts.getKeys(false);
// Iterate through set
for (String scriptName : scripts)
// 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.");
continue;
}
// Instantize a new scriptContainer of specified type.
Class typeClass = scriptContainerTypes.get(type.toUpperCase());
try {
scriptContainers.put(scriptName, typeClass.getConstructor(ConfigurationSection.class, String.class)
.newInstance(ScriptHelper._gs().getConfigurationSection(scriptName), scriptName));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static List<FileConfiguration> outside_scripts = new ArrayList<FileConfiguration>();
/**
* 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(FileConfiguration yaml_script) {
outside_scripts.add(yaml_script);
ScriptHelper.reloadScripts();
}
/**
* 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(FileConfiguration yaml_script) {
outside_scripts.remove(yaml_script);
ScriptHelper.reloadScripts();
}
public static <T extends ScriptContainer> T getScriptContainerAs(String name, Class<T> type) {
try {
if (scriptContainers.containsKey(name.toUpperCase()))
return (T) type.cast(scriptContainers.get(name.toUpperCase()));
else return null;
} catch (Exception e) { }
return null;
}
public static ScriptContainer getScriptContainer(String name) {
if (scriptContainers.containsKey(name.toUpperCase()))
return (ScriptContainer) scriptContainers.get(name.toUpperCase());
else return null;
}
}