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.
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!
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.
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%.
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.
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.
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.
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?
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
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
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.
Yeah and then as the plugin disables you store all that data from each PlayerData in the config its much more lag free
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
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?
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.