mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-15 08:25:21 +01:00
Plugin enabling & disabling
This commit is contained in:
@@ -7,22 +7,18 @@ import emu.grasscutter.server.game.GameServer;
|
||||
* The base class for all plugins to extend.
|
||||
*/
|
||||
public abstract class Plugin {
|
||||
private final PluginIdentifier identifier;
|
||||
private PluginIdentifier identifier;
|
||||
|
||||
/**
|
||||
* Empty constructor for developers.
|
||||
* Should not be called by users.
|
||||
*/
|
||||
public Plugin() {
|
||||
this(new PluginIdentifier("", "", "", new String[]{}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for plugins.
|
||||
* This method is reflected into.
|
||||
*
|
||||
* Set plugin variables.
|
||||
* @param identifier The plugin's identifier.
|
||||
*/
|
||||
public Plugin(PluginIdentifier identifier) {
|
||||
this.identifier = identifier;
|
||||
private void initializePlugin(PluginIdentifier identifier) {
|
||||
if(this.identifier == null)
|
||||
this.identifier = identifier;
|
||||
else Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,13 +5,13 @@ import emu.grasscutter.utils.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Manages the server's plugins & the event system.
|
||||
@@ -42,7 +42,7 @@ public final class PluginManager {
|
||||
|
||||
List<File> plugins = Arrays.stream(files)
|
||||
.filter(file -> file.getName().endsWith(".jar"))
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
plugins.forEach(plugin -> {
|
||||
try {
|
||||
@@ -59,9 +59,8 @@ public final class PluginManager {
|
||||
}
|
||||
|
||||
Class<?> pluginClass = loader.loadClass(pluginConfig.mainClass);
|
||||
Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor(PluginIdentifier.class)
|
||||
.newInstance(PluginIdentifier.fromPluginConfig(pluginConfig));
|
||||
this.loadPlugin(pluginInstance);
|
||||
Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor().newInstance();
|
||||
this.loadPlugin(pluginInstance, PluginIdentifier.fromPluginConfig(pluginConfig));
|
||||
|
||||
fileReader.close(); // Close the file reader.
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
@@ -77,11 +76,20 @@ public final class PluginManager {
|
||||
* Load the specified plugin.
|
||||
* @param plugin The plugin instance.
|
||||
*/
|
||||
private void loadPlugin(Plugin plugin) {
|
||||
Grasscutter.getLogger().info("Loading plugin: " + plugin.getName());
|
||||
private void loadPlugin(Plugin plugin, PluginIdentifier identifier) {
|
||||
Grasscutter.getLogger().info("Loading plugin: " + identifier.name);
|
||||
|
||||
// Add the plugin's identifier.
|
||||
try {
|
||||
Class<Plugin> pluginClass = Plugin.class;
|
||||
Method method = pluginClass.getDeclaredMethod("initializePlugin", PluginIdentifier.class);
|
||||
method.setAccessible(true); method.invoke(plugin, identifier); method.setAccessible(false);
|
||||
} catch (Exception ignored) {
|
||||
Grasscutter.getLogger().warn("Failed to add plugin identifier: " + identifier.name);
|
||||
}
|
||||
|
||||
// Add the plugin to the list of loaded plugins.
|
||||
this.plugins.put(plugin.getName(), plugin);
|
||||
this.plugins.put(identifier.name, plugin);
|
||||
// Call the plugin's onLoad method.
|
||||
plugin.onLoad();
|
||||
}
|
||||
@@ -90,13 +98,19 @@ public final class PluginManager {
|
||||
* Enables all registered plugins.
|
||||
*/
|
||||
public void enablePlugins() {
|
||||
|
||||
this.plugins.forEach((name, plugin) -> {
|
||||
Grasscutter.getLogger().info("Enabling plugin: " + name);
|
||||
plugin.onEnable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables all registered plugins.
|
||||
*/
|
||||
public void disablePlugins() {
|
||||
|
||||
this.plugins.forEach((name, plugin) -> {
|
||||
Grasscutter.getLogger().info("Disabling plugin: " + name);
|
||||
plugin.onDisable();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user