Search in sources :

Example 1 with PluginException

use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.

the class PluginManager method registerEvent.

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);
    }
}
Also used : PluginException(cn.nukkit.utils.PluginException) Timing(co.aikar.timings.Timing)

Example 2 with PluginException

use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.

the class MetadataStore method setMetadata.

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);
}
Also used : ServerException(cn.nukkit.utils.ServerException) PluginException(cn.nukkit.utils.PluginException) Plugin(cn.nukkit.plugin.Plugin)

Example 3 with PluginException

use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.

the class PluginDescription method loadMap.

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"));
    }
}
Also used : PluginException(cn.nukkit.utils.PluginException) PluginException(cn.nukkit.utils.PluginException)

Example 4 with PluginException

use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.

the class PluginManager method registerEvents.

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());
    }
}
Also used : PluginException(cn.nukkit.utils.PluginException) Method(java.lang.reflect.Method)

Example 5 with PluginException

use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.

the class JavaPluginLoader method loadPlugin.

@Override
public Plugin loadPlugin(File file) throws Exception {
    PluginDescription description = this.getPluginDescription(file);
    if (description != null) {
        this.server.getLogger().info(this.server.getLanguage().translateString("nukkit.plugin.load", description.getFullName()));
        File dataFolder = new File(file.getParentFile(), description.getName());
        if (dataFolder.exists() && !dataFolder.isDirectory()) {
            throw new IllegalStateException("Projected dataFolder '" + dataFolder.toString() + "' for " + description.getName() + " exists and is not a directory");
        }
        String className = description.getMain();
        PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(), file);
        this.classLoaders.put(description.getName(), classLoader);
        PluginBase plugin;
        try {
            Class javaClass = classLoader.loadClass(className);
            try {
                Class<? extends PluginBase> pluginClass = javaClass.asSubclass(PluginBase.class);
                plugin = pluginClass.newInstance();
                this.initPlugin(plugin, description, dataFolder, file);
                return plugin;
            } catch (ClassCastException e) {
                throw new PluginException("main class `" + description.getMain() + "' does not extend PluginBase");
            } catch (InstantiationException | IllegalAccessException e) {
                Server.getInstance().getLogger().logException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new PluginException("Couldn't load plugin " + description.getName() + ": main class not found");
        }
    }
    return null;
}
Also used : PluginException(cn.nukkit.utils.PluginException) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

PluginException (cn.nukkit.utils.PluginException)5 Plugin (cn.nukkit.plugin.Plugin)1 ServerException (cn.nukkit.utils.ServerException)1 Timing (co.aikar.timings.Timing)1 File (java.io.File)1 Method (java.lang.reflect.Method)1 JarFile (java.util.jar.JarFile)1