org.bukkit.plugin.RegisteredListener Java Examples

The following examples show how to use org.bukkit.plugin.RegisteredListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HandlerListInjector.java    From Bukkit-Connect with GNU General Public License v3.0 6 votes vote down vote up
@Override
public synchronized void bake() {
	super.bake();
	RegisteredListener[] handlers = super.getRegisteredListeners();
	// TODO we can speed this up greatly using arrays. It's not really necessary though, as this isn't a hot function
	for(RegisteredListener handler : handlers) {
		if(handler.getPlugin().equals(plugin)) {
			if(handler.getPriority().equals(EventPriority.LOWEST)) {
				this.startListeners.add(handler);
				continue;
			} else if(handler.getPriority().equals(EventPriority.MONITOR)) {
				this.endListeners.add(handler);
				continue;
			}
		}
		this.middleListeners.add(handler);
	}
	List<RegisteredListener> handlerList = new ArrayList<RegisteredListener>(handlers.length);
	handlerList.addAll(this.startListeners);
	handlerList.addAll(this.middleListeners);
	handlerList.addAll(this.endListeners);
	handlerList.toArray(handlers);
}
 
Example #2
Source File: MatchImpl.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
private void startListener(Listener listener) {
  for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry :
      PGM.get().getPluginLoader().createRegisteredListeners(listener, PGM.get()).entrySet()) {
    Class<? extends Event> eventClass = entry.getKey();
    HandlerList handlerList = Events.getEventListeners(eventClass);

    // TODO: expand support to other events, such as player, world, entity -- reduce boilerplate
    if (MatchEvent.class.isAssignableFrom(eventClass)) {
      for (final RegisteredListener registeredListener : entry.getValue()) {
        PGM.get()
            .getServer()
            .getPluginManager()
            .registerEvent(
                eventClass,
                listener,
                registeredListener.getPriority(),
                new EventExecutor(registeredListener),
                PGM.get());
      }
    } else {
      handlerList.registerAll(entry.getValue());
    }
  }
}
 
Example #3
Source File: SubCommand_Debug.java    From QuickShop-Reremake with GNU General Public License v3.0 6 votes vote down vote up
public void printHandlerList(@NotNull CommandSender sender, String event) {
    try {
        final Class<?> clazz = Class.forName(event);
        final Method method = clazz.getMethod("getHandlerList");
        final Object[] obj = new Object[0];
        final HandlerList list = (HandlerList) method.invoke(null, obj);

        for (RegisteredListener listener1 : list.getRegisteredListeners()) {
            MsgUtil.sendMessage(sender,
                    ChatColor.AQUA
                            + listener1.getPlugin().getName()
                            + ChatColor.YELLOW
                            + " # "
                            + ChatColor.GREEN
                            + listener1.getListener().getClass().getCanonicalName());
        }
    } catch (Throwable th) {
        MsgUtil.sendMessage(sender, "ERR " + th.getMessage());
        th.printStackTrace();
    }
}
 
Example #4
Source File: CancellationDetector.java    From DiscordSRV with GNU General Public License v3.0 6 votes vote down vote up
private RegisteredListener injectRegisteredListener(final RegisteredListener listener) {
    return new DelegatedRegisteredListener(listener) {
        @SuppressWarnings("unchecked")
        @Override
        public void callEvent(Event event) throws EventException {
            if (event instanceof Cancellable) {
                boolean prior = getCancelState(event);

                listener.callEvent(event);

                // See if this plugin cancelled the event
                if (!prior && getCancelState(event)) {
                    invokeCancelled(getPlugin(), (TEvent) event);
                }
            } else {
                listener.callEvent(event);
            }
        }
    };
}
 
Example #5
Source File: ItemMenuListener.java    From AnnihilationPro with MIT License 6 votes vote down vote up
/**
 * Checks if the {@link ninja.amp.ampmenus.MenuListener} is registered to a
 * plugin.
 *
 * @param plugin
 *            The plugin.
 * @return True if the {@link ninja.amp.ampmenus.MenuListener} is registered
 *         to the plugin, else false.
 */
public boolean isRegistered(JavaPlugin plugin)
{
	if (plugin.equals(this.plugin))
	{
		for (RegisteredListener listener : HandlerList
				.getRegisteredListeners(plugin))
		{
			if (listener.getListener().equals(INSTANCE))
			{
				return true;
			}
		}
	}
	return false;
}
 
