Saving Every 24h

Discussion in 'Spigot Plugin Development' started by NormaalBart, May 24, 2017.

  1. Hello,

    I want to save some HashMaps every 24hours but, how can I do this?
    Like creating a scheduler what runs every 24h's once?
    Or how should you do it?
    Are there any other methods to do it?

    Please respond if you have a way/method.
  2. If you need to save hashmap that DOES NOT contain any data related to the Spigot API, you can do it using a BukkitRunnable and run it asyc every 24h (About 1728000 ticks). If you are saving something related to the API, then you need to run it in the main thread
  3. Well, it's maybe a bit noob in that but.
    I'm saving then the UUID's, location's etcetra of the skyblock information. That is related to the Spigot API right? if yes, how can I run it in the server's main thread ?
  4. You already run everything in the Main thread until you create a new thread
  5. UUID are not related to Spigot, they are a java utils, so there should be no problem with them(Not location though) (Be careful of Concurrent Exceptions, a quick search on google will help you). As stated before, both minecraft and spigot run in the main thread, so if you make a resource-heavy operation, will lag the server because you'll stop the main thread for an X amount of time
  6. It's a bit weird to save something at a set duration.

    If it's player data, for example, you should save it to database on player quit (and, optionally, every X duration).

    What exactly do you want to save every 24 hours? There's probably a better way to do it. e.g. if the server is stopped before the scheduler can run, no data will be saved.
    • Agree Agree x 1
  7. lol, this hasn't anything to do with the post...
  8. Ops, sorry. I just realized.
    When you are starting a bukkit runnable, you can choose if running it async (runTaskTimerAsynchronously) or sync with the main thread (runTaskTimer).
    I suggest you transform data from spigot api to something you can relate in an async way since working with IO can stop the main thread for some time.
    Anyway, as I stated, UUID are not related to spigot, not the same as location.
  9. if you are not willing to use the spigot API, you can always use Timers that are java-based
    with timers, actionListeners can be used.
    if you want to use this, ill be happy to help
  10. Why use runnables....
    Why not get the currentTimeMillis + (1000 * seconds);
    And on startup check if the currentTime is equal or greater than the above...
    Bad practice using Runnables...
  11. he wants to save a hashmap, its usually empty on startup, thats because it is created on startup
  12. Thats why you have to save it....
    I don't get it why he wants to save every 24h..
    Why not save it on disable and load on startup...
    And runnables are very bad for this..
  13. When I can, I also try to avoid using runnable (especially on cooldown). But they are needed for delayed method to be executed

    EDIT: I also prefer saving data on startup or on disable, but the question from the op was another one ...
  14. why should runnables be bad?
    that's why they exist.
    What you mentioned is completly unlogic. why should that only be checked on startup? what if a server is online for longer than 24 hours.
    It should run every 24 hours and not only once.
    Also why is have you quoted my post? lol
  15. Yeah but if there is a crash or what ever,, Bye bye runnable :) Thats why either you use currentTimeMillis or just save onDisable and load onEnable.
  16. Then you just start your runnable on server startup again.
    & store the lost ticks with a shutdown hook.
  17. Obviously, you need to save files also onEnable and onDisable. But if the server crashes, onDisable is not called and all data is lost. So generally, also a timed save is a good thing
  18. *Server crash* Bai runnable, Bai data...

    Best is to save onDisable and load onEnable and save the future currentTimeMillis in a config or Database. Have a repeatingTask checking every X minutes if the currentTimeMillis is equal or greater than the one for 24h.
  19. "*Server crash* Bai runnable, Bai data..."
    have you heard about java shutdown hooks? ..
    • Agree Agree x 1
  20. Ugh... When the server crashes, the onDisable is not called... Runnable is removed and data is lost...
    Why make things more complex when it get be done easy... omfg