cn.nukkit.utils.PluginException Java Examples

The following examples show how to use cn.nukkit.utils.PluginException. 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: PermissibleBase.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, Boolean value) {
    if (plugin == null) {
        throw new PluginException("Plugin cannot be null");
    } else if (!plugin.isEnabled()) {
        throw new PluginException("Plugin " + plugin.getDescription().getName() + " is disabled");
    }

    PermissionAttachment result = new PermissionAttachment(plugin, this.parent != null ? this.parent : this);
    this.attachments.add(result);
    if (name != null && value != null) {
        result.setPermission(name, value);
    }
    this.recalculatePermissions();

    return result;
}
 
Example #2
Source File: ServerScheduler.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private TaskHandler addTask(Plugin plugin, Runnable task, int delay, int period, boolean asynchronous) {
    if (plugin != null && plugin.isDisabled()) {
        throw new PluginException("Plugin '" + plugin.getName() + "' attempted to register a task while disabled.");
    }
    if (delay < 0 || period < 0) {
        throw new PluginException("Attempted to register a task with negative delay or period.");
    }

    TaskHandler taskHandler = new TaskHandler(plugin, task, nextTaskId(), asynchronous);
    taskHandler.setDelay(delay);
    taskHandler.setPeriod(period);
    taskHandler.setNextRunTick(taskHandler.isDelayed() ? currentTick + taskHandler.getDelay() : currentTick);

    if (task instanceof Task) {
        ((Task) task).setHandler(taskHandler);
    }

    pending.offer(taskHandler);
    taskMap.put(taskHandler.getTaskId(), taskHandler);

    return taskHandler;
}
 
Example #3
Source File: ServerScheduler.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private TaskHandler addTask(Plugin plugin, Runnable task, int delay, int period, boolean asynchronous) {
    if (plugin != null && plugin.isDisabled()) {
        throw new PluginException("Plugin '" + plugin.getName() + "' attempted to register a task while disabled.");
    }
    if (delay < 0 || period < 0) {
        throw new PluginException("Attempted to register a task with negative delay or period.");
    }

    TaskHandler taskHandler = new TaskHandler(plugin, task, nextTaskId(), asynchronous);
    taskHandler.setDelay(delay);
    taskHandler.setPeriod(period);
    taskHandler.setNextRunTick(taskHandler.isDelayed() ? currentTick + taskHandler.getDelay() : currentTick);

    if (task instanceof Task) {
        ((Task) task).setHandler(taskHandler);
    }

    pending.offer(taskHandler);
    taskMap.put(taskHandler.getTaskId(), taskHandler);

    return taskHandler;
}
 
Example #4
Source File: ServerScheduler.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
private TaskHandler addTask(Plugin plugin, Runnable task, int delay, int period, boolean asynchronous) {
    if (plugin != null && plugin.isDisabled()) {
        throw new PluginException("Plugin '" + plugin.getName() + "' attempted to register a task while disabled.");
    }
    if (delay < 0 || period < 0) {
        throw new PluginException("Attempted to register a task with negative delay or period.");
    }

    TaskHandler taskHandler = new TaskHandler(plugin, task, nextTaskId(), asynchronous);
    taskHandler.setDelay(delay);
    taskHandler.setPeriod(period);
    taskHandler.setNextRunTick(taskHandler.isDelayed() ? currentTick + taskHandler.getDelay() : currentTick);

    if (task instanceof Task) {
        ((Task) task).setHandler(taskHandler);
    }

    pending.offer(taskHandler);
    taskMap.put(taskHandler.getTaskId(), taskHandler);

    return taskHandler;
}
 
Example #5
Source File: MetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void removeMetadata(Object subject, String metadataKey, Plugin owningPlugin) {
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    String key = this.disambiguate((Metadatable) subject, metadataKey);
    Map entry = this.metadataMap.get(key);
    if (entry == null) {
        return;
    }
    entry.remove(owningPlugin);
    if (entry.isEmpty()) {
        this.metadataMap.remove(key);
    }
}
 
