| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.floats.FloatUnaryOperator;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 |
|
| 7 | @Environment(EnvType.CLIENT)
|
| 8 | public interface DeltaTracker {
|
| 9 | DeltaTracker ZERO = new DeltaTracker.DefaultValue(0.0F);
|
| 10 | DeltaTracker ONE = new DeltaTracker.DefaultValue(1.0F);
|
| 11 |
|
| 12 | float getGameTimeDeltaTicks();
|
| 13 |
|
| 14 | float getGameTimeDeltaPartialTick(boolean ignoreFrozenGame);
|
| 15 |
|
| 16 | float getRealtimeDeltaTicks();
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | class DefaultValue implements DeltaTracker {
|
| 20 | private final float value;
|
| 21 |
|
| 22 | private DefaultValue(final float value) {
|
| 23 | this.value = value;
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public float getGameTimeDeltaTicks() {
|
| 28 | return this.value;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public float getGameTimeDeltaPartialTick(final boolean ignored) {
|
| 33 | return this.value;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public float getRealtimeDeltaTicks() {
|
| 38 | return this.value;
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | @Environment(EnvType.CLIENT)
|
| 43 | class Timer implements DeltaTracker {
|
| 44 | private float deltaTicks;
|
| 45 | private float deltaTickResidual;
|
| 46 | private float realtimeDeltaTicks;
|
| 47 | private float pausedDeltaTickResidual;
|
| 48 | private long lastMs;
|
| 49 | private long lastUiMs;
|
| 50 | private final float msPerTick;
|
| 51 | private final FloatUnaryOperator targetMsptProvider;
|
| 52 | private boolean paused;
|
| 53 | private boolean frozen;
|
| 54 |
|
| 55 | public Timer(final float ticksPerSecond, final long currentMs, final FloatUnaryOperator targetMsptProvider) {
|
| 56 | this.msPerTick = 1000.0F / ticksPerSecond;
|
| 57 | this.lastUiMs = this.lastMs = currentMs;
|
| 58 | this.targetMsptProvider = targetMsptProvider;
|
| 59 | }
|
| 60 |
|
| 61 | public int advanceGameTime(final long currentMs) {
|
| 62 | this.deltaTicks = (float)(currentMs - this.lastMs) / this.targetMsptProvider.apply(this.msPerTick);
|
| 63 | this.lastMs = currentMs;
|
| 64 | this.deltaTickResidual = this.deltaTickResidual + this.deltaTicks;
|
| 65 | int ticks = (int)this.deltaTickResidual;
|
| 66 | this.deltaTickResidual -= ticks;
|
| 67 | return ticks;
|
| 68 | }
|
| 69 |
|
| 70 | public void advanceRealTime(final long currentMs) {
|
| 71 | this.realtimeDeltaTicks = (float)(currentMs - this.lastUiMs) / this.msPerTick;
|
| 72 | this.lastUiMs = currentMs;
|
| 73 | }
|
| 74 |
|
| 75 | public void updatePauseState(final boolean pauseState) {
|
| 76 | if (pauseState) {
|
| 77 | this.pause();
|
| 78 | } else {
|
| 79 | this.unPause();
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private void pause() {
|
| 84 | if (!this.paused) {
|
| 85 | this.pausedDeltaTickResidual = this.deltaTickResidual;
|
| 86 | }
|
| 87 |
|
| 88 | this.paused = true;
|
| 89 | }
|
| 90 |
|
| 91 | private void unPause() {
|
| 92 | if (this.paused) {
|
| 93 | this.deltaTickResidual = this.pausedDeltaTickResidual;
|
| 94 | }
|
| 95 |
|
| 96 | this.paused = false;
|
| 97 | }
|
| 98 |
|
| 99 | public void updateFrozenState(final boolean frozen) {
|
| 100 | this.frozen = frozen;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public float getGameTimeDeltaTicks() {
|
| 105 | return this.deltaTicks;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public float getGameTimeDeltaPartialTick(final boolean ignoreFrozenGame) {
|
| 110 | if (!ignoreFrozenGame && this.frozen) {
|
| 111 | return 1.0F;
|
| 112 | } else {
|
| 113 | return this.paused ? this.pausedDeltaTickResidual : this.deltaTickResidual;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public float getRealtimeDeltaTicks() {
|
| 119 | return this.realtimeDeltaTicks > 7.0F ? 0.5F : this.realtimeDeltaTicks;
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|