Transition to Maven

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
Bukkit/Spigot
2011-01-01 11:23:14 +01:00
parent e8a16bb993
commit a0fc0979f7
42 changed files with 31 additions and 5 deletions

View File

@@ -0,0 +1,36 @@
package org.bukkit.plugin;
/**
* Thrown when attempting to load an invalid PluginDescriptionFile
*/
public class InvalidDescriptionException extends Exception {
private static final long serialVersionUID = 5721389122281775894L;
private final Throwable cause;
/**
* Constructs a new InvalidDescriptionException based on the given Exception
*
* @param throwable Exception that triggered this Exception
*/
public InvalidDescriptionException(Throwable throwable) {
cause = throwable;
}
/**
* Constructs a new InvalidDescriptionException
*/
public InvalidDescriptionException() {
cause = null;
}
/**
* If applicable, returns the Exception that triggered this Exception
*
* @return Inner exception, or null if one does not exist
*/
@Override
public Throwable getCause() {
return cause;
}
}

View File

@@ -0,0 +1,36 @@
package org.bukkit.plugin;
/**
* Thrown when attempting to load an invalid Plugin file
*/
public class InvalidPluginException extends Exception {
private static final long serialVersionUID = -8242141640709409542L;
private final Throwable cause;
/**
* Constructs a new InvalidPluginException based on the given Exception
*
* @param throwable Exception that triggered this Exception
*/
public InvalidPluginException(Throwable throwable) {
cause = throwable;
}
/**
* Constructs a new InvalidPluginException
*/
public InvalidPluginException() {
cause = null;
}
/**
* If applicable, returns the Exception that triggered this Exception
*
* @return Inner exception, or null if one does not exist
*/
@Override
public Throwable getCause() {
return cause;
}
}

View File

@@ -0,0 +1,47 @@
package org.bukkit.plugin;
import org.bukkit.Server;
/**
* Represents a Plugin
*/
public interface Plugin {
/**
* Returns the plugin.yaml file containing the details for this plugin
*
* @return Contents of the plugin.yaml file
*/
public PluginDescriptionFile getDescription();
/**
* Gets the associated PluginLoader responsible for this plugin
*
* @return PluginLoader that controls this plugin
*/
public PluginLoader getPluginLoader();
/**
* Returns the Server instance currently running this plugin
*
* @return Server running this plugin
*/
public Server getServer();
/**
* Returns a value indicating whether or not this plugin is currently enabled
*
* @return true if this plugin is enabled, otherwise false
*/
public boolean isEnabled();
/**
* Called when this plugin is disabled
*/
public void onDisable();
/**
* Called when this plugin is enabled
*/
public void onEnable();
}

View File

@@ -0,0 +1,87 @@
package org.bukkit.plugin;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
/**
* Provides access to a Plugins description file, plugin.yaml
*/
public final class PluginDescriptionFile {
private static final Yaml yaml = new Yaml(new SafeConstructor());
private String name = null;
private String main = null;
public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException {
try {
loadMap((Map<String, Object>)yaml.load(stream));
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex);
}
}
/**
* Loads a PluginDescriptionFile from the specified reader
* @param reader
*/
public PluginDescriptionFile(final Reader reader) {
loadMap((Map<String, Object>)yaml.load(reader));
}
/**
* Creates a new PluginDescriptionFile with the given detailed
*
* @param pluginName Name of this plugin
* @param mainClass Full location of the main class of this plugin
*/
public PluginDescriptionFile(final String pluginName, final String mainClass) {
name = pluginName;
main = mainClass;
}
/**
* Saves this PluginDescriptionFile to the given writer
*
* @param writer Writer to output this file to
*/
public void save(Writer writer) {
yaml.dump(saveMap(), writer);
}
/**
* Returns the name of a plugin
*
* @return String name
*/
public String getName() {
return name;
}
/**
* Returns the main class for a plugin
*
* @return Java classpath
*/
public String getMain() {
return main;
}
private void loadMap(Map<String, Object> map) throws ClassCastException {
name = (String)map.get("name");
main = (String)map.get("main");
}
private Map<String, Object> saveMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("main", main);
return map;
}
}

View File

