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 0810ca0de7035bc913c63f8c26ccd247d27957be..5c772c05908f59b0f3d8636492685c3b853bcb47 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -1910,6 +1910,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; @@ -1919,12 +1920,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess double d = vec31.get(axis); if (d != 0.0) { Vec3 vec32 = vec3.relative(axis.getPositive(), d); - this.checkInsideBlocks(vec3, vec32, stepBasedCollector, set); + this.checkInsideBlocks(vec3, vec32, stepBasedCollector, set, chunkCache); // Sakura - optimise check inside blocks vec3 = vec32; } } } else { - this.checkInsideBlocks(movement.from(), movement.to(), stepBasedCollector, set); + this.checkInsideBlocks(movement.from(), movement.to(), stepBasedCollector, set, chunkCache); // Sakura - optimise check inside blocks } } @@ -1932,8 +1933,16 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess } } - private void checkInsideBlocks(Vec3 vec3, Vec3 vec31, InsideBlockEffectApplier.StepBasedCollector stepBasedCollector, LongSet set) { - // Sakura start - configure server mechanics + // Sakura start - optimise check inside blocks + private void checkInsideBlocks( + final Vec3 vec3, + final Vec3 vec31, + final InsideBlockEffectApplier.StepBasedCollector stepBasedCollector, + final LongSet set, + final net.minecraft.world.level.chunk.ChunkAccess[] chunkCache + ) { + // Sakura end - optimise check inside blocks + // Sakura start - configure cannon physics final double margin; if (this.mechanicsTarget.atLeast(me.samsuik.sakura.mechanics.MechanicVersion.v1_21_2)) { margin = 1.0e-5f; @@ -1953,7 +1962,20 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess if (!this.isAlive()) { return false; } else { - 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 true; + } + chunkCache[chunkKey] = chunk; + } + final BlockState blockState = chunk.getBlockState(pos); + // Sakura end - optimise check inside blocks if (blockState.isAir()) { this.debugBlockIntersection(pos, false, false); return true; diff --git a/net/minecraft/world/level/BlockGetter.java b/net/minecraft/world/level/BlockGetter.java index b6ddb1ad889a115daeba64321d38b236033f62ff..0d8dda4e914e7570d08f595fd20e5f45e366cd0d 100644 --- a/net/minecraft/world/level/BlockGetter.java +++ b/net/minecraft/world/level/BlockGetter.java @@ -227,7 +227,7 @@ public interface BlockGetter extends LevelHeightAccessor { Vec3 vec3 = to.subtract(from); if (vec3.lengthSqr() < Mth.square(0.99999F) || mechanicsTarget != null && mechanicsTarget.before(me.samsuik.sakura.mechanics.MechanicVersion.v1_21_2)) { // Sakura end - configure server mechanics - for (BlockPos blockPos : BlockPos.betweenClosed(boundingBox)) { + for (final BlockPos blockPos : me.samsuik.sakura.utils.BlockPosIterator.iterable(boundingBox)) { // Sakura - optimise check inside blocks if (!visitor.visit(blockPos, 0)) { return false; } @@ -235,6 +235,20 @@ public interface BlockGetter extends LevelHeightAccessor { return true; } else { + // 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 (final BlockPos blockPos : me.samsuik.sakura.utils.BlockPosIterator.traverseArea(vec3, boundingBox)) { + if (!visitor.visit(blockPos, blockIndex++)) { + return false; + } + } + return true; + } + // Sakura end - optimise check inside blocks LongSet set = new LongOpenHashSet(); Vec3 minPosition = boundingBox.getMinPosition(); Vec3 vec31 = minPosition.subtract(vec3); @@ -242,7 +256,7 @@ public interface BlockGetter extends LevelHeightAccessor { if (i < 0) { return false; } else { - for (BlockPos blockPos1 : BlockPos.betweenClosed(boundingBox)) { + for (final BlockPos blockPos1 : me.samsuik.sakura.utils.BlockPosIterator.iterable(boundingBox)) { // Sakura - optimise check inside blocks if (!set.contains(blockPos1.asLong()) && !visitor.visit(blockPos1, i + 1)) { return false; }