Save per player configs

Discussion in 'Plugin Development' started by iAmGuus, Jan 31, 2015.

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

    iAmGuus

    Hi all,

    So this is my second post for the day (I'll stop soon! :D) but I am just making a big plugin and would need some help for that (you guys <3). So I made a function that generates per player yml files, it generates fine and all, but when I try to add something in it and save the file, it is not working.

    Code:
    Code:
        public static FileConfiguration getPlayerConfig(Player p) {
            FileConfiguration yml = null;
            File file = getPlayerFile(p);
            yml = YamlConfiguration.loadConfiguration(file);
           
            return yml;
        }
       
        public static File getPlayerFile(Player p) {
            return new File(Main.getPlugin().getDataFolder(), "users" + File.separator + p.getUniqueId() + ".yml");
        }
       
        public static void savePlayerConfig(Player p) {
            try {
                getPlayerConfig(p).save(getPlayerFile(p));
                System.out.println("done");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    Hopefully you guys are seeing a problem

    Regards,
    Guus Huizen - iAmGuus
     
  2. Offline

    Monkey_Swag

    I can see loads of problems here
     
  3. Offline

    iAmGuus

    @Monkey_Swag, how else would I get the methods from other classes?
     
  4. Offline

    mibac138

    Make it multi-instance - add constructor and in it pass player. (and remove static from functions)
     
  5. Offline

    iAmGuus

  6. Offline

    ColonelHedgehog

  7. Offline

    Skionz

    Basic Java knowledge is a prerequisite to an API written in Java. Static isn't a magic solution to accessing methods across classes, public is.
     
  8. Offline

    nverdier

    I would say that it is a magic solution to access method from other classes. A dark magic solution. That causes memory leaks and hurts servers.
     
  9. Offline

    ColonelHedgehog

    No magic is inherently good or bad. It's how you use it.
     
  10. Offline

    nverdier

    http://prntscr.com/5zldla
    Google says otherwise.
     
  11. Offline

    ColonelHedgehog

  12. Offline

    Skionz

    Unlike other websites, wikipedia is community oriented and not bias which makes it a great source :p
     
    ZP18 and nverdier like this.
  13. Offline

    TheCoderGuy

    @Skionz Thank you! Someone that finally agrees with me!

    And to answer the OP's post, why don't you just load the file? For example:
    Code:
    FileConfiguration <file config name, or just whatever you want> = YamlConfiguration.loadConfiguration(<file name, the thing the you made the file with>)
    
    <file config name, the one you used up there>.set(...);
    I know its a little messy, but you should just be able to figure it out.

    EDIT: Oh also, make sure you save the file every time you use it. You might have to surround it with a try catch.
     
  14. Offline

    ColonelHedgehog

    I was quoting every modern teacher ever. :p
     
    ZP18, nverdier and Skionz like this.
  15. Offline

    nverdier

    So true :D
     
  16. Offline

    ZP18

    Totally agree with both of these, wikipedia is oftenly reviewed and fixed, so there is barely any errors
     
Thread Status:
Not open for further replies.

Share This Page