Package org.bukkit.plugin

Examples of org.bukkit.plugin.InvalidPluginException


        try {
            Class<?> jarClass;
            try {
                jarClass = Class.forName(description.getMain(), true, this);
            } catch (ClassNotFoundException ex) {
                throw new InvalidPluginException("Cannot find main class `" + description.getMain() + "'", ex);
            }

            Class<? extends JavaPlugin> pluginClass;
            try {
                pluginClass = jarClass.asSubclass(JavaPlugin.class);
            } catch (ClassCastException ex) {
                throw new InvalidPluginException("main class `" + description.getMain() + "' does not extend JavaPlugin", ex);
            }

            plugin = pluginClass.newInstance();
        } catch (IllegalAccessException ex) {
            throw new InvalidPluginException("No public constructor", ex);
        } catch (InstantiationException ex) {
            throw new InvalidPluginException("Abnormal plugin type", ex);
        }
    }
View Full Code Here


    public Plugin loadPlugin(final File file) throws InvalidPluginException {
        Validate.notNull(file, "File cannot be null");

        if (!file.exists()) {
            throw new InvalidPluginException(new FileNotFoundException(file.getPath() + " does not exist"));
        }

        final PluginDescriptionFile description;
        try {
            description = getPluginDescription(file);
        } catch (InvalidDescriptionException ex) {
            throw new InvalidPluginException(ex);
        }

        final File parentFile = file.getParentFile();
        final File dataFolder = new File(parentFile, description.getName());
        @SuppressWarnings("deprecation")
        final File oldDataFolder = new File(parentFile, description.getRawName());

        // Found old data folder
        if (dataFolder.equals(oldDataFolder)) {
            // They are equal -- nothing needs to be done!
        } else if (dataFolder.isDirectory() && oldDataFolder.isDirectory()) {
            server.getLogger().warning(String.format(
                "While loading %s (%s) found old-data folder: `%s' next to the new one `%s'",
                description.getFullName(),
                file,
                oldDataFolder,
                dataFolder
            ));
        } else if (oldDataFolder.isDirectory() && !dataFolder.exists()) {
            if (!oldDataFolder.renameTo(dataFolder)) {
                throw new InvalidPluginException("Unable to rename old data folder: `" + oldDataFolder + "' to: `" + dataFolder + "'");
            }
            server.getLogger().log(Level.INFO, String.format(
                "While loading %s (%s) renamed data folder: `%s' to `%s'",
                description.getFullName(),
                file,
                oldDataFolder,
                dataFolder
            ));
        }

        if (dataFolder.exists() && !dataFolder.isDirectory()) {
            throw new InvalidPluginException(String.format(
                "Projected datafolder: `%s' for %s (%s) exists and is not a directory",
                dataFolder,
                description.getFullName(),
                file
            ));
        }

        for (final String pluginName : description.getDepend()) {
            if (loaders == null) {
                throw new UnknownDependencyException(pluginName);
            }
            PluginClassLoader current = loaders.get(pluginName);

            if (current == null) {
                throw new UnknownDependencyException(pluginName);
            }
        }

        final PluginClassLoader loader;
        try {
            loader = new PluginClassLoader(this, getClass().getClassLoader(), description, dataFolder, file);
        } catch (InvalidPluginException ex) {
            throw ex;
        } catch (Throwable ex) {
            throw new InvalidPluginException(ex);
        }

        loaders.put(description.getName(), loader);

        return loader.plugin;
View Full Code Here

    public Plugin loadPlugin(File file, boolean ignoreSoftDependencies)
            throws InvalidPluginException/*, InvalidDescriptionException, UnknownDependencyException*/ {

        if (!file.exists()) {
            throw new InvalidPluginException(new FileNotFoundException(String.format("%s does not exist",
                    file.getPath())));
        }

        PluginDataFile data = null;

        if (file.getName().endsWith(".py")) {
            if (file.isDirectory())
                throw new InvalidPluginException(new Exception("python files cannot be directories! try .py.dir instead."));
            data = new PluginPythonFile(file);
        } else if (file.getName().endsWith("dir")) {
            if (!file.isDirectory())
                throw new InvalidPluginException(new Exception("python directories cannot be normal files! try .py or .py.zip instead."));
            data = new PluginPythonDirectory(file);
        } else if (file.getName().endsWith("zip") || file.getName().endsWith("pyp")) {
            if (file.isDirectory())
                throw new InvalidPluginException(new Exception("python zips cannot be directories! try .py.dir instead."));
            data = new PluginPythonZip(file);
        } else {
            throw new InvalidPluginException(new Exception("filename '"+file.getName()+"' does not end in py, dir, zip, or pyp! did you add a regex without altering loadPlugin()?"));
        }

        try {
            return loadPlugin(file, ignoreSoftDependencies, data);
        } finally {
View Full Code Here

                hassolidmeta = true;
            } else {
                String stripped = stripExtension(file.getName());
                if (stripped == null)
                    //throw new BukkitScrewedUpException("This would only occur if bukkit called the loader on a plugin which does not match the loader's regex.");
                    throw new InvalidPluginException(new Exception("This shouldn't be happening; go tell whoever altered the plugin loading api in bukkit that they're whores."));
                description = new PluginDescriptionFile(stripped, "dev", "plugin.py");
            }
            if (stream != null)
                stream.close();
        } catch (IOException ex) {
            throw new InvalidPluginException(ex);
        } catch (YAMLException ex) {
            throw new InvalidPluginException(ex);
        } catch (InvalidDescriptionException ex) {
            throw new InvalidPluginException(ex);
        }

        File dataFolder = new File(file.getParentFile(), description.getName());

        if (dataFolder.getAbsolutePath().equals(file.getAbsolutePath())) {
            throw new InvalidPluginException(new Exception(String.format("Projected datafolder: '%s' for %s is the same file as the plugin itself (%s)",
                    dataFolder,
                    description.getName(),
                    file)));
        }

        if (dataFolder.exists() && !dataFolder.isDirectory()) {
            throw new InvalidPluginException(new Exception(String.format("Projected datafolder: '%s' for %s (%s) exists and is not a directory",
                    dataFolder,
                    description.getName(),
                    file)));
        }

        ArrayList<String> depend;

        try {
            depend = (ArrayList<String>) description.getDepend();
            if (depend == null) {
                depend = new ArrayList<String>();
            }
        } catch (ClassCastException ex) {
            throw new InvalidPluginException(ex);
        }

        for (String pluginName : depend) {
            if (!isPluginLoaded(pluginName)) {
                throw new UnknownDependencyException(pluginName);
            }
        }

        PyList pythonpath = Py.getSystemState().path;
        PyString filepath = new PyString(file.getAbsolutePath());
        if (data.shouldAddPathEntry()) {
            if (pythonpath.__contains__(filepath)) {
                throw new InvalidPluginException(new Exception("path " + filepath
                        + " already on pythonpath!")); //can't imagine how this would happen, but better safe than sorry
            }
            pythonpath.append(filepath);
        }


        String mainfile = description.getMain();
        InputStream instream = null;
        try {
            instream = data.getStream(mainfile);

            if (instream == null) {
                mainfile = "plugin.py";
                instream = data.getStream(mainfile);
            }
            if (instream == null) {
                mainfile = "main.py";
                instream = data.getStream(mainfile);
            }
        } catch (IOException e) {
            throw new InvalidPluginException(e);
        }

        if (instream == null) {
            throw new InvalidPluginException(new FileNotFoundException("Data file does not contain "+mainfile));
        }
        try {
            PythonHooks hook = new PythonHooks(description);

            PythonInterpreter interp = new PythonInterpreter();

            interp.set("hook", hook);
            interp.set("info", description);
           
            // Decorator Enhancements
            interp.exec("import __builtin__");
            interp.exec("__builtin__.hook = hook");
            interp.exec("__builtin__.info = info");
           
            // Hardcoded for now, may be worth thinking about generalizing it as sort of "addons" for the PythonPluginLoader
            // Could be used to extend the capabilities of python plugins the same way the metaclass decorators do, without requiring any changes to the PythonPluginLoader itself
            String[] pre_plugin_scripts = {"imports.py", "meta_decorators.py"};
            String[] post_plugin_scripts = {"meta_loader.py"};
           
            // Run scripts designed to be run before plugin creation
            for (String script : pre_plugin_scripts) {
              InputStream metastream = this.getClass().getClassLoader().getResourceAsStream("scripts/"+script);
              interp.execfile(metastream);
              metastream.close();
            }

            interp.execfile(instream);

            instream.close();

            try {
                if (!hasyml) {
                    Object name = interp.get("__plugin_name__");
                    Object version = interp.get("__plugin_version__");
                    Object website = interp.get("__plugin_website__");
                    Object main = interp.get("__plugin_mainclass__");
                    hassolidmeta = name != null && version != null;
                    if (name != null)
                        ReflectionHelper.setPrivateValue(description, "name", name.toString());
                    if (version != null)
                        ReflectionHelper.setPrivateValue(description, "version", version.toString());
                    if (website != null)
                        ReflectionHelper.setPrivateValue(description, "website", website.toString());
                    if (main != null)
                        ReflectionHelper.setPrivateValue(description, "main", main.toString());
                }
            } catch (Throwable t) {
                Logger.getLogger("Minecraft").log(Level.SEVERE, "Error while setting python-set description values", t);
            }

            String mainclass = description.getMain();
            PyObject pyClass = interp.get(mainclass);
            if (pyClass == null)
                pyClass = interp.get("Plugin");
            if (pyClass == null)
                result = new PythonPlugin();
            else
                result = (PythonPlugin) pyClass.__call__().__tojava__(PythonPlugin.class);
           
            interp.set("pyplugin", result);
           
            result.hooks = hook;
            result.interp = interp;
           
            // Run scripts designed to be run after plugin creation
            for (String script : post_plugin_scripts) {
              InputStream metastream = this.getClass().getClassLoader().getResourceAsStream("scripts/"+script);
              interp.execfile(metastream);
              metastream.close();
            }
           
            result.initialize(this, server, description, dataFolder, file);
            result.setDataFile(data);
           
        } catch (Throwable t) {
            if (data.shouldAddPathEntry() && pythonpath.__contains__(filepath)) {
                pythonpath.remove(filepath);
            }
            throw new InvalidPluginException(t);
        }

        if (data.getNeedsSolidMeta() && !hassolidmeta) {
            throw new InvalidPluginException(new Exception("Released plugins require either a plugin.yml or both __plugin_name__ and __plugin_version__ set in the main python file!"));
        }

        if (!loadedplugins.contains(description.getName()))
            loadedplugins.add(description.getName());
        return result;
View Full Code Here

        PluginDataFile data = null;

        if (file.getName().endsWith(".py")) {
            if (file.isDirectory())
                //cause we can't throw InvalidPluginExceptions from here we throw an InvalidDescriptionExecption with the InvalidPlugin as cause
                throw new InvalidDescriptionException(new InvalidPluginException(new Exception("python files cannot be directories! try .py.dir instead.")));
            data = new PluginPythonFile(file);
        } else if (file.getName().endsWith("dir")) {
            if (!file.isDirectory())
                throw new InvalidDescriptionException(new InvalidPluginException(new Exception("python directories cannot be normal files! try .py or .py.zip instead.")));
            data = new PluginPythonDirectory(file);
        } else if (file.getName().endsWith("zip") || file.getName().endsWith("pyp")) {
            if (file.isDirectory())
                throw new InvalidDescriptionException(new InvalidPluginException(new Exception("python zips cannot be directories! try .py.dir instead.")));
            try {
                data = new PluginPythonZip(file);
            } catch (InvalidPluginException ex) {
                throw new InvalidDescriptionException(ex);
            }
        } else {
            throw new InvalidDescriptionException(new InvalidPluginException(new Exception("filename '"+file.getName()+"' does not end in py, dir, zip, or pyp! did you add a regex without altering loadPlugin()?")));
        }

        try {
            stream = data.getStream("plugin.yml");

            if(stream == null) {
                //TODO Does this cause serious problems with plugins which have no plugin.yml file?
                throw new InvalidDescriptionException(new InvalidPluginException(new FileNotFoundException("Jar does not contain plugin.yml")));
            }

            return new PluginDescriptionFile(stream);
        } catch (IOException e) {
            e.printStackTrace();
View Full Code Here

     */
    public PluginPythonZip(File file) throws InvalidPluginException {
        try {
            zip = new ZipFile(file);
        } catch (IOException e) {
            throw new InvalidPluginException(e);
        }
    }
View Full Code Here

TOP

Related Classes of org.bukkit.plugin.InvalidPluginException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.