Skip to content

Commit b263f43

Browse files
committed
fix: Add interpolated tick properties for both time and timeOfDay
1 parent 9c1e216 commit b263f43

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dev.hugeblank.asahi.client;
2+
3+
import java.util.function.Consumer;
4+
import java.util.function.Supplier;
5+
6+
public class InterpolatedTickProperty {
7+
private double factor = 0;
8+
private double remainder = 0;
9+
10+
private final Consumer<Long> setProperty;
11+
private final Supplier<Long> getProperty;
12+
13+
public InterpolatedTickProperty(Consumer<Long> setProperty, Supplier<Long> getProperty) {
14+
this.setProperty = setProperty;
15+
this.getProperty = getProperty;
16+
}
17+
18+
public void increment() {
19+
remainder += factor; // add remainder to factor
20+
long increment = (long) remainder; // truncate floating value
21+
this.setProperty.accept(this.getProperty.get() + increment);
22+
// subtract the incremented integer, preserving the floating point remainder for later
23+
remainder -= increment;
24+
}
25+
26+
public void setFactor(double factor) {
27+
this.factor = factor;
28+
}
29+
}

src/main/java/dev/hugeblank/asahi/client/mixin/ClientWorldMixin.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.hugeblank.asahi.client.EvictingList;
44
import dev.hugeblank.asahi.client.TimeSmoother;
5+
import dev.hugeblank.asahi.client.InterpolatedTickProperty;
56
import net.fabricmc.loader.api.FabricLoader;
67
import net.minecraft.client.network.packet.WorldTimeUpdateS2CPacket;
78
import net.minecraft.util.profiler.Profiler;
@@ -23,26 +24,31 @@
2324
public abstract class ClientWorldMixin implements ExtendedBlockView, IWorld, AutoCloseable, TimeSmoother {
2425

2526
@Unique private final EvictingList<Double> points = new EvictingList<>(10);
26-
@Unique private double factor = 0D;
27-
@Unique private double remainder = 0D;
2827
@Unique private long lastPacketTimeOfDay = 0;
2928
@Unique private boolean shouldTickDay = true;
3029

30+
@Unique private InterpolatedTickProperty timeProperty;
31+
@Unique private InterpolatedTickProperty dayTimeProperty;
32+
3133
protected ClientWorldMixin(
3234
LevelProperties levelProperties, DimensionType dimensionType, BiFunction<World, Dimension, ChunkManager> biFunction, Profiler profiler, boolean bl
3335
) {
3436
super();
3537
}
3638

39+
@Inject(at=@At("TAIL"), method = "<init>")
40+
private void init(LevelProperties levelProperties, DimensionType dimensionType, BiFunction biFunction, Profiler profiler, boolean bl, CallbackInfo ci) {
41+
if (this.isClient) {
42+
this.timeProperty = new InterpolatedTickProperty(this::setTime, this::getTime);
43+
this.dayTimeProperty = new InterpolatedTickProperty(this::setTimeOfDay, this::getTimeOfDay);
44+
}
45+
}
46+
3747
@Inject(at=@At("HEAD"), method = "tickTime", cancellable = true)
3848
public void tickTime(CallbackInfo ci) {
3949
if (this.isClient) {
40-
remainder += factor; // add remainder to factor
41-
long increment = (long) remainder; // truncate floating value
42-
this.setTime(this.getTime() + increment);
43-
if (shouldTickDay) this.setTimeOfDay(this.getTimeOfDay() + increment);
44-
// subtract the incremented integer, preserving the floating point remainder for later
45-
remainder -= increment;
50+
timeProperty.increment();
51+
if (shouldTickDay) dayTimeProperty.increment();
4652
ci.cancel();
4753
}
4854
}
@@ -59,8 +65,6 @@ public void tickTime(CallbackInfo ci) {
5965

6066
@Override
6167
public void asahi$updateTimes(WorldTimeUpdateS2CPacket packet) {
62-
final int TPS = 20;
63-
long currentPacketTime = packet.getTime();
6468
if (lastPacketTimeOfDay == packet.getTimeOfDay()) {
6569
if (!shouldTickDay) {
6670
this.setTimeOfDay(packet.getTimeOfDay());
@@ -69,7 +73,9 @@ public void tickTime(CallbackInfo ci) {
6973
} else {
7074
shouldTickDay = true;
7175
}
72-
int localDiff = (int) (currentPacketTime - this.getTime());
76+
77+
final int TPS = 20;
78+
int localDiff = (int) (packet.getTime() - this.getTime());
7379
if (Math.abs(localDiff) >= 60 * TPS) { // SKIP_DURATION
7480
this.setTime(packet.getTime());
7581
if (shouldTickDay) this.setTimeOfDay(packet.getTimeOfDay());
@@ -85,10 +91,13 @@ public void tickTime(CallbackInfo ci) {
8591
avg += points.get(i)*weight;
8692
}
8793
avg /= weights;
94+
double factor = avg < 0 ? Math.min(avg, -minMoveFactor) : Math.max(avg, minMoveFactor);
95+
timeProperty.setFactor(factor);
96+
dayTimeProperty.setFactor(factor);
97+
8898
if (FabricLoader.getInstance().isDevelopmentEnvironment())
89-
System.out.println((localDiff < 0 ? "ahead of" : "behind") + " server by " + Math.abs(localDiff) + " ticks. Speed: " + avg);
90-
factor = avg < 0 ? Math.min(avg, -minMoveFactor) : Math.max(avg, minMoveFactor);
91-
lastPacketTimeOfDay = packet.getTimeOfDay();
99+
System.out.format("%s server by %d ticks. Speed: %f\n", (localDiff < 0 ? "ahead of" : "behind"), Math.abs(localDiff), avg);
92100
}
101+
lastPacketTimeOfDay = packet.getTimeOfDay();
93102
}
94103
}

0 commit comments

Comments
 (0)