Multiple configs not working?

Discussion in 'Plugin Development' started by thepaperboy99, Mar 14, 2014.

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

    thepaperboy99

    Hello bukkit, yesterday I was trying to code a plugin that would allow me to have a config file for every player (like essentials). But I can't figure out why this code won't work:

    public FileConfiguration getProfile(String player){

    File f = new File(getDataFolder() + "/users/" + player + ".yml");
    return YamlConfiguartion.loadConfiguartion(f);
    }

    public void saveProfile(String player){
    File f = new File(getDataFolder() + "/users/" + player + ".yml");
    YamlConfiguartion c = YamlConfiguartion.loadConfiguration(f);

    try{
    c.save(f);

    catch(Exception e){

    }
    }

    The code does not give errors, it just doesn't work. Any help is appreciated, thanks!

    (Sorry if I misspelled anything, this was all typed on an iPad :p)
     
  2. Offline

    codermason

  3. Offline

    rfsantos1996

    This is the code I use to use multiple configuration files.

    Code:java
    1. package com.jabyftw.sgames.util;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.logging.Level;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.plugin.Plugin;
    10.  
    11. public class CustomConfig {
    12.  
    13. private final Plugin pl;
    14. private final String name;
    15. private final File file;
    16. private FileConfiguration fileConfig;
    17.  
    18. public CustomConfig(Plugin pl, String name) {
    19. this.pl = pl;
    20. this.name = name;
    21. file = new File(pl.getDataFolder(), name + ".yml");
    22. }
    23.  
    24. public FileConfiguration getConfig() {
    25. if (fileConfig == null) {
    26. reloadConfig();
    27. }
    28. return fileConfig;
    29. }
    30.  
    31. private void reloadConfig() {
    32. fileConfig = YamlConfiguration.loadConfiguration(file);
    33. InputStream defConfigStream = pl.getResource(name + ".yml");
    34. if (defConfigStream != null) {
    35. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    36. fileConfig.setDefaults(defConfig);
    37. }
    38. }
    39.  
    40. public void saveConfig() {
    41. try {
    42. getConfig().options().copyDefaults(true);
    43. getConfig().save(file);
    44. reloadConfig();
    45. } catch (IOException ex) {
    46. pl.getLogger().log(Level.WARNING, "Couldn't save {0}.yml", name);
    47. }
    48. }
    49. }


    To create files, just:
    Code:java
    1. CustomConfig configYML = new CustomConfig(pl, "config");
    2. FileConfiguration config = configYML.getConfig();
    3. config.addDefault(path, value);
    4. configYML.saveConfig();
    5.  
    6. // To get values:
    7. FileConfiguration config = configYML.getConfig();
    8. String string = config.getString(path);
    9. // as a normal/default getConfig()'s config file.
     
Thread Status:
Not open for further replies.

Share This Page