    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 var1 = Minecraft.getSystemTime();
    069        long var3 = var1 - this.lastSyncSysClock;
    070        long var5 = System.nanoTime() / 1000000L;
    071        double var7 = (double)var5 / 1000.0D;
    072
    073        if (var3 <= 1000L && var3 >= 0L)
    074        {
    075            this.field_74285_i += var3;
    076
    077            if (this.field_74285_i > 1000L)
    078            {
    079                long var9 = var5 - this.lastSyncHRClock;
    080                double var11 = (double)this.field_74285_i / (double)var9;
    081                this.timeSyncAdjustment += (var11 - this.timeSyncAdjustment) * 0.20000000298023224D;
    082                this.lastSyncHRClock = var5;
    083                this.field_74285_i = 0L;
    084            }
    085
    086            if (this.field_74285_i < 0L)
    087            {
    088                this.lastSyncHRClock = var5;
    089            }
    090        }
    091        else
    092        {
    093            this.lastHRTime = var7;
    094        }
    095
    096        this.lastSyncSysClock = var1;
    097        double var13 = (var7 - this.lastHRTime) * this.timeSyncAdjustment;
    098        this.lastHRTime = var7;
    099
    100        if (var13 < 0.0D)
    101        {
    102            var13 = 0.0D;
    103        }
    104
    105        if (var13 > 1.0D)
    106        {
    107            var13 = 1.0D;
    108        }
    109
    110        this.elapsedPartialTicks = (float)((double)this.elapsedPartialTicks + var13 * (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}