22
33import dev .hugeblank .asahi .client .EvictingList ;
44import dev .hugeblank .asahi .client .TimeSmoother ;
5+ import dev .hugeblank .asahi .client .InterpolatedTickProperty ;
56import net .fabricmc .loader .api .FabricLoader ;
67import net .minecraft .client .network .packet .WorldTimeUpdateS2CPacket ;
78import net .minecraft .util .profiler .Profiler ;
2324public 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