11package dev .hugeblank .asahi .client .mixin ;
22
3- import dev .hugeblank .asahi .client .EvictingList ;
4- import net .fabricmc .loader .api .FabricLoader ;
3+ import dev .hugeblank .asahi .client .InterpolatedTickProperty ;
4+ import net .minecraft .client .network .ClientPlayNetworkHandler ;
5+ import net .minecraft .client .render .WorldRenderer ;
56import net .minecraft .client .world .ClientWorld ;
7+ import net .minecraft .registry .RegistryKey ;
8+ import net .minecraft .registry .entry .RegistryEntry ;
69import net .minecraft .world .tick .TickManager ;
710import org .spongepowered .asm .mixin .*;
811import org .spongepowered .asm .mixin .injection .At ;
@@ -16,57 +19,28 @@ public abstract class ClientWorldMixin{
1619
1720 @ Shadow @ Final private TickManager tickManager ;
1821
19- @ Shadow private boolean shouldTickTimeOfDay ;
20-
21- @ Unique private final EvictingList <Double > points = new EvictingList <>(10 );
22- @ Unique private double factor = 0D ;
23- @ Unique private double remainder = 0D ;
22+ @ Unique private InterpolatedTickProperty timeProperty ;
23+ @ Unique private InterpolatedTickProperty dayTimeProperty ;
24+ @ Unique private boolean tickTimeOfDay = true ;
2425
26+ @ Inject (at =@ At ("TAIL" ), method = "<init>" )
27+ private void init (ClientPlayNetworkHandler networkHandler , ClientWorld .Properties properties , RegistryKey registryRef , RegistryEntry dimensionType , int loadDistance , int simulationDistance , WorldRenderer worldRenderer , boolean debugWorld , long seed , int seaLevel , CallbackInfo ci ) {
28+ this .timeProperty = new InterpolatedTickProperty ("time" , this .clientWorldProperties ::setTime , this .clientWorldProperties ::getTime , tickManager );
29+ this .dayTimeProperty = new InterpolatedTickProperty ("timeOfDay" , this .clientWorldProperties ::setTimeOfDay , this .clientWorldProperties ::getTimeOfDay , tickManager );
30+ }
2531
26- // Poor man's non-nuclear redirect
27- @ Inject (at =@ At ("HEAD" ), method = "tickTime" , cancellable = true , order = 10000 )
28- private void tickTime (CallbackInfo ci ) {
29- remainder += factor ; // add remainder to factor
30- long increment = (long ) remainder ; // truncate floating value
31- clientWorldProperties .setTime (clientWorldProperties .getTime () + increment );
32- if (this .shouldTickTimeOfDay )
33- clientWorldProperties .setTimeOfDay (clientWorldProperties .getTimeOfDay () + increment );
34- // subtract the incremented integer, preserving the floating point remainder for later
35- remainder -= increment ;
32+ @ Inject (at =@ At ("HEAD" ), method = "tickTime" , cancellable = true )
33+ public void tickTime (CallbackInfo ci ) {
34+ timeProperty .tick ();
35+ if (tickTimeOfDay ) dayTimeProperty .tick ();
3636 ci .cancel ();
3737 }
3838
3939 @ Inject (at =@ At ("HEAD" ), method = "setTime" , cancellable = true , order = 10000 )
4040 public void setTime (long time , long timeOfDay , boolean tickTimeOfDay , CallbackInfo ci ) {
41- float tickRate = tickManager .getTickRate (); // Get the TPS
42- int localDiff = (int ) (time - clientWorldProperties .getTime ());
43- // If the next position is greater than where the cycle would be 60 seconds from now, just snap to the position.
44- // We do this instead of rapidly speeding the cycle up to its true position (i.e. after sleeping).
45- if (Math .abs (localDiff ) >= 60 *tickRate ) {
46- clientWorldProperties .setTime (time );
47- if (tickTimeOfDay || clientWorldProperties .getTimeOfDay () != timeOfDay )
48- // Only snap the time of day when necessary.
49- // (When doDaylightCycle == true, if the time of day is out of sync from the server)
50- clientWorldProperties .setTimeOfDay (timeOfDay );
51- } else {
52- float minMoveFactor = 1f /tickRate ; // Create a minimum move factor, so that the sun never appears frozen.
53- points .add ((double ) (localDiff + tickRate ) / tickRate ); // Project where the sun will be in the next second
54- // Get the weighted average of the last n ticks
55- // (see EvictingList instantiation for n)
56- double avg = 0 , weights = 0 ;
57- int size = points .size ();
58- for (int i = 0 ; i < size ; i ++) {
59- double weight = size - i + 1 ;
60- weight *= weight ;
61- weights += weight ;
62- avg += points .get (i )*weight ;
63- }
64- avg /= weights ;
65- factor = avg < 0 ? Math .min (avg , -minMoveFactor ) : Math .max (avg , minMoveFactor );
66- if (FabricLoader .getInstance ().isDevelopmentEnvironment ())
67- System .out .println ((localDiff < 0 ? "ahead of" : "behind" ) + " server by " + Math .abs (localDiff ) + " ticks. Speed: " + avg );
68- }
69- this .shouldTickTimeOfDay = tickTimeOfDay ;
41+ this .tickTimeOfDay = tickTimeOfDay ;
42+ timeProperty .update (time );
43+ dayTimeProperty .update (timeOfDay );
7044 ci .cancel ();
7145 }
7246}
0 commit comments