@@ -0,0 +1,37 @@
package org.bukkit.plugin;
import java.io.File;
import java.util.regex.Pattern;
import org.bukkit.event.Event;
/**
* Represents a plugin loader, which handles direct access to specific types
* of plugins
*/
public interface PluginLoader {
/**
* Loads the plugin contained in the specified file
*
* @param file File to attempt to load
* @return Plugin that was contained in the specified file, or null if
* unsuccessful
* @throws InvalidPluginException Thrown when the specified file is not a plugin
*/
public Plugin loadPlugin(File file) throws InvalidPluginException;
/**
* Returns a list of all filename filters expected by this PluginLoader
*/
public Pattern[] getPluginFileFilters();
/**
* Calls a player related event with the given details
*
* @param registration Registered information on the plugin to call about this event
* @param type Type of player related event to call
* @param event Event details
*/
public void callEvent(RegisteredListener registration, Event event);
}

View File

@@ -0,0 +1,87 @@
package org.bukkit.plugin;
import java.io.File;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Listener;
/**
* Handles all plugin management from the Server
*/
public interface PluginManager {
/**
* Registers the specified plugin loader
*
* @param loader Class name of the PluginLoader to register
* @throws IllegalArgumentException Thrown when the given Class is not a valid PluginLoader
*/
public void RegisterInterface(Class loader) throws IllegalArgumentException;
/**
* Checks if the given plugin is loaded and returns it when applicable
*
* Please note that the name of the plugin is case-sensitive
*
* @param name Name of the plugin to check
* @return Plugin if it exists, otherwise null
*/
public Plugin getPlugin(String name);
/**
* Checks if the given plugin is enabled or not
*
* Please note that the name of the plugin is case-sensitive.
*
* @param name Name of the plugin to check
* @return true if the plugin is enabled, otherwise false
*/
public boolean isPluginEnabled(String name);
/**
* Checks if the given plugin is enabled or not
*
* @param plugin Plugin to check
* @return true if the plugin is enabled, otherwise false
*/
public boolean isPluginEnabled(Plugin plugin);
/**
* Loads the plugin in the specified file
*
* File must be valid according to the current enabled Plugin interfaces
*
* @param file File containing the plugin to load
* @return The Plugin loaded, or null if it was invalid
* @throws InvalidPluginException Thrown when the specified file is not a valid plugin
*/
public Plugin loadPlugin(File file) throws InvalidPluginException;
/**
* Loads the plugins contained within the specified directory
*
* @param directory Directory to check for plugins
* @return A list of all plugins loaded
*/
public Plugin[] loadPlugins(File directory);
/**
* Calls a player related event with the given details
*
* @param type Type of player related event to call
* @param event Event details
*/
public void callEvent(Event event);
/**
* Registers the given event to the specified listener
*
* @param type EventType to register
* @param listener Listener to register
* @param priority Priority of this event
* @param plugin Plugin to register
*/
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin);
}

View File

@@ -0,0 +1,44 @@
package org.bukkit.plugin;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
/**
* Stores relevant information for plugin listeners
*/
public class RegisteredListener {
private final Listener listener;
private final Event.Priority priority;
private final Plugin plugin;
public RegisteredListener(final Listener pluginListener, final Event.Priority eventPriority, final Plugin registeredPlugin) {
listener = pluginListener;
priority = eventPriority;
plugin = registeredPlugin;
}
/**
* Gets the listener for this registration
* @return Registered Listener
*/
public Listener getListener() {
return listener;
}
/**
* Gets the plugin for this registration
* @return Registered Plugin
*/
public Plugin getPlugin() {
return plugin;
}
/**
* Gets the priority for this registration
* @return Registered Priority
*/
public Event.Priority getPriority() {
return priority;
}
}

View File

