Solved YML Not saving correctly

Discussion in 'Spigot Plugin Development' started by RayNightStudios, Jul 6, 2019.

Thread Status:
Not open for further replies.
  1. I have a problem with saving and loading my playerconfigs.
    In the console it says it saved the variable "coins" but it doesnt.
    It saves all variables except "coins". Variable "coins" is type long.

    My Code
    Code (Java):
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.UUID;

    public class PlayerConfigManager {

        HashMap<UUID, HashMap<String, Object>> values;

        public PlayerConfigManager() {

            values = new HashMap<>();

        }

        public HashMap<String, Object> getValues(Player player) {
            createIfNotExist(player.getUniqueId());
            return values.get(player.getUniqueId());
        }

        public HashMap<String, Object> getValues(UUID uniqueId) {
            createIfNotExist(uniqueId);
            return values.get(uniqueId);
        }

        public HashMap<UUID, HashMap<String, Object>> getValues() {
            return values;
        }

        public void createConfig(UUID uuid) {

            values.put(uuid, new HashMap<>());

        }

        public void createIfNotExist(UUID uuid) {

            if (!values.containsKey(uuid)) {

                createConfig(uuid);

            }

        }

        public PlayerConfigManager load() {

            for (File file : new File("plugins/RayNight/Playerdata/").listFiles()) {

                if (file.isFile()) {

                    FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                    UUID uuid = UUID.fromString(file.getName());
                    values.put(uuid, new HashMap<>());

                    cfg.getConfigurationSection("").getKeys(true).forEach(e -> {

                        values.get(uuid).put(e, cfg.get(e));
                        System.out.println(
                                "Loading " + e + " " +
                                        "| Value=" + cfg.get(e).toString() + " for " +
                                        uuid.toString());

                    });

                }

            }

            return this;

        }

        public void saveAll() {

            for (UUID uuid : values.keySet()) {

                File file = new File("plugins/RayNight/Playerdata/" + uuid.toString());
                YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                for (String e : values.get(uuid).keySet()) {
                    System.out.println("Saving " + e + " | Value=" + values.get(uuid).get(e).toString() + " for " + uuid.toString());
                    cfg.set(e, values.get(uuid).get(e));

                }

                try {
                    cfg.save(file);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }

        }
    Config
    Code (YAML):
    enderchest:
      pages
    : 1
      page
    :
        '0'
    : []
    stats
    :
      deaths
    : 0
      money
    : 1000
      kills
    : 0
    elo
    : 0
    homes
    : []
    perk
    :
      no_hunger
    : false
      strength
    : false
      speed
    : false
      fire_resistenz
    : false
      jump_boost
    : true
     
     
  2. It is because you're iterating it as a string
     
  3. "coins" is no where in the code you provided. Maybe CachCraft is right, but we have no Idea where "coins" is or how you save it...
    You should be initializing your variables though:
    HashMap<UUID, HashMap<String, Object>> values = new HashMap<UUID, HashMap<String, Object>>();
     
  4. I do initialize it in my Main and "coins" is set right.
    Code (Text):
    RayNight.playerConfigManager = new PlayerConfigManager().load()
    Im also getting it as Long in my PlayerConfig.

    Its just not saving the long :(
     
  5. Coins is set:
    Code (Text):
    new PlayerConfig(player).setLong("stats.money", money);
    // PlayerConfig just gets the values from the hashmap for the uuid
     
  6. It resets completely now
     
Thread Status:
Not open for further replies.