Solved Loading a custom config file from plugin and saving it

Discussion in 'Plugin Development' started by Crack498, Jul 23, 2015.

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

    Crack498

    I know, I know, there are a huge amount of threads like this, but trust me, I've been looking through all of them carefully and I always end up getting a blank "lang.yml" file.
    My actual code:
    Code:
    public class Main extends JavaPlugin implements Listener{
      
        File langFile = new File(this.getDataFolder() + "lang.yml"); //Or "/lang.yml", tried both
        FileConfiguration lang = YamlConfiguration.loadConfiguration(langFile);
    [...]
    @Override
        public void onEnable() {
            try {
                lang.save(langFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
    Any thoughts on why it doesn't work? I have a lang.yml file next to my plugin.yml one.
     
    Ganga likes this.
  2. I don't see any code that copies the contents of your file to the config
     
  3. Offline

    DoggyCode™

    EDIT: I don't think what I wrote works anyways.

    removed...
     
    Ganga likes this.
  4. Offline

    Crack498

    What about this: (still doesn't work)
    Code:
    public class Main extends JavaPlugin implements Listener{
      
        File langFile = new File(this.getDataFolder() + "/lang.yml");
        FileConfiguration lang = YamlConfiguration.loadConfiguration(langFile);
      
        private static ItemStack wrench = null;
        private static int msl = 0;
        HashMap<UUID, Location[]> pos = new HashMap<UUID, Location[]>();
        HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>> undo = new HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>>();
      
        //Thing
        private void copy(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while((len=in.read(buf))>0){
                    out.write(buf,0,len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //Not a thing
      
        @Override
        public void onEnable() {
            if(!langFile.exists()){
                langFile.getParentFile().mkdirs();
                copy(getResource("lang.yml"), langFile);
            }
            try {
                lang.save(langFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
     
  5. Offline

    Ganga

    @DoggyCode™ Does this also work for , example:

    /load town
    and now it loads a config called town, in which you can do what you want.
    the town is a string in your config path
     
  6. Offline

    DoggyCode™

    @Ganga I don't think I know exactly what you mean by that. But yea, I guess.
     
  7. Offline

    Crack498

    Apparently saveResource("lang.yml", true); does the work.
    Thanks for the help, still!
     
    Ganga likes this.
  8. Offline

    Ganga

    @Crack498
    could you send me the working code? I struggle haha
     
Thread Status:
Not open for further replies.

Share This Page