@@ -0,0 +1,216 @@
package org.bukkit.plugin;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import org.bukkit.Server;
import java.util.regex.Pattern;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Listener;
/**
* Handles all plugin management from the Server
*/
public final class SimplePluginManager implements PluginManager {
private final Server server;
private final Map<Pattern, PluginLoader> fileAssociations = new HashMap<Pattern, PluginLoader>();
private final List<Plugin> plugins = new ArrayList<Plugin>();
private final Map<String, Plugin> lookupNames = new HashMap<String, Plugin>();
private final Map<Event.Type, List<RegisteredListener>> listeners = new EnumMap<Event.Type, List<RegisteredListener>>(Event.Type.class);
public SimplePluginManager(Server instance) {
server = instance;
}
/**
* Registers the specified plugin loader
*
* @param loader Class name of the PluginLoader to register
* @throws IllegalArgumentException Thrown when the given Class is not a valid PluginLoader
*/
public void RegisterInterface(Class loader) throws IllegalArgumentException {
PluginLoader instance;
if (PluginLoader.class.isAssignableFrom(loader)) {
Constructor constructor;
try {
constructor = loader.getConstructor(Server.class);
instance = (PluginLoader) constructor.newInstance(server);
} catch (NoSuchMethodException ex) {
throw new IllegalArgumentException(String.format("Class %s does not have a public %s(Server) constructor", loader.getName()), ex);
} catch (Exception ex) {
throw new IllegalArgumentException(String.format("Unexpected exception %s while attempting to construct a new instance of %s", ex.getClass().getName(), loader.getName()), ex);
}
} else {
throw new IllegalArgumentException(String.format("Class %s does not implement interface PluginLoader", loader.getName()));
}
Pattern[] patterns = instance.getPluginFileFilters();
for (Pattern pattern : patterns) {
fileAssociations.put(pattern, instance);
}
}
/**
* Loads the plugins contained within the specified directory
*
* @param directory Directory to check for plugins
* @return A list of all plugins loaded
*/
public Plugin[] loadPlugins(File directory) {
List<Plugin> result = new ArrayList<Plugin>();
File[] files = directory.listFiles();
for (File file : files) {
Plugin plugin = null;
try {
plugin = loadPlugin(file);
} catch (InvalidPluginException ex) {
Logger.getLogger(SimplePluginManager.class.getName()).log(Level.SEVERE, "Could not load " + file.getPath() + " in " + directory.getPath(), ex);
}
if (plugin != null) {
result.add(plugin);
}
}
return result.toArray(new Plugin[result.size()]);
}
/**
* Loads the plugin in the specified file
*
* File must be valid according to the current enabled Plugin interfaces
*
* @param file File containing the plugin to load
* @return The Plugin loaded, or null if it was invalid
* @throws InvalidPluginException Thrown when the specified file is not a valid plugin
*/
public Plugin loadPlugin(File file) throws InvalidPluginException {
Set<Pattern> filters = fileAssociations.keySet();
Plugin result = null;
for (Pattern filter : filters) {
String name = file.getName();
Matcher match = filter.matcher(name);
if (match.find()) {
PluginLoader loader = fileAssociations.get(filter);
result = loader.loadPlugin(file);
}
}
if (result != null) {
plugins.add(result);
lookupNames.put(result.getDescription().getName(), result);
}
return result;
}
/**
* Checks if the given plugin is loaded and returns it when applicable
*
* Please note that the name of the plugin is case-sensitive
*
* @param name Name of the plugin to check
* @return Plugin if it exists, otherwise null
*/
public Plugin getPlugin(String name) {
return lookupNames.get(name);
}
/**
* Checks if the given plugin is enabled or not
*
* Please note that the name of the plugin is case-sensitive.
*
* @param name Name of the plugin to check
* @return true if the plugin is enabled, otherwise false
*/
public boolean isPluginEnabled(String name) {
Plugin plugin = getPlugin(name);
return isPluginEnabled(plugin);
}
/**
* Checks if the given plugin is enabled or not
*
* @param plugin Plugin to check
* @return true if the plugin is enabled, otherwise false
*/
public boolean isPluginEnabled(Plugin plugin) {
if ((plugin != null) && (plugins.contains(plugin))) {
return plugin.isEnabled();
} else {
return false;
}
}
/**
* Calls a player related event with the given details
*
* @param type Type of player related event to call
* @param event Event details
*/
public void callEvent(Event event) {
List<RegisteredListener> eventListeners = listeners.get(event.getType());
if (eventListeners != null) {
for (RegisteredListener registration : eventListeners) {
Plugin plugin = registration.getPlugin();
PluginLoader loader = plugin.getPluginLoader();
if (plugin.isEnabled()) {
try {
loader.callEvent(registration, event);
} catch (Throwable ex) {
Logger.getLogger(SimplePluginManager.class.getName()).log(Level.SEVERE, "Could not pass event " + event.getType() + " to " + plugin.getDescription().getName(), ex);
}
}
}
}
}
/**
* Registers the given event to the specified listener
*
* @param type EventType to register
* @param listener PlayerListener to register
* @param priority Priority of this event
* @param plugin Plugin to register
*/
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) {
List<RegisteredListener> eventListeners = listeners.get(type);
int position = 0;
if (eventListeners != null) {
for (RegisteredListener registration : eventListeners) {
if (registration.getPriority().compareTo(priority) < 0) {
break;
}
position++;
}
} else {
eventListeners = new ArrayList<RegisteredListener>();
listeners.put(type, eventListeners);
}
eventListeners.add(position, new RegisteredListener(listener, priority, plugin));
}
}

View File

