From 33e56450543dcb041977904f7e235b0bd6d71d41 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Fri, 14 Jan 2011 23:33:45 -0800 Subject: [PATCH] Added configuration and plugin data directory support to plugins. By: sk89q --- .../src/main/java/org/bukkit/fillr/Fillr.java | 34 ++++++++++--------- .../main/java/org/bukkit/plugin/Plugin.java | 16 +++++++++ .../org/bukkit/plugin/java/JavaPlugin.java | 29 +++++++++++++++- .../bukkit/plugin/java/JavaPluginLoader.java | 28 +++++++++++++-- 4 files changed, 87 insertions(+), 20 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 4cb0654d92..185779c1fd 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Fillr.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Fillr.java @@ -8,24 +8,26 @@ import org.bukkit.event.*; import java.io.File; public class Fillr extends JavaPlugin { - private FillrListener listener; - public static final String NAME = "Fillr"; - public static final String VERSION = "1.0"; - public static final String DIRECTORY = "plugins"; + private FillrListener listener; + public static final String NAME = "Fillr"; + public static final String VERSION = "1.0"; + public static final String DIRECTORY = "plugins"; - public Fillr(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) { - super(pluginLoader, instance, desc, plugin, cLoader); - } + public Fillr(PluginLoader pluginLoader, Server instance, + PluginDescriptionFile desc, File folder, File plugin, + ClassLoader cLoader) { + super(pluginLoader, instance, desc, folder, plugin, cLoader); + } - public void onDisable() { - } + public void onDisable() { + } - public void onEnable() { - registerEvents(); - } + public void onEnable() { + registerEvents(); + } - private void registerEvents() { - listener = new FillrListener(getServer()); - getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this); - } + private void registerEvents() { + listener = new FillrListener(getServer()); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this); + } } diff --git a/paper-api/src/main/java/org/bukkit/plugin/Plugin.java b/paper-api/src/main/java/org/bukkit/plugin/Plugin.java index e9097cdbde..9084f38fa1 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/Plugin.java +++ b/paper-api/src/main/java/org/bukkit/plugin/Plugin.java @@ -1,18 +1,34 @@ package org.bukkit.plugin; +import java.io.File; import org.bukkit.Server; +import org.bukkit.util.config.Configuration; /** * Represents a Plugin */ public interface Plugin { + /** + * Returns the folder that the plugin data's files are located in. + * + * @return + */ + public File getDataFolder(); + /** * Returns the plugin.yaml file containing the details for this plugin * * @return Contents of the plugin.yaml file */ public PluginDescriptionFile getDescription(); + + /** + * Returns the main configuration file. It should be loaded. + * + * @return + */ + public Configuration getConfiguration(); /** * Gets the associated PluginLoader responsible for this plugin 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 ba2bc2325a..f8f97d1399 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 @@ -6,6 +6,7 @@ import org.bukkit.Server; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoader; +import org.bukkit.util.config.Configuration; /** * Represents a Java plugin @@ -16,6 +17,7 @@ public abstract class JavaPlugin implements Plugin { private final Server server; private final File file; private final PluginDescriptionFile description; + private final File dataFolder; private final ClassLoader classLoader; /** @@ -24,17 +26,30 @@ public abstract class JavaPlugin implements Plugin { * @param pluginLoader PluginLoader that is responsible for this plugin * @param instance Server instance that is running this plugin * @param desc PluginDescriptionFile containing metadata on this plugin + * @param folder Folder containing the plugin's data * @param plugin File containing this plugin * @param cLoader ClassLoader which holds this plugin */ - public JavaPlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) { + 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; } + /** + * Returns the folder that the plugin data's files are located in. + * + * @return + */ + public File getDataFolder() { + return dataFolder; + } + /** * Gets the associated PluginLoader responsible for this plugin * @@ -79,6 +94,18 @@ public abstract class JavaPlugin implements Plugin { public PluginDescriptionFile getDescription() { return description; } + + /** + * Returns the main configuration file. It will be loaded. + * + * @return + */ + public Configuration getConfiguration() { + Configuration config = + new Configuration(new File(dataFolder, "config.yaml")); + config.load(); + return config; + } /** * Returns the ClassLoader which holds this plugin 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 c0cef03ab9..8afc75dbe9 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 @@ -69,19 +69,41 @@ public final class JavaPluginLoader implements PluginLoader { throw new InvalidPluginException(ex); } + File dataFolder = getDataFolder(file); + try { 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, ClassLoader.class); - - result = constructor.newInstance(this, server, description, file, loader); + 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 (Throwable ex) { throw new InvalidPluginException(ex); } return (Plugin)result; } + + private File getDataFolder(File file) { + File dataFolder = null; + + String filename = file.getName(); + int index = file.getName().lastIndexOf("."); + + if (index != -1) { + String name = filename.substring(0, index); + dataFolder = new File(file.getParentFile(), name); + } else { + // This is if there is no extension, which should not happen + // Using _ to prevent name collision + dataFolder = new File(file.getParentFile(), filename + "_"); + } + + dataFolder.mkdirs(); + + return dataFolder; + } public Pattern[] getPluginFileFilters() { return fileFilters;