-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ScriptRegistry.java
134 lines (120 loc) · 5.37 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
package com.denizenscript.denizencore.scripts;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.containers.core.*;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
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.lang.invoke.MethodHandle;
import java.util.*;
public class ScriptRegistry {
public static Map<String, ScriptContainer> scriptContainers = new HashMap<>();
public static Map<String, MethodHandle> typeConstructors = new HashMap<>();
public static void _registerType(String typeName, Class<? extends ScriptContainer> scriptContainerClass) {
typeConstructors.put(CoreUtilities.toLowerCase(typeName), ReflectionHelper.getConstructor(scriptContainerClass, YamlConfiguration.class, String.class));
}
public static void _registerCoreTypes() {
_registerType("custom", CustomScriptContainer.class);
_registerType("task", TaskScriptContainer.class);
_registerType("procedure", ProcedureScriptContainer.class);
_registerType("world", WorldScriptContainer.class);
_registerType("data", DataScriptContainer.class);
_registerType("yaml data", DataScriptContainer.class);
}
public static boolean containsScript(String id, Class scriptContainerType) {
if (!scriptContainers.containsKey(CoreUtilities.toLowerCase(id))) {
return false;
}
ScriptContainer script = scriptContainers.get(CoreUtilities.toLowerCase(id));
return scriptContainerType.isInstance(script);
}
public static ArrayList<Map.Entry<String, YamlConfiguration>> toPostLoadAttempt = new ArrayList<>();
public static void postLoadScripts() {
try {
for (Map.Entry<String, YamlConfiguration> script : toPostLoadAttempt) {
attemptLoadSingle(script.getValue(), script.getKey(), true);
}
}
finally {
toPostLoadAttempt.clear();
}
}
public static void attemptLoadSingle(YamlConfiguration script, String scriptName, boolean shouldErrorOnType) {
// Make sure the script has a type
String type = script.getString("type");
if (type == null) {
Debug.echoError("Found type-less container: '<Y>" + scriptName + "<W>'.");
ScriptHelper.setHadError();
return;
}
type = CoreUtilities.toLowerCase(type);
// Check that types is a registered type
if (!typeConstructors.containsKey(type)) {
if (shouldErrorOnType) {
Debug.echoError("Trying to load an invalid script. '<A>" + scriptName + "<Y>(" + type + ")<W>' is an unknown type.");
ScriptHelper.setHadError();
}
else {
toPostLoadAttempt.add(new AbstractMap.SimpleEntry<>(scriptName, script));
}
return;
}
MethodHandle constructor = typeConstructors.get(type);
if (Debug.showLoading) {
Debug.log("Adding script " + scriptName + " as type " + type);
}
try {
String nameLow = CoreUtilities.toLowerCase(scriptName);
if (scriptContainers.containsKey(nameLow)) {
Debug.echoError("Duplicate script name '<Y>" + scriptName + "<W>'");
}
ScriptContainer instance = (ScriptContainer) constructor.invoke(script, scriptName);
scriptContainers.put(nameLow, instance);
}
catch (Throwable ex) {
Debug.echoError(ex);
ScriptHelper.setHadError();
}
}
public static void buildCoreYamlScriptContainers(List<YamlConfiguration> yamlScripts) {
scriptContainers.clear();
OldEventManager.world_scripts.clear();
OldEventManager.events.clear();
DenizenCore.implementation.refreshScriptContainers();
if (yamlScripts == null) {
return;
}
Debug.log("Loading <A>" + yamlScripts.size() + "<W> scripts...");
for (YamlConfiguration script : yamlScripts) {
for (StringHolder key : script.contents.keySet()) {
attemptLoadSingle(script.getConfigurationSection(key.str), key.str, false);
}
}
}
public static <T extends ScriptContainer> T getScriptContainerAs(String name, Class<T> type) {
try {
ScriptContainer container = scriptContainers.get(CoreUtilities.toLowerCase(name));
if (container != null) {
return type.cast(container);
}
else {
return null;
}
}
catch (Exception e) {
}
return null;
}
public static <T extends ScriptContainer> T getScriptContainer(String name) {
ScriptContainer container = scriptContainers.get(CoreUtilities.toLowerCase(name));
if (container != null) {
return (T) container;
}
else {
return null;
}
}
}