Example #6
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get a specific plugin's registered listeners associated with this
 * handler list
 *
 * @param plugin the plugin to get the listeners of
 * @return the list of registered listeners
 */
public static ArrayList<RegisteredListener> getRegisteredListeners(Plugin plugin) {
    ArrayList<RegisteredListener> listeners = new ArrayList<RegisteredListener>();
    synchronized (allLists) {
        for (HandlerList h : allLists) {
            synchronized (h) {
                for (List<RegisteredListener> list : h.handlerslots.values()) {
                    for (RegisteredListener listener : list) {
                        if (listener.getPlugin().equals(plugin)) {
                            listeners.add(listener);
                        }
                    }
                }
            }
        }
    }
    return listeners;
}
 
Example #7
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Bake HashMap and ArrayLists to 2d array - does nothing if not necessary
 */
public synchronized void bake() {
    if (handlers != null) return; // don't re-bake when still valid
    List<RegisteredListener> entries = new ArrayList<RegisteredListener>();
    for (Entry<EventPriority, ArrayList<RegisteredListener>> entry : handlerslots.entrySet()) {
        entries.addAll(entry.getValue());
    }
    handlers = entries.toArray(new RegisteredListener[entries.size()]);
}
 
Example #8
Source File: HandlerListInjector.java    From Bukkit-Connect with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static void prioritize(Plugin plugin, Class<? extends Event> event) throws Exception {
	HandlerList handlerList = ReflectionUtils.getPrivateField(event, null, HandlerList.class, "handlers");
	HandlerListInjector injector = new HandlerListInjector(plugin);
	// move the handlerslots
	EnumMap<EventPriority, ArrayList<RegisteredListener>> handlerListHandlerSlots = ReflectionUtils.getPrivateField(HandlerList.class, handlerList, EnumMap.class, "handlerslots");
	EnumMap<EventPriority, ArrayList<RegisteredListener>> injectorHandlerSlots = ReflectionUtils.getPrivateField(HandlerList.class, injector, EnumMap.class, "handlerslots");
	injectorHandlerSlots.putAll(handlerListHandlerSlots);
	// remove old from allLists
	ArrayList<HandlerList> allLists = ReflectionUtils.getPrivateField(HandlerList.class, null, ArrayList.class, "allLists");
	allLists.remove(handlerList);
	// replace event handlers
	ReflectionUtils.setFinalField(event, null, "handlers", injector);
}
 
Example #9
Source File: MenuListener.java    From AmpMenus with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the {@link ninja.amp.ampmenus.MenuListener} is registered to a plugin.
 *
 * @param plugin The plugin.
 * @return True if the {@link ninja.amp.ampmenus.MenuListener} is registered to the plugin, else false.
 */
public boolean isRegistered(JavaPlugin plugin) {
    if (plugin.equals(this.plugin)) {
        for (RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) {
            if (listener.getListener().equals(INSTANCE)) {
                return true;
            }
        }
    }
    return false;
}
 
Example #10
Source File: FreeBuildRegion.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public FreeBuildRegion() {
    super("freebuild");
    this.listeners = new ArrayList<>();
    RegisteredListener[] listeners = BlockBreakEvent.getHandlerList().getRegisteredListeners();
    for (RegisteredListener listener : listeners) {
        if (listener.getPriority() == EventPriority.MONITOR) continue;
        if (!listener.isIgnoringCancelled()) continue;
        this.listeners.add(listener);
    }
}
 
Example #11
Source File: CancellationDetector.java    From DiscordSRV with GNU General Public License v3.0 5 votes vote down vote up
private void injectProxy() {
    HandlerList list = getHandlerList(eventClazz);
    EnumMap<EventPriority, ArrayList<RegisteredListener>> slots = getSlots(list);

    // Keep a copy of this map
    backup = slots.clone();

    synchronized (list) {
        for (EventPriority p : slots.keySet().toArray(new EventPriority[0])) {
            final EventPriority priority = p;
            final ArrayList<RegisteredListener> proxyList = new ArrayList<RegisteredListener>() {
                private static final long serialVersionUID = 7869505892922082581L;

                @Override
                public boolean add(RegisteredListener e) {
                    super.add(injectRegisteredListener(e));
                    return backup.get(priority).add(e);
                }

                @Override
                public boolean remove(Object listener) {
                    // Remove this listener
                    for (Iterator<RegisteredListener> it = iterator(); it.hasNext(); ) {
                        DelegatedRegisteredListener delegated = (DelegatedRegisteredListener) it.next();
                        if (delegated.delegate == listener) {
                            it.remove();
                            break;
                        }
                    }
                    return backup.get(priority).remove(listener);
                }
            };
            slots.put(priority, proxyList);

            proxyList.addAll(backup.get(priority));
        }
    }
}
 
