From 42ef5c31b70a13f90f0199ae7127541e250f32a8 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sun, 26 Dec 2010 00:38:11 +0000 Subject: [PATCH] Register events By: Dinnerbone --- .../src/org/bukkit/plugin/PluginManager.java | 12 ++++++++ .../bukkit/plugin/SimplePluginManager.java | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/paper-api/src/org/bukkit/plugin/PluginManager.java b/paper-api/src/org/bukkit/plugin/PluginManager.java index 0f462702c4..fd1019dc21 100644 --- a/paper-api/src/org/bukkit/plugin/PluginManager.java +++ b/paper-api/src/org/bukkit/plugin/PluginManager.java @@ -2,7 +2,9 @@ package org.bukkit.plugin; import java.io.File; +import org.bukkit.event.Event.Priority; import org.bukkit.event.player.PlayerEvent; +import org.bukkit.event.player.PlayerListener; /** * Handles all plugin management from the Server @@ -71,4 +73,14 @@ public interface PluginManager { * @param event Event details */ public void callEvent(PlayerEvent.EventType type, PlayerEvent event); + + /** + * Registers the given player 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(PlayerEvent.EventType type, PlayerListener 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 c89d8f6bcc..9eaefe1fa8 100644 --- a/paper-api/src/org/bukkit/plugin/SimplePluginManager.java +++ b/paper-api/src/org/bukkit/plugin/SimplePluginManager.java @@ -14,7 +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.Priority; +import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerEvent; +import org.bukkit.event.player.PlayerListener; /** * Handles all plugin management from the Server @@ -178,4 +181,29 @@ public final class SimplePluginManager implements PluginManager { } } } + + /** + * Registers the given player 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(PlayerEvent.EventType type, PlayerListener listener, Priority priority, Plugin plugin) { + List listeners = playerListeners.get(type); + int position = 0; + + if (listeners != null) { + for (RegisteredListener registration : listeners) { + if (registration.getPriority().compareTo(priority) < 0) { + break; + } + + position++; + } + } + + listeners.add(position, new RegisteredListener(listener, priority, plugin)); + } }