Added configuration and plugin data directory support to plugins.

By: sk89q <the.sk89q@gmail.com>
This commit is contained in:
Bukkit/Spigot 2011-01-14 23:33:45 -08:00
parent 0bf9731255
commit 33e5645054
4 changed files with 87 additions and 20 deletions

View File

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

View File

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

View File

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

View File

@ -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<? extends JavaPlugin> plugin = jarClass.asSubclass(JavaPlugin.class);
Constructor<? extends JavaPlugin> constructor = plugin.getConstructor(PluginLoader.class, Server.class, PluginDescriptionFile.class, File.class, ClassLoader.class);
result = constructor.newInstance(this, server, description, file, loader);
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 (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;