PaperMC/paper-server/patches/features/0015-optimize-dirt-and-snow-spreading.patch

79 lines
5.2 KiB
Diff
Raw Normal View History

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: lukas81298 <lukas81298@gommehd.net>
Date: Fri, 22 Jan 2021 21:50:18 +0100
Subject: [PATCH] optimize dirt and snow spreading
2024-12-16 10:43:57 +01:00
diff --git a/net/minecraft/world/level/block/SpreadingSnowyDirtBlock.java b/net/minecraft/world/level/block/SpreadingSnowyDirtBlock.java
index 11937aa74efe08bdbd66a619c7a825f91d971afd..722f2b9a24679e0fc67aae2cd27051f96f962efe 100644
--- a/net/minecraft/world/level/block/SpreadingSnowyDirtBlock.java
+++ b/net/minecraft/world/level/block/SpreadingSnowyDirtBlock.java
@@ -17,8 +17,13 @@ public abstract class SpreadingSnowyDirtBlock extends SnowyDirtBlock {
}
2024-12-16 10:43:57 +01:00
private static boolean canBeGrass(BlockState state, LevelReader levelReader, BlockPos pos) {
+ // Paper start - Perf: optimize dirt and snow spreading
+ return canBeGrass(levelReader.getChunk(pos), state, levelReader, pos);
+ }
2024-12-16 10:43:57 +01:00
+ private static boolean canBeGrass(net.minecraft.world.level.chunk.ChunkAccess chunk, BlockState state, LevelReader levelReader, BlockPos pos) {
+ // Paper end - Perf: optimize dirt and snow spreading
BlockPos blockPos = pos.above();
- BlockState blockState = levelReader.getBlockState(blockPos);
+ BlockState blockState = chunk.getBlockState(blockPos); // Paper - Perf: optimize dirt and snow spreading
if (blockState.is(Blocks.SNOW) && blockState.getValue(SnowLayerBlock.LAYERS) == 1) {
return true;
2024-12-16 10:43:57 +01:00
} else if (blockState.getFluidState().getAmount() == 8) {
@@ -33,14 +38,27 @@ public abstract class SpreadingSnowyDirtBlock extends SnowyDirtBlock {
2023-12-06 17:21:56 +01:00
protected abstract MapCodec<? extends SpreadingSnowyDirtBlock> codec();
2024-12-16 10:43:57 +01:00
private static boolean canPropagate(BlockState state, LevelReader level, BlockPos pos) {
+ // Paper start - Perf: optimize dirt and snow spreading
+ return canPropagate(level.getChunk(pos), state, level, pos);
+ }
+
2024-12-16 10:43:57 +01:00
+ private static boolean canPropagate(net.minecraft.world.level.chunk.ChunkAccess chunk, BlockState state, LevelReader level, BlockPos pos) {
+ // Paper end - Perf: optimize dirt and snow spreading
BlockPos blockPos = pos.above();
- return canBeGrass(state, level, pos) && !level.getFluidState(blockPos).is(FluidTags.WATER);
+ return canBeGrass(chunk, state, level, pos) && !chunk.getFluidState(blockPos).is(FluidTags.WATER); // Paper - Perf: optimize dirt and snow spreading
}
@Override
2024-12-16 10:43:57 +01:00
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (this instanceof GrassBlock && level.paperConfig().tickRates.grassSpread != 1 && (level.paperConfig().tickRates.grassSpread < 1 || (net.minecraft.server.MinecraftServer.currentTick + pos.hashCode()) % level.paperConfig().tickRates.grassSpread != 0)) { return; } // Paper - Configurable random tick rates for blocks
- if (!canBeGrass(state, level, pos)) {
+ // Paper start - Perf: optimize dirt and snow spreading
2024-12-16 10:43:57 +01:00
+ final net.minecraft.world.level.chunk.ChunkAccess cachedBlockChunk = level.getChunkIfLoaded(pos);
+ if (cachedBlockChunk == null) { // Is this needed?
+ return;
+ }
2024-12-16 10:43:57 +01:00
+
+ if (!canBeGrass(cachedBlockChunk, state, level, pos)) {
+ // Paper end - Perf: optimize dirt and snow spreading
// CraftBukkit start
2024-12-16 10:43:57 +01:00
if (org.bukkit.craftbukkit.event.CraftEventFactory.callBlockFadeEvent(level, pos, Blocks.DIRT.defaultBlockState()).isCancelled()) {
return;
2024-12-16 10:43:57 +01:00
@@ -53,8 +71,20 @@ public abstract class SpreadingSnowyDirtBlock extends SnowyDirtBlock {
2024-12-03 20:34:55 +01:00
2024-12-16 10:43:57 +01:00
for (int i = 0; i < 4; i++) {
BlockPos blockPos = pos.offset(random.nextInt(3) - 1, random.nextInt(5) - 3, random.nextInt(3) - 1);
- if (level.getBlockState(blockPos).is(Blocks.DIRT) && canPropagate(blockState, level, blockPos)) {
- org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockSpreadEvent(level, pos, blockPos, blockState.setValue(SNOWY, Boolean.valueOf(isSnowySetting(level.getBlockState(blockPos.above()))))); // CraftBukkit
+ // Paper start - Perf: optimize dirt and snow spreading
2024-12-16 10:43:57 +01:00
+ if (pos.getX() == blockPos.getX() && pos.getY() == blockPos.getY() && pos.getZ() == blockPos.getZ()) {
+ continue;
+ }
2024-12-03 20:34:55 +01:00
+
+ final net.minecraft.world.level.chunk.ChunkAccess access;
2024-12-16 10:43:57 +01:00
+ if (cachedBlockChunk.locX == blockPos.getX() >> 4 && cachedBlockChunk.locZ == blockPos.getZ() >> 4) {
+ access = cachedBlockChunk;
+ } else {
2024-12-16 10:43:57 +01:00
+ access = level.getChunkAt(blockPos);
+ }
2024-12-16 10:43:57 +01:00
+ if (access.getBlockState(blockPos).is(Blocks.DIRT) && SpreadingSnowyDirtBlock.canPropagate(access, blockState, level, blockPos)) {
+ org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockSpreadEvent(level, pos, blockPos, (BlockState) blockState.setValue(SpreadingSnowyDirtBlock.SNOWY, isSnowySetting(access.getBlockState(blockPos.above())))); // CraftBukkit
+ // Paper end - Perf: optimize dirt and snow spreading
}
}
}