yamls not updating right?

Discussion in 'Plugin Development' started by nitrousspark, Dec 15, 2012.

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

    nitrousspark

    im using yamls for various things on my server, and sometimes when i set something in a yaml it resets after like 5 seconds, i dont get it and i want to fix it
     
  2. Offline

    tommycake50

    after you set something do,
    config.save(File);
     
  3. Offline

    fireblast709

    ^ the config does not open itself after 5 seconds. If you are using getConfig(), use saveConfig() after a change
     
  4. Offline

    nitrousspark

    i am getting the config, modifing it then saving it. and it doesnt work sometimes. so its like this

    Yaml yaml = this.getPlayerYaml(player);
    yaml.set("rank", "donor");
    yaml.save();
     
  5. Offline

    EnvisionRed

    That isn't how you get the config....
    Code:
    getConfig().set("rank", "donor");
    saveConfig();
     
  6. Offline

    Tirelessly

    He probably is using a file for each player as a database, and not the config.
     
  7. Offline

    nitrousspark

    correct, and im getting the file for the player, then modifing it then saving it. as stated earlier :p
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    You probably did not implement file handling properly.
     
  9. Offline

    nitrousspark

    sometimes it works, but most of the time it doesnt, its really wierd

    is there a solution to the problem?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  10. Offline

    fireblast709

    can you post the code you have?
     
  11. Offline

    nitrousspark

    i kinda allready did

    Yaml yaml = this.getPlayerYaml(player);
    yaml.set("rank", "default");
    yaml.save
     
  12. Offline

    evilmidget38

    You have a problem in your Yaml code if that isn't working. As such, if we are to help you, we'd need to see your Yaml code.
     
  13. Offline

    nitrousspark

    public void save() {

    try {
    this.yaml.save(this.file);
    } catch (Exception e) {
    e.printStackTrace();
    }

    }

    public void set(String s, Object o) {

    this.yaml.set(s, o);

    }
     
  14. Offline

    Sagacious_Zed Bukkit Docs

    As far as anyone is concerned, If you are using variables that are not assigned in your code snippet, you have not provided enough code. Also since FileConfiguration and YamlConfiguration are mutable classes, best to provide any code that touches it.
     
  15. Offline

    nitrousspark

    the entire yaml class

    Code:
     
        private File file = null;
        private YamlConfiguration yaml = new YamlConfiguration();
     
        public Yaml(File file) {
     
            this.file = file;
     
            if (!file.exists()) {
     
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
     
            }
     
            // logger.log(Level.INFO,
            // "[AdvancedStorage] New Yaml file loaded. Path: " +
            // this.file.getAbsolutePath());
     
            this.load();
     
        }
     
        public Yaml(String path) {
     
            this.file = new File(path);
     
            if (!file.exists()) {
     
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
     
            }
     
            // logger.log(Level.INFO,
            // "[AdvancedStorage] New Yaml file loaded. Path: " +
            // this.file.getAbsolutePath());
     
            this.load();
     
        }
     
        private void load() {
     
            try {
                this.yaml.load(this.file);
            } catch (Exception e) {
                e.printStackTrace();
            }
     
        }
     
        /**
        * Save the Yaml's data to the file passed in the constructor.
        */
        public void save() {
     
            try {
                this.yaml.save(this.file);
            } catch (Exception e) {
                e.printStackTrace();
            }
     
        }
     
        /**
        * Get an Integer from the given path.
        *
        * @param s
        *            Path to the Integer.
        * @return Integer at given path.
        */
        public int getInteger(String s) {
     
            return this.yaml.getInt(s);
     
        }
     
        /**
        * Save, then load the Yaml file. **Warning** Very Unstable.
        */
        public void reload() {
     
            this.save();
     
            this.load();
     
        }
     
        /**
        * Get a String from the path defined.
        *
        * @param s
        *            Path to the String.
        * @return String at given path.
        */
        public String getString(String s) {
     
            return this.yaml.getString(s);
     
        }
     
        /**
        * Gets an Object at the given path.
        *
        * @param s
        *            Path to given Object.
        * @return An Object at the given Path.
        */
        public Object get(String s) {
     
            return this.yaml.get(s);
     
        }
     
        /**
        * Gets a boolean at the given path.
        *
        * @param s
        *            Path to the boolean.
        * @return Boolean at the given path.
        */
        public boolean getBoolean(String s) {
     
            return this.yaml.getBoolean(s);
     
        }
     
        /**
        * If the given path has no variable, it will be given a variable.
        *
        * @param s
        *            Path to look for.
        * @param o
        *            Variable to be assigned if not existing.
        */
        public void add(String s, Object o) {
     
            if (!this.contains(s)) {
     
                this.set(s, o);
     
            }
     
        }
     
        /**
        * Adds a String to a List of Strings.
        *
        * @param s
        *            Path to given String List.
        * @param o
        *            String to add to the String List.
        */
        public void addToStringList(String s, String o) {
     
            this.yaml.getStringList(s).add(o);
     
        }
     
        /**
        * Removes a String to a List of Strings.
        *
        * @param s
        *            Path to given String List.
        * @param o
        *            String to remove from the String List.
        */
        public void removeFromStringList(String s, String o) {
     
            this.yaml.getStringList(s).remove(o);
     
        }
     
        /**
        * Looks for a String List at given Path.
        *
        * @param s
        *            Path to String List.
        * @return String List at given Path.
        */
        public java.util.List<String> getStringList(String s) {
     
            return this.yaml.getStringList(s);
     
        }
     
        /**
        * Adds an Integer to a List of Integers.
        *
        * @param s
        *            Path to given Integer List.
        * @param o
        *            Integer to add to the Integer List.
        */
        public void addToIntegerList(String s, int o) {
     
            this.yaml.getIntegerList(s).add(o);
     
        }
     
        /**
        * Removes an Integer to a List of Integers.
        *
        * @param s
        *            Path to given Integer List.
        * @param o
        *            Integer to remove to the Integer List.
        */
        public void removeFromIntegerList(String s, int o) {
     
            this.yaml.getIntegerList(s).remove(o);
     
        }
     
        /**
        * Looks for a Integer List at given Path.
        *
        * @param s
        *            Path to Integer List.
        * @return Integer List at given Path.
        */
        public java.util.List<Integer> getIntegerList(String s) {
     
            return this.yaml.getIntegerList(s);
     
        }
     
        /**
        * Creates a new String List at given Path.
        *
        * @param s
        *            Path to create String List at.
        * @param list
        *            List to add.
        */
        public void createNewStringList(String s, java.util.List<String> list) {
     
            this.yaml.set(s, list);
     
        }
     
        /**
        * Creates a new Integer List at given Path.
        *
        * @param s
        *            Path to create Integer List at.
        * @param list
        *            List to add.
        */
        public void createNewIntegerList(String s, java.util.List<Integer> list) {
     
            this.yaml.set(s, list);
     
        }
     
        /**
        * **Untested/Unstable** Attempts to remove a variable at the given Path.
        *
        * @param s
        *            Path to given variable needing removal.
        */
        public void remove(String s) {
     
            this.set(s, null);
     
        }
     
        /**
        * Returns true if the given Path has a value.
        *
        * @param s
        *            Path to value.
        * @return True if the given Path has a value.
        */
        public boolean contains(String s) {
     
            return this.yaml.contains(s);
     
        }
     
        /**
        * Gets a double at the given Path.
        *
        * @param s
        *            Path to double.
        * @return Double at given Path.
        */
        public double getDouble(String s) {
     
            return this.yaml.getDouble(s);
     
        }
     
        /**
        * Sets a Object to the given Path.
        *
        * @param s
        *            Path to variable being assigned.
        * @param o
        *            Variable being assigned.
        */
        public void set(String s, Object o) {
     
            this.yaml.set(s, o);
     
        }
     
        /**
        * Increases an Integer by 1.
        *
        * @param s
        *            Path to Integer being incremented.
        */
        public void increment(String s) {
     
            this.yaml.set(s, this.getInteger(s) + 1);
     
        }
     
        /**
        * Decreases an Integer by 1.
        *
        * @param s
        *            Path to Integer being decremented.
        */
        public void decrement(String s) {
     
            this.yaml.set(s, this.getInteger(s) - 1);
     
        }
     
        /**
        * Increases an Integer by i.
        *
        * @param s
        *            Path to Integer being incremented.
        */
        public void increment(String s, int i) {
     
            this.yaml.set(s, this.getInteger(s) + i);
     
        }
     
        /**
        * Decreases an Integer by 1.
        *
        * @param s
        *            Path to Integer being decremented.
        */
        public void decrement(String s, int i) {
     
            this.yaml.set(s, this.getInteger(s) - i);
     
        }
     
        /**
        * Returns the YamlConfiguration's Options.
        *
        * @return YamlConfiguration's Options.
        */
        public YamlConfigurationOptions options() {
     
            return this.yaml.options();
     
        }
     
  16. Offline

    Sagacious_Zed Bukkit Docs

    nitrousspark
    Given the current implementation of your yaml class. I think you are having problems managing instances of this class. The only strange thing about this implementation is that reload will save what is in memory to disk and then load what it just saved back, which is not what reload would do mirroring the convention set by Bukkit.
     
  17. Offline

    fireblast709

    Also, his List implementation is broken
     
  18. Offline

    Sagacious_Zed Bukkit Docs

    You're right, I didn't even read that far down!
     
  19. Offline

    nitrousspark

    so how do i fix it?
     
  20. Offline

    Sagacious_Zed Bukkit Docs

    getStringList returns a copy of the list, so changes to it does not affect the underlying YamlConfiguration
     
  21. Offline

    nitrousspark

    so... what can i do to fix it
     
  22. Offline

    Sagacious_Zed Bukkit Docs

    You can either modify the underlying list. or set the list again...
     
  23. Offline

    nitrousspark

    so your saying instead of this

    yaml.set("rank", "default");
    yaml.save();

    do this?

    yaml.set("rank", "default");
    yaml.save();
    yaml.set("rank", "default");
    yaml.save();
     
  24. Offline

    fireblast709

    no. He (and I) means (ment) that if you do getStringList("path"), it would return a copy of the list. So if you add an item to the copy, it would not edit the config.
    Code:java
    1. List<String> list = getConfig().getStringList("some.path");
    2. if(list == null)
    3. {
    4. list = new ArrayList<String>();
    5. }
    6. list.add("Something");
    7. getConfig().set("some.path", list);
    8. saveConfig();
     
  25. Offline

    nitrousspark

    but im not using string lists. its just one string.
     
  26. Offline

    fireblast709

  27. Offline

    nitrousspark

    so putting that in the file will fix it?
     
  28. Offline

    Sagacious_Zed Bukkit Docs

    It will fix one of the many problem you have.
     
  29. Offline

    nitrousspark

    -_- how can i fix the problem that causes the yaml to not work, so i can set something in it and it will actually set
     
  30. The solution already has posted, if you don't understand what the solution does, I recommend that you better learn java so you understand it.
    We already giving you:
    * How to fix the problem
    * What the problem is
    * Code past that solves you're problem
    But you're still not getting it, whits assumes me you don't know java properly for the task you're trying to do
     
Thread Status:
Not open for further replies.

Share This Page