All code to call events and delegate back to plugins

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2010-12-26 00:21:00 +00:00
parent 130fc2576a
commit acc9067060
9 changed files with 187 additions and 13 deletions

View file

@ -13,7 +13,41 @@ public abstract class Event {
server = instance;
}
/**
* Gets the Server instance that triggered this event
* @return Server which this event was triggered on
*/
public final Server getServer() {
return server;
}
/**
* Represents an events priority
*/
public enum Priority {
/**
* Event is critical and must be called near-first
*/
Highest,
/**
* Event is of high importance
*/
High,
/**
* Event is neither important or unimportant, and may be ran normally
*/
Normal,
/**
* Event is of low importance
*/
Low,
/**
* Event is of extremely low importance, most likely just to monitor events, and must be run near-last
*/
Lowest
}
}

View file

@ -0,0 +1,9 @@
package org.bukkit.event;
/**
* Simple interface for tagging all EventListeners
*/
public interface Listener {
}

View file

@ -16,7 +16,25 @@ public abstract class PlayerEvent extends Event {
player = who;
}
/**
* Returns the player involved in this event
* @return Player who is involved in this event
*/
public final Player getPlayer() {
return player;
}
/**
* Represents the different types of events
*/
public enum EventType {
/**
* A player joins a server
*/
Join,
/**
* A player leaves a server
*/
Quit
}
}

View file

@ -1,13 +1,24 @@
package org.bukkit.event.player;
import org.bukkit.event.Listener;
/**
* Handles all events thrown in relation to a Player
*/
public abstract class PlayerListener {
public abstract class PlayerListener implements Listener {
private PlayerListener() {
}
/**
* Called when a player joins a server
* @param event Relevant event details
*/
public abstract void onPlayerJoin(PlayerJoinEvent event);
/**
* Called when a player leaves a server
* @param event Relevant event details
*/
public abstract void onPlayerQuit(PlayerQuitEvent event);
}

View file

@ -3,6 +3,7 @@ package org.bukkit.plugin;
import java.io.File;
import java.util.regex.Pattern;
import org.bukkit.event.player.PlayerEvent;
/**
* Represents a plugin loader, which handles direct access to specific types
@ -23,4 +24,13 @@ public interface PluginLoader {
* 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, PlayerEvent.EventType type, PlayerEvent event);
}

View file

@ -2,6 +2,7 @@
package org.bukkit.plugin;
import java.io.File;
import org.bukkit.event.player.PlayerEvent;
/**
* Handles all plugin management from the Server
@ -14,7 +15,7 @@ public interface PluginManager {
* @param loader Class name of the PluginLoader to register
* @throws IllegalArgumentException Thrown when the given Class is not a valid PluginLoader
*/
void RegisterInterface(Class loader) throws IllegalArgumentException;
public void RegisterInterface(Class loader) throws IllegalArgumentException;
/**
* Checks if the given plugin is loaded and returns it when applicable
@ -24,7 +25,7 @@ public interface PluginManager {
* @param name Name of the plugin to check
* @return Plugin if it exists, otherwise null
*/
Plugin getPlugin(String name);
public Plugin getPlugin(String name);
/**
* Checks if the given plugin is enabled or not
@ -34,7 +35,7 @@ public interface PluginManager {
* @param name Name of the plugin to check
* @return true if the plugin is enabled, otherwise false
*/
boolean isPluginEnabled(String name);
public boolean isPluginEnabled(String name);
/**
* Checks if the given plugin is enabled or not
@ -42,7 +43,7 @@ public interface PluginManager {
* @param plugin Plugin to check
* @return true if the plugin is enabled, otherwise false
*/
boolean isPluginEnabled(Plugin plugin);
public boolean isPluginEnabled(Plugin plugin);
/**
* Loads the plugin in the specified file
@ -53,7 +54,7 @@ public interface PluginManager {
* @return The Plugin loaded, or null if it was invalid
* @throws InvalidPluginException Thrown when the specified file is not a valid plugin
*/
Plugin loadPlugin(File file) throws InvalidPluginException;
public Plugin loadPlugin(File file) throws InvalidPluginException;
/**
* Loads the plugins contained within the specified directory
@ -61,5 +62,13 @@ public interface PluginManager {
* @param directory Directory to check for plugins
* @return A list of all plugins loaded
*/
Plugin[] loadPlugins(File directory);
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(PlayerEvent.EventType type, PlayerEvent event);
}

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

@ -4,6 +4,7 @@ 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;
@ -13,6 +14,7 @@ import java.util.logging.Logger;
import java.util.regex.Matcher;
import org.bukkit.Server;
import java.util.regex.Pattern;
import org.bukkit.event.player.PlayerEvent;
/**
* Handles all plugin management from the Server
@ -22,6 +24,7 @@ public final class SimplePluginManager implements PluginManager {
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<PlayerEvent.EventType, List<RegisteredListener>> playerListeners = new EnumMap<PlayerEvent.EventType, List<RegisteredListener>>(PlayerEvent.EventType.class);
public SimplePluginManager(Server instance) {
server = instance;
@ -154,4 +157,25 @@ public final class SimplePluginManager implements PluginManager {
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(PlayerEvent.EventType type, PlayerEvent event) {
List<RegisteredListener> listeners = playerListeners.get(type);
if (listeners != null) {
for (RegisteredListener registration : listeners) {
Plugin plugin = registration.getPlugin();
PluginLoader loader = plugin.getPluginLoader();
if (plugin.isEnabled()) {
loader.callEvent(registration, type, event);
}
}
}
}
}

View file

@ -8,18 +8,17 @@ import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginLoader;
import java.util.regex.Pattern;
import org.bukkit.Server;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;
import org.bukkit.plugin.*;
/**
* Represents a Java plugin loader, allowing plugins in the form of .jars
* Represents a Java plugin loader, allowing plugins in the form of .jar
*/
public final class JavaPluginLoader implements PluginLoader {
private final Server server;
@ -75,4 +74,20 @@ public final class JavaPluginLoader implements PluginLoader {
return fileFilters;
}
public void callEvent(RegisteredListener registration, PlayerEvent.EventType type, PlayerEvent event) {
Listener listener = registration.getListener();
if (listener instanceof PlayerListener) {
PlayerListener trueListener = (PlayerListener)listener;
switch (type) {
case Join:
trueListener.onPlayerJoin((PlayerJoinEvent)event);
break;
case Quit:
trueListener.onPlayerQuit((PlayerQuitEvent)event);
break;
}
}
}
}