-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
ModulesHandler.java
218 lines (175 loc) · 9.03 KB
/
ModulesHandler.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.bgsoftware.superiorskyblock.module;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.commands.SuperiorCommand;
import com.bgsoftware.superiorskyblock.api.handlers.ModulesManager;
import com.bgsoftware.superiorskyblock.api.modules.ModuleLoadTime;
import com.bgsoftware.superiorskyblock.api.modules.PluginModule;
import com.bgsoftware.superiorskyblock.handler.AbstractHandler;
import com.bgsoftware.superiorskyblock.handler.HandlerLoadException;
import com.bgsoftware.superiorskyblock.module.container.ModulesContainer;
import com.bgsoftware.superiorskyblock.utils.FileUtils;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;
import com.google.common.base.Preconditions;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;
public final class ModulesHandler extends AbstractHandler implements ModulesManager {
private final ModulesContainer modulesContainer;
private final File modulesFolder;
private final File dataFolder;
public ModulesHandler(SuperiorSkyblockPlugin plugin, ModulesContainer modulesContainer) {
super(plugin);
this.modulesContainer = modulesContainer;
this.modulesFolder = new File(plugin.getDataFolder(), "modules");
this.dataFolder = new File(plugin.getDataFolder(), "datastore/modules");
}
@Override
public void loadData() {
if (!modulesFolder.exists())
//noinspection ResultOfMethodCallIgnored
modulesFolder.mkdirs();
registerModule(BuiltinModules.GENERATORS);
registerModule(BuiltinModules.MISSIONS);
registerModule(BuiltinModules.BANK);
registerModule(BuiltinModules.UPGRADES);
registerExternalModules();
}
@Override
public void registerModule(PluginModule pluginModule) {
Preconditions.checkNotNull(pluginModule, "pluginModule parameter cannot be null.");
this.modulesContainer.registerModule(pluginModule, modulesFolder, dataFolder, plugin);
}
@Override
public PluginModule registerModule(File moduleFile) throws IOException, ReflectiveOperationException {
if (!moduleFile.getName().endsWith(".jar"))
throw new IllegalArgumentException("The given file is not a valid jar file.");
String moduleName = moduleFile.getName().replace(".jar", "");
ModuleClassLoader moduleClassLoader = new ModuleClassLoader(moduleFile);
//noinspection deprecation
Optional<Class<?>> moduleClass = FileUtils.getClasses(moduleFile.toURL(), PluginModule.class, moduleClassLoader).stream().findFirst();
if (!moduleClass.isPresent())
throw new IllegalArgumentException("The file " + moduleName + " is not a valid module.");
PluginModule pluginModule = createInstance(moduleClass.get());
pluginModule.initModuleLoader(moduleFile, moduleClassLoader);
registerModule(pluginModule);
return pluginModule;
}
@Override
public void unregisterModule(PluginModule pluginModule) {
Preconditions.checkNotNull(pluginModule, "pluginModule parameter cannot be null.");
this.modulesContainer.unregisterModule(pluginModule, plugin);
}
@Override
@Nullable
public PluginModule getModule(String name) {
Preconditions.checkNotNull(name, "name parameter cannot be null.");
return this.modulesContainer.getModule(name);
}
@Override
public Collection<PluginModule> getModules() {
return this.modulesContainer.getModules();
}
@Override
public void enableModule(PluginModule pluginModule) {
Preconditions.checkNotNull(pluginModule, "pluginModule parameter cannot be null.");
long startTime = System.currentTimeMillis();
SuperiorSkyblockPlugin.log("&aEnabling the module " + pluginModule.getName() + "...");
try {
pluginModule.onEnable(plugin);
} catch (Exception ex) {
SuperiorSkyblockPlugin.log("&cAn error occurred while enabling the module " + pluginModule.getName() + ":");
SuperiorSkyblockPlugin.log("&cContact " + pluginModule.getAuthor() + " regarding this, this has nothing to do with the plugin.");
ex.printStackTrace();
PluginDebugger.debug(ex);
try {
// Calling onDisable so the plugin can unregister its data if needed
pluginModule.onDisable(plugin);
} catch (Throwable error) {
SuperiorSkyblockPlugin.log("&cAn error occurred while disabling the module " + pluginModule.getName() + ":");
SuperiorSkyblockPlugin.log("&cContact " + pluginModule.getAuthor() + " regarding this, this has nothing to do with the plugin.");
error.printStackTrace();
}
return;
}
Listener[] listeners = pluginModule.getModuleListeners(plugin);
SuperiorCommand[] commands = pluginModule.getSuperiorCommands(plugin);
SuperiorCommand[] adminCommands = pluginModule.getSuperiorAdminCommands(plugin);
if (listeners != null || commands != null || adminCommands != null)
this.modulesContainer.addModuleData(pluginModule, new ModuleData(listeners, commands, adminCommands));
if (listeners != null)
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, plugin));
if (commands != null)
Arrays.stream(commands).forEach(plugin.getCommands()::registerCommand);
if (adminCommands != null)
Arrays.stream(adminCommands).forEach(plugin.getCommands()::registerAdminCommand);
SuperiorSkyblockPlugin.log("&eFinished enabling the module " + pluginModule.getName() +
" (Took " + (System.currentTimeMillis() - startTime) + "ms)");
}
@Override
public void enableModules(ModuleLoadTime moduleLoadTime) {
Preconditions.checkNotNull(moduleLoadTime, "moduleLoadTime parameter cannot be null.");
filterModules(moduleLoadTime).forEach(this::enableModule);
}
public void reloadModules(SuperiorSkyblockPlugin plugin) {
getModules().forEach(pluginModule -> {
try {
pluginModule.onReload(plugin);
} catch (Throwable error) {
SuperiorSkyblockPlugin.log("&cAn error occurred while reloading the module " + pluginModule.getName() + ":");
SuperiorSkyblockPlugin.log("&cContact " + pluginModule.getAuthor() + " regarding this, this has nothing to do with the plugin.");
error.printStackTrace();
}
});
}
public void loadModulesData(SuperiorSkyblockPlugin plugin) {
getModules().forEach(pluginModule -> {
try {
pluginModule.loadData(plugin);
} catch (Throwable error) {
SuperiorSkyblockPlugin.log("&cAn error occurred while loading data for the module " + pluginModule.getName() + ":");
SuperiorSkyblockPlugin.log("&cContact " + pluginModule.getAuthor() + " regarding this, this has nothing to do with the plugin.");
error.printStackTrace();
}
});
}
private void registerExternalModules() {
File[] folderFiles = modulesFolder.listFiles();
if (folderFiles != null) {
for (File file : folderFiles) {
if (!file.isDirectory() && file.getName().endsWith(".jar")) {
try {
registerModule(file);
} catch (Exception ex) {
SuperiorSkyblockPlugin.log("Couldn't register module " + file.getName() + ": ");
HandlerLoadException handlerError = new HandlerLoadException(ex, "Couldn't register module " + file.getName() + ".",
HandlerLoadException.ErrorLevel.CONTINUE);
handlerError.printStackTrace();
PluginDebugger.debug(handlerError);
}
}
}
}
}
private Stream<PluginModule> filterModules(ModuleLoadTime moduleLoadTime) {
return this.modulesContainer.getModules().stream()
.filter(pluginModule -> pluginModule.getLoadTime() == moduleLoadTime);
}
private PluginModule createInstance(Class<?> clazz) throws ReflectiveOperationException {
Preconditions.checkArgument(PluginModule.class.isAssignableFrom(clazz), "Class " + clazz + " is not a PluginModule.");
for (Constructor<?> constructor : clazz.getConstructors()) {
if (constructor.getParameterCount() == 0) {
if (!constructor.isAccessible())
constructor.setAccessible(true);
return (PluginModule) constructor.newInstance();
}
}
throw new IllegalArgumentException("Class " + clazz + " has no valid constructors.");
}
}