Solved Extract a .yml from the jar in the plugin folder?

Discussion in 'Plugin Development' started by Eistee², Oct 18, 2012.

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

    Eistee²

    Hello ,
    I want that my plugin extract a .yml which is include in the Jar into my Plugins folder but I dont know how :/
    Would be nice if someone help me :)
     
  2. If I recall JavaPlugin has a method for copying files out to the plugin/plugin name folder
     
    Eistee² likes this.
  3. Offline

    p000ison

    Do you mean the default config.yml?
     
  4. Offline

    Eistee²

    Thanks i will look for this but may a bit more help would be nice :D


    EDIT:
    I want to have Config and some other Files which only get copy in the plugin Folder
     
  5. Offline

    p000ison

    If you want to collect defaults from inside of you jar you can do this:

    Code:
        customConfigFile = new File(getDataFolder(), "custom.yml");
        customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
        InputStream defaultStream = getResource("custom.yml");
        YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defaultStream);
        customConfig.setDefaults(defConfig);
     
    LunaTiX_ and Eistee² like this.
  6. Offline

    Eistee²

    Thanks but I dosent want to collect them , only Copy :)

    Bump :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  7. Offline

    Sagacious_Zed Bukkit Docs

    The wiki
     
    Eistee² likes this.
  8. Offline

    p000ison

    then use the method saveResource(String location) in the javaplugin class
     
    Eistee² likes this.
  9. Offline

    Eistee²

    Thanks ! :)
     
  10. Offline

    maxp0wer789

    Just as general info, you can also do it manually:

    Code:
    File yourFile = new File(yourPluginsDirectoryPath + "yourFile.extension");
    if(yourFile.exists())
    return;
    try {  
                FileOutputStream out = new FileOutputStream(yourFile);
               
                InputStream in = YourPluginClass.class.getResourceAsStream("yourFile.extension");
               
                int read = -1;
               
                while((read = in.read()) != -1)
                    out.write(read);
               
                out.flush();
                out.close();
                in.close();
               
            } catch (Exception e) {
                e.printStackTrace();
            }
    The file you want to use as ressource has to be in the same package as "YourPluginClass", otherwise use the relativ package path to the ressource. Of course when using the bukkit api it's easier to just use their method as stated by p000ison
     
    kabbage and Eistee² like this.
  11. Offline

    Devil0s

  12. Offline

    p000ison

    As far as i can remember, this will break after a reload. To fix this, you need to get a URL object and turn caching off. I can post an example tomorrow
     
  13. Offline

    maxp0wer789

    Never tested it thoroughly within a plugin, only within other java projects. Also the code doesn't get executed on reload cause the file usually already exists at that time. But if you want to test it and it's unsave, let me know.
     
  14. Offline

    p000ison

    The copy method:

    Code:
    public static void copy(InputStream input, File target) throws IOException
        {
            if (target.exists()) {
                throw new IOException("File already exists!");
            }
     
            File parentDir = target.getParentFile();
     
            if (!parentDir.mkdirs()) {
                throw new IOException("Failed at creating directories!");
            }
     
            if (!parentDir.isDirectory()) {
                throw new IOException("The parent of this file is no directory!?");
            }
     
            if (!target.createNewFile()) {
                throw new IOException("Failed at creating new empty file!");
            }
     
            byte[] buffer = new byte[1024];
     
            OutputStream output = new FileOutputStream(target);
     
            int realLength;
     
            while ((realLength = input.read(buffer)) > 0) {
                output.write(buffer, 0, realLength);
            }
     
            output.flush();
            output.close();
        }
    To get the input stream:

    Code:
    public static InputStream getInputFromJar(String path) throws IOException
    {
    if (filename == null) {
    throw new IllegalArgumentException("The path can not be null");
    }
     
    URL url = YOURCLASS.class.getResource(filename);
     
    if (url == null) {
    return null;
    }
     
    URLConnection connection = url.openConnection();
    connection.setUseCaches(false);
    return connection.getInputStream();
    }
     
Thread Status:
Not open for further replies.

Share This Page