Sueta

TickTime

1.2

Measure time in Minecraft Ticks, not Seconds! Avoid unfair advantages!

·Plugin

Did you know that measuring time in seconds could lead to unfair disadvantages?

  • Be poor Steve, playing in a minigame with a time limit, who lost because the server lagged constantly and they lost 5 seconds, which could have saved the match.
  • Be Sally, who cannot have a consistent 1:1 time measurement in her world because Minecraft ticks, (while they're mostly every 1/20 of a second) they're inconsistent due to lag.

How can we prevent these situations? With TickTime. This lightweight, sigle-class API is a must have in any plugin that measures time in any way. While using System.nanotime() is normally the way to go, measuring the time like that in Minecraft can lead to some inconsistencies due to lag, plugins, players, entities, armor stands , etc, etc. With TickTime, you can measure the time in Minecraft ticks, so measurements will be consistent with worlds, entities, players, etc.

Every time Minecraft thinks time has passed, TickTime will too.

TickTime has no commands, no permissions and no events, so just drag-and-drop into your plugins folder. Because TickTime only uses two imports (that every spigot.jar has) it is essentially version-independent.

Do you want to use TickTime too? Drag-and-drop into your project and use it like this:

long  time0  =  TickTime.  currentTickTime   (   )   ;  

letsMakeSomeCode  (   )   ;  

 long  time1  =  TickTime.  currentTickTime   (   )   ;  

 // calculate time between ticks!  
 long  time_taken  =  time1  -  time0  ;  

 // 1 second = 20 mc ticks  
 long  seconds  =  time_taken  /   20   ;

I don't even need to put the code in GitHub. (The author would like to tell you that he doesn't even know how to use GitHub, that noob)

Spoiler
package   dev.ckitty.mc.ticktime   ;  

 import   org.bukkit.plugin.java.JavaPlugin   ;  
 import   org.bukkit.scheduler.BukkitRunnable   ;  

 public   class  TickTime  extends  JavaPlugin  {  
  
     private   static   long  TICK_TIME  ;  
  
     public   static   long  currentTickTime  (   )   {  
         return  TICK_TIME  ;  
     }  
  
     private    Timer   timer  ;  

    @Override 
     public   void  onEnable  (   )   {  
        timer  =   new    Timer    (   )   ;  
        timer.  runTaskTimerAsynchronously   (   this  ,  0  ,  1   )   ;  
     }  
  
    @Override 
     public   void  onDisable  (   )   {  
        timer.  cancel   (   )   ;  
        timer  =   null   ;  
     }  
  
     private   class    Timer    extends  BukkitRunnable  {  

        @Override 
         public   void  run  (   )   {  
            TICK_TIME  ++;  
         }  
    
     }  
  
 }

General Information:

  • The values given by TickTime.currentTickTime() are not consistent after the server stopping, but they are after the /reload command.
  • You shouldn't worry that the TICK_TIME variable could overflow. Even System.nanotime() won't overflow in at least two years of non-stop use. So, try to restart your server every few days.
  • Tips, suggestions, features? Please feel free to ask them in the discussion and not in the reviews. You cannot communicate very well in the reviews...

Similar projects

Plugin →