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
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2024-01-22 21:04:08 +01:00
|
|
|
index 093d9fa59fce871ba1d592482f7fc6616fd9b1ac..49ab2773658a2c6fababa316707762a8e2b19c58 100644
|
2023-09-24 07:10:40 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2023-12-06 19:18:53 +01:00
|
|
|
@@ -311,6 +311,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2023-09-24 07:10:40 +02:00
|
|
|
public volatile Thread shutdownThread; // Paper
|
|
|
|
public volatile boolean abnormalExit = false; // Paper
|
2024-01-18 18:52:00 +01:00
|
|
|
public boolean isIteratingOverLevels = false; // Paper - Throw exception on world create while being ticked
|
2023-09-24 07:10:40 +02:00
|
|
|
+ // Paper start - lag compensation
|
|
|
|
+ public static final long SERVER_INIT = System.nanoTime();
|
|
|
|
+ // Paper end - lag compensation
|
|
|
|
|
|
|
|
public static <S extends MinecraftServer> S spin(Function<Thread, S> serverFactory) {
|
|
|
|
AtomicReference<S> atomicreference = new AtomicReference();
|
2024-01-14 10:46:04 +01:00
|
|
|
@@ -1685,6 +1688,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2024-01-18 18:52:00 +01:00
|
|
|
Iterator iterator = this.getAllLevels().iterator(); // Paper - Throw exception on world create while being ticked; move down
|
2023-09-24 07:10:40 +02:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
ServerLevel worldserver = (ServerLevel) iterator.next();
|
|
|
|
+ worldserver.updateLagCompensationTick(); // Paper - lag compensation
|
|
|
|
worldserver.hasPhysicsEvent = org.bukkit.event.block.BlockPhysicsEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper
|
2024-01-15 12:38:39 +01:00
|
|
|
net.minecraft.world.level.block.entity.HopperBlockEntity.skipHopperEvents = worldserver.paperConfig().hopper.disableMoveEvent || org.bukkit.event.inventory.InventoryMoveItemEvent.getHandlerList().getRegisteredListeners().length == 0; // Paper - Perf: Optimize Hoppers
|
2024-01-19 22:13:42 +01:00
|
|
|
worldserver.hasEntityMoveEvent = io.papermc.paper.event.entity.EntityMoveEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper - Add EntityMoveEvent
|
2023-09-24 07:10:40 +02:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
2024-01-22 21:04:08 +01:00
|
|
|
index 1f0e9689acc1470a65a3c51505d5a386da7abc8e..ffe910da16c33ebbce77fef441d9d5c2d097ce61 100644
|
2023-09-24 07:10:40 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
2024-01-21 17:39:05 +01:00
|
|
|
@@ -565,6 +565,17 @@ public class ServerLevel extends Level implements WorldGenLevel {
|
2023-09-24 07:10:40 +02:00
|
|
|
return player != null && player.level() == this ? player : null;
|
|
|
|
}
|
2024-01-21 12:53:04 +01:00
|
|
|
// Paper end - optimise getPlayerByUUID
|
2023-09-24 07:10:40 +02:00
|
|
|
+ // Paper start - lag compensation
|
|
|
|
+ private long lagCompensationTick = net.minecraft.server.MinecraftServer.SERVER_INIT;
|
|
|
|
+
|
|
|
|
+ public long getLagCompensationTick() {
|
|
|
|
+ return this.lagCompensationTick;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void updateLagCompensationTick() {
|
|
|
|
+ this.lagCompensationTick = (System.nanoTime() - net.minecraft.server.MinecraftServer.SERVER_INIT) / (java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(50L));
|
|
|
|
+ }
|
|
|
|
+ // Paper end - lag compensation
|
|
|
|
|
|
|
|
// Add env and gen to constructor, IWorldDataServer -> WorldDataServer
|
|
|
|
public ServerLevel(MinecraftServer minecraftserver, Executor executor, LevelStorageSource.LevelStorageAccess convertable_conversionsession, PrimaryLevelData iworlddataserver, ResourceKey<Level> resourcekey, LevelStem worlddimension, ChunkProgressListener worldloadlistener, boolean flag, long i, List<CustomSpawner> list, boolean flag1, @Nullable RandomSequences randomsequences, org.bukkit.World.Environment env, org.bukkit.generator.ChunkGenerator gen, org.bukkit.generator.BiomeProvider biomeProvider) {
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
|
2024-01-22 21:04:08 +01:00
|
|
|
index 83e017efd15d0ecaffc327f02d6d5330c8ed6937..cfd4ac06a9af6bf3fac293110482e1df690e075e 100644
|
2023-09-24 07:10:40 +02:00
|
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
|
|
@@ -124,7 +124,7 @@ public class ServerPlayerGameMode {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void tick() {
|
|
|
|
- this.gameTicks = MinecraftServer.currentTick; // CraftBukkit;
|
|
|
|
+ this.gameTicks = (int)this.level.getLagCompensationTick(); // CraftBukkit; // Paper - lag compensation
|
|
|
|
BlockState iblockdata;
|
|
|
|
|
|
|
|
if (this.hasDelayedDestroy) {
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
2024-01-22 21:04:08 +01:00
|
|
|
index b920f72e659956cc2d2d44743caddb0136d467e4..ca1900d29a86b9b39078da39b071950128c08c23 100644
|
2023-09-24 07:10:40 +02:00
|
|
|
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
2024-01-22 21:04:08 +01:00
|
|
|
@@ -3827,6 +3827,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2023-09-24 07:10:40 +02:00
|
|
|
this.getEntityData().resendPossiblyDesyncedDataValues(java.util.List.of(DATA_LIVING_ENTITY_FLAGS), serverPlayer);
|
|
|
|
}
|
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-01-22 21:04:08 +01:00
|
|
|
@@ -3845,7 +3849,12 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2023-09-24 07:10:40 +02:00
|
|
|
this.triggerItemUseEffects(stack, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
- if (--this.useItemRemaining == 0 && !this.level().isClientSide && !stack.useOnRelease()) {
|
|
|
|
+ // Paper start - lag compensate eating
|
|
|
|
+ // we add 1 to the expected time to avoid lag compensating when we should not
|
|
|
|
+ boolean shouldLagCompensate = this.useItem.getItem().isEdible() && this.eatStartTime != -1 && (System.nanoTime() - this.eatStartTime) > ((1 + this.totalEatTimeTicks) * 50 * (1000 * 1000));
|
|
|
|
+ if ((--this.useItemRemaining == 0 || shouldLagCompensate) && !this.level().isClientSide && !stack.useOnRelease()) {
|
|
|
|
+ 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-01-22 21:04:08 +01:00
|
|
|
@@ -3893,7 +3902,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
2023-09-24 07:10:40 +02:00
|
|
|
|
2024-01-21 12:53:04 +01:00
|
|
|
if (!itemstack.isEmpty() && !this.isUsingItem() || forceUpdate) { // Paper - Prevent consuming the wrong itemstack
|
2023-09-24 07:10:40 +02:00
|
|
|
this.useItem = itemstack;
|
|
|
|
- this.useItemRemaining = itemstack.getUseDuration();
|
|
|
|
+ // Paper start - lag compensate eating
|
|
|
|
+ this.useItemRemaining = this.totalEatTimeTicks = itemstack.getUseDuration();
|
|
|
|
+ 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-01-22 21:04:08 +01:00
|
|
|
@@ -3918,7 +3930,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-01-22 21:04:08 +01:00
|
|
|
@@ -4053,7 +4068,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() {
|