diff --git a/paper-api/src/org/bukkit/event/Event.java b/paper-api/src/org/bukkit/event/Event.java index f2d17753e7..3fdb4a1f6c 100644 --- a/paper-api/src/org/bukkit/event/Event.java +++ b/paper-api/src/org/bukkit/event/Event.java @@ -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 + } } diff --git a/paper-api/src/org/bukkit/event/Listener.java b/paper-api/src/org/bukkit/event/Listener.java new file mode 100644 index 0000000000..004d18edbb --- /dev/null +++ b/paper-api/src/org/bukkit/event/Listener.java @@ -0,0 +1,9 @@ + +package org.bukkit.event; + +/** + * Simple interface for tagging all EventListeners + */ +public interface Listener { + +} diff --git a/paper-api/src/org/bukkit/event/player/PlayerEvent.java b/paper-api/src/org/bukkit/event/player/PlayerEvent.java index 631e745908..c5900be486 100644 --- a/paper-api/src/org/bukkit/event/player/PlayerEvent.java +++ b/paper-api/src/org/bukkit/event/player/PlayerEvent.java @@ -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 + } } diff --git a/paper-api/src/org/bukkit/event/player/PlayerListener.java b/paper-api/src/org/bukkit/event/player/PlayerListener.java index 7fb30bae9e..df10bbd821 100644 --- a/paper-api/src/org/bukkit/event/player/PlayerListener.java +++ b/paper-api/src/org/bukkit/event/player/PlayerListener.java @@ -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); } diff --git a/paper-api/src/org/bukkit/plugin/PluginLoader.java b/paper-api/src/org/bukkit/plugin/PluginLoader.java index 541b14c514..05c96296f4 100644 --- a/paper-api/src/org/bukkit/plugin/PluginLoader.java +++ b/paper-api/src/org/bukkit/plugin/PluginLoader.java @@ -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); } diff --git a/paper-api/src/org/bukkit/plugin/PluginManager.java b/paper-api/src/org/bukkit/plugin/PluginManager.java index dbd43ccca7..0f462702c4 100644 --- a/paper-api/src/org/bukkit/plugin/PluginManager.java +++ b/paper-api/src/org/bukkit/plugin/PluginManager.java @@ -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); } diff --git a/paper-api/src/org/bukkit/plugin/RegisteredListener.java b/paper-api/src/org/bukkit/plugin/RegisteredListener.java new file mode 100644 index 0000000000..1ee5a1c779 --- /dev/null +++ b/paper-api/src/org/bukkit/plugin/RegisteredListener.java @@ -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; + } +} diff --git a/paper-api/src/org/bukkit/plugin/SimplePluginManager.java b/paper-api/src/org/bukkit/plugin/SimplePluginManager.java index 01e8c9c366..c89d8f6bcc 100644 --- a/paper-api/src/org/bukkit/plugin/SimplePluginManager.java +++ b/paper-api/src/org/bukkit/plugin/SimplePluginManager.java @@ -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 fileAssociations = new HashMap(); private final List plugins = new ArrayList(); private final Map lookupNames = new HashMap(); + private final Map> playerListeners = new EnumMap>(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 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); + } + } + } + } } diff --git a/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java b/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java index aaabe4f41c..26e0ac2618 100644 --- a/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java @@ -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; + } + } + } }