Removed TSLPC (That Stupidly Long Plugin Constructor)

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-02-20 00:13:25 +00:00
parent 08fd2073ce
commit 4bb9f83d53
3 changed files with 66 additions and 24 deletions

View file

@ -13,12 +13,6 @@ public class Fillr extends JavaPlugin {
public static final String VERSION = "1.0";
public static final String DIRECTORY = "plugins";
public Fillr(PluginLoader pluginLoader, Server instance,
PluginDescriptionFile desc, File folder, File plugin,
ClassLoader cLoader) {
super(pluginLoader, instance, desc, folder, plugin, cLoader);
}
public void onDisable() {
}

View file

@ -2,6 +2,7 @@
package org.bukkit.plugin.java;
import java.io.File;
import java.util.ArrayList;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -15,13 +16,14 @@ import org.bukkit.util.config.Configuration;
*/
public abstract class JavaPlugin implements Plugin {
private boolean isEnabled = false;
private final PluginLoader loader;
private final Server server;
private final File file;
private final PluginDescriptionFile description;
private final File dataFolder;
private final ClassLoader classLoader;
private final Configuration config;
private boolean initialized = false;
private PluginLoader loader = null;
private Server server = null;
private File file = null;
private PluginDescriptionFile description = null;
private File dataFolder = null;
private ClassLoader classLoader = null;
private Configuration config = null;
/**
* Constructs a new Java plugin instance
@ -36,14 +38,15 @@ public abstract class JavaPlugin implements Plugin {
public JavaPlugin(PluginLoader pluginLoader, Server instance,
PluginDescriptionFile desc, File folder, File plugin,
ClassLoader cLoader) {
loader = pluginLoader;
server = instance;
file = plugin;
description = desc;
dataFolder = folder;
classLoader = cLoader;
config = new Configuration(new File(dataFolder, "config.yml"));
config.load();
System.out.println("Using the stupidly long constructor " + desc.getMain() + "(PluginLoader, Server, PluginDescriptionFile, File, File, ClassLoader) is no longer recommended. Go nag the plugin author of " + desc.getName() + " to remove it! (Nothing is broken, we just like to keep code clean.)");
ArrayList<String> authors = desc.getAuthors();
if (authors.size() > 0) {
System.out.println("Hint! It's probably someone called '" + authors.get(0) + "'");
}
}
public JavaPlugin() {
}
/**
@ -145,4 +148,43 @@ public abstract class JavaPlugin implements Plugin {
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
return false; // default implementation: do nothing!
}
/**
* Initializes this plugin with the given variables.
*
* This method should never be called manually.
*
* @param loader PluginLoader that is responsible for this plugin
* @param server Server instance that is running this plugin
* @param description PluginDescriptionFile containing metadata on this plugin
* @param dataFolder Folder containing the plugin's data
* @param file File containing this plugin
* @param classLoader ClassLoader which holds this plugin
*/
protected void initialize(PluginLoader loader, Server server,
PluginDescriptionFile description, File dataFolder, File file,
ClassLoader classLoader) {
if (initialized) {
throw new UnsupportedOperationException("Cannot reinitialize a plugin");
}
this.initialized = true;
this.loader = loader;
this.server = server;
this.file = file;
this.description = description;
this.dataFolder = dataFolder;
this.classLoader = classLoader;
this.config = new Configuration(new File(dataFolder, "config.yml"));
this.config.load();
}
/**
* Gets the initialization status of this plugin
*
* @return true if this plugin is initialized, otherwise false
*/
public boolean isInitialized() {
return initialized;
}
}

View file

@ -25,7 +25,6 @@ import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.WorldEvent;
import org.bukkit.event.world.WorldListener;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.*;
/**
@ -72,9 +71,16 @@ public final class JavaPluginLoader implements PluginLoader {
ClassLoader loader = new PluginClassLoader(this, new URL[]{file.toURI().toURL()}, getClass().getClassLoader());
Class<?> jarClass = Class.forName(description.getMain(), true, loader);
Class<? extends JavaPlugin> plugin = jarClass.asSubclass(JavaPlugin.class);
Constructor<? extends JavaPlugin> constructor = plugin.getConstructor(PluginLoader.class, Server.class, PluginDescriptionFile.class, File.class, File.class, ClassLoader.class);
result = constructor.newInstance(this, server, description, dataFolder, file, loader);
try {
Constructor<? extends JavaPlugin> constructor = plugin.getConstructor(PluginLoader.class, Server.class, PluginDescriptionFile.class, File.class, File.class, ClassLoader.class);
result = constructor.newInstance(this, server, description, dataFolder, file, loader);
} catch (NoSuchMethodException ex) {
Constructor<? extends JavaPlugin> constructor = plugin.getConstructor();
result = constructor.newInstance();
}
result.initialize(this, server, description, dataFolder, file, loader);
} catch (Throwable ex) {
throw new InvalidPluginException(ex);
}