Solved How do I get server tick time?

Discussion in 'Plugin Development' started by Blue_Blaze72, Mar 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Blue_Blaze72

    Hello!

    I'm currently working on my first plugin and have ran into the following issue. I am using the BarAPI to count down the time until a task is run (begun by runTaskLater()) and I need the boss bar to accurately depict when the task will occur.

    I have successfully implemented this for the basic case, however, this boss bar is not to be always visible. This boss bar is to only show when the player teleports into the world in which the task will occur. So, while I can accurately estimate the time that will pass until the task will run from the beginning, I have no way of measuring how long it will take until the task will be run if a player teleports in the middle of the waiting sequence.

    There are two major solutions to this problem. I can
    1). Measure the server time at the point where I call runTaskLater() and compare this to the time the player teleports into the world of the task
    2). Perhaps somehow ask the scheduler how many ticks away from executing a particular task is.

    I am aware that I can measure time with the system time, however this is not in ticks and could lead to inaccuracies. While the inaccuracies may be small, this task could be delayed for hours, or even days, so even the slightest inaccuracies could mess up the time measurement.

    I've done some scouting around the API as well as these forums and haven't found the information I am looking for.

    For those who don't like to read, my question is this:
    How do I accurately measure the server's tick time?
     
  2. Offline

    Serilum

    Haven't tested it, but this line of code should work:

    Code:
        private long lastPoll = System.currentTimeMillis() - 3000;
     
        public void Integer(final float tps) {
            final long now = System.currentTimeMillis();
            long timeSpent = (now - this.lastPoll) / 1000;
            if(timeSpent == 0){
                timeSpent = 1;
            }
            tps = ((float)this.plugin.getInterval()) / ((float)timeSpent);
        }
     
  3. Try this:
    Code:java
    1. long time = System.currentTimeMillis();
     
  4. Offline

    2MBKindiegames

    If you're using the Bukkit Scheduler, maybe your solution lies in "Bukkit.getScheduler().getPendingTasks()". I didn't see something right away, but maybe if you search a bit you'll find the thing you're looking for
     
  5. Offline

    CubieX

    I hope I understand you correctly.
    You can use "myWorld.getFullTime()" to get an absolute tick count of the world (since last restart I guess).
    With this, you should be able to do your timing stuff.

    However, I have not understood what you mean with
    "measuring how long it will take until the task will be run if a player teleports in the middle of the waiting sequence".
    What difference does this make?
    The task will run as scheduled. Regardless if a player is present or not. So you can store this value when scheduling the task and read it whenever you like.
     
    Blue_Blaze72 likes this.
  6. Offline

    Blue_Blaze72

    Thank you all for the fast responses! I think CubieX's answer best fits what I'm looking for. The reason is that I want to measure the time in tick to account for server lag. While the server is SUPPOSED to run at 20 ticks per second, it's likely that ticks could lag behind in time (even if it's only a few microseconds) if I measure using System.currentTimeMillis(). Thus if I get the tick count this will always be accurate relative to the ticks, even when the server lags.

    Also, CubieX, to answer your question, I was looking for a way to accurately determine the amount of time remaining before a task run using runTaskLater(). The reason why teleporting in the middle of the waiting sequence could be an issue is that I need to measure the amount of time that has passed since having called the runTaskLater(). As others have suggested, I could use System.currentTimeMillis(), but I was worried that measuring in system time may not match perfectly with the actual server ticks. What you gave me should work great since I can measure the number of ticks that have changed, which will be accurate regardless of the amount of lag since to my knowledge, everything is run based on ticks for bukkit.

    Thank you to all who responded! I will mark this as solved since if for whatever reason CubieX's line doesn't work for my purposes, I will resort to using system time as you have all suggested.
     
Thread Status:
Not open for further replies.

Share This Page