-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Supported.java
81 lines (73 loc) · 3.4 KB
/
Supported.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
package net.gnomeffinway.depenizen.support;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.gnomeffinway.depenizen.Depenizen;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import java.net.*;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Supported {
private final static Class<Supported> clazz = Supported.class;
private static Depenizen depenizen;
private static SupportManager supportManager;
private static Map<String, Class<? extends Support>> supportClasses;
public static void setup(Depenizen dep, PluginManager pluginManager, ClassLoader loader) {
depenizen = dep;
getSupportClasses(loader);
supportManager = new SupportManager(depenizen);
for (String name : depenizen.getDescription().getSoftDepend()) {
if (set(name.toUpperCase(), pluginManager.getPlugin(name)))
Depenizen.depenizenLog(name + " hooked, enabling add-ons.");
else
Depenizen.depenizenLog(name + " not found, add-ons will not enable.");
}
supportManager.registerNewObjects();
}
private static boolean set(String name, Plugin plugin) {
try {
if (plugin == null || !plugin.isEnabled()) return false;
supportManager.register(supportClasses.get(name).newInstance().setPlugin(plugin));
return true;
} catch (IllegalAccessException e) {
dB.echoError("Report this error to Morphan1 or the Denizen dev team: SupportedIllegalAccess");
dB.echoError(e);
} catch (InstantiationException e) {
dB.echoError("Report this error to Morphan1 or the Denizen dev team: SupportedInstantiationSupport" + name);
dB.echoError(e);
}
return false;
}
public static Support get(String plugin) {
return supportManager.supported.get(plugin.toUpperCase());
}
private static void getSupportClasses(ClassLoader loader) {
supportClasses = new HashMap<String, Class<? extends Support>>();
final String pkg = "net.gnomeffinway.depenizen.support.plugins";
try {
final Enumeration<URL> resources = loader.getResources(pkg.replace('.', '/'));
URL url;
while (resources.hasMoreElements()) {
url = resources.nextElement();
if (url == null) continue;
URLConnection conn = url.openConnection();
if (conn instanceof JarURLConnection) {
final JarFile jarFile = ((JarURLConnection) conn).getJarFile();
final Enumeration<JarEntry> entries = jarFile.entries();
String name;
while (entries.hasMoreElements()) {
name = entries.nextElement().getName().replace('/', '.');
if (name.startsWith(pkg) && name.endsWith("Support.class")) {
String key = name.replace("Support.class", "");
key = key.substring(key.lastIndexOf('.')+1).toUpperCase();
supportClasses.put(key,
(Class<? extends Support>) Class.forName(name.substring(0, name.lastIndexOf('.'))));
}
}
}
}
} catch (Exception e) {
dB.echoError(e);
}
}
}