Added service register and unregister events. Addresses BUKKIT-816

By: zml2008 <zach@zachsthings.com>
This commit is contained in:
Bukkit/Spigot 2011-11-12 20:22:30 -08:00
parent 1d03cfb423
commit 860011dcc1
4 changed files with 84 additions and 4 deletions

View File

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

View File

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

View File

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

View File

@ -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<RegisteredServiceProvider<?>>();
providers.put(service, registered);
}
RegisteredServiceProvider<T> registeredProvider = new RegisteredServiceProvider<T>(service, provider, priority, plugin);
registered.add(new RegisteredServiceProvider<T>(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