Example #6
Source File: PermissionAttachment.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public PermissionAttachment(Plugin plugin, Permissible permissible) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin " + plugin.getDescription().getName() + " is disabled");
    }
    this.permissible = permissible;
    this.plugin = plugin;
}
 
Example #7
Source File: MetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void invalidateAll(Plugin owningPlugin) {
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    for (Map value : this.metadataMap.values()) {
        if (value.containsKey(owningPlugin)) {
            ((MetadataValue) value.get(owningPlugin)).invalidate();
        }
    }
}
 
Example #8
Source File: MetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void removeMetadata(Object subject, String metadataKey, Plugin owningPlugin) {
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    String key = this.disambiguate((Metadatable) subject, metadataKey);
    Map entry = this.metadataMap.get(key);
    if (entry == null) {
        return;
    }
    entry.remove(owningPlugin);
    if (entry.isEmpty()) {
        this.metadataMap.remove(key);
    }
}
 
Example #9
Source File: MetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void setMetadata(Object subject, String metadataKey, MetadataValue newMetadataValue) {
    if (newMetadataValue == null) {
        throw new ServerException("Value cannot be null");
    }
    Plugin owningPlugin = newMetadataValue.getOwningPlugin();
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    String key = this.disambiguate((Metadatable) subject, metadataKey);
    Map<Plugin, MetadataValue> entry = this.metadataMap.computeIfAbsent(key, k -> new WeakHashMap<>(1));
    entry.put(owningPlugin, newMetadataValue);
}
 
Example #10
Source File: PluginManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) throws PluginException {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + event + " while not enabled");
    }

    try {
        Timing timing = Timings.getPluginEventTiming(event, listener, executor, plugin);
        this.getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled, timing));
    } catch (IllegalAccessException e) {
        Server.getInstance().getLogger().logException(e);
    }
}
 
Example #11
Source File: PermissibleBase.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, Boolean value) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin " + plugin.getDescription().getName() + " is disabled");
    }

    PermissionAttachment result = new PermissionAttachment(plugin, this.parent != null ? this.parent : this);
    this.attachments.add(result);
    if (name != null && value != null) {
        result.setPermission(name, value);
    }
    this.recalculatePermissions();

    return result;
}
 
Example #12
Source File: PermissionAttachment.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public PermissionAttachment(Plugin plugin, Permissible permissible) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin " + plugin.getDescription().getName() + " is disabled");
    }
    this.permissible = permissible;
    this.plugin = plugin;
}
 
Example #13
Source File: MetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void invalidateAll(Plugin owningPlugin) {
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    for (Map value : this.metadataMap.values()) {
        if (value.containsKey(owningPlugin)) {
            ((MetadataValue) value.get(owningPlugin)).invalidate();
        }
    }
}
 
Example #14
Source File: MetadataStore.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void setMetadata(Object subject, String metadataKey, MetadataValue newMetadataValue) {
    if (newMetadataValue == null) {
        throw new ServerException("Value cannot be null");
    }
    Plugin owningPlugin = newMetadataValue.getOwningPlugin();
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    String key = this.disambiguate((Metadatable) subject, metadataKey);
    Map<Plugin, MetadataValue> entry = this.metadataMap.computeIfAbsent(key, k -> new WeakHashMap<>(1));
    entry.put(owningPlugin, newMetadataValue);
}
 
Example #15
Source File: PluginManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) throws PluginException {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + event + " while not enabled");
    }

    try {
        Timing timing = Timings.getPluginEventTiming(event, listener, executor, plugin);
        this.getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled, timing));
    } catch (IllegalAccessException e) {
        Server.getInstance().getLogger().logException(e);
    }
}
 
