Eventing redone, revision 1

By: durron597 <martin.jared@gmail.com>
This commit is contained in:
Bukkit/Spigot 2010-12-28 10:48:03 +08:00
parent e364e973c7
commit ed3dcbdd2b
8 changed files with 214 additions and 44 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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<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);
private final Map<Event.Type, List<RegisteredListener>> listeners = new EnumMap<Event.Type, List<RegisteredListener>>(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<RegisteredListener> listeners = playerListeners.get(type);
public void callEvent(Event event) {
List<RegisteredListener> 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<RegisteredListener> listeners = playerListeners.get(type);
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) {
List<RegisteredListener> 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<RegisteredListener>();
playerListeners.put(type, listeners);
eventListeners = new ArrayList<RegisteredListener>();
listeners.put(type, eventListeners);
}
listeners.add(position, new RegisteredListener(listener, priority, plugin));
eventListeners.add(position, new RegisteredListener(listener, priority, plugin));
}
}

View file

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