Solved Yaml Configuration save not working

Discussion in 'Plugin Development' started by Rexcantor64, Jan 30, 2016.

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

    Rexcantor64

    I don't know why, but the file doesn't save. I debugged it, and the variables are correct, but it always save false!

    Please help me, my players can by the kits!
    (It worked a long time ago, but it stopped working and I did nothing.)

    Code:
    public void savePlayer(@Nonnull GamePlayer player) {
            try {
                File dataDirectory = Main.get().getDataFolder();
                File playerDataDirectory = new File(dataDirectory, "players");
    
                if ((!playerDataDirectory.exists())
                        && (!playerDataDirectory.mkdirs())) {
                    System.out.println("Failed to save player " + player
                            + ": Could not create player_data directory.");
                    return;
                }
    
                File playerFile = new File(playerDataDirectory, player.getUUID()
                        .toString() + ".yml");
                if ((!playerFile.exists()) && (!playerFile.createNewFile())) {
                    System.out.println("Failed to save player " + player
                            + ": Could not create player file.");
                    return;
                }
    
                YamlConfiguration fileConfiguration = YamlConfiguration
                        .loadConfiguration(playerFile);
                fileConfiguration.set("skywars.name", player.getName());
                fileConfiguration.set("skywars.wins",
                        Integer.valueOf(player.getGamesWon()));
                fileConfiguration.set("skywars.played",
                        Integer.valueOf(player.getGamesPlayed()));
                fileConfiguration.set("skywars.deaths",
                        Integer.valueOf(player.getDeaths()));
                fileConfiguration.set("skywars.kills",
                        Integer.valueOf(player.getKills()));
                boolean[] kits = player.getKits();
                for (int i = 0; i < kits.length; i++) {
                    System.out.println(i);
                    System.out.println(kits[i]); //It prints true
                    System.out.println("------");
                    fileConfiguration.set("skywars.kits." + (i + 1), kits[i]); //It always save false
                }
                fileConfiguration.save(playerFile); // I think the error is here...
            } catch (IOException ioException) {
                System.out.println("Failed to save player " + player.getName()
                        + ": " + ioException.getMessage());
            }
        }
     
  2. Offline

    MOMOTHEREAL

    @Rexcantor64
    Can you please give us any stacktraces you get when executing the code, as well as the line(s) they could be pointing too?
     
  3. Offline

    Rexcantor64

    There are no errors on console.

    Debug (open)

    NOTE: the log is inverted, the first message appearing was "0"

    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 9
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 8
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 7
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 6
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 5
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO true
    31.01 06:37:19 [Server] INFO 4
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 3
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 2
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 1
    31.01 06:37:19 [Server] INFO ------
    31.01 06:37:19 [Server] INFO false
    31.01 06:37:19 [Server] INFO 0


    Edit:
    Hum... I'm a bit confused. On my test server it doesn't happen. I think is is from my host. I will try to speak with they, but it's not so easy (because my host is the worst in the world). I will get you updated here!
     
    Last edited: Jan 31, 2016
  4. Offline

    MOMOTHEREAL

    @Rexcantor64

    Could you please ensure player.getKits() returns an array containing at least a positive value? Please debug the array at first so that we can make sure this is related to the given code.
     
  5. Offline

    Rexcantor64

    Did you saw the spoiler? The 4 value is true!
     
  6. Offline

    MOMOTHEREAL

    @Rexcantor64

    Okay, just tested it myself, but it does in fact work correctly on my end. It is most certainly not related to the given code, but rather related to something wrong with your file management. Try deleting the file and re-running the code?
     
  7. Offline

    Rexcantor64

    I'm feeling VERY VERY stupid now. This error stayed on my head for a long time, but yesterday something makes me think: "If it's another plugin deleting and re-saving the variable?".
    And, in fac, it was.
    I made a "Example Project" and when I make another minigame, I just only edit it to fit my ideas. But I didn't saw when I was making 1v1, I forgot to delete the lines I posted above. So, every time it saves the variable, 1v1 plugin re-saves it to "false".

    It's hard to understand, but it was a VERY VERY stupid error.

    Thanks for all you tried to do,
    Rexcantor64
     
Thread Status:
Not open for further replies.

Share This Page