Added enablePlugin and disablePlugin, triggers events

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-01-03 02:20:11 +00:00
parent 91e76c3c32
commit f436f334ed
5 changed files with 93 additions and 0 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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
*

View file

@ -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();
}
}
}
}

View file

@ -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);
}
}
}