Running a timer every 16.6 ticks

Discussion in 'Spigot Plugin Development' started by Remixful, May 1, 2017.

  1. 1 minute in minecraft time = 16.6 ticks. I am trying to create a clock that uses BukkitScheduler.
    Code (Text):
    runTaskTimer(Plugin plugin, Runnable task, long delay, long period)
    This only takes long numbers for the delay/period parameters. If I use 16 or 17, eventually, the clock will be off only after a few minutes.
     
  2. Umm one minute is 1200 ticks not 16.6 assuming the server is running at a constant 20 ticks per second.
     
  3. @Virizion. I believe he's talking about an in-game minute, not a real life one.

    Also, there is probably a more efficient way, but every x ticks add y ticks to account for the difference.
    NOTE: Pretty resource inefficient.
     
  4. I'm not sure how to keep up with the time in Minecraft accurately.
     
  5. Do you need it to happen every 16.6 ticks? That's less than once a second. You could use 83 which would be once every 5 minecraft minutes.

    Roughly 4.15 seconds. This should keep in time provided you don't ever have any tps lag. Then your timer will be off as well.

    Another solution might be to read the last game ticks, store that variable and run the timer more often like every tick. When the current time ticks - old ticks
    >= 17 run your code. This should never get over .6 ticks of error.

    Perhaps let us know what you're trying to do and we can come up with a solution that will keep in time.


    Sent from my iPhone using Tapatalk
     
  6. Oh ok I get what he means. The only other way I can think of is loop every tick and check if 830 milliseconds passed using System.currentTimeMillis().
     
    • Agree Agree x 2
  7. I've tried both of these methods, and eventually the time is off again.
    upload_2017-5-1_15-14-52.png

    Code (Text):
    lastTime = Bukkit.getWorld("main").getTime();
    if(Bukkit.getWorld("main").getTime() - lastTime >= 17) {
        //I add a minute to the clock here
        lastTime = Bukkit.getWorld("main").getTime();
    }
    I've tried setting lastTime to both the world's time and System.currentTimeMillis (but I check if its 830 ms apart instead). This is running every tick.
     
  8. Just keep track of currentTimeMillis, trying to measure ticks will never be very accurate, as the exact length can vary if the server is under load, not to mention there is no way to make the bukkit scheduler run on a fraction of a tick interval.
     
  9. Why not just update the time shown there every tick with the game time? Then it should stay In sync.


    Sent from my iPhone using Tapatalk
     
  10. Every tick might be a bit excessive. Better off doing it once every ~4 ticks for something like this.
     
  11. But he's trying to keep it in exactly in sync with the game clock which is going to be hard to do especially if the server gets under heavy load.

    What are you doing that minecraft minutes which are less than a second needs so much resolution? A clock? One to two mins of error shouldn't be a huge deal as long as it does not grow. The time will change between updates and you running that command because the unit of measure is milliseconds.


    Sent from my iPhone using Tapatalk
     
  12. It's actually 16 2/3 (=16.66... (praise satan)) ticks. Instead of increasing a number, you could also get the actual current day time and perform some calculations on that to get the value.
     
  13. That time is from Essentials /time command.
     
  14. I know, there's a second time there which I assume is yours.

    It will take a finite amount of time for your code to display the time. Then when you type /time a finite amount of time for that to process. There is going to be some difference in those two because the time is constantly passing. And you're looking at milliseconds not actual seconds.

    So by the time both of those things have happened you're going to end up with an error. As long as that error is always 2 minutes you could subtract 2 mins from your timer but you need a method that is consistent


    Sent from my iPhone using Tapatalk