From 4bb9f83d53afb8cc219f2465ba9d7851665ac72c Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sun, 20 Feb 2011 00:13:25 +0000 Subject: [PATCH] Removed TSLPC (That Stupidly Long Plugin Constructor) By: Dinnerbone --- .../src/main/java/org/bukkit/fillr/Fillr.java | 6 -- .../org/bukkit/plugin/java/JavaPlugin.java | 72 +++++++++++++++---- .../bukkit/plugin/java/JavaPluginLoader.java | 12 +++- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/fillr/Fillr.java b/paper-api/src/main/java/org/bukkit/fillr/Fillr.java index 185779c1fd..d192a3d001 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Fillr.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Fillr.java @@ -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() { } 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 5c47d39632..14b526c1bd 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 @@ -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 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; + } } 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 7c932bb3c6..bb9ddf185f 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 @@ -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 plugin = jarClass.asSubclass(JavaPlugin.class); - Constructor 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 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 constructor = plugin.getConstructor(); + result = constructor.newInstance(); + } + + result.initialize(this, server, description, dataFolder, file, loader); } catch (Throwable ex) { throw new InvalidPluginException(ex); }