mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-22 23:29:32 +01:00
131 lines
8 KiB
Diff
131 lines
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
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index 140dd3283eb2bb0e1ea76034f71f9184b9761789..c1c1763de0ad0376c504946cbfd7bbd5b0cff0fd 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -311,6 +311,9 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
public volatile Thread shutdownThread; // Paper
|
|
public volatile boolean abnormalExit = false; // Paper
|
|
public boolean isIteratingOverLevels = false; // Paper - Throw exception on world create while being ticked
|
|
+ // 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();
|
|
@@ -1685,6 +1688,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
Iterator iterator = this.getAllLevels().iterator(); // Paper - Throw exception on world create while being ticked; move down
|
|
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
|
|
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.hasEntityMoveEvent = io.papermc.paper.event.entity.EntityMoveEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
index 799ffd47bca76dd9a8c19ab5d37d09e82b477717..8a7de73ce83966c81c456d4744b04fd0f886646d 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
@@ -570,6 +570,17 @@ public class ServerLevel extends Level implements WorldGenLevel {
|
|
return player != null && player.level() == this ? player : null;
|
|
}
|
|
// Paper end
|
|
+ // 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 9024cd25faabb4d5aed3681d4389baad6ad7b964..82aabd5234102c3763891ebe3009e75f243e539b 100644
|
|
--- 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
|
|
index 5f0d7448bca32a80d4488a4e9ad87918fedf356c..2a9b9c28e667c5fafe1eeb9bc7a41fa80843bd82 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
@@ -3819,6 +3819,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
this.getEntityData().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)) {
|
|
@@ -3837,7 +3841,12 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
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;
|
|
+ // Paper end - lag compensate eating
|
|
this.completeUsingItem();
|
|
}
|
|
|
|
@@ -3885,7 +3894,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
|
|
|
|
if (!itemstack.isEmpty() && !this.isUsingItem() || forceUpdate) { // Paper use override flag
|
|
this.useItem = itemstack;
|
|
- this.useItemRemaining = itemstack.getUseDuration();
|
|
+ // Paper start - lag compensate eating
|
|
+ this.useItemRemaining = this.totalEatTimeTicks = itemstack.getUseDuration();
|
|
+ this.eatStartTime = System.nanoTime();
|
|
+ // Paper end - lag compensate eating
|
|
if (!this.level().isClientSide) {
|
|
this.setLivingEntityFlag(1, true);
|
|
this.setLivingEntityFlag(2, hand == InteractionHand.OFF_HAND);
|
|
@@ -3910,7 +3922,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
|
|
}
|
|
}
|
|
|
|
@@ -4045,7 +4060,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() {
|