Solved Saving a file very often can cause lags ?

Discussion in 'Spigot Plugin Development' started by Soolz, Feb 13, 2019.

  1. Soolz

    Soolz Previously eot13


    I'm making a Jobs plugin and I use an API (Developer API) to create custom configuration files.
    Each time a new player connects, it creates a configuration file for it with its UUID (name).
    Until then, it's ok.

    But now, I create the job: miner.
    Whenever a player breaks a specific block, he gains miner's xp.

    But I have to save the configuration file every time to reuse the configuration data elsewhere...

    Code (Java):
    FileBuilder f = new FileBuilder("plugins/EpixJobs/data", p.getUniqueId() + ".yml");
            f.set("jobs.miner.xp", f.getInt("jobs.miner.xp") + xp);
  2. Only save it to file every so often and when disabling the plugin

    Also: No need to use a filebuilder as you can just use Bukkit's configuration api to handle yml files
  3. you know you can safe the data in a f.e. hashmap so that wont causes laggs and when he leaves etc. you save it into the file
  4. You could save the generated data (like experience) in a custom object and save that data at appropriate times (like when the player leaves, or your plugin gets disabled).
  5. I have the player data saving on a 5 minute interval timer. If they do something important like change a personal setting or upgrade an ability then i save it then and there because that is an important thing to save for my plugin. Exp gain is less of a loss as it can easily be regained since it's just 5 minutes of work but its a bigger issue if users dont realize their upgrade didnt go through and try to utilize that later
    • Agree Agree x 1
  6. Soolz

    Soolz Previously eot13

    it doesn't return to the same?

    Yes, but I should create 3 Hashmaps per jobs (public static). Is it problematic?
  7. Why not utilize OOP and make a JobsPlayer and store Jobs info in there?
    • Agree Agree x 2
  8. You should try to avoid using the 'public static'-modifiers, there are cleaner ways to access variables from other classes.
    Also, creating 3 HashMap's per job would not be a problem. But if you only wanted one HashMap (what I would recommend), you could create a class that has those 3 values as fields.
    • Agree Agree x 1
  9. Soolz

    Soolz Previously eot13

    Ok, thank's but how can I do that without public static ?
    Is it OOP ?
    I had never heard that word until now
  10. That isnt good because the "basic concept" of java is oop
  11. OOP stands for object oriented programming which is a very powerful asset of a java programmer. I would recommend taking a look through some of these videos as the concepts you can learn will be extremely helpful in clarifying ideas, cleaning up workaround code, and helping with maintaining the project in the future.
    • Useful Useful x 1
  12. You could set it to private and remove the static keyword. Then you would have some options: create a public getter-method that returns the HashMap so an object of another class can modify and/or store data. Another option is to create a public method that would look like:
    Code (Java):
    public SomeObject getSomeObject(HashMapKey key)
    SomeObject would be the class name of the class that stores the information you're currently writing to a file. The HashMapKey should be replaced by whatever class you're using as a key. You also might want to use a setter so you can add stuff to the HashMap from objects that are instances of other classes. Note that for both of these 'strategies', you'd need to have an instance of the class that stores the list in the object that's getting/setting data. I hope this explanation was sufficient. If not, feel free to ask as many questions as you want.
    Someone else beat me to explaining OOP, I'd like to add that it's quite important when writing Java (or any other object oriented language).
    • Agree Agree x 1
    • Useful Useful x 1
  13. Soolz

    Soolz Previously eot13

    Ok, thank you all :D
    I found a french tutorial about OBJ<?>
    I will mark this thread as solved when I would have succeeded.
    • Friendly Friendly x 1
  14. If you have any need for further help, feel free to hit up my dms on either discord or spigot and I'll see what I can do to help ya out.
    • Winner Winner x 1
  15. Soolz

    Soolz Previously eot13

    Ok, thank you very much

Share This Page