9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-29 20:09:17 +00:00
Files
Leaf/patches/server/0134-TT20-Lag-compensation.patch
Kobe ⑧ 6fff957697 TT20 lag compensation (#148)
* TT20 lag compensation

* Move to misc
2024-11-03 11:07:38 -05:00

217 lines
8.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
Date: Mon, 1 Nov 2077 00:00:00 +0800
Subject: [PATCH] TT20 Lag compensation
This patch was ported from project: https://github.com/snackbag/TT20
Project license: AGPL-3.0
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 09e55f62b4cea5b058e04356252f4f56957646b8..279289e0c3dc65fed0ee4b2f5b0ef077ab433d5e 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1628,6 +1628,12 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
this.server.spark.tickStart(); // Paper - spark
new com.destroystokyo.paper.event.server.ServerTickStartEvent(this.tickCount+1).callEvent(); // Paper - Server Tick Events
+ // Leaf start - Lag compensation tick hook
+ if (org.dreeam.leaf.config.modules.misc.LagCompensation.enabled) {
+ org.dreeam.leaf.misc.LagCompensation.TPSCalculator.onTick();
+ }
+ // Leaf end - Lag Compensation tick hook
+
++this.tickCount;
this.tickRateManager.tick();
this.tickChildren(shouldKeepTicking);
diff --git a/src/main/java/net/minecraft/world/level/material/LavaFluid.java b/src/main/java/net/minecraft/world/level/material/LavaFluid.java
index 2d492d849ff73a738dfbcb16507feb89bf19a962..0e70f0b88d67c2da4094fd73998d1bddf835b724 100644
--- a/src/main/java/net/minecraft/world/level/material/LavaFluid.java
+++ b/src/main/java/net/minecraft/world/level/material/LavaFluid.java
@@ -180,7 +180,13 @@ public abstract class LavaFluid extends FlowingFluid {
@Override
public int getTickDelay(LevelReader world) {
- return world.dimensionType().ultraWarm() ? world.getWorldBorder().world.purpurConfig.lavaSpeedNether : world.getWorldBorder().world.purpurConfig.lavaSpeedNotNether; // Purpur
+ // Leaf start - Lag compensation
+ if (org.dreeam.leaf.config.modules.misc.LagCompensation.enabled && org.dreeam.leaf.config.modules.misc.LagCompensation.enableForLava) {
+ return world.dimensionType().ultraWarm() ? org.dreeam.leaf.misc.LagCompensation.tt20(world.getWorldBorder().world.purpurConfig.lavaSpeedNether, true) : org.dreeam.leaf.misc.LagCompensation.tt20(world.getWorldBorder().world.purpurConfig.lavaSpeedNotNether, true); // Purpur // Leaf
+ } else {
+ return world.dimensionType().ultraWarm() ? world.getWorldBorder().world.purpurConfig.lavaSpeedNether : world.getWorldBorder().world.purpurConfig.lavaSpeedNotNether; // Purpur
+ }
+ // Leaf end - Lag compensation
}
@Override
diff --git a/src/main/java/net/minecraft/world/level/material/WaterFluid.java b/src/main/java/net/minecraft/world/level/material/WaterFluid.java
index 9dcdb2f4001115db0c26fdbf86531dbe6098485d..53a28a4026c50a66e53241ffe660b4d72600db39 100644
--- a/src/main/java/net/minecraft/world/level/material/WaterFluid.java
+++ b/src/main/java/net/minecraft/world/level/material/WaterFluid.java
@@ -122,7 +122,13 @@ public abstract class WaterFluid extends FlowingFluid {
@Override
public int getTickDelay(LevelReader world) {
- return 5;
+ // Leaf start - Lag compensation
+ if (org.dreeam.leaf.config.modules.misc.LagCompensation.enabled && org.dreeam.leaf.config.modules.misc.LagCompensation.enableForWater) {
+ return org.dreeam.leaf.misc.LagCompensation.tt20(5, true);
+ } else {
+ return 5;
+ }
+ // Leaf end - Lag compensation
}
@Override
diff --git a/src/main/java/org/dreeam/leaf/config/modules/misc/LagCompensation.java b/src/main/java/org/dreeam/leaf/config/modules/misc/LagCompensation.java
new file mode 100644
index 0000000000000000000000000000000000000000..d90ab4cb8c69318f8bf77af12e2840fb5d519931
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/config/modules/misc/LagCompensation.java
@@ -0,0 +1,26 @@
+package org.dreeam.leaf.config.modules.misc;
+
+import org.dreeam.leaf.config.ConfigModules;
+import org.dreeam.leaf.config.EnumConfigCategory;
+
+public class LagCompensation extends ConfigModules {
+
+ public String getBasePath() {
+ return EnumConfigCategory.MISC.getBaseKeyName() + ".lag-compensation";
+ }
+
+ public static boolean enabled = false;
+ public static boolean enableForWater = false;
+ public static boolean enableForLava = false;
+
+ @Override
+ public void onLoaded() {
+ config.addComment(getBasePath(), """
+ This section contains lag compensation features,
+ which could ensure basic playing experience during a lag.""");
+
+ enabled = config.getBoolean(getBasePath() + ".enabled", enabled);
+ enableForWater = config.getBoolean(getBasePath() + ".enable-for-water", enableForWater);
+ enableForLava = config.getBoolean(getBasePath() + ".enable-for-lava", enableForLava);
+ }
+}
diff --git a/src/main/java/org/dreeam/leaf/misc/LagCompensation.java b/src/main/java/org/dreeam/leaf/misc/LagCompensation.java
new file mode 100644
index 0000000000000000000000000000000000000000..6ec73caf7163ed0a66fc2dfec781e190163d06b8
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/misc/LagCompensation.java
@@ -0,0 +1,114 @@
+package org.dreeam.leaf.misc;
+
+import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
+
+import java.util.Collections;
+import java.util.List;
+
+public class LagCompensation {
+
+ public static float tt20(float ticks, boolean limitZero) {
+ float newTicks = (float) rawTT20(ticks);
+
+ if (limitZero) return newTicks > 0 ? newTicks : 1;
+ else return newTicks;
+ }
+
+ public static int tt20(int ticks, boolean limitZero) {
+ int newTicks = (int) Math.ceil(rawTT20(ticks));
+
+ if (limitZero) return newTicks > 0 ? newTicks : 1;
+ else return newTicks;
+ }
+
+ public static double tt20(double ticks, boolean limitZero) {
+ double newTicks = rawTT20(ticks);
+
+ if (limitZero) return newTicks > 0 ? newTicks : 1;
+ else return newTicks;
+ }
+
+ public static double rawTT20(double ticks) {
+ return ticks == 0 ? 0 : ticks * TPSCalculator.getMostAccurateTPS() / TPSCalculator.MAX_TPS;
+ }
+
+ public static class TPSCalculator {
+ public static Long lastTick;
+ public static Long currentTick;
+ private static double allMissedTicks = 0;
+ private static final List<Double> tpsHistory = Collections.synchronizedList(new DoubleArrayList());
+ private static final int historyLimit = 40;
+
+ public static final int MAX_TPS = 20;
+ public static final int FULL_TICK = 50;
+
+ private TPSCalculator() {}
+
+ public static void onTick() {
+ if (currentTick != null) {
+ lastTick = currentTick;
+ }
+
+ currentTick = System.currentTimeMillis();
+ addToHistory(getTPS());
+ clearMissedTicks();
+ missedTick();
+ }
+
+ private static void addToHistory(double tps) {
+ if (tpsHistory.size() >= historyLimit) {
+ tpsHistory.removeFirst();
+ }
+
+ tpsHistory.add(tps);
+ }
+
+ public static long getMSPT() {
+ return currentTick - lastTick;
+ }
+
+ public static double getAverageTPS() {
+ double sum = 0.0;
+ for (double value : tpsHistory) {
+ sum += value;
+ }
+ return tpsHistory.isEmpty() ? 0.1 : sum / tpsHistory.size();
+ }
+
+ public static double getTPS() {
+ if (lastTick == null) return -1;
+ if (getMSPT() <= 0) return 0.1;
+
+ double tps = 1000 / (double) getMSPT();
+ return tps > MAX_TPS ? MAX_TPS : tps;
+ }
+
+ public static void missedTick() {
+ if (lastTick == null) return;
+
+ long mspt = getMSPT() <= 0 ? 50 : getMSPT();
+ double missedTicks = (mspt / (double) FULL_TICK) - 1;
+ allMissedTicks += missedTicks <= 0 ? 0 : missedTicks;
+ }
+
+ public static double getMostAccurateTPS() {
+ return Math.min(getTPS(), getAverageTPS());
+ }
+
+ public double getAllMissedTicks() {
+ return allMissedTicks;
+ }
+
+ public static int applicableMissedTicks() {
+ return (int) Math.floor(allMissedTicks);
+ }
+
+ public static void clearMissedTicks() {
+ allMissedTicks -= applicableMissedTicks();
+ }
+
+ public void resetMissedTicks() {
+ allMissedTicks = 0;
+ }
+ }
+}
\ No newline at end of file