Example #16
Source File: PermissibleBase.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, Boolean value) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin " + plugin.getDescription().getName() + " is disabled");
    }

    PermissionAttachment result = new PermissionAttachment(plugin, this.parent != null ? this.parent : this);
    this.attachments.add(result);
    if (name != null && value != null) {
        result.setPermission(name, value);
    }
    this.recalculatePermissions();

    return result;
}
 
Example #17
Source File: PermissionAttachment.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public PermissionAttachment(Plugin plugin, Permissible permissible) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin " + plugin.getDescription().getName() + " is disabled");
    }
    this.permissible = permissible;
    this.plugin = plugin;
}
 
Example #18
Source File: MetadataStore.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void invalidateAll(Plugin owningPlugin) {
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    for (Map value : this.metadataMap.values()) {
        if (value.containsKey(owningPlugin)) {
            ((MetadataValue) value.get(owningPlugin)).invalidate();
        }
    }
}
 
Example #19
Source File: MetadataStore.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void removeMetadata(Object subject, String metadataKey, Plugin owningPlugin) {
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    String key = this.disambiguate((Metadatable) subject, metadataKey);
    Map entry = this.metadataMap.get(key);
    if (entry == null) {
        return;
    }
    entry.remove(owningPlugin);
    if (entry.isEmpty()) {
        this.metadataMap.remove(key);
    }
}
 
Example #20
Source File: MetadataStore.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void setMetadata(Object subject, String metadataKey, MetadataValue newMetadataValue) {
    if (newMetadataValue == null) {
        throw new ServerException("Value cannot be null");
    }
    Plugin owningPlugin = newMetadataValue.getOwningPlugin();
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    String key = this.disambiguate((Metadatable) subject, metadataKey);
    Map<Plugin, MetadataValue> entry = this.metadataMap.computeIfAbsent(key, k -> new WeakHashMap<>(1));
    entry.put(owningPlugin, newMetadataValue);
}
 
Example #21
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) throws PluginException {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + event + " while not enabled");
    }

    try {
        Timing timing = Timings.getPluginEventTiming(event, listener, executor, plugin);
        this.getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled, timing));
    } catch (IllegalAccessException e) {
        Server.getInstance().getLogger().logException(e);
    }
}
 
Example #22
Source File: PluginManager.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) throws PluginException {
    this.registerEvent(event, listener, priority, executor, plugin, false);
}
 
Example #23
Source File: PluginManager.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvents(Listener listener, Plugin plugin) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + listener.getClass().getName() + " while not enabled");
    }

    Map<Class<? extends Event>, Set<RegisteredListener>> ret = new HashMap<>();
    Set<Method> methods;
    try {
        Method[] publicMethods = listener.getClass().getMethods();
        Method[] privateMethods = listener.getClass().getDeclaredMethods();
        methods = new HashSet<>(publicMethods.length + privateMethods.length, 1.0f);
        Collections.addAll(methods, publicMethods);
        Collections.addAll(methods, privateMethods);
    } catch (NoClassDefFoundError e) {
        plugin.getLogger().error("Plugin " + plugin.getDescription().getFullName() + " has failed to register events for " + listener.getClass() + " because " + e.getMessage() + " does not exist.");
        return;
    }

    for (final Method method : methods) {
        final EventHandler eh = method.getAnnotation(EventHandler.class);
        if (eh == null) continue;
        if (method.isBridge() || method.isSynthetic()) {
            continue;
        }
        final Class<?> checkClass;

        if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) {
            plugin.getLogger().error(plugin.getDescription().getFullName() + " attempted to register an invalid EventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());
            continue;
        }

        final Class<? extends Event> eventClass = checkClass.asSubclass(Event.class);
        method.setAccessible(true);

        for (Class<?> clazz = eventClass; Event.class.isAssignableFrom(clazz); clazz = clazz.getSuperclass()) {
            // This loop checks for extending deprecated events
            if (clazz.getAnnotation(Deprecated.class) != null) {
                if (Boolean.valueOf(String.valueOf(this.server.getConfig("settings.deprecated-verbpse", true)))) {
                    this.server.getLogger().warning(this.server.getLanguage().translateString("nukkit.plugin.deprecatedEvent", plugin.getName(), clazz.getName(), listener.getClass().getName() + "." + method.getName() + "()"));
                }
                break;
            }
        }
        this.registerEvent(eventClass, listener, eh.priority(), new MethodEventExecutor(method), plugin, eh.ignoreCancelled());
    }
}
 
