2023-09-24 07:10:40 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
|
|
Date: Sat, 23 Sep 2023 22:05:35 -0700
|
|
|
|
Subject: [PATCH] Lag compensation ticks
|
|
|
|
|
|
|
|
Areas affected by lag comepnsation:
|
|
|
|
- Block breaking and destroying
|
|
|
|
- Eating food items
|
|
|
|
|
2024-12-17 12:12:59 +01:00
|
|
|
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
|
2024-12-18 19:09:46 +01:00
|
|
|
index 5649482a8b85056bc009b868e19ca11f21d59fbf..f4fba4e2d12c7ab4b4eb9858cd738a9678a2d203 100644
|
2024-12-17 12:12:59 +01:00
|
|
|
--- a/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/net/minecraft/server/MinecraftServer.java
|
2024-12-17 13:32:46 +01:00
|
|
|
@@ -301,6 +301,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2024-12-17 12:12:59 +01:00
|
|
|
public final io.papermc.paper.configuration.PaperConfigurations paperConfigurations; // Paper - add paper configuration files
|
|
|
|
public boolean isIteratingOverLevels = false; // Paper - Throw exception on world create while being ticked
|
|
|
|
private final Set<String> pluginsBlockingSleep = new java.util.HashSet<>(); // Paper - API to allow/disallow tick sleeping
|
2024-01-23 12:06:27 +01:00
|
|
|
+ public static final long SERVER_INIT = System.nanoTime(); // Paper - Lag compensation
|
2023-09-24 07:10:40 +02:00
|
|
|
|
2024-12-17 12:12:59 +01:00
|
|
|
public static <S extends MinecraftServer> S spin(Function<Thread, S> threadFunction) {
|
2024-12-03 23:21:36 +01:00
|
|
|
ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.init(); // Paper - rewrite data converter system
|
2024-12-18 19:09:46 +01:00
|
|
|
@@ -1561,6 +1562,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2024-12-17 12:12:59 +01:00
|
|
|
for (ServerLevel serverLevel : this.getAllLevels()) {
|
|
|
|
serverLevel.hasPhysicsEvent = org.bukkit.event.block.BlockPhysicsEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper - BlockPhysicsEvent
|
|
|
|
serverLevel.hasEntityMoveEvent = io.papermc.paper.event.entity.EntityMoveEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper - Add EntityMoveEvent
|
|
|
|
+ serverLevel.updateLagCompensationTick(); // Paper - lag compensation
|
|
|
|
profilerFiller.push(() -> serverLevel + " " + serverLevel.dimension().location());
|
|
|
|
/* Drop global time updates
|
|
|
|
if (this.tickCount % 20 == 0) {
|
|
|
|
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
|
2024-12-18 19:09:46 +01:00
|
|
|
index 4d20bda4cba578c47216d450c99389b744a59008..47b7d487467225505e3f20cea92e114331d660fd 100644
|
2024-12-17 12:12:59 +01:00
|
|
|
--- a/net/minecraft/server/level/ServerLevel.java
|
|
|
|
+++ b/net/minecraft/server/level/ServerLevel.java
|
|
|
|
@@ -2362,4 +2362,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
|
|
|
|
return this.server.getPlayerList().getPlayer(uuid);
|
2023-09-24 07:10:40 +02:00
|
|
|
}
|
2024-12-17 12:12:59 +01:00
|
|
|
// Paper end - check global player list where appropriate
|
|
|
|
+
|
2023-09-24 07:10:40 +02:00
|
|
|
+ // Paper start - lag compensation
|
2024-12-17 12:12:59 +01:00
|
|
|
+ private long lagCompensationTick = MinecraftServer.SERVER_INIT;
|
2023-09-24 07:10:40 +02:00
|
|
|
+
|
|
|
|
+ public long getLagCompensationTick() {
|
|
|
|
+ return this.lagCompensationTick;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void updateLagCompensationTick() {
|
2024-12-17 12:12:59 +01:00
|
|
|
+ this.lagCompensationTick = (System.nanoTime() - MinecraftServer.SERVER_INIT) / (java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(50L));
|
2023-09-24 07:10:40 +02:00
|
|
|
+ }
|
|
|
|
+ // Paper end - lag compensation
|
2024-12-17 12:12:59 +01:00
|
|
|
}
|
|
|
|
diff --git a/net/minecraft/server/level/ServerPlayerGameMode.java b/net/minecraft/server/level/ServerPlayerGameMode.java
|
2024-12-18 19:09:46 +01:00
|
|
|
index 9fdb825be2b04a5806bbb3d39948bad0863e7ae5..261445152f741b68f91cd8fc1eac17275ee52427 100644
|
2024-12-17 12:12:59 +01:00
|
|
|
--- a/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
|
|
+++ b/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
|
|
@@ -111,7 +111,7 @@ public class ServerPlayerGameMode {
|
2023-09-24 07:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void tick() {
|
2024-12-18 19:09:46 +01:00
|
|
|
- this.gameTicks = net.minecraft.server.MinecraftServer.currentTick; // CraftBukkit
|
2024-12-17 12:12:59 +01:00
|
|
|
+ this.gameTicks = (int) this.level.getLagCompensationTick(); // Paper - lag compensation
|
2023-09-24 07:10:40 +02:00
|
|
|
if (this.hasDelayedDestroy) {
|
2024-12-17 12:12:59 +01:00
|
|
|
BlockState blockState = this.level.getBlockStateIfLoaded(this.delayedDestroyPos); // Paper - Don't allow digging into unloaded chunks
|
|
|
|
if (blockState == null || blockState.isAir()) { // Paper - Don't allow digging into unloaded chunks
|
|
|
|
diff --git a/net/minecraft/world/entity/LivingEntity.java b/net/minecraft/world/entity/LivingEntity.java
|
2024-12-18 19:09:46 +01:00
|
|
|
index 228ceecd5ab9040dcc6710d1cdd0feda2f901016..884e1f8f72cc31520ba0c54af17be3879918a5d3 100644
|
2024-12-17 12:12:59 +01:00
|
|
|
--- a/net/minecraft/world/entity/LivingEntity.java
|
|
|
|
+++ b/net/minecraft/world/entity/LivingEntity.java
|
|
|
|
@@ -3831,6 +3831,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2024-04-25 21:40:53 +02:00
|
|
|
this.resendPossiblyDesyncedDataValues(java.util.List.of(DATA_LIVING_ENTITY_FLAGS), serverPlayer);
|
2023-09-24 07:10:40 +02:00
|
|
|
}
|
2024-01-15 12:38:39 +01:00
|
|
|
// Paper end - Properly cancel usable items
|
2023-09-24 07:10:40 +02:00
|
|
|
+ // Paper start - lag compensate eating
|
|
|
|
+ protected long eatStartTime;
|
|
|
|
+ protected int totalEatTimeTicks;
|
|
|
|
+ // Paper end - lag compensate eating
|
|
|
|
private void updatingUsingItem() {
|
|
|
|
if (this.isUsingItem()) {
|
|
|
|
if (ItemStack.isSameItem(this.getItemInHand(this.getUsedItemHand()), this.useItem)) {
|
2024-12-17 12:12:59 +01:00
|
|
|
@@ -3844,7 +3848,12 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2023-09-24 07:10:40 +02:00
|
|
|
|
2024-12-17 12:12:59 +01:00
|
|
|
protected void updateUsingItem(ItemStack usingItem) {
|
|
|
|
usingItem.onUseTick(this.level(), this, this.getUseItemRemainingTicks());
|
|
|
|
- if (--this.useItemRemaining == 0 && !this.level().isClientSide && !usingItem.useOnRelease()) {
|
2023-09-24 07:10:40 +02:00
|
|
|
+ // Paper start - lag compensate eating
|
|
|
|
+ // we add 1 to the expected time to avoid lag compensating when we should not
|
2024-10-27 10:26:44 +01:00
|
|
|
+ final boolean shouldLagCompensate = this.useItem.has(DataComponents.FOOD) && this.eatStartTime != -1 && (System.nanoTime() - this.eatStartTime) > ((1L + this.totalEatTimeTicks) * 50L * (1000L * 1000L));
|
2024-12-17 12:12:59 +01:00
|
|
|
+ if ((--this.useItemRemaining == 0 || shouldLagCompensate) && !this.level().isClientSide && !usingItem.useOnRelease()) {
|
2023-09-24 07:10:40 +02:00
|
|
|
+ this.useItemRemaining = 0;
|
2024-01-13 18:34:33 +01:00
|
|
|
+ // Paper end - lag compensate eating
|
2023-09-24 07:10:40 +02:00
|
|
|
this.completeUsingItem();
|
|
|
|
}
|
2024-12-17 12:12:59 +01:00
|
|
|
}
|
|
|
|
@@ -3878,7 +3887,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
|
|
ItemStack itemInHand = this.getItemInHand(hand);
|
|
|
|
if (!itemInHand.isEmpty() && !this.isUsingItem() || forceUpdate) { // Paper - Prevent consuming the wrong itemstack
|
|
|
|
this.useItem = itemInHand;
|
|
|
|
- this.useItemRemaining = itemInHand.getUseDuration(this);
|
2023-09-24 07:10:40 +02:00
|
|
|
+ // Paper start - lag compensate eating
|
2024-12-17 12:12:59 +01:00
|
|
|
+ this.useItemRemaining = this.totalEatTimeTicks = itemInHand.getUseDuration(this);
|
2023-09-24 07:10:40 +02:00
|
|
|
+ this.eatStartTime = System.nanoTime();
|
2024-01-13 18:34:33 +01:00
|
|
|
+ // Paper end - lag compensate eating
|
2023-09-24 07:10:40 +02:00
|
|
|
if (!this.level().isClientSide) {
|
|
|
|
this.setLivingEntityFlag(1, true);
|
|
|
|
this.setLivingEntityFlag(2, hand == InteractionHand.OFF_HAND);
|
2024-12-17 12:12:59 +01:00
|
|
|
@@ -3902,7 +3914,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2023-09-24 07:10:40 +02:00
|
|
|
}
|
|
|
|
} else if (!this.isUsingItem() && !this.useItem.isEmpty()) {
|
|
|
|
this.useItem = ItemStack.EMPTY;
|
|
|
|
- this.useItemRemaining = 0;
|
|
|
|
+ // Paper start - lag compensate eating
|
|
|
|
+ this.useItemRemaining = this.totalEatTimeTicks = 0;
|
|
|
|
+ this.eatStartTime = -1L;
|
2024-01-13 18:34:33 +01:00
|
|
|
+ // Paper end - lag compensate eating
|
2023-09-24 07:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
2024-12-17 12:12:59 +01:00
|
|
|
}
|
|
|
|
@@ -4026,7 +4041,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2023-09-24 07:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.useItem = ItemStack.EMPTY;
|
|
|
|
- this.useItemRemaining = 0;
|
|
|
|
+ // Paper start - lag compensate eating
|
|
|
|
+ this.useItemRemaining = this.totalEatTimeTicks = 0;
|
|
|
|
+ this.eatStartTime = -1L;
|
2024-01-13 18:34:33 +01:00
|
|
|
+ // Paper end - lag compensate eating
|
2023-09-24 07:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBlocking() {
|