diff --git a/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java b/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java index 2d435b7416..6b348aff1a 100644 --- a/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java +++ b/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java @@ -4,7 +4,7 @@ package com.dinnerbone.bukkit.sample; import java.io.File; import org.bukkit.Server; import org.bukkit.event.Event.Priority; -import org.bukkit.event.player.PlayerEvent.EventType; +import org.bukkit.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.java.JavaPlugin; @@ -34,7 +34,7 @@ public class SamplePlugin extends JavaPlugin { } private void registerEvents() { - getServer().getPluginManager().registerEvent(EventType.Join, playerListener, Priority.Normal, this); - getServer().getPluginManager().registerEvent(EventType.Quit, playerListener, Priority.Normal, this); + getServer().getPluginManager().registerEvent(Event.Type.JOIN, playerListener, Priority.Normal, this); + getServer().getPluginManager().registerEvent(Event.Type.QUIT, playerListener, Priority.Normal, this); } } diff --git a/paper-api/src/org/bukkit/event/Event.java b/paper-api/src/org/bukkit/event/Event.java index 3fdb4a1f6c..03957646f8 100644 --- a/paper-api/src/org/bukkit/event/Event.java +++ b/paper-api/src/org/bukkit/event/Event.java @@ -1,16 +1,20 @@ package org.bukkit.event; +import org.bukkit.Player; import org.bukkit.Server; +import org.bukkit.event.player.PlayerEvent; /** * Represents an event */ public abstract class Event { private final Server server; + private final Type type; - protected Event(final Server instance) { + protected Event(final Server instance, final Type type) { server = instance; + this.type = type; } /** @@ -21,6 +25,14 @@ public abstract class Event { return server; } + /** + * Gets the Type of this event + * @return Server which this event was triggered on + */ + public Type getType() { + return type; + } + /** * Represents an events priority */ @@ -50,4 +62,125 @@ public abstract class Event { */ Lowest } + + public enum Category { + PLAYER, + BLOCK, + ITEM, + ENVIRONMENT, + ENTITY, + VEHICLE, + INVENTORY, + SIGN, + CUSTOM; + } + + public enum Type { + /** + * Player Events + */ + LOGINCHECK (Category.PLAYER), + JOIN (Category.PLAYER), + CHAT (Category.PLAYER), + COMMAND (Category.PLAYER), + SERVERCOMMAND (Category.PLAYER), + BAN (Category.PLAYER), + IPBAN (Category.PLAYER), + KICK (Category.PLAYER), + QUIT (Category.PLAYER), + PLAYER_MOVE (Category.PLAYER), + ARM_SWING (Category.PLAYER), + TELEPORT (Category.PLAYER), + /** + * Block Events + */ + BLOCK_DESTROYED (Category.BLOCK), + BLOCK_BROKEN (Category.BLOCK), + BLOCK_PLACE (Category.BLOCK), + BLOCK_RIGHTCLICKED (Category.BLOCK), + REDSTONE_CHANGE (Category.BLOCK), + BLOCK_PHYSICS (Category.BLOCK), + /** + * Item Events + */ + ITEM_DROP (Category.ITEM), + ITEM_PICK_UP (Category.ITEM), + ITEM_USE (Category.ITEM), + /** + * Environment Events + */ + IGNITE (Category.ENVIRONMENT), + FLOW (Category.ENVIRONMENT), + EXPLODE (Category.ENVIRONMENT), + LIQUID_DESTROY (Category.ENVIRONMENT), + /** + * Non-player Entity Events + */ + MOB_SPAWN (Category.ENTITY), + DAMAGE (Category.ENTITY), + HEALTH_CHANGE (Category.ENTITY), + ATTACK (Category.ENTITY), // Need to look into this category more + /** + * Vehicle Events + */ + VEHICLE_CREATE (Category.VEHICLE), + VEHICLE_UPDATE (Category.VEHICLE), + VEHICLE_DAMAGE (Category.VEHICLE), + VEHICLE_COLLISION (Category.VEHICLE), + VEHICLE_DESTROYED (Category.VEHICLE), + VEHICLE_ENTERED (Category.VEHICLE), + VEHICLE_POSITIONCHANGE (Category.VEHICLE), + /** + * Inventory Events + */ + OPEN_INVENTORY (Category.INVENTORY), + /** + * Sign Events (Item events??) + */ + SIGN_SHOW (Category.SIGN), + SIGN_CHANGE (Category.SIGN), + /** + * Custom Event Placeholder? + */ + CUSTOM_EVENT (Category.CUSTOM); + + private Category category; + + private Type(Category category) { + this.category = category; + } + + public Category getCategory() { + return category; + } + } + + public static Event eventFactory(Server server, Event.Type type, Object[] data) throws EventException { + switch (type.getCategory()) { + case PLAYER: + if (data.length < 1 || !(data[0] instanceof Player)) { + throw new EventException("Data is not a Player!"); + } + return new PlayerEvent(server, type, (Player) data[0]); +// TODO: IMPLEMENT ME + case BLOCK: + return null; + case ITEM: + return null; + case ENVIRONMENT: + return null; + case ENTITY: + return null; + case VEHICLE: + return null; + case INVENTORY: + return null; + case SIGN: + return null; + case CUSTOM: + return null; + default: + return null; + } + } } diff --git a/paper-api/src/org/bukkit/event/EventException.java b/paper-api/src/org/bukkit/event/EventException.java new file mode 100644 index 0000000000..2e1e86a2b5 --- /dev/null +++ b/paper-api/src/org/bukkit/event/EventException.java @@ -0,0 +1,47 @@ +package org.bukkit.event; + +public class EventException extends Exception { + private final Throwable cause; + + /** + * Constructs a new EventException based on the given Exception + * + * @param throwable Exception that triggered this Exception + */ + public EventException(Throwable throwable) { + cause = throwable; + } + + /** + * Constructs a new EventException + */ + public EventException() { + cause = null; + } + + /** + * Constructs a new EventException with the given message + */ + public EventException(Throwable cause, String message) { + super(message); + this.cause = cause; + } + + /** + * Constructs a new EventException with the given message + */ + public EventException(String message) { + super(message); + 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; + } +} diff --git a/paper-api/src/org/bukkit/event/player/PlayerEvent.java b/paper-api/src/org/bukkit/event/player/PlayerEvent.java index bce15f6a88..034ccb2c84 100644 --- a/paper-api/src/org/bukkit/event/player/PlayerEvent.java +++ b/paper-api/src/org/bukkit/event/player/PlayerEvent.java @@ -11,8 +11,8 @@ import org.bukkit.event.Event; public class PlayerEvent extends Event { private final Player player; - public PlayerEvent(final Server server, final Player who) { - super(server); + public PlayerEvent(final Server server, Event.Type type, final Player who) { + super(server, type); player = who; } @@ -23,18 +23,4 @@ public class PlayerEvent extends 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/plugin/PluginLoader.java b/paper-api/src/org/bukkit/plugin/PluginLoader.java index 05c96296f4..7d75f577f3 100644 --- a/paper-api/src/org/bukkit/plugin/PluginLoader.java +++ b/paper-api/src/org/bukkit/plugin/PluginLoader.java @@ -3,7 +3,8 @@ package org.bukkit.plugin; import java.io.File; import java.util.regex.Pattern; -import org.bukkit.event.player.PlayerEvent; + +import org.bukkit.event.Event; /** * Represents a plugin loader, which handles direct access to specific types @@ -32,5 +33,5 @@ public interface PluginLoader { * @param type Type of player related event to call * @param event Event details */ - public void callEvent(RegisteredListener registration, PlayerEvent.EventType type, PlayerEvent event); + public void callEvent(RegisteredListener registration, Event event); } diff --git a/paper-api/src/org/bukkit/plugin/PluginManager.java b/paper-api/src/org/bukkit/plugin/PluginManager.java index fd1019dc21..b36ecc0dae 100644 --- a/paper-api/src/org/bukkit/plugin/PluginManager.java +++ b/paper-api/src/org/bukkit/plugin/PluginManager.java @@ -2,9 +2,10 @@ package org.bukkit.plugin; import java.io.File; + +import org.bukkit.event.Event; import org.bukkit.event.Event.Priority; -import org.bukkit.event.player.PlayerEvent; -import org.bukkit.event.player.PlayerListener; +import org.bukkit.event.Listener; /** * Handles all plugin management from the Server @@ -72,15 +73,15 @@ public interface PluginManager { * @param type Type of player related event to call * @param event Event details */ - public void callEvent(PlayerEvent.EventType type, PlayerEvent event); + public void callEvent(Event event); /** * Registers the given player event to the specified listener * * @param type EventType to register - * @param listener PlayerListener to register + * @param listener Listener to register * @param priority Priority of this event * @param plugin Plugin to register */ - public void registerEvent(PlayerEvent.EventType type, PlayerListener listener, Priority priority, Plugin plugin); + public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin); } diff --git a/paper-api/src/org/bukkit/plugin/SimplePluginManager.java b/paper-api/src/org/bukkit/plugin/SimplePluginManager.java index 201e8ed026..bc15be8c9b 100644 --- a/paper-api/src/org/bukkit/plugin/SimplePluginManager.java +++ b/paper-api/src/org/bukkit/plugin/SimplePluginManager.java @@ -14,9 +14,10 @@ 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.player.PlayerEvent; -import org.bukkit.event.player.PlayerListener; +import org.bukkit.event.Listener; /** * Handles all plugin management from the Server @@ -26,7 +27,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); + private final Map> listeners = new EnumMap>(Event.Type.class); public SimplePluginManager(Server instance) { server = instance; @@ -166,16 +167,16 @@ public final class SimplePluginManager implements PluginManager { * @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); + public void callEvent(Event event) { + List eventListeners = listeners.get(event.getType()); if (listeners != null) { - for (RegisteredListener registration : listeners) { + for (RegisteredListener registration : eventListeners) { Plugin plugin = registration.getPlugin(); PluginLoader loader = plugin.getPluginLoader(); if (plugin.isEnabled()) { - loader.callEvent(registration, type, event); + loader.callEvent(registration, event); } } } @@ -189,12 +190,12 @@ public final class SimplePluginManager implements PluginManager { * @param priority Priority of this event * @param plugin Plugin to register */ - public void registerEvent(PlayerEvent.EventType type, PlayerListener listener, Priority priority, Plugin plugin) { - List listeners = playerListeners.get(type); + public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) { + List eventListeners = listeners.get(type); int position = 0; if (listeners != null) { - for (RegisteredListener registration : listeners) { + for (RegisteredListener registration : eventListeners) { if (registration.getPriority().compareTo(priority) < 0) { break; } @@ -202,10 +203,10 @@ public final class SimplePluginManager implements PluginManager { position++; } } else { - listeners = new ArrayList(); - playerListeners.put(type, listeners); + eventListeners = new ArrayList(); + listeners.put(type, eventListeners); } - listeners.add(position, new RegisteredListener(listener, priority, plugin)); + eventListeners.add(position, new RegisteredListener(listener, priority, plugin)); } } diff --git a/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java b/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java index a297e1b93c..3f17112fc5 100644 --- a/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java @@ -13,6 +13,7 @@ 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.player.*; import org.bukkit.plugin.*; @@ -74,17 +75,17 @@ public final class JavaPluginLoader implements PluginLoader { return fileFilters; } - public void callEvent(RegisteredListener registration, PlayerEvent.EventType type, PlayerEvent event) { + public void callEvent(RegisteredListener registration, Event event) { Listener listener = registration.getListener(); if (listener instanceof PlayerListener) { PlayerListener trueListener = (PlayerListener)listener; - switch (type) { - case Join: + switch (event.getType()) { + case JOIN: trueListener.onPlayerJoin((PlayerEvent)event); break; - case Quit: + case QUIT: trueListener.onPlayerQuit((PlayerEvent)event); break; }