From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samsuik Date: Fri, 8 Nov 2024 19:35:49 +0000 Subject: [PATCH] Optimise check inside blocks and traverse blocks diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java index 6cd8e8bae0c62799fde7533f994eb71bf133fe35..61fc1dafd5af4c3d5a84f8c82b4350f2c311dc96 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -1886,6 +1886,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess private void checkInsideBlocks(List movements, InsideBlockEffectApplier.StepBasedCollector stepBasedCollector) { if (this.isAffectedByBlocks()) { LongSet set = this.visitedBlocks; + final net.minecraft.world.level.chunk.ChunkAccess[] chunkCache = new net.minecraft.world.level.chunk.ChunkAccess[4]; // Sakura - optimise check inside blocks for (Entity.Movement movement : movements) { Vec3 vec3 = movement.from(); @@ -1901,7 +1902,20 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess this.physics, // Sakura - configure cannon physics (pos, step) -> { if (this.isAlive()) { - BlockState blockState = this.level().getBlockState(pos); + // Sakura start - optimise check inside blocks + final int chunkX = pos.getX() >> 4; + final int chunkZ = pos.getZ() >> 4; + final int chunkKey = ((chunkX << 2) | chunkZ) & 3; + net.minecraft.world.level.chunk.ChunkAccess chunk = chunkCache[chunkKey]; + if (chunk == null || chunk.locX != chunkX || chunk.locZ != chunkZ) { + chunk = this.level().getChunkIfLoadedImmediately(chunkX, chunkZ); + if (chunk == null) { + return; + } + chunkCache[chunkKey] = chunk; + } + final BlockState blockState = chunk.getBlockState(pos); + // Sakura end - optimise check inside blocks if (!blockState.isAir()) { if (set.add(pos.asLong())) { VoxelShape entityInsideCollisionShape = blockState.getEntityInsideCollisionShape(this.level(), pos, this); diff --git a/net/minecraft/world/level/BlockGetter.java b/net/minecraft/world/level/BlockGetter.java index 673e07e24c0cc0bc8301a15ca028c0bd72a3439c..11f9181e5c829cff7adf4feef9a6e6942b021850 100644 --- a/net/minecraft/world/level/BlockGetter.java +++ b/net/minecraft/world/level/BlockGetter.java @@ -221,18 +221,30 @@ public interface BlockGetter extends LevelHeightAccessor { Vec3 vec3 = to.subtract(from); if ((physics == null || physics.afterOrEqual(1_21_2)) && !(vec3.lengthSqr() < Mth.square(0.99999F))) { // Sakura end - configure cannon physics + // Sakura start - optimise check inside blocks + final boolean xZero = vec3.x() == 0.0; + final boolean yZero = vec3.y() == 0.0; + final boolean zZero = vec3.z() == 0.0; + if (xZero && yZero || yZero && zZero || xZero && zZero) { + int blockIndex = 0; + for (BlockPos blockPos : me.samsuik.sakura.utils.BlockPosIterator.traverseArea(vec3, boundingBox)) { + stepVisitor.visit(blockPos, blockIndex++); + } + return; + } + // Sakura end - optimise check inside blocks LongSet set = new LongOpenHashSet(); Vec3 minPosition = boundingBox.getMinPosition(); Vec3 vec31 = minPosition.subtract(vec3); int i = addCollisionsAlongTravel(set, vec31, minPosition, boundingBox, stepVisitor); - for (BlockPos blockPos1 : BlockPos.betweenClosed(boundingBox)) { + for (BlockPos blockPos1 : me.samsuik.sakura.utils.BlockPosIterator.iterable(boundingBox)) { // Sakura - optimise check inside blocks if (!set.contains(blockPos1.asLong())) { stepVisitor.visit(blockPos1, i + 1); } } } else { - for (BlockPos blockPos : BlockPos.betweenClosed(boundingBox)) { + for (BlockPos blockPos : me.samsuik.sakura.utils.BlockPosIterator.iterable(boundingBox)) { // Sakura - optimise check inside blocks stepVisitor.visit(blockPos, 0); } }