Config files per player

Discussion in 'Plugin Development' started by janderup, Feb 25, 2017.

Thread Status:
Not open for further replies.
  1. I am now making in my plugin files per player. I can create the folder and the file but I don't know how I can modify the file. Any Ideas?
    Code:
    public class Config {
       
        private static ConsoleCommandSender cmd = Bukkit.getServer().getConsoleSender();
        private static File playerdatafolder;
        private static File playerdatafile;
        public static YamlConfiguration playerdata;
       
        public static void loadFiles() {
            playerdatafolder = new File(Main.plugin.getDataFolder() + File.separator + "PlayerData");
           
            if (!Main.plugin.getDataFolder().exists()) {
                Main.plugin.getDataFolder().mkdirs();
            }
           
            if (!playerdatafolder.exists()) {
                playerdatafolder.mkdirs();
            }
        }
       
        public static void createPlayerFile(Player p) {
            playerdatafile = new File(playerdatafolder, p.getUniqueId() + ".yml");
            playerdata = YamlConfiguration.loadConfiguration(playerdatafile);
           
            try {
                playerdatafile.createNewFile();
            } catch (Exception e) {
                cmd.sendMessage("§cCould not create " + p.getUniqueId() + ".yml");
                return;
            }
        }
       
        public static YamlConfiguration getFile(Player p) {
            // What do I need to return here?
            return playerdata;
        }
    }
     
  2. Offline

    Zombie_Striker

    @janderup
    What do you mean modify the file? As long as you have the "playerdata" instance, you can modifiy the actual values. As for modifying the file, the only way you can do that is if you save the file, which just requires the "YamlConfig.save" method.
     
  3. But for example, I've got 3 people on my server: player1, player2, player3. I want to edit the file of player2. How can Java know that I want to edit that file?
     
  4. Offline

    Zombie_Striker

    @janderup
    The phrasing of this question gives me the impression you do not fully understand Java. If you want to edit the file for player 2, you need to tell Java when to trigger it (if you want it to trigger every ___ amount of time, use Delayed Tasks. If you want to edit the file ever y time an event happens, use an event) and what to change.
     
Thread Status:
Not open for further replies.

Share This Page