Config set not working

Discussion in 'Plugin Development' started by Gecco1234, Apr 19, 2014.

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

    Gecco1234

    I am creating a config per player and it does that but is not setting the "Emeralds" equal to 1.
    Here is my code:
    Code:java
    1. @EventHandler
    2. public void JoinEvent(PlayerJoinEvent event){
    3. Player p = event.getPlayer();
    4. File userDir = new File(this.getDataFolder() + File.separator + "Users");
    5. if(!userDir.exists()){
    6. userDir.mkdir();
    7. }
    8. File playerFile = new File(this.getDataFolder() + File.separator + "Users" + File.separator + p.getName() + ".yml");
    9. FileConfiguration fc = YamlConfiguration.loadConfiguration(playerFile);
    10. fc.set("Emeralds", 1);
    11. saveConfig();
    12. if(!playerFile.exists()){
    13. try {
    14. playerFile.createNewFile();
    15. } catch (IOException e) {
    16. e.printStackTrace();
    17. }
    18. }
    19. }


    There are no errors in the console



    EDIT: NEW PROBLEM
     
  2. Offline

    1Rogue

    You're calling "saveConfig()" which will save your "config.yml", not your player file:

    Code:java
    1. try {
    2. fc.save(playerFile);
    3. } catch (IOException ex) {
    4. //couldn't save
    5. }


    Also, why are you checking if the file exists before loading it as a FileConfiguration? That should likely come before that.
     
  3. Offline

    Gecco1234

    Thanks, it works now :)

    I now have another problem:
    When I try to grab "Emeralds" it's a 0
    This is what Im using to grab it:
    Code:java
    1. File playerFile = new File(this.getDataFolder() + File.separator + "Users" + File.separator + player.getName() + ".yml");
    2. FileConfiguration fc = YamlConfiguration.loadConfiguration(playerFile);
    3. int intbalance = fc.getInt(player.getName()+"Emeralds");
    4. String balance = String.valueOf(intbalance);


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

    clmcd42

    The problem is:
    Code:java
    1. fc.set("Emeralds", 1);

    Code:java
    1. fc.getInt(player.getName()+"Emeralds");


    What you should have to get the int:
    Code:java
    1. fc.getInt("Emeralds");
     
Thread Status:
Not open for further replies.

Share This Page