diff --git a/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java b/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java index 7d75f577f3..a652b4bacf 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java +++ b/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java @@ -34,4 +34,22 @@ public interface PluginLoader { * @param event Event details */ public void callEvent(RegisteredListener registration, Event event); + + /** + * Enables the specified plugin + * + * Attempting to enable a plugin that is already enabled will have no effect + * + * @param plugin Plugin to enable + */ + public void enablePlugin(Plugin plugin); + + /** + * Disables the specified plugin + * + * Attempting to disable a plugin that is not enabled will have no effect + * + * @param plugin Plugin to disable + */ + public void disablePlugin(Plugin plugin); } diff --git a/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java b/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java index fcef6b8941..0b6d7a308d 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java +++ b/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java @@ -84,4 +84,22 @@ public interface PluginManager { * @param plugin Plugin to register */ public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin); + + /** + * Enables the specified plugin + * + * Attempting to enable a plugin that is already enabled will have no effect + * + * @param plugin Plugin to enable + */ + public void enablePlugin(Plugin plugin); + + /** + * Disables the specified plugin + * + * Attempting to disable a plugin that is not enabled will have no effect + * + * @param plugin Plugin to disable + */ + public void disablePlugin(Plugin plugin); } diff --git a/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java index c761f1dafe..44cc0eb9df 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java +++ b/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java @@ -161,6 +161,18 @@ public final class SimplePluginManager implements PluginManager { } } + public void enablePlugin(final Plugin plugin) { + if (!plugin.isEnabled()) { + plugin.getPluginLoader().enablePlugin(plugin); + } + } + + public void disablePlugin(final Plugin plugin) { + if (plugin.isEnabled()) { + plugin.getPluginLoader().disablePlugin(plugin); + } + } + /** * Calls a player related event with the given details * diff --git a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index 7692e3d5d6..ab5eb5bf35 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java @@ -88,4 +88,21 @@ public abstract class JavaPlugin implements Plugin { protected ClassLoader getClassLoader() { return classLoader; } + + /** + * Sets the enabled state of this plugin + * + * @param enabled true if enabled, otherwise false + */ + protected void setEnabled(final boolean enabled) { + if (isEnabled != enabled) { + isEnabled = enabled; + + if (isEnabled) { + onEnable(); + } else { + onDisable(); + } + } + } } diff --git a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 6152d9d7da..d90d6a5922 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -138,4 +138,32 @@ public final class JavaPluginLoader implements PluginLoader { } } } + + public void enablePlugin(final Plugin plugin) { + if (!(plugin instanceof JavaPlugin)) { + throw new IllegalArgumentException("Plugin is not associated with this PluginLoader"); + } + + if (plugin.isEnabled()) { + JavaPlugin jPlugin = (JavaPlugin)plugin; + + server.getPluginManager().callEvent(new PluginEvent(Event.Type.PLUGIN_ENABLE, plugin)); + + jPlugin.setEnabled(true); + } + } + + public void disablePlugin(Plugin plugin) { + if (!(plugin instanceof JavaPlugin)) { + throw new IllegalArgumentException("Plugin is not associated with this PluginLoader"); + } + + if (!plugin.isEnabled()) { + JavaPlugin jPlugin = (JavaPlugin)plugin; + + server.getPluginManager().callEvent(new PluginEvent(Event.Type.PLUGIN_DISABLE, plugin)); + + jPlugin.setEnabled(false); + } + } }