From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Sat, 1 Feb 2025 18:38:26 +0300 Subject: [PATCH] Lag compensation diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java index 18c3599ceb7a1c9dc769af0ad4c3eff9e37e2a76..89aa020832fb9894e057b0f2b1226eb48c8accd1 100644 --- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java @@ -287,6 +287,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop= this.portal.getPortalTransitionTime(level, entity); + return canChangeDimensions && lagCompensation(this.portalTime++, level) >= this.portal.getPortalTransitionTime(level, entity); // DivineMC - Lag compensation } } + // DivineMC start - Lag compensation + private int lagCompensation(int original, ServerLevel world) { + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled || !org.bxteam.divinemc.config.DivineConfig.MiscCategory.portalAcceleration) return original; + if (world.isClientSide()) return original; + + portalTime = portalTime + world.tpsCalculator.applicableMissedTicks(); + return portalTime; + } + // DivineMC end - Lag compensation + @Nullable public TeleportTransition getPortalDestination(ServerLevel level, Entity entity) { return this.portal.getPortalDestination(level, entity, this.entryPosition); diff --git a/net/minecraft/world/entity/item/ItemEntity.java b/net/minecraft/world/entity/item/ItemEntity.java index 5aef1e6cf4f325c330579a268ac5f5540759da92..b5d7e5738ce043fdc08cd4872c9daaf952251b9a 100644 --- a/net/minecraft/world/entity/item/ItemEntity.java +++ b/net/minecraft/world/entity/item/ItemEntity.java @@ -149,8 +149,25 @@ public class ItemEntity extends Entity implements TraceableEntity { } // Paper end - EAR 2 + // DivineMC start - Lag compensation + private void lagCompensation() { + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled || !org.bxteam.divinemc.config.DivineConfig.MiscCategory.pickupAcceleration) return; + if ((this).level().isClientSide()) return; + + if (pickupDelay == 0) return; + + if (pickupDelay - ((ServerLevel) this.level()).tpsCalculator.applicableMissedTicks() <= 0) { + pickupDelay = 0; + return; + } + + pickupDelay = pickupDelay - ((ServerLevel) this.level()).tpsCalculator.applicableMissedTicks(); + } + // DivineMC end - Lag compensation + @Override public void tick() { + lagCompensation(); // DivineMC - Lag compensation if (this.getItem().isEmpty()) { this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause } else { diff --git a/net/minecraft/world/item/Item.java b/net/minecraft/world/item/Item.java index 6702b7199e159b16ecd06c57d87c692d04dbf7dc..1cced6128ad4ea0a00e0c772c34431a8ef689d0f 100644 --- a/net/minecraft/world/item/Item.java +++ b/net/minecraft/world/item/Item.java @@ -286,10 +286,25 @@ public class Item implements FeatureElement, ItemLike { } } + // DivineMC start - Lag compensation + private int lagCompensation(int original, net.minecraft.server.level.ServerLevel level) { + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled || !org.bxteam.divinemc.config.DivineConfig.MiscCategory.eatingAcceleration || original == 0) return original; + return org.bxteam.divinemc.util.tps.TPSUtil.tt20(original, true, level); + } + // DivineMC end - Lag compensation + public int getUseDuration(ItemStack stack, LivingEntity entity) { Consumable consumable = stack.get(DataComponents.CONSUMABLE); if (consumable != null) { - return consumable.consumeTicks(); + // DivineMC start - Lag compensation + int original = consumable.consumeTicks(); + + if (entity.level() instanceof net.minecraft.server.level.ServerLevel serverLevel) { + return lagCompensation(original, serverLevel); + } + + return original; + // DivineMC end - Lag compensation } else { BlocksAttacks blocksAttacks = stack.get(DataComponents.BLOCKS_ATTACKS); return blocksAttacks != null ? 72000 : 0; diff --git a/net/minecraft/world/level/GameRules.java b/net/minecraft/world/level/GameRules.java index 45132e6608b57a30b1b1228ef89879b58d4600ef..a3daac69a79551a4a6a6d215cab3ce6838b2b09e 100644 --- a/net/minecraft/world/level/GameRules.java +++ b/net/minecraft/world/level/GameRules.java @@ -379,8 +379,31 @@ public class GameRules { } public int getInt(GameRules.Key key) { - return this.getRule(key).get(); + return lagCompensation(this.getRule(key).get(), key); // DivineMC - Lag compensation + } + + // DivineMC start - Lag compensation + private final java.util.concurrent.atomic.AtomicReference level = new java.util.concurrent.atomic.AtomicReference<>(); + + private int lagCompensation(int original, GameRules.Key rule) { + net.minecraft.server.level.ServerLevel level = getOrCacheLevel(); + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled || !org.bxteam.divinemc.config.DivineConfig.MiscCategory.randomTickSpeedAcceleration) return original; + if (!(rule == GameRules.RULE_RANDOMTICKING)) return original; + return (int) (original * org.bxteam.divinemc.util.tps.TPSCalculator.MAX_TPS / (float) level.tpsCalculator.getMostAccurateTPS()); + } + + private net.minecraft.server.level.ServerLevel getOrCacheLevel() { + if (level.get() == null) { + for (final net.minecraft.server.level.ServerLevel level : MinecraftServer.getServer().getAllLevels()) { + if (level.getGameRules() == this) { + this.level.set(level); + break; + } + } + } + return level.get(); } + // DivineMC end - Lag compensation public static class BooleanValue extends GameRules.Value { private boolean value; diff --git a/net/minecraft/world/level/block/state/BlockBehaviour.java b/net/minecraft/world/level/block/state/BlockBehaviour.java index a7dadab83644e9555dec4f597fee682be37e85c8..5f6fbc7eaa5522bd5e0692c9c2d280457a284e71 100644 --- a/net/minecraft/world/level/block/state/BlockBehaviour.java +++ b/net/minecraft/world/level/block/state/BlockBehaviour.java @@ -350,13 +350,21 @@ public abstract class BlockBehaviour implements FeatureElement { protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) { } + // DivineMC start - Lag compensation + private float lagCompensation(float original, Player player) { + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled || !org.bxteam.divinemc.config.DivineConfig.MiscCategory.blockBreakingAcceleration) return original; + if (player.level().isClientSide) return original; + return original * org.bxteam.divinemc.util.tps.TPSCalculator.MAX_TPS / (float) ((ServerLevel) player.level()).tpsCalculator.getMostAccurateTPS(); + } + // DivineMC end - Lag compensation + protected float getDestroyProgress(BlockState state, Player player, BlockGetter level, BlockPos pos) { float destroySpeed = state.getDestroySpeed(level, pos); if (destroySpeed == -1.0F) { - return 0.0F; + return lagCompensation(0.0F, player); // DivineMC - Lag compensation } else { int i = player.hasCorrectToolForDrops(state) ? 30 : 100; - return player.getDestroySpeed(state) / destroySpeed / i; + return lagCompensation(player.getDestroySpeed(state) / destroySpeed / i, player); // DivineMC - Lag compensation } } diff --git a/net/minecraft/world/level/chunk/LevelChunk.java b/net/minecraft/world/level/chunk/LevelChunk.java index b03fa199a532ff7e0e14d9ad449ee8308e6e76bc..3a9843c30f685d2e1f0cd54ace5dddfa9e2314fa 100644 --- a/net/minecraft/world/level/chunk/LevelChunk.java +++ b/net/minecraft/world/level/chunk/LevelChunk.java @@ -957,6 +957,19 @@ public class LevelChunk extends ChunkAccess implements DebugValueSource, ca.spot this.ticker = ticker; } + // DivineMC start - Lag compensation + private void lagCompensation(Runnable original) { + original.run(); + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled) return; + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.blockEntityAcceleration) return; + if (LevelChunk.this.level.isClientSide()) return; + + for (int i = 0; i < ((ServerLevel) this.blockEntity.getLevel()).tpsCalculator.applicableMissedTicks(); i++) { + original.run(); + } + } + // DivineMC end - Lag compensation + @Override public void tick() { if (!this.blockEntity.isRemoved() && this.blockEntity.hasLevel()) { @@ -967,7 +980,11 @@ public class LevelChunk extends ChunkAccess implements DebugValueSource, ca.spot profilerFiller.push(this::getType); BlockState blockState = LevelChunk.this.getBlockState(blockPos); if (this.blockEntity.getType().isValid(blockState)) { - this.ticker.tick(LevelChunk.this.level, this.blockEntity.getBlockPos(), blockState, this.blockEntity); + // DivineMC start - Lag compensation + lagCompensation(() -> { + this.ticker.tick(LevelChunk.this.level, this.blockEntity.getBlockPos(), blockState, this.blockEntity); + }); + // DivineMC end - Lag compensation this.loggedInvalidBlockState = false; // Paper start - Remove the Block Entity if it's invalid } else { diff --git a/net/minecraft/world/level/material/LavaFluid.java b/net/minecraft/world/level/material/LavaFluid.java index bbc658abfb2df1b3ee3a31d46c4b9014bb34e0a2..38d4392a09d1d3ccebe602cf2319395217d55649 100644 --- a/net/minecraft/world/level/material/LavaFluid.java +++ b/net/minecraft/world/level/material/LavaFluid.java @@ -188,9 +188,22 @@ public abstract class LavaFluid extends FlowingFluid { return fluidState.getHeight(blockReader, pos) >= 0.44444445F && fluid.is(FluidTags.WATER); } + // DivineMC start - Lag compensation + private int lagCompensation(int original, ServerLevel level) { + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled || !org.bxteam.divinemc.config.DivineConfig.MiscCategory.fluidAcceleration) return original; + return org.bxteam.divinemc.util.tps.TPSUtil.tt20(original, true, level); + } + // DivineMC end - Lag compensation + @Override public int getTickDelay(LevelReader level) { - return level.dimensionType().ultraWarm() ? level.getWorldBorder().world.purpurConfig.lavaSpeedNether : level.getWorldBorder().world.purpurConfig.lavaSpeedNotNether; // Purpur - Make lava flow speed configurable + // DivineMC start - Lag compensation + int original = level.dimensionType().ultraWarm() ? level.getWorldBorder().world.purpurConfig.lavaSpeedNether : level.getWorldBorder().world.purpurConfig.lavaSpeedNotNether; // Purpur - Make lava flow speed configurable + if (level instanceof ServerLevel serverLevel) { + return lagCompensation(original, serverLevel); + } + return original; + // DivineMC end - Lag compensation } @Override diff --git a/net/minecraft/world/level/material/WaterFluid.java b/net/minecraft/world/level/material/WaterFluid.java index b248fe1d66940c05d56fc322df61c52ece72e77f..2a35dcf66dc01e787f9767a90c08a6cb283576e4 100644 --- a/net/minecraft/world/level/material/WaterFluid.java +++ b/net/minecraft/world/level/material/WaterFluid.java @@ -124,8 +124,16 @@ public abstract class WaterFluid extends FlowingFluid { return 1; } + // DivineMC start - Lag compensation + private int lagCompensation(ServerLevel level) { + if (!org.bxteam.divinemc.config.DivineConfig.MiscCategory.lagCompensationEnabled || !org.bxteam.divinemc.config.DivineConfig.MiscCategory.fluidAcceleration) return 5; + return org.bxteam.divinemc.util.tps.TPSUtil.tt20(5, true, level); + } + // DivineMC end - Lag compensation + @Override public int getTickDelay(LevelReader level) { + if (level instanceof ServerLevel serverLevel) return lagCompensation(serverLevel); // DivineMC - Lag compensation return 5; }