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


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 {
     }
 
     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);
+    }
+    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;
         } else if (blockState.getFluidState().getAmount() == 8) {
@@ -33,14 +38,27 @@ public abstract class SpreadingSnowyDirtBlock extends SnowyDirtBlock {
     protected abstract MapCodec<? extends SpreadingSnowyDirtBlock> codec();
 
     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);
+    }
+
+    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
     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
+        final net.minecraft.world.level.chunk.ChunkAccess cachedBlockChunk = level.getChunkIfLoaded(pos);
+        if (cachedBlockChunk == null) { // Is this needed?
+            return;
+        }
+
+        if (!canBeGrass(cachedBlockChunk, state, level, pos)) {
+            // Paper end - Perf: optimize dirt and snow spreading
             // CraftBukkit start
             if (org.bukkit.craftbukkit.event.CraftEventFactory.callBlockFadeEvent(level, pos, Blocks.DIRT.defaultBlockState()).isCancelled()) {
                 return;
@@ -53,8 +71,20 @@ public abstract class SpreadingSnowyDirtBlock extends SnowyDirtBlock {
 
                 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
+                    if (pos.getX() == blockPos.getX() && pos.getY() == blockPos.getY() && pos.getZ() == blockPos.getZ()) {
+                        continue;
+                    }
+
+                    final net.minecraft.world.level.chunk.ChunkAccess access;
+                    if (cachedBlockChunk.locX == blockPos.getX() >> 4 && cachedBlockChunk.locZ == blockPos.getZ() >> 4) {
+                        access = cachedBlockChunk;
+                    } else {
+                        access = level.getChunkAt(blockPos);
+                    }
+                    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
                     }
                 }
             }
