mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-23 08:59:23 +00:00
* uwu * reorder * Change author * Sync upstream * Lithium: equipment tracking * Faster CraftServer#getworlds list creation Co-Authored-By: Kobe ⑧ <102713261+HaHaWTH@users.noreply.github.com>
220 lines
8.8 KiB
Diff
220 lines
8.8 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
|
|
|
|
Original license: AGPL-3.0
|
|
Original project: https://github.com/snackbag/TT20
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index 9dce5c34f67799285ee395a9c5d0db5ee6319536..6b9b3d366343a0bef76036b2b150691787f93db6 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -1627,6 +1627,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..73b9199b0b5ba2daeda2984a07daefdd49351625
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/config/modules/misc/LagCompensation.java
|
|
@@ -0,0 +1,29 @@
|
|
+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.addCommentRegionBased(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
|