-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ScriptRegistry.java
162 lines (141 loc) · 6.28 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.denizenscript.denizencore.scripts;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.containers.core.*;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.events.OldEventManager;
import java.util.*;
public class ScriptRegistry {
// Currently loaded 'script-containers'
private static Map<String, Object> scriptContainers = new HashMap<>();
private static Map<String, Class<? extends ScriptContainer>> scriptContainerTypes = new HashMap<>();
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 YamlConfiguration fullYaml;
public static ArrayList<String> toPostLoadAttempt = new ArrayList<>();
public static void postLoadScripts() {
for (String scriptName : toPostLoadAttempt) {
attemptLoadSingle(scriptName, true);
}
toPostLoadAttempt.clear();
fullYaml = null;
}
public static void attemptLoadSingle(String scriptName, boolean shouldErrorOnType) {
// Make sure the script has a type
if (fullYaml.contains(scriptName + ".TYPE")) {
String type = fullYaml.getString(scriptName + ".TYPE");
// Check that types is a registered type
if (!scriptContainerTypes.containsKey(type.toUpperCase())) {
if (shouldErrorOnType) {
Debug.log("<G>Trying to load an invalid script. '<A>" + scriptName + "<Y>(" + type + ")'<G> is an unknown type.");
ScriptHelper.setHadError();
}
else {
toPostLoadAttempt.add(scriptName);
}
return;
}
// Instantiate a new scriptContainer of specified type.
Class typeClass = scriptContainerTypes.get(type.toUpperCase());
Debug.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) {
Debug.echoError(e);
ScriptHelper.setHadError();
}
}
else {
Debug.echoError("Found type-less container: '" + scriptName + "'.");
ScriptHelper.setHadError();
}
}
public static void buildCoreYamlScriptContainers(YamlConfiguration yamlScripts) {
fullYaml = 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 scriptName : scripts) {
attemptLoadSingle(scriptName.str, false);
}
}
public static List<YamlConfiguration> outside_scripts = new ArrayList<>();
/**
* 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;
}
}
}