From 860011dcc16154dd0e08119c3aedc4e206d9d180 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sat, 12 Nov 2011 20:22:30 -0800 Subject: [PATCH] Added service register and unregister events. Addresses BUKKIT-816 By: zml2008 --- .../org/bukkit/event/server/ServiceEvent.java | 18 ++++++++++++++ .../event/server/ServiceRegisterEvent.java | 23 ++++++++++++++++++ .../event/server/ServiceUnregisterEvent.java | 23 ++++++++++++++++++ .../bukkit/plugin/SimpleServicesManager.java | 24 +++++++++++++++---- 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 paper-api/src/main/java/org/bukkit/event/server/ServiceEvent.java create mode 100644 paper-api/src/main/java/org/bukkit/event/server/ServiceRegisterEvent.java create mode 100644 paper-api/src/main/java/org/bukkit/event/server/ServiceUnregisterEvent.java diff --git a/paper-api/src/main/java/org/bukkit/event/server/ServiceEvent.java b/paper-api/src/main/java/org/bukkit/event/server/ServiceEvent.java new file mode 100644 index 0000000000..311e153b27 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/server/ServiceEvent.java @@ -0,0 +1,18 @@ +package org.bukkit.event.server; + +import org.bukkit.plugin.RegisteredServiceProvider; + +/** + * An event relating to a registered service. This is called in a {@link org.bukkit.plugin.ServicesManager} + */ +public abstract class ServiceEvent extends ServerEvent { + private final RegisteredServiceProvider provider; + + public ServiceEvent(final RegisteredServiceProvider provider) { + this.provider = provider; + } + + public RegisteredServiceProvider getProvider() { + return provider; + } +} \ No newline at end of file diff --git a/paper-api/src/main/java/org/bukkit/event/server/ServiceRegisterEvent.java b/paper-api/src/main/java/org/bukkit/event/server/ServiceRegisterEvent.java new file mode 100644 index 0000000000..89c852d5a7 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/server/ServiceRegisterEvent.java @@ -0,0 +1,23 @@ +package org.bukkit.event.server; + +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.RegisteredServiceProvider; + +/** + * This event is called when a service is registered. + */ +public class ServiceRegisterEvent extends ServiceEvent { + private static final HandlerList handlers = new HandlerList(); + + public ServiceRegisterEvent(RegisteredServiceProvider registeredProvider) { + super(registeredProvider); + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/paper-api/src/main/java/org/bukkit/event/server/ServiceUnregisterEvent.java b/paper-api/src/main/java/org/bukkit/event/server/ServiceUnregisterEvent.java new file mode 100644 index 0000000000..608f5576bd --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/event/server/ServiceUnregisterEvent.java @@ -0,0 +1,23 @@ +package org.bukkit.event.server; + +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.RegisteredServiceProvider; + +/** + * This event is called when a service is unregistered. + */ +public class ServiceUnregisterEvent extends ServiceEvent { + private static final HandlerList handlers = new HandlerList(); + + public ServiceUnregisterEvent(RegisteredServiceProvider serviceProvider) { + super(serviceProvider); + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/paper-api/src/main/java/org/bukkit/plugin/SimpleServicesManager.java b/paper-api/src/main/java/org/bukkit/plugin/SimpleServicesManager.java index c0f0a54c31..94600518b7 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/SimpleServicesManager.java +++ b/paper-api/src/main/java/org/bukkit/plugin/SimpleServicesManager.java @@ -1,5 +1,9 @@ package org.bukkit.plugin; +import org.bukkit.Bukkit; +import org.bukkit.event.server.ServiceRegisterEvent; +import org.bukkit.event.server.ServiceUnregisterEvent; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -37,12 +41,15 @@ public class SimpleServicesManager implements ServicesManager { registered = new ArrayList>(); providers.put(service, registered); } + RegisteredServiceProvider registeredProvider = new RegisteredServiceProvider(service, provider, priority, plugin); - registered.add(new RegisteredServiceProvider(service, provider, priority, plugin)); + registered.add(registeredProvider); // Make sure that providers are in the right order in order // for priorities to work correctly Collections.sort(registered); + + Bukkit.getServer().getPluginManager().callEvent(new ServiceRegisterEvent(registeredProvider)); } } @@ -64,8 +71,11 @@ public class SimpleServicesManager implements ServicesManager { // Removed entries that are from this plugin while (it2.hasNext()) { - if (it2.next().getPlugin() == plugin) { + RegisteredServiceProvider registered = it2.next(); + + if (registered.getPlugin() == plugin) { it2.remove(); + Bukkit.getServer().getPluginManager().callEvent(new ServiceUnregisterEvent(registered)); } } } catch (NoSuchElementException e) { // Why does Java suck @@ -105,8 +115,11 @@ public class SimpleServicesManager implements ServicesManager { // Removed entries that are from this plugin while (it2.hasNext()) { - if (it2.next().getProvider() == provider) { + RegisteredServiceProvider registered = it2.next(); + + if (registered.getProvider() == provider) { it2.remove(); + Bukkit.getServer().getPluginManager().callEvent(new ServiceUnregisterEvent(registered)); } } } catch (NoSuchElementException e) { // Why does Java suck @@ -139,8 +152,11 @@ public class SimpleServicesManager implements ServicesManager { // Removed entries that are from this plugin while (it2.hasNext()) { - if (it2.next().getProvider() == provider) { + RegisteredServiceProvider registered = it2.next(); + + if (registered.getProvider() == provider) { it2.remove(); + Bukkit.getServer().getPluginManager().callEvent(new ServiceUnregisterEvent(registered)); } } } catch (NoSuchElementException e) { // Why does Java suck