| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import net.minecraft.client.Minecraft;
|
| 4 |
|
| 5 | public class Timer
|
| 6 | {
|
| 7 | /** The number of timer ticks per second of real time */
|
| 8 | float ticksPerSecond;
|
| 9 |
|
| 10 | /**
|
| 11 | * The time reported by the high-resolution clock at the last call of updateTimer(), in seconds
|
| 12 | */
|
| 13 | private double lastHRTime;
|
| 14 |
|
| 15 | /**
|
| 16 | * How many full ticks have turned over since the last call to updateTimer(), capped at 10.
|
| 17 | */
|
| 18 | public int elapsedTicks;
|
| 19 |
|
| 20 | /**
|
| 21 | * How much time has elapsed since the last tick, in ticks, for use by display rendering routines (range: 0.0 -
|
| 22 | * 1.0). This field is frozen if the display is paused to eliminate jitter.
|
| 23 | */
|
| 24 | public float renderPartialTicks;
|
| 25 |
|
| 26 | /**
|
| 27 | * A multiplier to make the timer (and therefore the game) go faster or slower. 0.5 makes the game run at half-
|
| 28 | * speed.
|
| 29 | */
|
| 30 | public float timerSpeed = 1.0F;
|
| 31 |
|
| 32 | /**
|
| 33 | * How much time has elapsed since the last tick, in ticks (range: 0.0 - 1.0).
|
| 34 | */
|
| 35 | public float elapsedPartialTicks;
|
| 36 |
|
| 37 | /**
|
| 38 | * The time reported by the system clock at the last sync, in milliseconds
|
| 39 | */
|
| 40 | private long lastSyncSysClock;
|
| 41 |
|
| 42 | /**
|
| 43 | * The time reported by the high-resolution clock at the last sync, in milliseconds
|
| 44 | */
|
| 45 | private long lastSyncHRClock;
|
| 46 |
|
| 47 | /** Increase per 1 every tick, reset when reach 1000 */
|
| 48 | private long counter;
|
| 49 |
|
| 50 | /**
|
| 51 | * A ratio used to sync the high-resolution clock to the system clock, updated once per second
|
| 52 | */
|
| 53 | private double timeSyncAdjustment = 1.0D;
|
| 54 |
|
| 55 | public Timer(float tps)
|
| 56 | {
|
| 57 | this.ticksPerSecond = tps;
|
| 58 | this.lastSyncSysClock = Minecraft.getSystemTime();
|
| 59 | this.lastSyncHRClock = System.nanoTime() / 1000000L;
|
| 60 | }
|
| 61 |
|
| 62 | /**
|
| 63 | * Updates all fields of the Timer using the current time
|
| 64 | */
|
| 65 | public void updateTimer()
|
| 66 | {
|
| 67 | long i = Minecraft.getSystemTime();
|
| 68 | long j = i - this.lastSyncSysClock;
|
| 69 | long k = System.nanoTime() / 1000000L;
|
| 70 | double d0 = (double)k / 1000.0D;
|
| 71 |
|
| 72 | if (j <= 1000L && j >= 0L)
|
| 73 | {
|
| 74 | this.counter += j;
|
| 75 |
|
| 76 | if (this.counter > 1000L)
|
| 77 | {
|
| 78 | long l = k - this.lastSyncHRClock;
|
| 79 | double d1 = (double)this.counter / (double)l;
|
| 80 | this.timeSyncAdjustment += (d1 - this.timeSyncAdjustment) * 0.20000000298023224D;
|
| 81 | this.lastSyncHRClock = k;
|
| 82 | this.counter = 0L;
|
| 83 | }
|
| 84 |
|
| 85 | if (this.counter < 0L)
|
| 86 | {
|
| 87 | this.lastSyncHRClock = k;
|
| 88 | }
|
| 89 | }
|
| 90 | else
|
| 91 | {
|
| 92 | this.lastHRTime = d0;
|
| 93 | }
|
| 94 |
|
| 95 | this.lastSyncSysClock = i;
|
| 96 | double d2 = (d0 - this.lastHRTime) * this.timeSyncAdjustment;
|
| 97 | this.lastHRTime = d0;
|
| 98 | d2 = MathHelper.clamp_double(d2, 0.0D, 1.0D);
|
| 99 | this.elapsedPartialTicks = (float)((double)this.elapsedPartialTicks + d2 * (double)this.timerSpeed * (double)this.ticksPerSecond);
|
| 100 | this.elapsedTicks = (int)this.elapsedPartialTicks;
|
| 101 | this.elapsedPartialTicks -= (float)this.elapsedTicks;
|
| 102 |
|
| 103 | if (this.elapsedTicks > 10)
|
| 104 | {
|
| 105 | this.elapsedTicks = 10;
|
| 106 | }
|
| 107 |
|
| 108 | this.renderPartialTicks = this.elapsedPartialTicks;
|
| 109 | }
|
| 110 | }
|
| 111 |
|