Solved Get A Plugin's File

Discussion in 'Plugin Development' started by meguy26, Jul 5, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    meguy26

    So, basically what i want to do is get a file object for the Plugin's JarFile. I do not know if this is possible, but ive done some googling and cant find anything. This must be possible, but im not sure how to go about it.
     
  2. @meguy26 I'm not sure if this is exactly what you meant, but you could always get it by doing:
    Code:
    File pluginJar = new File("plugins" + File.seperator + "pluginName.jar");
     
  3. Offline

    meguy26

    @CodePlaysMinecraft
    no, you cant, what if the jar it is renamed? what if the author has a custom name? That wont work... unfortunately.
     
  4. @meguy26 If the jar is renamed, there isn't any way you could get the actual file object. Why would someone want to rename this, anyway?!
     
  5. Offline

    meguy26

    @CodePlaysMinecraft
    Dunno... but maybe i was not clear enough, I need to get another plugins file, not my own... so i dont know the name. Bukkit must store them somewhere.

    @CodePlaysMinecraft
    Probably could have been easier, but i used the following code to populate a map of plugin name to plugin file path:
    Code:
    /**
    *
    */
    package com.gmail.neonblue858.mipa.main;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Map;
    import java.util.jar.JarFile;
    import java.util.regex.Pattern;
    import java.util.zip.ZipEntry;
    
    import com.gmail.neonblue858.mipa.pluginutil.bukkitclasses.MipaRunnable;
    
    /**
    *
    *
    * @author Meguy26
    *
    */
    class PluginMapPopulatorTask extends MipaRunnable<MipaYrarbil> {
        private Map<String, String> pluginFiles;
    
        /**
         * @param plug
         *
         * @author Meguy26
         */
        public PluginMapPopulatorTask(MipaYrarbil plug, Map<String, String> pluginFiles) {
            super(plug);
            this.pluginFiles = pluginFiles;
        }
    
        /*
         * (non-Javadoc)
         *
         * @see
         * com.gmail.neonblue858.mipa.pluginutil.bukkitclasses.MipaRunnable#run()
         */
        @Override
        public void run() {
            this.populatePluginMap();
        }
    
        private void populatePluginMap() {
            Map<String, String> m = this.pluginFiles;
            File pd = new File("plugins");
            for (File fi : pd.listFiles()) {
                if (fi.getName().endsWith(".jar")) {
                    try {
                        JarFile f = new JarFile(fi);
                        ZipEntry ent = f.getEntry("plugin.yml");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(f.getInputStream(ent)));
                        String line;
                        boolean done = false;
                        while ((line = reader.readLine()) != null) {
                            if (line.startsWith("name: ")) {
                                String name = line.replaceFirst(Pattern.quote("name: "), "");
                                String loc = fi.getAbsolutePath();
                                m.put(name, loc);
                                done = true;
                                break;
                            }
                        }
                        if (!done) {
                            this.getPluginLogger().severe("The jarfile \"" + fi.getAbsolutePath()
                                    + "\" could not be read into map as a plugin. Ignoring.");
                        }
                        f.close();
                    } catch (IOException e) {
                        this.getPluginLogger().severe("The jarfile \"" + fi.getAbsolutePath()
                                + "\" could not be read into map as a plugin. Ignoring.");
                    }
                }
            }
    
        }
    }
    
    Yes anyone else can use this. I will also be including this functionality in my plugin/library "MipaYrarbil"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page