Paper/patches/server/0995-Lag-compensation-ticks.patch

132 lines
8.0 KiB
Diff
Raw Normal View History

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 174f3dab7c7f02dd09d02dc8b4e69190284c3f48..ec1197e498d0fa8d58466b8dffe46fb0683b530c 100644
--- 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
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();
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10164) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 63c208dd Remove no longer used import 70be76c7 PR-958: Further clarify deprecation of TAG_CONTAINER_ARRAY ae21f4ac PR-955: Add methods to place structures with block/entity transformers e3d960f2 SPIGOT-7547: Remark that Damageable#setAbsorptionAmount() is capped to a specific value b125516c Fix typo in RecipeChoice.ExactChoice docs 309497c1 Add EntityMountEvent and EntityDismount Event 2fd45ae3 Improve ItemFactory#enchantItem consistency 2b198268 PR-933: Define native persistent data types for lists CraftBukkit Changes: 771182f70 PR-1327: Add methods to place structures with block/entity transformers e41ad4c82 SPIGOT-7567: SpawnReason for SNOWMAN is reported as BUILD_IRONGOLEM 76931e8bd Add EntityMountEvent and EntityDismount Event 9b29b21c7 PR-1183: Better handle lambda expression and renaming of classes in Commodore 1462ebe85 Reformat Commodore.java 9fde4c037 PR-1324: Improve ItemFactory#enchantItem consistency 4e419c774 PR-1295: Define native persistent data types for lists dd8cca388 SPIGOT-7562: Fix Score#getScore and Score#isScoreSet 690278200 Only fetch an online UUID in online mode 1da8d9a53 Fire PreLogin events even in offline mode 2e88514ad PR-1325: Use CraftBlockType and CraftItemType instead of CraftMagicNumbers to convert between minecraft and bukkit block / item representation Spigot Changes: 864e4acc Restore accidentally removed package-info.java f91a10d5 Remove obsolete EntityMountEvent and EntityDismountEvent 828f0593 SPIGOT-7558: Deprecate silenceable lightning API as sound is now client-side and cannot be removed cdc4e035 Remove obsolete patch fetching correct mode UUIDs 49e36b8e Merge related BungeeCord patches 6e87b9ab Remove obsolete firing of PreLogin events in offline mode 5c76b183 Remove redundant patch dealing with exceptions in the crash reporter 3a2219d1 Remove redundant patch logging cause of unexpected exception
2024-01-14 10:46:04 +01:00
@@ -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 - Add EntityMoveEvent
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index ab472d2e81c0756020ffb727db4b71aeb8d9d020..ae6919fb2ed273d5527a6e4e7731c074d201393f 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -565,6 +565,17 @@ public class ServerLevel extends Level implements WorldGenLevel {
return player != null && player.level() == this ? player : null;
}
// Paper end - optimise getPlayerByUUID
+ // 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 52764f0f5b42e3b7fd9624bc342e9927037880d2..6abc5061888d965161de979465bb625c173c59c6 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 629d38851558207f302e9baa0157218d7c8596d7..91d88337f9147637b0e8fa1ac8bf9193b44751f2 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 - Prevent consuming the wrong itemstack
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() {