Example #12
Source File: CancellationDetector.java    From DiscordSRV with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private EnumMap<EventPriority, ArrayList<RegisteredListener>> getSlots(HandlerList list) {
    try {
        return (EnumMap<EventPriority, ArrayList<RegisteredListener>>) getSlotsField(list).get(list);
    } catch (Exception e) {
        throw new RuntimeException("Unable to retrieve slots.", e);
    }
}
 
Example #13
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Remove a specific listener from this handler
 *
 * @param listener listener to remove
 */
public synchronized void unregister(Listener listener) {
    boolean changed = false;
    for (List<RegisteredListener> list : handlerslots.values()) {
        for (ListIterator<RegisteredListener> i = list.listIterator(); i.hasNext(); ) {
            if (i.next().getListener().equals(listener)) {
                i.remove();
                changed = true;
            }
        }
    }
    if (changed) handlers = null;
}
 
Example #14
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Remove a specific plugin's listeners from this handler
 *
 * @param plugin plugin to remove
 */
public synchronized void unregister(Plugin plugin) {
    boolean changed = false;
    for (List<RegisteredListener> list : handlerslots.values()) {
        for (ListIterator<RegisteredListener> i = list.listIterator(); i.hasNext(); ) {
            if (i.next().getPlugin().equals(plugin)) {
                i.remove();
                changed = true;
            }
        }
    }
    if (changed) handlers = null;
}
 
Example #15
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Register a new listener in this handler list
 *
 * @param listener listener to register
 */
public synchronized void register(RegisteredListener listener) {
    if (handlerslots.get(listener.getPriority()).contains(listener))
        throw new IllegalStateException("This listener is already registered to priority " + listener.getPriority().toString());
    handlers = null;
    handlerslots.get(listener.getPriority()).add(listener);
}
 
Example #16
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a new handler list and initialize using EventPriority.
 * <p>
 * The HandlerList is then added to meta-list for use in bakeAll()
 */
public HandlerList() {
    handlerslots = new EnumMap<EventPriority, ArrayList<RegisteredListener>>(EventPriority.class);
    for (EventPriority o : EventPriority.values()) {
        handlerslots.put(o, new ArrayList<RegisteredListener>());
    }
    synchronized (allLists) {
        allLists.add(this);
    }
}
 
Example #17
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Unregister all listeners from all handler lists.
 */
public static void unregisterAll() {
    synchronized (allLists) {
        for (HandlerList h : allLists) {
            synchronized (h) {
                for (List<RegisteredListener> list : h.handlerslots.values()) {
                    list.clear();
                }
                h.handlers = null;
            }
        }
    }
}
 
Example #18
Source File: RuntimePluginLoader.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Map<Class<? extends Event>, Set<RegisteredListener>> createRegisteredListeners(
    Listener listener, Plugin plugin) {
  return loader.createRegisteredListeners(listener, plugin);
}
 
Example #19
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get the baked registered listeners associated with this handler list
 *
 * @return the array of registered listeners
 */
public RegisteredListener[] getRegisteredListeners() {
    RegisteredListener[] handlers;
    while ((handlers = this.handlers) == null) bake(); // This prevents fringe cases of returning null
    return handlers;
}
 
Example #20
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Remove a listener from a specific order slot
 *
 * @param listener listener to remove
 */
public synchronized void unregister(RegisteredListener listener) {
    if (handlerslots.get(listener.getPriority()).remove(listener)) {
        handlers = null;
    }
}
 
Example #21
Source File: HandlerList.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Register a collection of new listeners in this handler list
 *
 * @param listeners listeners to register
 */
