how to have a method run every 24 hours (automatic)

Discussion in 'Spigot Plugin Development' started by dogboy60, Apr 22, 2017.

  1. So I have a plugin, in this plugin, I need a method to run every 24 hours. How can I do this?

    Example:
    1/1/17 10am:
    server starts
    plugin runs "addIntrest" method
    1/2/17 10am:
    plugin runs "addIntrest" method
    1/3/17 10am:
    plugin runs "addIntrest" method
     
  2. Store your current Server Time, add 86400000 milliseconds to it, check if your current time is over that time and then do your stuff
     
  3. Make a repeating task that runs upon starting the server.
     
  4. Yes, that is if the server is not stopped. The repeating task is lost when the server is stopped.
    The time-stamp approach is the way you need to go, because you can store the time stamp
    to a file when the server is stopped, recall the time stamp on restart and go from there.
     
  5. I use timers in my plugin and it doesn't generate any exceptions. This probably depends on what kind of methods we want to execute. You probably won't get any exceptions if you schedule those tasks.
     
  6. You shouldn't use timers, I recommend you to put the System.currentTimeMillis in a yml file and then compare it. Why? Because if the server crash the timer will restart.

    Then just make a timer that executes every X seconds and check if System.currentTimeMillis() >= ymlMillis
     
    • Agree Agree x 1
  7. What do you mean by restart? A timer runs at the specified time. It can either not run when it should or run when it shouldn't.
     
  8. I guess you already have a repeating thread for your plugin.
    Once the server starts, create a Date object and adds one day to it.
    After that, check in your repeating thread if the saved date is after the new date.
    If true, run your method and set the saved date to the new date and add one day.
     
  9. When I first started writing plugins, I used regular Java Timers. Those timers can go as fast as per millisecond tick.

    Minecraft ticks once per 50ms.

    So, I'm not sure how you've managed to get your
    plugins to work with a regular Java timer because they
    aren't in sync with an MC server.
     
  10. Best thing to do would to just store a timestamp. Add 24 hours to it every time 24 hours have surpassed.
     
    • Agree Agree x 2