From 5820061fb510bcf4a66b640458723f835bac31e0 Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 14 May 2019 08:56:54 -0300 Subject: [PATCH] tick rate and warp --- ...in.java => MinecraftServer_coreMixin.java} | 2 +- .../MinecraftServer_tickspeedMixin.java | 105 ++++++++++++++++++ src/main/resources/carpet.mixins.json | 3 +- 3 files changed, 108 insertions(+), 2 deletions(-) rename src/main/java/carpet/mixins/{MinecraftServerMixin.java => MinecraftServer_coreMixin.java} (97%) create mode 100644 src/main/java/carpet/mixins/MinecraftServer_tickspeedMixin.java diff --git a/src/main/java/carpet/mixins/MinecraftServerMixin.java b/src/main/java/carpet/mixins/MinecraftServer_coreMixin.java similarity index 97% rename from src/main/java/carpet/mixins/MinecraftServerMixin.java rename to src/main/java/carpet/mixins/MinecraftServer_coreMixin.java index d2989430..11107bbf 100644 --- a/src/main/java/carpet/mixins/MinecraftServerMixin.java +++ b/src/main/java/carpet/mixins/MinecraftServer_coreMixin.java @@ -19,7 +19,7 @@ import java.net.Proxy; import java.util.function.BooleanSupplier; @Mixin(MinecraftServer.class) -public abstract class MinecraftServerMixin +public abstract class MinecraftServer_coreMixin { @Inject(method = "", at = @At(value = "RETURN")) private void onMinecraftServerCTOR(File file_1, Proxy proxy_1, DataFixer dataFixer_1, diff --git a/src/main/java/carpet/mixins/MinecraftServer_tickspeedMixin.java b/src/main/java/carpet/mixins/MinecraftServer_tickspeedMixin.java new file mode 100644 index 00000000..77b0fdb2 --- /dev/null +++ b/src/main/java/carpet/mixins/MinecraftServer_tickspeedMixin.java @@ -0,0 +1,105 @@ +package carpet.mixins; + +import carpet.helpers.TickSpeed; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.SystemUtil; +import net.minecraft.util.profiler.DisableableProfiler; +import org.apache.logging.log4j.Logger; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.function.BooleanSupplier; + +@Mixin(MinecraftServer.class) +public abstract class MinecraftServer_tickspeedMixin +{ + @Shadow private volatile boolean running; + + @Shadow private long timeReference; + + @Shadow private long field_4557; + + @Shadow @Final private static Logger LOGGER; + + @Shadow private boolean profilerStartQueued; + + @Shadow @Final private DisableableProfiler profiler; + + @Shadow protected abstract void tick(BooleanSupplier booleanSupplier_1); + + @Shadow protected abstract boolean shouldKeepTicking(); + + @Shadow private boolean field_19249; + + @Shadow private long field_19248; + + @Shadow protected abstract void method_16208(); + + @Shadow private volatile boolean loading; + + // Cancel a while statement + @Redirect(method = "run", at = @At(value = "FIELD", target = "Lnet/minecraft/server/MinecraftServer;running:Z")) + private boolean cancelRunLoop(MinecraftServer server) + { + return false; + } + + // Replaced the above cancelled while statement with this one + // could possibly just inject that mspt selection at the beginning of the loop, but then adding all mspt's to + // replace 50L will be a hassle + @Inject(method = "run", at = @At(value = "INVOKE", shift = At.Shift.AFTER, + target = "Lnet/minecraft/server/MinecraftServer;setFavicon(Lnet/minecraft/server/ServerMetadata;)V")) + private void modifiedRunLoop(CallbackInfo ci) + { + while (this.running) + { + //long long_1 = SystemUtil.getMeasuringTimeMs() - this.timeReference; + //CM deciding on tick speed + long mspt = 0L; + long long_1 = 0L; + if (TickSpeed.time_warp_start_time != 0 && TickSpeed.continueWarp()) + { + //making sure server won't flop after the warp or if the warp is interrupted + this.timeReference = this.field_4557 = SystemUtil.getMeasuringTimeMs(); + } + else + { + mspt = TickSpeed.mspt; // regular tick + long_1 = SystemUtil.getMeasuringTimeMs() - this.timeReference; + } + //end tick deciding + //smoothed out delay to include mcpt component. With 50L gives defaults. + if (long_1 > /*2000L*/1000L+20*mspt && this.timeReference - this.field_4557 >= /*15000L*/10000L+100*mspt) + { + long long_2 = long_1 / mspt;//50L; + LOGGER.warn("Can't keep up! Is the server overloaded? Running {}ms or {} ticks behind", long_1, long_2); + this.timeReference += long_2 * mspt;//50L; + this.field_4557 = this.timeReference; + } + + this.timeReference += mspt;//50L; + if (this.profilerStartQueued) + { + this.profilerStartQueued = false; + this.profiler.getController().enable(); + } + + this.profiler.startTick(); + this.profiler.push("tick"); + this.tick(this::shouldKeepTicking); + this.profiler.swap("nextTickWait"); + this.field_19249 = true; + this.field_19248 = Math.max(SystemUtil.getMeasuringTimeMs() + /*50L*/ mspt, this.timeReference); + this.method_16208(); + this.profiler.pop(); + this.profiler.endTick(); + this.loading = true; + } + + } +} diff --git a/src/main/resources/carpet.mixins.json b/src/main/resources/carpet.mixins.json index 88a9c791..a9a23099 100644 --- a/src/main/resources/carpet.mixins.json +++ b/src/main/resources/carpet.mixins.json @@ -4,7 +4,8 @@ "compatibilityLevel": "JAVA_8", "mixins": [ "CommandManagerMixin", - "MinecraftServerMixin", + "MinecraftServer_coreMixin", + "MinecraftServer_tickspeedMixin", "PlayerListHeaderS2CPacketMixin", "WeightedPickerEntryMixin", "ExperienceOrbEntityMixin",