Solved Folder inside of the plugins folder

Discussion in 'Plugin Development' started by FreakyPear5, Apr 2, 2020.

Thread Status:
Not open for further replies.
  1. Hi!
    I have just started to use multiple config files. I want to have a folder that is full of all the data files for players. I have managed to create the folder and create new files (they are named the player's UUID). The files are there, but how could I access a certain one?

    Thanks,
    Lawrence
     
  2. Online

    timtower Administrator Administrator Moderator

  3. I have a ConfigManager class, in which I have something that I call in a JoinEvent.
    Code:
    public void newPlayerDataFile(String uuid) {
            playerData = new File(plugin.getDataFolder()+"\\playerdata\\", uuid+".yml");
            if (!playerData.exists()) {
                try {
                    playerData.createNewFile();
                    System.out.println(uuid+" file was created in playerdata");
                }
                catch (IOException e) {
                    System.out.println(uuid+" file could not be created");
                }
            }
            else {
                System.out.println("player already has a file, skipping");
            }
        }
    
     
  4. Offline

    KarimAKL

    @FreakyPear5 Get the file, load the configuration, do what you must.
    Code:Java
    1. File file = new File(plugin.getDataFolder().getPath() + File.separator + "playerdata", player.getUniqueId().toString() + ".yml");
    2. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
     
  5. Ok, thanks! I'll give that a go.
    Should this work?
    Code:
    public void getPlayerFile(String uuid) {
            playerData = new File(plugin.getDataFolder()+"\\playerdata\\", uuid+".yml");
            if (!playerData.exists()) {
                System.out.println(uuid+" file does not exist, please create");
            }
            else {
                playerCFG = YamlConfiguration.loadConfiguration(playerData);
            }
        }
    
     
  6. Offline

    KarimAKL

    @FreakyPear5 Yeah, although i'd probably change a few things:
    1. I'd remove the "else", since you're already creating the file before you load the configuration.
    2. I'd rename the method, "getPlayerFile" is pretty misleading, i'd probably call it something more like "initFileConfig".
    3. You use "\\" but that's Windows only, you should use "File.separator", that returns either "/" or "\" depending on the OS used.
     
  7. Ok, thank you

    I've just done what you said. Now, how could I call a player's file?

    Doing CFGm.getPlayerFile(uuid) doesn't do anything; I can't put getString() after it.

    (CFGm is what I have the configmanager under)

    @KarimAKL can you help?
     
    Last edited: Apr 3, 2020
  8. Offline

    KarimAKL

    @FreakyPear5 That's because it's a void method, change the return type to FileConfiguration.
     
  9. I tried that but it doesn’t edit the file
     
  10. Offline

    KarimAKL

  11. Code:
    public FileConfiguration getPlayerFile(String uuid) {
    playerData = new File(plugin.getDataFolder()+"\\playerdata\\", uuid+".yml");
    playerCFG = YamlConfiguration.loadConfiguration(playerData);
    return playerCFG;
    }
    }
    
     
  12. Offline

    KarimAKL

  13. I realized one of my problems and I switched things around, which kind of works...

    Code:
    public FileConfiguration getPlayerFile(String uuid) {
            playerData = new File(plugin.getDataFolder()+File.separator+"playerdata"+File.separator+uuid+".yml");
            playerCFG = YamlConfiguration.loadConfiguration(playerData);
            return playerCFG;
        }
    
    Code:
    public void setPlayerDefaults(String uuid) {
            playerData = new File(plugin.getDataFolder()+File.separator+"playerdata"+File.separator+uuid+".yml");
                playerCFG = YamlConfiguration.loadConfiguration(playerData);
                playerCFG.addDefault("name", "unset");
                playerCFG.addDefault("uuid", "unset");
                playerCFG.addDefault("nick", "unset");
                playerCFG.addDefault("clan", "unset");
                playerCFG.addDefault("homes", "unset");
                playerCFG.addDefault("settings", "unset");
                playerCFG.addDefault("settings.language", "unset");
                playerCFG.addDefault("settings", "unset");
                playerCFG.addDefault("settings.jointitle", "true");
                playerCFG.options().copyDefaults();
                this.savePlayerFile(uuid);
        }
    
    Calling it here:
    Code:
    String uuid = event.getPlayer().getUniqueId().toString();
            CFGm.newPlayerDataFile(uuid);
            CFGm.setPlayerDefaults(uuid);
            CFGm.getPlayerFile(uuid).set("name", event.getPlayer().getName());
            CFGm.getPlayerFile(uuid).set("uuid", uuid);
            CFGm.savePlayerFile(uuid);
            CFGm.reloadPlayerFile(uuid);
    
    After testing, for some reason it sets the UUID string in the config, but not the name and there aren't any of the defaults in it.
     
  14. Offline

    KarimAKL

    @FreakyPear5
    1. You can use the getPlayerFile(String) method you created in the setPlayerDefaults(String) method.
    2. I'm not sure how defaults work as i've never used them but, you could just use ConfigurationSection#set(String, Object) instead.
    3. Declare a variable and initialize it with CFGm.getPlayerFile(uuid) instead of calling it multiple times, that way you won't have to get the file, load the configuration, and return the value multiple times.
    4. I think 3. is why you're having trouble, you're getting the config, setting the name, then you reset the config by reassigning it and setting the UUID, and then you finally save.
     
  15. Great, it works! Thanks!
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page