Example #24
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) throws PluginException {
    this.registerEvent(event, listener, priority, executor, plugin, false);
}
 
Example #25
Source File: PluginDescription.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private void loadMap(Map<String, Object> plugin) throws PluginException {
    this.name = ((String) plugin.get("name")).replaceAll("[^A-Za-z0-9 _.-]", "");
    if (this.name.equals("")) {
        throw new PluginException("Invalid PluginDescription name");
    }
    this.name = this.name.replace(" ", "_");
    this.version = String.valueOf(plugin.get("version"));
    this.main = (String) plugin.get("main");
    Object api = plugin.get("api");
    if (api instanceof List) {
        this.api = (List<String>) api;
    } else {
        List<String> list = new ArrayList<>();
        list.add((String) api);
        this.api = list;
    }
    if (this.main.startsWith("cn.nukkit.")) {
        throw new PluginException("Invalid PluginDescription main, cannot start within the cn.nukkit. package");
    }

    if (plugin.containsKey("commands") && plugin.get("commands") instanceof Map) {
        this.commands = (Map<String, Object>) plugin.get("commands");
    }

    if (plugin.containsKey("depend")) {
        this.depend = (List<String>) plugin.get("depend");
    }

    if (plugin.containsKey("softdepend")) {
        this.softDepend = (List<String>) plugin.get("softdepend");
    }

    if (plugin.containsKey("loadbefore")) {
        this.loadBefore = (List<String>) plugin.get("loadbefore");
    }

    if (plugin.containsKey("website")) {
        this.website = (String) plugin.get("website");
    }

    if (plugin.containsKey("description")) {
        this.description = (String) plugin.get("description");
    }

    if (plugin.containsKey("prefix")) {
        this.prefix = (String) plugin.get("prefix");
    }

    if (plugin.containsKey("load")) {
        String order = (String) plugin.get("load");
        try {
            this.order = PluginLoadOrder.valueOf(order);
        } catch (Exception e) {
            throw new PluginException("Invalid PluginDescription load");
        }
    }

    if (plugin.containsKey("author")) {
        this.authors.add((String) plugin.get("author"));
    }

    if (plugin.containsKey("authors")) {
        this.authors.addAll((Collection<? extends String>) plugin.get("authors"));
    }

    if (plugin.containsKey("permissions")) {
        this.permissions = Permission.loadPermissions((Map<String, Object>) plugin.get("permissions"));
    }
}
 
