[Not Solved] Creating Custom configs for players.

Discussion in 'Plugin Development' started by germanchocolate, Sep 7, 2012.

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

    germanchocolate

    I need help when a player joins i want it to create a custom config for them saying
    USERNAME.yml and then add defaults to it ect...
    ive been trying to figure this out but it just doesnt work out so well
    im using PlayerLoginEvent
     
  2. Offline

    kyle1320

    germanchocolate
    This seems to work:
    Code:
    public YamlConfiguration getConfig(String name) {
        File folder = new File("plugins/PlayerNames");
        File file = new File("plugins/PlayerNames/" + name +".yml");
        if(!folder.exists()) {
            folder.mkdir();
            if(!file.exists()) {
                try {
                    file.createNewFile();
                    YamlConfiguration.loadConfiguration(file).save(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return YamlConfiguration.loadConfiguration(file);
    }
    Pass it the player name
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    Have you read the article in the wiki?

    You should not hard code the plugins folder. Plugins need not reside in the folder named plugins.

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

    germanchocolate

    it works thanks but how can i add defaults to the config?
     
  5. Offline

    kyle1320

    Ah you're right, in my own defense I'm pretty new to dealing with files :p
    Code:
    public YamlConfiguration getConfig(String name) {
            File folder = new File(getDataFolder() + ("/Names/"));
            File file = new File(getDataFolder() + "/Names/" + name +".yml");
            if(!folder.exists()) {
                folder.mkdir();
                if(!file.exists()) {
                    try {
                        file.createNewFile();
                        YamlConfiguration.loadConfiguration(file).save(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return YamlConfiguration.loadConfiguration(file);
        }
    Hopefully better?
     
  6. Offline

    germanchocolate

    ya i added a folder inside my plugin data but ya do you know how to add defaults to this?
     
  7. Offline

    kyle1320

    It should return a YamlConfiguration, so you can just do something like:
    Code:
    YamlConfiguration playerConfig = getConfig(player.getName);
    playerConfig.addDefault("new.test", 1);
    or whatever, I don't really know I'm pretty new to using files, but that seems okay to me :p
     
  8. Offline

    germanchocolate

    ya its not working but im going to keep trying different ways on this thanks for the rest of the code though

    Ugh i still cant add defaults and its bugging me out -_- anyone help?

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

    kyle1320

    In the code I posted, change the nested if statements to 2 separate ones, like this:
    Code:
    if(!folder.exists()) {
        folder.mkdir();
    }
     
    if(!file.exists()) {
        try {
            file.createNewFile();
            YamlConfiguration.loadConfiguration(file).save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Before it would only make a new file if the folder didn't exist, and you don't want that if you're adding lots of player files
     
  10. Offline

    germanchocolate

    thanks also can you try and figure out the adding defaults thing for me?

    i still cant figure out how to add defaults to the yml file of the player by default :(

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

    jacklin213

    Code:java
    1. this.getConfig().addDefault(path, value);

    so i would use it like
    Code:java
    1. this.getConfig().addDefault("First.value", "the first value which is a string");
    2.  



    it would show up like this
    Code:
    First:
      Value: the first value which is a string
     
  12. Offline

    germanchocolate

    still doesnt work im using a custom config though because when the player logs in it creates
    a .yml for them and its empty i cant get it to add anything automatically
     
  13. Offline

    jacklin213

    germanchocolate heres a easy way to make it
    Code:java
    1. public void createconfig(){
    2. File configfile = new File(this.getDataFolder + File.seperator + "config.yml");
    3. if (!configFile.exists){
    4. //do stuff
    5. }
     
  14. Offline

    germanchocolate

    i know how to make it :p im saying i need to add defaults its just not working
    here is how i create it:



    Code:
        public YamlConfiguration getConfig(String name) {
            File folder = new File("plugins/Name/PlayerFiles");
            File file = new File("plugins/Name/PlayerFiles/" + name +".yml");
            if(!folder.exists()) {
                folder.mkdir();
            }
                if(!file.exists()) {
                    try {
                        file.createNewFile();
                        YamlConfiguration.loadConfiguration(file).save(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
            return YamlConfiguration.loadConfiguration(file);
        }
    and here is how i get it created/try to add defaults :(


    Code:
        @EventHandler
          public void onPlayerLogin(PlayerLoginEvent event){
            String playerName = event.getPlayer().getName();
            getConfig(playerName);
            getConfig(playerName).set("Points", 50);  //this is where i try to addDefaults to it
            }
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    germanchocolate
    You add default values to a config by calling the addDefault method on the respective FileConfiguration object.
     
  16. Offline

    germanchocolate

    did not work here is what i typed for the adddefault method

    Code:
    getConfig(playerName).addDefault("plugins.name.playerfiles." + playerName + ".yml", 50);
     
  17. Offline

    Sagacious_Zed Bukkit Docs

    How are you measuring the effect?
     
  18. Offline

    jacklin213

    what are you trying to add?
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    There is an even simpler way, this.saveDefaultConfig() where this is an instance of JavaPlugin
     
  20. Offline

    germanchocolate

    sorry didnt read this right how would you get the default values from a config.yml you would create in your plugin and put it into the custom config that gets created?
     
  21. Offline

    Sagacious_Zed Bukkit Docs

    you invoke addDefault on the newly created instance of FileConfiguration and supply the value you get the value from the config.yml instance of FileConfiguration.
     
  22. Offline

    SunShe

    Hi germanchocolate, Do you want a file per player? Like:

    Sunshe.yml
    germanchocolate.yml
    otherplayer.yml

    etc... ? I'm not sure to uderstand how you want storing your data.
     
  23. Offline

    germanchocolate

    yes
     
  24. Offline

    SirTyler

    This should be more then enough to get you going, but why you want yml files per player is beyond me.
    Code:java
    1.  
    2. //Saves all Online Players
    3. public void savePlayers() {
    4. File folder = new File(plugin.getDataFolder(), "players");
    5. if(!folder.exists()) folder.mkdir();
    6. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    7. File f = (new File(folder, player.getName() + ".yml");
    8. if(!f.exists()) f.createNewFile();
    9. FileConfiguration con = new YamlConfiguration(f);
    10. // Add your player values to config here.
    11. con.save(f);
    12. }
    13. }
    14.  
    15. //Saves Single Player
    16. public void savePlayer(String name) {
    17. File folder = new File(plugin.getDataFolder(), "players");
    18. if(!folder.exists()) folder.mkdir();
    19. File f = (new File(folder, name + ".yml");
    20. if(!f.exists()) f.createNewFile();
    21. FileConfiguration con = new YamlConfiguration(f);
    22. //Add you player values to config here.
    23. con.save(f);
    24. }
    25.  
    26. //Saves Single Player with supplied Config
    27. public void savePlayerWithConfig(String name, FileConfiguration con) {
    28. File folder = new File(plugin.getDataFolder(), "players");
    29. if(!folder.exists()) folder.mkdir();
    30. File f = (new File(folder, name + ".yml");
    31. if(!f.exists()) f.createNewFile();
    32. con.save(f);
    33. }
    34.  
    35. //Loads all currently Onine Players and returns a List
    36. public List<FileConfiguration> loadOnlineConfigs() {
    37. List<FileConfiguration> list = new ArrayList<FileConfiguration>();
    38. File folder = new File(plugin.getDataFolder(), "players");
    39. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    40. File f = (new File(folder, player.getName() + ".yml");
    41. if(f.exists()) {
    42. list.add(new YamlConfiguration(f));
    43. } else {
    44. /*
    45.   * Player does not have a File already.
    46.   * Handle this as you like.
    47.   */
    48. }
    49. }
    50. return list;
    51. }
    52.  
    53. //Loads all FileConfiguration from players folder and returns a List
    54. public List<FileConfiguration> loadConfigs() {
    55. List<FileConfigruation> list = new ArrayList<FileConfiguration>();
    56. File folder = new File(plugin.getDataFolder(), "players");
    57. for(File f : folder.listFiles()) {
    58. list.add(new YamlConfiguration(f));
    59. }
    60. }
    61.  
     
  25. Offline

    jacklin213

    cant you just add make a void called NewFile and have the settings in there, then use a PlayerJoinEvent and have it make a file unique to player name??
     
  26. Offline

    krazyswaggaO

    ........ I have the feeling he's just copying and pasting the codes....
     
  27. Offline

    SirTyler

    Yeah, these are just functions to give him and idea on how they all work; so that he can take it and change it to work how he wants.
     
  28. Offline

    jacklin213

    actually tbh im trying to c if what i just said works lol
     
Thread Status:
Not open for further replies.

Share This Page