@@ -0,0 +1,91 @@
package org.bukkit.plugin.java;
import java.io.File;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
/**
* Represents a Java plugin
*/
public abstract class JavaPlugin implements Plugin {
private boolean isEnabled = true;
private final PluginLoader loader;
private final Server server;
private final File file;
private final PluginDescriptionFile description;
private final ClassLoader classLoader;
/**
* Constructs a new Java plugin instance
*
* @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 plugin File containing this plugin
* @param cLoader ClassLoader which holds this plugin
*/
public JavaPlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) {
loader = pluginLoader;
server = instance;
file = plugin;
description = desc;
classLoader = cLoader;
}
/**
* Gets the associated PluginLoader responsible for this plugin
*
* @return PluginLoader that controls this plugin
*/
public final PluginLoader getPluginLoader() {
return loader;
}
/**
* Returns the Server instance currently running this plugin
*
* @return Server running this plugin
*/
public final Server getServer() {
return server;
}
/**
* Returns a value indicating whether or not this plugin is currently enabled
*
* @return true if this plugin is enabled, otherwise false
*/
public final boolean isEnabled() {
return isEnabled;
}
/**
* Returns the file which contains this plugin
*
* @return File containing this plugin
*/
protected File getFile() {
return file;
}
/**
* Returns the plugin.yaml file containing the details for this plugin
*
* @return Contents of the plugin.yaml file
*/
public PluginDescriptionFile getDescription() {
return description;
}
/**
* Returns the ClassLoader which holds this plugin
*
* @return ClassLoader holding this plugin
*/
protected ClassLoader getClassLoader() {
return classLoader;
}
}

View File

@@ -0,0 +1,123 @@
package org.bukkit.plugin.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import org.bukkit.Server;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;
import org.bukkit.plugin.*;
/**
* Represents a Java plugin loader, allowing plugins in the form of .jar
*/
public final class JavaPluginLoader implements PluginLoader {
private final Server server;
private final Pattern[] fileFilters = new Pattern[] {
Pattern.compile("\\.jar$"),
};
public JavaPluginLoader(Server instance) {
server = instance;
}
public Plugin loadPlugin(File file) throws InvalidPluginException {
JavaPlugin result = null;
PluginDescriptionFile description = null;
if (!file.exists()) {
throw new InvalidPluginException(new FileNotFoundException(String.format("%s does not exist", file.getPath())));
}
try {
JarFile jar = new JarFile(file);
JarEntry entry = jar.getJarEntry("plugin.yml");
if (entry == null) {
throw new InvalidPluginException(new FileNotFoundException("Jar does not contain plugin.yml"));
}
InputStream stream = jar.getInputStream(entry);
description = new PluginDescriptionFile(stream);
stream.close();
jar.close();
} catch (IOException ex) {
throw new InvalidPluginException(ex);
} catch (InvalidDescriptionException ex) {
throw new InvalidPluginException(ex);
}
try {
ClassLoader loader = URLClassLoader.newInstance(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);
} catch (Throwable ex) {
throw new InvalidPluginException(ex);
}
return (Plugin)result;
}
public Pattern[] getPluginFileFilters() {
return fileFilters;
}
public void callEvent(RegisteredListener registration, Event event) {
Listener listener = registration.getListener();
if (listener instanceof PlayerListener) {
PlayerListener trueListener = (PlayerListener)listener;
switch (event.getType()) {
case PLAYER_JOIN:
trueListener.onPlayerJoin((PlayerEvent)event);
break;
case PLAYER_QUIT:
trueListener.onPlayerQuit((PlayerEvent)event);
break;
case PLAYER_COMMAND:
trueListener.onPlayerCommand((PlayerChatEvent)event);
break;
case PLAYER_CHAT:
trueListener.onPlayerChat((PlayerChatEvent)event);
break;
case PLAYER_MOVE:
trueListener.onPlayerMove((PlayerMoveEvent)event);
break;
case PLAYER_TELEPORT:
trueListener.onPlayerTeleport((PlayerMoveEvent)event);
break;
case PLAYER_LOGIN:
trueListener.onPlayerLogin((PlayerLoginEvent)event);
break;
}
} else if (listener instanceof BlockListener) {
BlockListener trueListener = (BlockListener)listener;
switch (event.getType()) {
case BLOCK_PHYSICS:
trueListener.onBlockPhysics((BlockPhysicsEvent)event);
break;
case BLOCK_CANBUILD:
trueListener.onBlockCanBuild((BlockCanBuildEvent)event);
break;
case BLOCK_FLOW:
trueListener.onBlockFlow((BlockFromToEvent)event);
break;
}
}
}
}