mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-02 17:32:03 +01:00
Refactored event calling so its front loading avoiding the lookup for each event call.
This now uses an annoymous class implementing IExecutor that facilitates direct event method handler calling Changed commands from being executed exclusively by a player to by a CommandSender to facilitate external command callers such as rcon Fixed CustomEventListener Merged in additional events Added getFullName to PluginDescriptionFile which returns the combination of Name and Version There's also a few bits of reformatting as it seems someones been editing with either tabs or dos eol :( By: stevenh <steven.hartland@multiplay.co.uk>
This commit is contained in:
parent
9755073204
commit
df05c36540
18 changed files with 534 additions and 361 deletions
|
@ -3,7 +3,7 @@ package org.bukkit.command;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
public abstract class Command {
|
public abstract class Command {
|
||||||
private final String name;
|
private final String name;
|
||||||
|
@ -17,7 +17,7 @@ public abstract class Command {
|
||||||
this.usageMessage = "/" + name;
|
this.usageMessage = "/" + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract boolean execute(Player player, String currentAlias, String[] args);
|
public abstract boolean execute(CommandSender sender, String currentAlias, String[] args);
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
|
|
|
@ -24,6 +24,6 @@ public interface CommandMap {
|
||||||
* @param cmdLine command + arguments. Example: "/test abc 123"
|
* @param cmdLine command + arguments. Example: "/test abc 123"
|
||||||
* @return targetFound returns false if no target is found.
|
* @return targetFound returns false if no target is found.
|
||||||
*/
|
*/
|
||||||
public boolean dispatch(Player sender, String cmdLine);
|
public boolean dispatch(CommandSender sender, String cmdLine);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,15 +13,15 @@ public final class PluginCommand extends Command {
|
||||||
this.usageMessage = "";
|
this.usageMessage = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean execute(Player player, String commandLabel, String[] args) {
|
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
||||||
boolean cmdSuccess = owningPlugin.onCommand(player, this, commandLabel, args);
|
boolean cmdSuccess = owningPlugin.onCommand(sender, this, commandLabel, args);
|
||||||
if (!cmdSuccess && !usageMessage.isEmpty()) {
|
if (!cmdSuccess && !usageMessage.isEmpty()) {
|
||||||
String tmpMsg = usageMessage.replace("<command>", commandLabel);
|
String tmpMsg = usageMessage.replace("<command>", commandLabel);
|
||||||
String[] usageLines = tmpMsg.split("\\n");
|
String[] usageLines = tmpMsg.split("\\n");
|
||||||
for(String line: usageLines) {
|
for(String line: usageLines) {
|
||||||
while (line.length() > 0) {
|
while (line.length() > 0) {
|
||||||
int stripChars = (line.length() > 53 ? 53:line.length());
|
int stripChars = (line.length() > 53 ? 53:line.length());
|
||||||
player.sendMessage(ChatColor.RED + line.substring(0, stripChars));
|
sender.sendMessage(ChatColor.RED + line.substring(0, stripChars));
|
||||||
line = line.substring(stripChars);
|
line = line.substring(stripChars);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,12 @@ public final class SimpleCommandMap implements CommandMap {
|
||||||
|
|
||||||
register("reload", "bukkit", new Command("reload") {
|
register("reload", "bukkit", new Command("reload") {
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(Player player, String currentAlias, String[] args) {
|
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
|
||||||
if (player.isOp()) {
|
if (sender.isOp()) {
|
||||||
server.reload();
|
server.reload();
|
||||||
player.sendMessage(ChatColor.GREEN + "Reload complete.");
|
sender.sendMessage(ChatColor.GREEN + "Reload complete.");
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage(ChatColor.RED + "You do not have sufficient access"
|
sender.sendMessage(ChatColor.RED + "You do not have sufficient access"
|
||||||
+ " to reload this server.");
|
+ " to reload this server.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ public final class SimpleCommandMap implements CommandMap {
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public boolean dispatch(Player sender, String commandLine) {
|
public boolean dispatch(CommandSender sender, String commandLine) {
|
||||||
String[] args = commandLine.split(" ");
|
String[] args = commandLine.split(" ");
|
||||||
String sentCommandLabel = args[0].substring(1);
|
String sentCommandLabel = args[0].substring(1);
|
||||||
|
|
||||||
|
@ -98,12 +98,12 @@ public final class SimpleCommandMap implements CommandMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(Player player, String currentAlias, String[] args) {
|
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
player.sendMessage("This server is running " + ChatColor.GREEN
|
sender.sendMessage("This server is running " + ChatColor.GREEN
|
||||||
+ server.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + server.getVersion());
|
+ server.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + server.getVersion());
|
||||||
player.sendMessage("This server is also sporting some funky dev build of Bukkit!");
|
sender.sendMessage("This server is also sporting some funky dev build of Bukkit!");
|
||||||
player.sendMessage("Plugins: " + getPluginList());
|
sender.sendMessage("Plugins: " + getPluginList());
|
||||||
} else {
|
} else {
|
||||||
StringBuilder name = new StringBuilder();
|
StringBuilder name = new StringBuilder();
|
||||||
|
|
||||||
|
@ -118,26 +118,26 @@ public final class SimpleCommandMap implements CommandMap {
|
||||||
|
|
||||||
if (plugin != null) {
|
if (plugin != null) {
|
||||||
PluginDescriptionFile desc = plugin.getDescription();
|
PluginDescriptionFile desc = plugin.getDescription();
|
||||||
player.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
|
sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
|
||||||
|
|
||||||
if (desc.getDescription() != null) {
|
if (desc.getDescription() != null) {
|
||||||
player.sendMessage(desc.getDescription());
|
sender.sendMessage(desc.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (desc.getWebsite() != null) {
|
if (desc.getWebsite() != null) {
|
||||||
player.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
|
sender.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!desc.getAuthors().isEmpty()) {
|
if (!desc.getAuthors().isEmpty()) {
|
||||||
if (desc.getAuthors().size() == 1) {
|
if (desc.getAuthors().size() == 1) {
|
||||||
player.sendMessage("Author: " + getAuthors(desc));
|
sender.sendMessage("Author: " + getAuthors(desc));
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("Authors: " + getAuthors(desc));
|
sender.sendMessage("Authors: " + getAuthors(desc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("This server is not running any plugin by that name.");
|
sender.sendMessage("This server is not running any plugin by that name.");
|
||||||
player.sendMessage("Plugins: " + getPluginList());
|
sender.sendMessage("Plugins: " + getPluginList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,13 @@ package org.bukkit.entity;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a player, connected or not
|
* Represents a player, connected or not
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface Player extends HumanEntity {
|
public interface Player extends HumanEntity, CommandSender {
|
||||||
/**
|
/**
|
||||||
* Checks if this player is currently online
|
* Checks if this player is currently online
|
||||||
*
|
*
|
||||||
|
@ -16,20 +17,6 @@ public interface Player extends HumanEntity {
|
||||||
*/
|
*/
|
||||||
public boolean isOnline();
|
public boolean isOnline();
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if this player is currently op
|
|
||||||
*
|
|
||||||
* @return true if they are online
|
|
||||||
*/
|
|
||||||
public boolean isOp();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends this player a message, which will be displayed in their chat
|
|
||||||
*
|
|
||||||
* @param message Message to be displayed
|
|
||||||
*/
|
|
||||||
public void sendMessage(String message);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the "friendly" name to display of this player. This may include color.
|
* Gets the "friendly" name to display of this player. This may include color.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,19 @@
|
||||||
package org.bukkit.event;
|
package org.bukkit.event;
|
||||||
|
|
||||||
public interface CustomEventListener {
|
import org.bukkit.event.Listener;
|
||||||
public void onCustomEvent(Event event);
|
|
||||||
|
/**
|
||||||
|
* Handles all custom events
|
||||||
|
*/
|
||||||
|
public class CustomEventListener implements Listener {
|
||||||
|
public CustomEventListener() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a player joins a server
|
||||||
|
*
|
||||||
|
* @param event Relevant event details
|
||||||
|
*/
|
||||||
|
public void onCustomEvent(Event event) {
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -38,8 +38,7 @@ public abstract class Event {
|
||||||
* @return Name of this event
|
* @return Name of this event
|
||||||
*/
|
*/
|
||||||
public final String getEventName() {
|
public final String getEventName() {
|
||||||
if(type!=Type.CUSTOM_EVENT) return type.toString();
|
return ( type != Type.CUSTOM_EVENT) ? type.toString() : name;
|
||||||
else return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -275,6 +274,13 @@ public abstract class Event {
|
||||||
*/
|
*/
|
||||||
BLOCK_INTERACT (Category.BLOCK),
|
BLOCK_INTERACT (Category.BLOCK),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a block is destroyed from being burnt by fire
|
||||||
|
*
|
||||||
|
* @see org.bukkit.event.block.BlockBurnEvent
|
||||||
|
*/
|
||||||
|
BLOCK_BURN (Category.BLOCK),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when leaves are decaying naturally
|
* Called when leaves are decaying naturally
|
||||||
*
|
*
|
||||||
|
@ -356,6 +362,13 @@ public abstract class Event {
|
||||||
*/
|
*/
|
||||||
PLUGIN_DISABLE (Category.SERVER),
|
PLUGIN_DISABLE (Category.SERVER),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a plugin is disabled
|
||||||
|
*
|
||||||
|
* @see org.bukkit.event.server.PluginEvent
|
||||||
|
*/
|
||||||
|
SERVER_COMMAND (Category.SERVER),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WORLD EVENTS
|
* WORLD EVENTS
|
||||||
*/
|
*/
|
||||||
|
@ -453,6 +466,13 @@ public abstract class Event {
|
||||||
*/
|
*/
|
||||||
ENTITY_EXPLODE (Category.LIVING_ENTITY),
|
ENTITY_EXPLODE (Category.LIVING_ENTITY),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an entity targets another entity
|
||||||
|
*
|
||||||
|
* @see org.bukkit.event.entity.EntityTargetEvent
|
||||||
|
*/
|
||||||
|
ENTITY_TARGET (Category.LIVING_ENTITY),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* VEHICLE EVENTS
|
* VEHICLE EVENTS
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -94,4 +94,11 @@ public class BlockListener implements Listener {
|
||||||
public void onLeavesDecay(LeavesDecayEvent event) {
|
public void onLeavesDecay(LeavesDecayEvent event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a block is destroyed from burning
|
||||||
|
*
|
||||||
|
* @param event Relevant event details
|
||||||
|
*/
|
||||||
|
public void onBlockBurn(BlockBurnEvent event) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,10 @@ public class EntityListener implements Listener {
|
||||||
|
|
||||||
public void onEntityExplode(EntityExplodeEvent event) {
|
public void onEntityExplode(EntityExplodeEvent event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onEntityDeath(EntityDeathEvent event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onEntityTarget(EntityTargetEvent event) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,4 +22,12 @@ public class ServerListener implements Listener {
|
||||||
*/
|
*/
|
||||||
public void onPluginDisabled(PluginEvent event) {
|
public void onPluginDisabled(PluginEvent event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a server command is used
|
||||||
|
*
|
||||||
|
* @param event Relevant event details
|
||||||
|
*/
|
||||||
|
public void onServerCommand(PluginEvent event) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ package org.bukkit.plugin;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.util.config.Configuration;
|
import org.bukkit.util.config.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,5 +69,5 @@ public interface Plugin {
|
||||||
* @param commandLabel
|
* @param commandLabel
|
||||||
* @return TODO
|
* @return TODO
|
||||||
*/
|
*/
|
||||||
public boolean onCommand(Player player, Command command, String commandLabel, String[] args);
|
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,6 +77,15 @@ public final class PluginDescriptionFile {
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of a plugin including the version
|
||||||
|
*
|
||||||
|
* @return String name
|
||||||
|
*/
|
||||||
|
public String getFullName() {
|
||||||
|
return name + " v" + version;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the main class for a plugin
|
* Returns the main class for a plugin
|
||||||
*
|
*
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.io.File;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a plugin loader, which handles direct access to specific types
|
* Represents a plugin loader, which handles direct access to specific types
|
||||||
|
@ -27,13 +28,12 @@ public interface PluginLoader {
|
||||||
public Pattern[] getPluginFileFilters();
|
public Pattern[] getPluginFileFilters();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a player related event with the given details
|
* Creates and returns an event executor
|
||||||
*
|
*
|
||||||
* @param registration Registered information on the plugin to call about this event
|
* @param type Type of the event executor to create
|
||||||
* @param type Type of player related event to call
|
* @param listener the object that will handle the eventual call back
|
||||||
* @param event Event details
|
|
||||||
*/
|
*/
|
||||||
public void callEvent(RegisteredListener registration, Event event);
|
public IExecutor createExecutor(Event.Type type, Listener listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the specified plugin
|
* Enables the specified plugin
|
||||||
|
|
|
@ -93,6 +93,17 @@ public interface PluginManager {
|
||||||
*/
|
*/
|
||||||
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin);
|
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the given event to the specified executor
|
||||||
|
*
|
||||||
|
* @param type EventType to register
|
||||||
|
* @param listener Listener to register
|
||||||
|
* @param executor IExecutor to register
|
||||||
|
* @param priority Priority of this event
|
||||||
|
* @param plugin Plugin to register
|
||||||
|
*/
|
||||||
|
public void registerEvent(Event.Type type, Listener listener, IExecutor executor, Priority priority, Plugin plugin);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the specified plugin
|
* Enables the specified plugin
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.bukkit.plugin;
|
||||||
|
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.IExecutor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores relevant information for plugin listeners
|
* Stores relevant information for plugin listeners
|
||||||
|
@ -11,11 +12,20 @@ public class RegisteredListener {
|
||||||
private final Listener listener;
|
private final Listener listener;
|
||||||
private final Event.Priority priority;
|
private final Event.Priority priority;
|
||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
|
private final IExecutor executor;
|
||||||
|
|
||||||
public RegisteredListener(final Listener pluginListener, final Event.Priority eventPriority, final Plugin registeredPlugin) {
|
public RegisteredListener(final Listener pluginListener, final IExecutor eventExecutor, final Event.Priority eventPriority, final Plugin registeredPlugin ) {
|
||||||
listener = pluginListener;
|
listener = pluginListener;
|
||||||
priority = eventPriority;
|
priority = eventPriority;
|
||||||
plugin = registeredPlugin;
|
plugin = registeredPlugin;
|
||||||
|
executor = eventExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RegisteredListener(final Listener pluginListener, final Event.Priority eventPriority, final Plugin registeredPlugin, Event.Type type ) {
|
||||||
|
listener = pluginListener;
|
||||||
|
priority = eventPriority;
|
||||||
|
plugin = registeredPlugin;
|
||||||
|
executor = registeredPlugin.getPluginLoader().createExecutor( type, pluginListener );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,4 +51,12 @@ public class RegisteredListener {
|
||||||
public Event.Priority getPriority() {
|
public Event.Priority getPriority() {
|
||||||
return priority;
|
return priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls the event executor
|
||||||
|
* @return Registered Priority
|
||||||
|
*/
|
||||||
|
public void callEvent(Event event) {
|
||||||
|
executor.execute( listener, event );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,15 +193,11 @@ public final class SimplePluginManager implements PluginManager {
|
||||||
|
|
||||||
if (eventListeners != null) {
|
if (eventListeners != null) {
|
||||||
for (RegisteredListener registration : eventListeners) {
|
for (RegisteredListener registration : eventListeners) {
|
||||||
Plugin plugin = registration.getPlugin();
|
// NOTE: no need to check isEnabled as all disabled plugins have their listeners disabled
|
||||||
PluginLoader loader = plugin.getPluginLoader();
|
|
||||||
|
|
||||||
if (plugin.isEnabled()) {
|
|
||||||
try {
|
try {
|
||||||
loader.callEvent(registration, event);
|
registration.callEvent( event );
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
Logger.getLogger(SimplePluginManager.class.getName()).log(Level.SEVERE, "Could not pass event " + event.getType() + " to " + plugin.getDescription().getName(), ex);
|
Logger.getLogger(SimplePluginManager.class.getName()).log(Level.SEVERE, "Could not pass event " + event.getType() + " to " + registration.getPlugin().getDescription().getName(), ex);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -216,19 +212,43 @@ public final class SimplePluginManager implements PluginManager {
|
||||||
* @param plugin Plugin to register
|
* @param plugin Plugin to register
|
||||||
*/
|
*/
|
||||||
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) {
|
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) {
|
||||||
|
getEventListeners( type ).offer(new RegisteredListener(listener, priority, plugin, type));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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, IExecutor executor, Priority priority, Plugin plugin) {
|
||||||
|
getEventListeners( type ).offer(new RegisteredListener(listener, executor, priority, plugin));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a PriorityQueue of RegisteredListener for the specified event type creating a new queue if needed
|
||||||
|
*
|
||||||
|
* @param type EventType to lookup
|
||||||
|
* @return PriorityQueue<RegisteredListener> the looked up or create queue matching the requested type
|
||||||
|
*/
|
||||||
|
private PriorityQueue<RegisteredListener> getEventListeners( Event.Type type )
|
||||||
|
{
|
||||||
PriorityQueue<RegisteredListener> eventListeners = listeners.get(type);
|
PriorityQueue<RegisteredListener> eventListeners = listeners.get(type);
|
||||||
|
|
||||||
if (eventListeners == null) {
|
if (eventListeners != null) {
|
||||||
eventListeners = new PriorityQueue<RegisteredListener>(11,
|
return eventListeners;
|
||||||
new Comparator<RegisteredListener>( ) {
|
}
|
||||||
|
|
||||||
|
eventListeners = new PriorityQueue<RegisteredListener>(
|
||||||
|
11, new Comparator<RegisteredListener>() {
|
||||||
public int compare(RegisteredListener i, RegisteredListener j) {
|
public int compare(RegisteredListener i, RegisteredListener j) {
|
||||||
return i.getPriority().compareTo(j.getPriority());
|
return i.getPriority().compareTo(j.getPriority());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
listeners.put(type, eventListeners);
|
listeners.put(type, eventListeners);
|
||||||
}
|
return eventListeners;
|
||||||
|
|
||||||
eventListeners.offer(new RegisteredListener(listener, priority, plugin));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ package org.bukkit.plugin.java;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.PluginDescriptionFile;
|
import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
import org.bukkit.plugin.PluginLoader;
|
import org.bukkit.plugin.PluginLoader;
|
||||||
|
@ -142,7 +142,7 @@ public abstract class JavaPlugin implements Plugin {
|
||||||
/**
|
/**
|
||||||
* Called when a command registered by this plugin is received.
|
* Called when a command registered by this plugin is received.
|
||||||
*/
|
*/
|
||||||
public boolean onCommand(Player player, Command cmd, String commandLabel, String[] args) {
|
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
||||||
return false; // default implementation: do nothing!
|
return false; // default implementation: do nothing!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,16 +16,9 @@ import org.bukkit.event.CustomEventListener;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.*;
|
import org.bukkit.event.block.*;
|
||||||
import org.bukkit.event.entity.EntityCombustEvent;
|
import org.bukkit.event.entity.*;
|
||||||
import org.bukkit.event.entity.EntityDamageByBlockEvent;
|
|
||||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
||||||
import org.bukkit.event.entity.EntityDamageByProjectileEvent;
|
|
||||||
import org.bukkit.event.entity.EntityDamageEvent;
|
|
||||||
import org.bukkit.event.entity.EntityExplodeEvent;
|
|
||||||
import org.bukkit.event.entity.EntityListener;
|
|
||||||
import org.bukkit.event.player.*;
|
import org.bukkit.event.player.*;
|
||||||
import org.bukkit.event.server.PluginEvent;
|
import org.bukkit.event.server.*;
|
||||||
import org.bukkit.event.server.ServerListener;
|
|
||||||
import org.bukkit.event.vehicle.*;
|
import org.bukkit.event.vehicle.*;
|
||||||
import org.bukkit.event.world.ChunkLoadEvent;
|
import org.bukkit.event.world.ChunkLoadEvent;
|
||||||
import org.bukkit.event.world.ChunkUnloadEvent;
|
import org.bukkit.event.world.ChunkUnloadEvent;
|
||||||
|
@ -118,158 +111,238 @@ public final class JavaPluginLoader implements PluginLoader {
|
||||||
classes.put(name, clazz);
|
classes.put(name, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void callEvent(RegisteredListener registration, Event event) {
|
public IExecutor createExecutor( Event.Type type, Listener listener ) {
|
||||||
Listener listener = registration.getListener();
|
// TODO: remove multiple Listener type and hence casts
|
||||||
|
switch (type) {
|
||||||
if (listener instanceof PlayerListener) {
|
// Player Events
|
||||||
PlayerListener trueListener = (PlayerListener)listener;
|
|
||||||
|
|
||||||
switch (event.getType()) {
|
|
||||||
case PLAYER_JOIN:
|
case PLAYER_JOIN:
|
||||||
trueListener.onPlayerJoin((PlayerEvent)event);
|
return new IExecutor() {
|
||||||
break;
|
public void execute( Listener listener, Event event ) {
|
||||||
|
((PlayerListener)listener).onPlayerJoin( (PlayerEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_QUIT:
|
case PLAYER_QUIT:
|
||||||
trueListener.onPlayerQuit((PlayerEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerQuit( (PlayerEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_COMMAND:
|
case PLAYER_COMMAND:
|
||||||
trueListener.onPlayerCommand((PlayerChatEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerCommand( (PlayerChatEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_CHAT:
|
case PLAYER_CHAT:
|
||||||
trueListener.onPlayerChat((PlayerChatEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerChat( (PlayerChatEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_MOVE:
|
case PLAYER_MOVE:
|
||||||
trueListener.onPlayerMove((PlayerMoveEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerMove( (PlayerMoveEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_TELEPORT:
|
case PLAYER_TELEPORT:
|
||||||
trueListener.onPlayerTeleport((PlayerMoveEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerTeleport( (PlayerMoveEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_ITEM:
|
case PLAYER_ITEM:
|
||||||
trueListener.onPlayerItem((PlayerItemEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerItem( (PlayerItemEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_LOGIN:
|
case PLAYER_LOGIN:
|
||||||
trueListener.onPlayerLogin((PlayerLoginEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerLogin( (PlayerLoginEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_EGG_THROW:
|
case PLAYER_EGG_THROW:
|
||||||
trueListener.onPlayerEggThrow((PlayerEggThrowEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerEggThrow( (PlayerEggThrowEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLAYER_ANIMATION:
|
case PLAYER_ANIMATION:
|
||||||
trueListener.onPlayerAnimation((PlayerAnimationEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((PlayerListener)listener).onPlayerAnimation( (PlayerAnimationEvent)event );
|
||||||
}
|
}
|
||||||
} else if (listener instanceof BlockListener) {
|
};
|
||||||
BlockListener trueListener = (BlockListener)listener;
|
|
||||||
|
|
||||||
switch (event.getType()) {
|
// Block Events
|
||||||
case BLOCK_PHYSICS:
|
case BLOCK_PHYSICS:
|
||||||
trueListener.onBlockPhysics((BlockPhysicsEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockPhysics( (BlockPhysicsEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case BLOCK_CANBUILD:
|
case BLOCK_CANBUILD:
|
||||||
trueListener.onBlockCanBuild((BlockCanBuildEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockCanBuild( (BlockCanBuildEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case BLOCK_RIGHTCLICKED:
|
case BLOCK_RIGHTCLICKED:
|
||||||
trueListener.onBlockRightClick((BlockRightClickEvent) event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockRightClick( (BlockRightClickEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case BLOCK_PLACED:
|
case BLOCK_PLACED:
|
||||||
trueListener.onBlockPlace((BlockPlaceEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockPlace( (BlockPlaceEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case BLOCK_DAMAGED:
|
case BLOCK_DAMAGED:
|
||||||
trueListener.onBlockDamage((BlockDamageEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockDamage( (BlockDamageEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case BLOCK_INTERACT:
|
case BLOCK_INTERACT:
|
||||||
trueListener.onBlockInteract((BlockInteractEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockInteract( (BlockInteractEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case BLOCK_FLOW:
|
case BLOCK_FLOW:
|
||||||
trueListener.onBlockFlow((BlockFromToEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockFlow( (BlockFromToEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case LEAVES_DECAY:
|
case LEAVES_DECAY:
|
||||||
trueListener.onLeavesDecay((LeavesDecayEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onLeavesDecay( (LeavesDecayEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case BLOCK_IGNITE:
|
case BLOCK_IGNITE:
|
||||||
trueListener.onBlockIgnite((BlockIgniteEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockIgnite( (BlockIgniteEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case REDSTONE_CHANGE:
|
case REDSTONE_CHANGE:
|
||||||
trueListener.onBlockRedstoneChange((BlockFromToEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((BlockListener)listener).onBlockRedstoneChange( (BlockFromToEvent)event );
|
||||||
}
|
}
|
||||||
} else if(listener instanceof ServerListener) {
|
};
|
||||||
ServerListener trueListener = (ServerListener)listener;
|
|
||||||
|
|
||||||
switch (event.getType()) {
|
case BLOCK_BURN:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((BlockListener)listener).onBlockBurn( (BlockBurnEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Server Events
|
||||||
case PLUGIN_ENABLE:
|
case PLUGIN_ENABLE:
|
||||||
trueListener.onPluginEnabled((PluginEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((ServerListener)listener).onPluginEnabled( (PluginEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case PLUGIN_DISABLE:
|
case PLUGIN_DISABLE:
|
||||||
trueListener.onPluginDisabled((PluginEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((ServerListener)listener).onPluginDisabled( (PluginEvent)event );
|
||||||
}
|
}
|
||||||
} else if(listener instanceof WorldListener) {
|
};
|
||||||
WorldListener trueListener = (WorldListener)listener;
|
|
||||||
|
|
||||||
switch (event.getType()) {
|
case SERVER_COMMAND:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((ServerListener)listener).onServerCommand( (ServerCommandEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// World Events
|
||||||
case CHUNK_LOADED:
|
case CHUNK_LOADED:
|
||||||
trueListener.onChunkLoaded((ChunkLoadEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((WorldListener)listener).onChunkLoaded( (ChunkLoadEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case CHUNK_UNLOADED:
|
case CHUNK_UNLOADED:
|
||||||
trueListener.onChunkUnloaded((ChunkUnloadEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((WorldListener)listener).onChunkUnloaded( (ChunkUnloadEvent)event );
|
||||||
}
|
}
|
||||||
} else if(listener instanceof EntityListener) {
|
};
|
||||||
EntityListener trueListener = (EntityListener) listener;
|
|
||||||
switch(event.getType())
|
|
||||||
{
|
|
||||||
case ENTITY_DAMAGEDBY_BLOCK:
|
|
||||||
trueListener.onEntityDamageByBlock((EntityDamageByBlockEvent)event);
|
|
||||||
break;
|
|
||||||
case ENTITY_DAMAGEDBY_ENTITY:
|
|
||||||
trueListener.onEntityDamageByEntity((EntityDamageByEntityEvent)event);
|
|
||||||
break;
|
|
||||||
case ENTITY_DAMAGEDBY_PROJECTILE:
|
|
||||||
trueListener.onEntityDamageByProjectile((EntityDamageByProjectileEvent)event);
|
|
||||||
break;
|
|
||||||
case ENTITY_DAMAGED:
|
|
||||||
trueListener.onEntityDamage((EntityDamageEvent)event);
|
|
||||||
break;
|
|
||||||
case ENTITY_DEATH:
|
|
||||||
// TODO: ENTITY_DEATH hook
|
|
||||||
break;
|
|
||||||
case ENTITY_COMBUST:
|
|
||||||
trueListener.onEntityCombust((EntityCombustEvent)event);
|
|
||||||
break;
|
|
||||||
case ENTITY_EXPLODE:
|
|
||||||
trueListener.onEntityExplode((EntityExplodeEvent)event);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else if (listener instanceof VehicleListener) {
|
|
||||||
VehicleListener trueListener = (VehicleListener)listener;
|
|
||||||
|
|
||||||
switch (event.getType()) {
|
// Entity Events
|
||||||
|
case ENTITY_DAMAGEDBY_BLOCK:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityDamageByBlock( (EntityDamageByBlockEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case ENTITY_DAMAGEDBY_ENTITY:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityDamageByEntity( (EntityDamageByEntityEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case ENTITY_DAMAGEDBY_PROJECTILE:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityDamageByProjectile( (EntityDamageByProjectileEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case ENTITY_DAMAGED:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityDamage( (EntityDamageEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case ENTITY_DEATH:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityDeath( (EntityDeathEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case ENTITY_COMBUST:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityCombust( (EntityCombustEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case ENTITY_EXPLODE:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityExplode( (EntityExplodeEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case ENTITY_TARGET:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((EntityListener)listener).onEntityTarget( (EntityTargetEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Vehicle Events
|
||||||
case VEHICLE_CREATE:
|
case VEHICLE_CREATE:
|
||||||
trueListener.onVehicleCreate((VehicleCreateEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((VehicleListener)listener).onVehicleCreate( (VehicleCreateEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case VEHICLE_DAMAGE:
|
case VEHICLE_DAMAGE:
|
||||||
trueListener.onVehicleDamage((VehicleDamageEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((VehicleListener)listener).onVehicleDamage( (VehicleDamageEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case VEHICLE_COLLISION_BLOCK:
|
case VEHICLE_COLLISION_BLOCK:
|
||||||
trueListener.onVehicleBlockCollision((VehicleBlockCollisionEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((VehicleListener)listener).onVehicleBlockCollision( (VehicleBlockCollisionEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case VEHICLE_COLLISION_ENTITY:
|
case VEHICLE_COLLISION_ENTITY:
|
||||||
trueListener.onVehicleEntityCollision((VehicleEntityCollisionEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((VehicleListener)listener).onVehicleEntityCollision( (VehicleEntityCollisionEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case VEHICLE_ENTER:
|
case VEHICLE_ENTER:
|
||||||
trueListener.onVehicleEnter((VehicleEnterEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((VehicleListener)listener).onVehicleEnter( (VehicleEnterEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case VEHICLE_EXIT:
|
case VEHICLE_EXIT:
|
||||||
trueListener.onVehicleExit((VehicleExitEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((VehicleListener)listener).onVehicleExit( (VehicleExitEvent)event );
|
||||||
|
}
|
||||||
|
};
|
||||||
case VEHICLE_MOVE:
|
case VEHICLE_MOVE:
|
||||||
trueListener.onVehicleMove((VehicleMoveEvent)event);
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
break;
|
((VehicleListener)listener).onVehicleMove( (VehicleMoveEvent)event );
|
||||||
}
|
}
|
||||||
} else if(listener instanceof CustomEventListener) {
|
};
|
||||||
if(event.getType()==Event.Type.CUSTOM_EVENT) {
|
|
||||||
((CustomEventListener)listener).onCustomEvent(event);
|
// Custom Events
|
||||||
|
case CUSTOM_EVENT:
|
||||||
|
return new IExecutor() { public void execute( Listener listener, Event event ) {
|
||||||
|
((CustomEventListener)listener).onCustomEvent( event );
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException( "Event " + type + " is not supported" );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enablePlugin(final Plugin plugin) {
|
public void enablePlugin(final Plugin plugin) {
|
||||||
|
|
Loading…
Reference in a new issue