Solved How To Check If callEvent Was Fired

Discussion in 'Spigot Plugin Development' started by TheShermanTanker, Aug 9, 2018.

  1. How do you check if the callEvent Method in the PluginManager was called?
     
  2. You check whether the event passed is cancelled with Event#isCancelled()
     
  3. implement your own plugin manager (c+p from SimplePluginManager), reflect it in and put a thing in the callEvent method, or you can use runtime bytecode manipulation on the callEvent method, or you can fork bukkit if you're feeling like a legend
     
  4. While the idea of replacing the plugin manager for such reasons is perfectly okay, but for convenience you maybe don't want to implement your own by copy pacing, but just alter the behaviour of a single method. In this case you have the decorator design pattern, that helps you archiving this goal, while leaving the default behaviour intact. First we will create the decorator, that will act as a delegate to the original PluginManager:

    Code (Java):


    import org.bukkit.event.Event;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.permissions.Permissible;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.EventExecutor;
    import org.bukkit.plugin.InvalidDescriptionException;
    import org.bukkit.plugin.InvalidPluginException;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.UnknownDependencyException;

    import java.io.File;
    import java.util.Set;

    /**
    * @author Felix Klauke <[email protected]>
    */

    public class PluginManagerDecorator implements PluginManager {

        private final PluginManager pluginManager;

        public PluginManagerDecorator(PluginManager pluginManager) {
            this.pluginManager = pluginManager;
        }

        @Override
        public void registerInterface(Class<? extends PluginLoader> aClass) throws IllegalArgumentException {
            pluginManager.registerInterface(aClass);
        }

        @Override
        public Plugin getPlugin(String s) {
            return pluginManager.getPlugin(s);
        }

        @Override
        public Plugin[] getPlugins() {
            return pluginManager.getPlugins();
        }

        @Override
        public boolean isPluginEnabled(String s) {
            return pluginManager.isPluginEnabled(s);
        }

        @Override
        public boolean isPluginEnabled(Plugin plugin) {
            return pluginManager.isPluginEnabled(plugin);
        }

        @Override
        public Plugin loadPlugin(File file) throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException {
            return pluginManager.loadPlugin(file);
        }

        @Override
        public Plugin[] loadPlugins(File file) {
            return pluginManager.loadPlugins(file);
        }

        @Override
        public void disablePlugins() {
            pluginManager.disablePlugins();
        }

        @Override
        public void clearPlugins() {
            pluginManager.clearPlugins();
        }

        @Override
        public void callEvent(Event event) throws IllegalStateException {
            pluginManager.callEvent(event);
        }

        @Override
        public void registerEvents(Listener listener, Plugin plugin) {
            pluginManager.registerEvents(listener, plugin);
        }

        @Override
        public void registerEvent(Class<? extends Event> aClass, Listener listener, EventPriority eventPriority, EventExecutor eventExecutor, Plugin plugin) {
            pluginManager.registerEvent(aClass, listener, eventPriority, eventExecutor, plugin);
        }

        @Override
        public void registerEvent(Class<? extends Event> aClass, Listener listener, EventPriority eventPriority, EventExecutor eventExecutor, Plugin plugin, boolean b) {
            pluginManager.registerEvent(aClass, listener, eventPriority, eventExecutor, plugin, b);
        }

        @Override
        public void enablePlugin(Plugin plugin) {
            pluginManager.enablePlugin(plugin);
        }

        @Override
        public void disablePlugin(Plugin plugin) {
            pluginManager.disablePlugin(plugin);
        }

        @Override
        public Permission getPermission(String s) {
            return pluginManager.getPermission(s);
        }

        @Override
        public void addPermission(Permission permission) {
            pluginManager.addPermission(permission);
        }

        @Override
        public void removePermission(Permission permission) {
            pluginManager.removePermission(permission);
        }

        @Override
        public void removePermission(String s) {
            pluginManager.removePermission(s);
        }

        @Override
        public Set<Permission> getDefaultPermissions(boolean b) {
            return pluginManager.getDefaultPermissions(b);
        }

        @Override
        public void recalculatePermissionDefaults(Permission permission) {
            pluginManager.recalculatePermissionDefaults(permission);
        }

        @Override
        public void subscribeToPermission(String s, Permissible permissible) {
            pluginManager.subscribeToPermission(s, permissible);
        }

        @Override
        public void unsubscribeFromPermission(String s, Permissible permissible) {
            pluginManager.unsubscribeFromPermission(s, permissible);
        }

        @Override
        public Set<Permissible> getPermissionSubscriptions(String s) {
            return pluginManager.getPermissionSubscriptions(s);
        }

        @Override
        public void subscribeToDefaultPerms(boolean b, Permissible permissible) {
            pluginManager.subscribeToDefaultPerms(b, permissible);
        }

        @Override
        public void unsubscribeFromDefaultPerms(boolean b, Permissible permissible) {
            pluginManager.unsubscribeFromDefaultPerms(b, permissible);
        }

        @Override
        public Set<Permissible> getDefaultPermSubscriptions(boolean b) {
            return pluginManager.getDefaultPermSubscriptions(b);
        }

        @Override
        public Set<Permission> getPermissions() {
            return pluginManager.getPermissions();
        }

        @Override
        public boolean useTimings() {
            return pluginManager.useTimings();
        }
    }
     

    Now we can use our decorator to create a specific plugin manager that will just save all called events (maybe a dumb example, but this is for demonstration reasons):

    Code (Java):
    import com.google.common.collect.Lists;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;

    import java.util.List;

    /**
    * @author Felix Klauke <[email protected]>
    */

    public class MySpecialPluginManagerDecorator extends PluginManagerDecorator {

        private final List<Event> calledEvents = Lists.newArrayList();

        public MySpecialPluginManagerDecorator(PluginManager pluginManager) {
            super(pluginManager);
        }

        @Override
        public void callEvent(Event event) throws IllegalStateException {
            calledEvents.add(event);
           
            super.callEvent(event);
        }

        public List<Event> getCalledEvents() {
            return calledEvents;
        }
    }

    Now you can simply create a new instance of the MySpecialPluginManagerDecorator by passing the instance of the default plugin manager to it. The last step is to replace the PluginManager in Bukkit via reflection, like mentioned by the post before mine.
     
    • Like Like x 2
  5. Or simply listen for the event?
     
    • Agree Agree x 1
  6. What I'm making is an API for a game's classes and one of the features is to specify which events would trigger one of the particular skills every class has to have and the ClassManager automatically implements the event. I can't manually listen to the event since it derives the onSkill method from the base class
     
  7. Thanks for the help, but i kind of figured out a hackier and slightly easier method of implementing an EventExecutor in a custom RegisteredListener class and rewriting the execute Method in the EventExecutor in my class. Still appreciate the help though!
     

Share This Page