[Solved] How fast is getting/setting Config values?

Discussion in 'Spigot Plugin Development' started by BryceTheCoder, May 22, 2015.

  1. Hello everyone!
    I need infromation from someone who is experienced with config gettings and settings of values.

    I want to have a plugin store infromation on players like this format:

    PlayerName:
    level: 34
    otherstuff: 231

    So, my plugin will constantly be change otherstuff values on players while the mine blocks.
    I simply do:
    getConfig().set(otherstuff + 1, to that player);
    saveConfig();


    Now, will this be really bad for my server's performance? Or will this be lightning fast, this will work properly?
    If this won't work so efficiently, please let me know on how to make it better.
     
  2. First, always doing getConfig() cost some performence, so make a variable and use that when u need to use getConfig(). I wouldnt do it with a config. Every second (mining) saving to the config are memory leaks.

    Do it like so:
    Create a hashmap with the String/UUID and a Integer and add it + 1 when he mines it. Then when he quits save/add (if he already have a value) it then to the config (His hashmap value).

    Succes, hope this helps!
     
    • Informative Informative x 1
  3. Thanks for explaining this, usefull knowledge for me aswell. :)
     
  4. clip

    Benefactor

    That is bad... i suggest making an object that holds data related to the player and store the player data objects for online players in a cache until they logout. Save then or even save all loaded data objects on an interval.

    Code (Text):
    public class PlayerData {

        private String name;

        private String uuid;

        private int level;

        private double somethingElse;

        public PlayerData(Player p) {
            this.name = p.getName();

            // other stuff here
        }

        // getters and setters

    }
    on my phone so im not going to write out a ton of stuff. Just giving an example of one way you could do what you are trying to do.
     
    #4 clip, May 22, 2015
    Last edited: May 22, 2015
    • Informative Informative x 2
  5. Sooo, I just made an array list of PlayerData and called it dataList.
    Sooo, how would I get a specific person though? I have a field called uuid in PlayerData however I'm not sure how to get that specific player's index in the array list.
    I'm trying something like this:
    dataList.get(INDEX).getCombatLevel()
    However, I can't think of a way to get their index.

    EDIT: I'm powering my brain through this concept. I got semi-working now. I changed to a HashMap to keep 2 values in the list, the player and then the playerdata.
    I will reply once I get stuff working 100%.
     
    #5 BryceTheCoder, May 22, 2015
    Last edited: May 22, 2015
  6. getConfig().set(otherstuff + 1, to that player);

    This will work "lightning fast" as it's stored in a map by the MemoryConfiguration class.


    saveConfig();

    This may take ,"some" time, as it is a file io operation. Only call it on onDisable should be sufficient.
     
    • Agree Agree x 1
  7. I personally use YamlConfiguration objects for all of the info I store to a file and recall in plugins. It's been pretty fast in my experience while using it as a flatfile database. I just load the old yml file onEnable() and occasionally save it (asynchronously is best for larger sets of data if you know how to manage the timing, otherwise you'll lag the server). I've never had to think twice about getting/setting values to a YamlConfiguration object.
     
  8. It is better to load the information into a HashMap when a player joins, and edit it WHILE they are online, and then onDisable or when the player leaves, save the information back to the database. To answer your question though, it is very fast, but with tons of people, and if it is being constantly changing, you will probably experience some lag issues.
     
    #8 ZyphiorMC, May 23, 2015
    Last edited: May 23, 2015
    • Optimistic Optimistic x 1
  9. What he said ^^^ way less laggy
     
  10. So you're telling me I can do this:

    public void onBreak(BlockBreakEvent e) {
    getConfig().set("users." + playersName + ".mining", getCurrentInt + 1);
    }


    and then a schedueled task that is called every 5min:
    saveConfig();



    Is that okay? Or will that not work properly?
     
  11. Actually. I checked out your "Basic Stats" plugin and I'm confused. You have it saving the config after EVERY kill/death for a player.
    So obviously it's not that bad? Or is just the way you are doing it also is bad? I'm confused now :p
     
  12. Why do you need to constantly save data on a player? You could simply just save their data in a specific class titled "PlayerData" and save data in their rather than constantly saving it to a config
     
  13. When the plugin stops then that object of PlayerData will be released from memory.. I would most definitely save it in the config or another file.
     
  14. He is doing it bad. Maybe is it an old plugin.
     
  15. Yeah and then as the plugin disables you store all that data from each PlayerData in the config its much more lag free
     
  16. That way you only have to save data to the config once
     
  17. If you get the value from config every single time, there will be lag for sure, try to store values in a hashmap and in an interval, add them to a config
     
  18. Exactly
     
  19. I'm pretty sure I'm getting this all perfectly using the HashMap however the only problem I have is this:
    I cannot call getConfig() inside my constructor in my PlayerData object.
    I want to call it, however, I don't want my PlayerData to extend a JavaPlugin.
    How can I do this anyone?
     
  20. getConfig() is what is known as an accessor. This has zero performance cost (as they are optimized away by the JIT), which is why proper programming practice in Java is to make all fields private and then make accessors.