mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-21 18:15:54 +01:00
8c5b837e05
Firstly, the old methods all routed to the CompletableFuture method. However, the CF method could not guarantee that if the caller was off-main that the future would be "completed" on-main. Since the callback methods used the CF one, this meant that the callback methods did not guarantee that the callbacks were to be called on the main thread. Now, all methods route to getChunkAtAsync(x, z, gen, urgent, cb) so that the methods with the callback are guaranteed to invoke the callback on the main thread. The CF behavior remains unchanged; it may still appear to complete on main if invoked off-main. Secondly, remove the scheduleOnMain invocation in the async chunk completion. This unnecessarily delays the callback by 1 tick. Thirdly, add getChunksAtAsync(minX, minZ, maxX, maxZ, ...) which will load chunks within an area. This method is provided as a helper as keeping all chunks loaded within an area can be complicated to implement for plugins (due to the lacking ticket API), and is already implemented internally anyways. Fourthly, remove the ticket addition that occured with getChunkAt and getChunkAtAsync. The ticket addition may delay the unloading of the chunk unnecessarily. It also fixes a very rare timing bug where the future/callback would be completed after the chunk unloads.
132 lines
7.8 KiB
Diff
132 lines
7.8 KiB
Diff
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
|
|
|
|
Feature patch
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index b33b68649e67de08719b30e98650c84f4c3c18d6..e636a96ea6220fda671a31d3d9cdea468a558768 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -331,6 +331,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
|
|
public volatile Thread shutdownThread; // Paper
|
|
public volatile boolean abnormalExit = false; // Paper
|
|
+ public static final long SERVER_INIT = System.nanoTime(); // Paper - Lag compensation
|
|
|
|
public static <S extends MinecraftServer> S spin(Function<Thread, S> serverFactory) {
|
|
AtomicReference<S> atomicreference = new AtomicReference();
|
|
@@ -1841,6 +1842,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
worldserver.hasPhysicsEvent = org.bukkit.event.block.BlockPhysicsEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper - BlockPhysicsEvent
|
|
worldserver.hasEntityMoveEvent = io.papermc.paper.event.entity.EntityMoveEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper - Add EntityMoveEvent
|
|
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
|
|
+ worldserver.updateLagCompensationTick(); // Paper - lag compensation
|
|
|
|
gameprofilerfiller.push(() -> {
|
|
String s = String.valueOf(worldserver);
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
index 5f244cafe6c3cfb4a3bd08f9ae0f1a5aeec9b359..91c99cf68bf07eb81dcb63690365559df41b912e 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
@@ -582,6 +582,17 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
|
|
);
|
|
}
|
|
// Paper end - chunk tick iteration
|
|
+ // 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
|
|
index 504c996220b278c194c93e001a3b326d549868ec..a96f859a5d0c6ec692d4627a69f3c9ee49199dbc 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
@@ -127,7 +127,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
|
|
index 59c992173fda6153c58722caae061b0e6bee86a1..6a3a8f0466998409a01223bc0c16d92b96e50118 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
@@ -4051,6 +4051,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
this.resendPossiblyDesyncedDataValues(java.util.List.of(DATA_LIVING_ENTITY_FLAGS), serverPlayer);
|
|
}
|
|
// Paper end - Properly cancel usable items
|
|
+ // 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)) {
|
|
@@ -4065,7 +4069,12 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
|
|
protected void updateUsingItem(ItemStack stack) {
|
|
stack.onUseTick(this.level(), this, this.getUseItemRemainingTicks());
|
|
- 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
|
|
+ final boolean shouldLagCompensate = this.useItem.has(DataComponents.FOOD) && this.eatStartTime != -1 && (System.nanoTime() - this.eatStartTime) > ((1L + this.totalEatTimeTicks) * 50L * (1000L * 1000L));
|
|
+ if ((--this.useItemRemaining == 0 || shouldLagCompensate) && !this.level().isClientSide && !stack.useOnRelease()) {
|
|
+ this.useItemRemaining = 0;
|
|
+ // Paper end - lag compensate eating
|
|
this.completeUsingItem();
|
|
}
|
|
|
|
@@ -4103,7 +4112,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
|
|
if (!itemstack.isEmpty() && !this.isUsingItem() || forceUpdate) { // Paper - Prevent consuming the wrong itemstack
|
|
this.useItem = itemstack;
|
|
- this.useItemRemaining = itemstack.getUseDuration(this);
|
|
+ // Paper start - lag compensate eating
|
|
+ this.useItemRemaining = this.totalEatTimeTicks = itemstack.getUseDuration(this);
|
|
+ this.eatStartTime = System.nanoTime();
|
|
+ // Paper end - lag compensate eating
|
|
if (!this.level().isClientSide) {
|
|
this.setLivingEntityFlag(1, true);
|
|
this.setLivingEntityFlag(2, hand == InteractionHand.OFF_HAND);
|
|
@@ -4128,7 +4140,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
}
|
|
} 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;
|
|
+ // Paper end - lag compensate eating
|
|
}
|
|
}
|
|
|
|
@@ -4259,7 +4274,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
}
|
|
|
|
this.useItem = ItemStack.EMPTY;
|
|
- this.useItemRemaining = 0;
|
|
+ // Paper start - lag compensate eating
|
|
+ this.useItemRemaining = this.totalEatTimeTicks = 0;
|
|
+ this.eatStartTime = -1L;
|
|
+ // Paper end - lag compensate eating
|
|
}
|
|
|
|
public boolean isBlocking() {
|