    001package net.minecraft.util;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import net.minecraft.client.Minecraft;
    006
    007@SideOnly(Side.CLIENT)
    008public class Timer
    009{
    010    /** The number of timer ticks per second of real time */
    011    float ticksPerSecond;
    012
    013    /**
    014     * The time reported by the high-resolution clock at the last call of updateTimer(), in seconds
    015     */
    016    private double lastHRTime;
    017
    018    /**
    019     * How many full ticks have turned over since the last call to updateTimer(), capped at 10.
    020     */
    021    public int elapsedTicks;
    022
    023    /**
    024     * How much time has elapsed since the last tick, in ticks, for use by display rendering routines (range: 0.0 -
    025     * 1.0).  This field is frozen if the display is paused to eliminate jitter.
    026     */
    027    public float renderPartialTicks;
    028
    029    /**
    030     * A multiplier to make the timer (and therefore the game) go faster or slower.  0.5 makes the game run at half-
    031     * speed.
    032     */
    033    public float timerSpeed = 1.0F;
    034
    035    /**
    036     * How much time has elapsed since the last tick, in ticks (range: 0.0 - 1.0).
    037     */
    038    public float elapsedPartialTicks = 0.0F;
    039
    040    /**
    041     * The time reported by the system clock at the last sync, in milliseconds
    042     */
    043    private long lastSyncSysClock;
    044
    045    /**
    046     * The time reported by the high-resolution clock at the last sync, in milliseconds
    047     */
    048    private long lastSyncHRClock;
    049    private long field_74285_i;
    050
    051    /**
    052     * A ratio used to sync the high-resolution clock to the system clock, updated once per second
    053     */
    054    private double timeSyncAdjustment = 1.0D;
    055
    056    public Timer(float par1)
    057    {
    058        this.ticksPerSecond = par1;
    059        this.lastSyncSysClock = Minecraft.getSystemTime();
    060        this.lastSyncHRClock = System.nanoTime() / 1000000L;
    061    }
    062
    063    /**
    064     * Updates all fields of the Timer using the current time
    065     */
    066    public void updateTimer()
    067    {
    068        long i = Minecraft.getSystemTime();
    069        long j = i - this.lastSyncSysClock;
    070        long k = System.nanoTime() / 1000000L;
    071        double d0 = (double)k / 1000.0D;
    072
    073        if (j <= 1000L && j >= 0L)
    074        {
    075            this.field_74285_i += j;
    076
    077            if (this.field_74285_i > 1000L)
    078            {
    079                long l = k - this.lastSyncHRClock;
    080                double d1 = (double)this.field_74285_i / (double)l;
    081                this.timeSyncAdjustment += (d1 - this.timeSyncAdjustment) * 0.20000000298023224D;
    082                this.lastSyncHRClock = k;
    083                this.field_74285_i = 0L;
    084            }
    085
    086            if (this.field_74285_i < 0L)
    087            {
    088                this.lastSyncHRClock = k;
    089            }
    090        }
    091        else
    092        {
    093            this.lastHRTime = d0;
    094        }
    095
    096        this.lastSyncSysClock = i;
    097        double d2 = (d0 - this.lastHRTime) * this.timeSyncAdjustment;
    098        this.lastHRTime = d0;
    099
    100        if (d2 < 0.0D)
    101        {
    102            d2 = 0.0D;
    103        }
    104
    105        if (d2 > 1.0D)
    106        {
    107            d2 = 1.0D;
    108        }
    109
    110        this.elapsedPartialTicks = (float)((double)this.elapsedPartialTicks + d2 * (double)this.timerSpeed * (double)this.ticksPerSecond);
    111        this.elapsedTicks = (int)this.elapsedPartialTicks;
    112        this.elapsedPartialTicks -= (float)this.elapsedTicks;
    113
    114        if (this.elapsedTicks > 10)
    115        {
    116            this.elapsedTicks = 10;
    117        }
    118
    119        this.renderPartialTicks = this.elapsedPartialTicks;
    120    }
    121}