Example #26
Source File: PluginDescription.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void loadMap(Map<String, Object> plugin) throws PluginException {
       this.name = ((String) plugin.get("name")).replaceAll("[^A-Za-z0-9 _.-]", "");
       if (this.name.equals("")) {
           throw new PluginException("Invalid PluginDescription name");
       }
       this.name = this.name.replace(" ", "_");
       this.version = String.valueOf(plugin.get("version"));
       this.main = (String) plugin.get("main");
       Object api = plugin.get("api");
       if (api instanceof List) {
           this.api = (List<String>) api;
       } else {
           List<String> list = new ArrayList<>();
           list.add((String) api);
           this.api = list;
       }
       if (this.main.startsWith("cn.nukkit.")) {
           throw new PluginException("Invalid PluginDescription main, cannot start within the cn.nukkit. package");
       }

       if (plugin.containsKey("commands") && plugin.get("commands") instanceof Map) {
           this.commands = (Map<String, Object>) plugin.get("commands");
       }

       if (plugin.containsKey("depend")) {
           this.depend = (List<String>) plugin.get("depend");
       }

       if (plugin.containsKey("softdepend")) {
           this.softDepend = (List<String>) plugin.get("softdepend");
       }

       if (plugin.containsKey("loadbefore")) {
           this.loadBefore = (List<String>) plugin.get("loadbefore");
       }

       if (plugin.containsKey("website")) {
           this.website = (String) plugin.get("website");
       }

       if (plugin.containsKey("description")) {
           this.description = (String) plugin.get("description");
       }

       if (plugin.containsKey("prefix")) {
           this.prefix = (String) plugin.get("prefix");
       }

       if (plugin.containsKey("load")) {
           String order = (String) plugin.get("load");
           try {
               this.order = PluginLoadOrder.valueOf(order);
           } catch (Exception e) {
               throw new PluginException("Invalid PluginDescription load");
           }
       }

       if (plugin.containsKey("author")) {
           this.authors.add((String) plugin.get("author"));
       }

       if (plugin.containsKey("authors")) {
           this.authors.addAll((Collection<? extends String>) plugin.get("authors"));
       }

       if (plugin.containsKey("permissions")) {
           this.permissions = Permission.loadPermissions((Map<String, Object>) plugin.get("permissions"));
       }
   }
 
Example #27
Source File: PluginDescription.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private void loadMap(Map<String, Object> plugin) throws PluginException {
    this.name = ((String) plugin.get("name")).replaceAll("[^A-Za-z0-9 _.-]", "");
    if (this.name.equals("")) {
        throw new PluginException("Invalid PluginDescription name");
    }
    this.name = this.name.replace(" ", "_");
    this.version = String.valueOf(plugin.get("version"));
    this.main = (String) plugin.get("main");
    Object api = plugin.get("api");
    if (api instanceof List) {
        this.api = (List<String>) api;
    } else {
        List<String> list = new ArrayList<>();
        list.add((String) api);
        this.api = list;
    }
    if (this.main.startsWith("cn.nukkit.")) {
        throw new PluginException("Invalid PluginDescription main, cannot start within the cn.nukkit. package");
    }

    if (plugin.containsKey("commands") && plugin.get("commands") instanceof Map) {
        this.commands = (Map<String, Object>) plugin.get("commands");
    }

    if (plugin.containsKey("depend")) {
        this.depend = (List<String>) plugin.get("depend");
    }

    if (plugin.containsKey("softdepend")) {
        this.softDepend = (List<String>) plugin.get("softdepend");
    }

    if (plugin.containsKey("loadbefore")) {
        this.loadBefore = (List<String>) plugin.get("loadbefore");
    }

    if (plugin.containsKey("website")) {
        this.website = (String) plugin.get("website");
    }

    if (plugin.containsKey("description")) {
        this.description = (String) plugin.get("description");
    }

    if (plugin.containsKey("prefix")) {
        this.prefix = (String) plugin.get("prefix");
    }

    if (plugin.containsKey("load")) {
        String order = (String) plugin.get("load");
        try {
            this.order = PluginLoadOrder.valueOf(order);
        } catch (Exception e) {
            throw new PluginException("Invalid PluginDescription load");
        }
    }

    if (plugin.containsKey("author")) {
        this.authors.add((String) plugin.get("author"));
    }

    if (plugin.containsKey("authors")) {
        this.authors.addAll((Collection<? extends String>) plugin.get("authors"));
    }

    if (plugin.containsKey("permissions")) {
        this.permissions = Permission.loadPermissions((Map<String, Object>) plugin.get("permissions"));
    }
}
 
