Solved Config values not saving after reload

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

  1. So I have a playerconfig
    Code (Java):

    import org.bukkit.entity.Player;

    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    public class PlayerConfig {

        public static HashMap< UUID, PlayerConfig > cache = new HashMap<>();
       
        HashMap<String, Object> values;
        UUID uuid;

        public PlayerConfig(Player player) {

            values = Main.playerConfigManager.getValues(player);
            this.uuid = player.getUniqueId();
           
        }

        public PlayerConfig(UUID uuid) {

            values = Main.playerConfigManager.getValues(uuid);
            this.uuid = uuid;
           
        }

        public Object getValueIfNotExist(String key, Object value) {
            if(!containsEntry(key)) set(key, value);
            return values.get(key);
        }
       
        public UUID getUuid() {
            return uuid;
        }
       
        public String getString(String entry) {
           
            if (values.containsKey(entry)) {

                return values.get(entry).toString();

            } else {

                if(entry.equals("clan")) {
                   
                    values.put(entry, "");
                    return "";
                   
                }
               
                throw (new NullPointerException("Konnte " + entry + " keinen Wert zuweisen (Null)"));

            }      
           
        }

        public List getList(String entry) {

            if (values.containsKey(entry)) {

                return (List) values.get(entry);

            } else {

                throw (new NullPointerException("Konnte " + entry + " keinen Wert zuweisen (Null)"));

            }

        }
       
        public void setInt(String entry, int value) {

            values.put(entry, value);

        }

        public void set(String entry, Object value) {
            values.put(entry, value);
        }

        public int getInt(String entry) {

            if (values.containsKey(entry)) {

                return Integer.valueOf(values.get(entry).toString());

            } else {

                throw (new NullPointerException("Konnte " + entry + " keinen Wert zuweisen (Null)"));

            }

        }

        public void setLong(String entry, Long value) {
            values.put(entry, value);
        }

        public boolean containsEntry(String entry) {
            return values.containsKey(entry);
        }

        public Long getLong(String entry) {

            if (values.containsKey(entry)) {

                return Long.valueOf(values.get(entry).toString());

            } else {

                throw (new NullPointerException("Konnte " + entry + " keinen Wert zuweisen (Null)"));

            }

        }

        public boolean getBoolean(String entry) {

            if (values.containsKey(entry)) {

                return Boolean.valueOf((boolean) values.get(entry));

            } else {

                throw (new NullPointerException("Konnte " + entry + " keinen Wert zuweisen (Null)"));

            }

        }

    }
     
    it is laoded in with class
    Code (Java):
    import org.apache.commons.io.FileUtils;
    import org.bukkit.Bukkit;
    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().replace(".yml", ""));
                    values.put(uuid, new HashMap<>());

                    cfg.getConfigurationSection("").getKeys(true).forEach(e -> {
                        values.get(uuid).put(e, cfg.get(e));
                    });

                }

            }

            return this;

        }

        public void saveAll() {

            try {
                FileUtils.deleteDirectory(new File("plugins/RayNight/Playerdata/"));

            } catch (IOException e) {
                e.printStackTrace();
            }

            new File("plugins/RayNight/Playerdata/").mkdirs();

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

                try {
                    File file = new File("plugins/RayNight/Playerdata/" + uuid.toString() + ".yml");
                    file.createNewFile();

                    YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                    for (String e : values.get(uuid).keySet()) {

                        cfg.set(e, values.get(uuid).get(e));

                        cfg.save(file);
                    }
                    Bukkit.broadcastMessage("Saved " + cfg.get("stats.money"));
                } catch (IOException e1) {

                    e1.printStackTrace();
                }

            }

        }

    }
     
    The problem is when i set the value "stats.money" f.e. it saves but if a second player joins it doesnt save anymore.

    Code (Java):
    public static void addMoney(UUID player, Long money) {

                insertMoney(player);
                new PlayerConfig(player).setLong("stats.money", getMoney(player) + money);

            }

            public static void insertMoney(UUID player) {

                PlayerConfig config = new PlayerConfig(player);

                if (!config.containsEntry("stats.money")) {

                    config.setLong("stats.money", 1000L);

                }

            }
     
  2. @RayNightStudios you need to save the file aferer you add something to it. So on your setint and setString and etc class after values.put do values.save that should fix it I believe.
     
  3. i dont think so. I printed out all entries and it printed the correct number to save but it doesnt update it in the file no matter if i delete it. normally when im deleting it it has to be the correct one since the old one doesnt exist anymore. this
    is
    weird
     
  4. well I am pretty sure your not saving your config after you do setlong.... so maybe adding your saveAll() function in your setlong after you put the values and see if that works. Because no where in your code are you saving it i dont see so obviously its not going to save after a reload. I've had issues with configs not saving stuff after reload and it was because I had forgot to save the config file after writing to it.
     
  5. Oh i forgot to say that i do saveall on my onDisable and loading on onEnable
     
  6. Well i could use a preloaded yamlconfiguration or not?
     
  7. I guess that works, I'm not sure why you wouldn't just save your config file after you write to it each time. That's what I've always done and I assume other people do as well. That's what i would recommend doing if you are still having problems, I don't see why you wouldn't try this.
     
  8. So my server has 150 users and if i save it everytime it will cause lag
     
  9. No reason to save it every time you change something as all the data is stored in memory. Why aren't you just straight up using a YamlConfiguration? It does everything you're trying to do but better.
     
    • Like Like x 1
  10. I will try it :) Thank you all for your help
     
    • Like Like x 1
  11. Is the Yaml stored to system when i use #loadConfiguration(file) as a hashmap? Or does it read it everytime again from file when i use #get(key)?
    Im asking because i dont want lag on my server when the data of 200 users are read from the config
     
  12. All the data is loaded to memory when you call loadConfiguration so whenever you call get all it does is just return the value.
     
  13. You could, in the end, used a scheduled task and save the files each 10 minutes or such.

    Sent from my Smartphone_6.1 using Tapatalk
     
  14. Im already doing this. Thanks anyway :)