public void registerAll(Collection<RegisteredListener> listeners) {
    for (RegisteredListener listener : listeners) {
        register(listener);
    }
}
 
Example #22
Source File: CancellationDetector.java    From DiscordSRV with GNU General Public License v3.0 4 votes vote down vote up
public DelegatedRegisteredListener(RegisteredListener delegate) {
    // These values will be ignored however'
    super(delegate.getListener(), null, delegate.getPriority(), delegate.getPlugin(), false);
    this.delegate = delegate;
}
 
Example #23
Source File: QuickShop.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Load 3rdParty plugin support module.
 */
private void load3rdParty() {
    // added for compatibility reasons with OpenInv - see
    // https://github.com/KaiKikuchi/QuickShop/issues/139
    if (getConfig().getBoolean("plugin.OpenInv")) {
        this.openInvPlugin = Bukkit.getPluginManager().getPlugin("OpenInv");
        if (this.openInvPlugin != null) {
            getLogger().info("Successfully loaded OpenInv support!");
        }
    }
    if (getConfig().getBoolean("plugin.PlaceHolderAPI")) {
        this.placeHolderAPI = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
        if (this.placeHolderAPI != null) {
            getLogger().info("Successfully loaded PlaceHolderAPI support!");
        }
    }
    if (getConfig().getBoolean("plugin.BlockHub")) {
        this.blockHubPlugin = Bukkit.getPluginManager().getPlugin("BlockHub");
        if (this.blockHubPlugin != null) {
            getLogger().info("Successfully loaded BlockHub support!");
        }
    }
    if (getConfig().getBoolean("plugin.LWC")) {
        this.lwcPlugin = Bukkit.getPluginManager().getPlugin("LWC");
        if (this.lwcPlugin != null) {
            getLogger().info("Successfully loaded LWC support!");
        }
    }
    if (Bukkit.getPluginManager().getPlugin("NoCheatPlus") != null) {
        compatibilityTool.register(new NCPCompatibilityModule(this));
    }
    if (this.display) {
        //VirtualItem support
        if (DisplayItem.getNowUsing() == DisplayType.VIRTUALITEM) {
            getLogger().info("Using Virtual item Display, loading ProtocolLib support...");
            Plugin protocolLibPlugin = Bukkit.getPluginManager().getPlugin("ProtocolLib");
            if (protocolLibPlugin != null && protocolLibPlugin.isEnabled()) {
                getLogger().info("Successfully loaded ProtocolLib support!");
            } else {
                getLogger().warning("Failed to load ProtocolLib support, fallback to real item display");
                getConfig().set("shop.display-type", 0);
                saveConfig();
            }
        }

        if (DisplayItem.getNowUsing() != DisplayType.VIRTUALITEM && Bukkit.getPluginManager().getPlugin("ClearLag") != null) {
            try {
                Clearlag clearlag = (Clearlag) Bukkit.getPluginManager().getPlugin("ClearLag");
                for (RegisteredListener clearLagListener : ItemSpawnEvent.getHandlerList().getRegisteredListeners()) {
                    if (!clearLagListener.getPlugin().equals(clearlag)) {
                        continue;
                    }
                    int spamTimes = 20;
                    if (clearLagListener.getListener().getClass().equals(ItemMergeListener.class)) {
                        ItemSpawnEvent.getHandlerList().unregister(clearLagListener.getListener());
                        for (int i = 0; i < spamTimes; i++) {
                            getLogger().warning("+++++++++++++++++++++++++++++++++++++++++++");
                            getLogger().severe("Detected incompatible module of ClearLag-ItemMerge module, it will broken the QuickShop display, we already unregister this module listener!");
                            getLogger().severe("Please turn off it in the ClearLag config.yml or turn off the QuickShop display feature!");
                            getLogger().severe("If you didn't do that, this message will keep spam in your console every times you server boot up!");
                            getLogger().warning("+++++++++++++++++++++++++++++++++++++++++++");
                            getLogger().info("This message will spam more " + (spamTimes - i) + " times!");
                        }
                    }
                }
            } catch (Throwable ignored) {
            }
        }
    }
}
 
Example #24
Source File: MatchImpl.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
private EventExecutor(RegisteredListener listener) {
  this.listener = checkNotNull(listener);
}