Example #28
Source File: PluginManager.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvents(Listener listener, Plugin plugin) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + listener.getClass().getName() + " while not enabled");
    }

    Map<Class<? extends Event>, Set<RegisteredListener>> ret = new HashMap<>();
    Set<Method> methods;
    try {
        Method[] publicMethods = listener.getClass().getMethods();
        Method[] privateMethods = listener.getClass().getDeclaredMethods();
        methods = new HashSet<>(publicMethods.length + privateMethods.length, 1.0f);
        Collections.addAll(methods, publicMethods);
        Collections.addAll(methods, privateMethods);
    } catch (NoClassDefFoundError e) {
        plugin.getLogger().error("Plugin " + plugin.getDescription().getFullName() + " has failed to register events for " + listener.getClass() + " because " + e.getMessage() + " does not exist.");
        return;
    }

    for (final Method method : methods) {
        final EventHandler eh = method.getAnnotation(EventHandler.class);
        if (eh == null) continue;
        if (method.isBridge() || method.isSynthetic()) {
            continue;
        }
        final Class<?> checkClass;

        if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) {
            plugin.getLogger().error(plugin.getDescription().getFullName() + " attempted to register an invalid EventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());
            continue;
        }

        final Class<? extends Event> eventClass = checkClass.asSubclass(Event.class);
        method.setAccessible(true);

        for (Class<?> clazz = eventClass; Event.class.isAssignableFrom(clazz); clazz = clazz.getSuperclass()) {
            // This loop checks for extending deprecated events
            if (clazz.getAnnotation(Deprecated.class) != null) {
                if (Boolean.valueOf(String.valueOf(this.server.getConfig("settings.deprecated-verbpse", true)))) {
                    this.server.getLogger().warning(this.server.getLanguage().translateString("nukkit.plugin.deprecatedEvent", plugin.getName(), clazz.getName(), listener.getClass().getName() + "." + method.getName() + "()"));
                }
                break;
            }
        }
        this.registerEvent(eventClass, listener, eh.priority(), new MethodEventExecutor(method), plugin, eh.ignoreCancelled());
    }
}
 
Example #29
Source File: PluginManager.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) throws PluginException {
    this.registerEvent(event, listener, priority, executor, plugin, false);
}
 
Example #30
Source File: PluginManager.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public void registerEvents(Listener listener, Plugin plugin) {
    if (!plugin.isEnabled()) {
        throw new PluginException("Plugin attempted to register " + listener.getClass().getName() + " while not enabled");
    }

    Set<Method> methods;
    try {
        Method[] publicMethods = listener.getClass().getMethods();
        Method[] privateMethods = listener.getClass().getDeclaredMethods();
        methods = new HashSet<>(publicMethods.length + privateMethods.length, 1.0f);
        Collections.addAll(methods, publicMethods);
        Collections.addAll(methods, privateMethods);
    } catch (NoClassDefFoundError e) {
        plugin.getLogger().error("Plugin " + plugin.getDescription().getFullName() + " has failed to register events for " + listener.getClass() + " because " + e.getMessage() + " does not exist.");
        return;
    }

    for (final Method method : methods) {
        final EventHandler eh = method.getAnnotation(EventHandler.class);
        if (eh == null) continue;
        if (method.isBridge() || method.isSynthetic()) {
            continue;
        }
        final Class<?> checkClass;

        if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) {
            plugin.getLogger().error(plugin.getDescription().getFullName() + " attempted to register an invalid EventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());
            continue;
        }

        final Class<? extends Event> eventClass = checkClass.asSubclass(Event.class);
        method.setAccessible(true);

        for (Class<?> clazz = eventClass; Event.class.isAssignableFrom(clazz); clazz = clazz.getSuperclass()) {
            // This loop checks for extending deprecated events
            if (clazz.getAnnotation(Deprecated.class) != null) {
                if (Boolean.valueOf(String.valueOf(this.server.getConfig("settings.deprecated-verbpse", true)))) {
                    this.server.getLogger().warning(this.server.getLanguage().translateString("nukkit.plugin.deprecatedEvent", plugin.getName(), clazz.getName(), listener.getClass().getName() + "." + method.getName() + "()"));
                }
                break;
            }
        }
        this.registerEvent(eventClass, listener, eh.priority(), new MethodEventExecutor(method), plugin, eh.ignoreCancelled());
    }
}