9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-23 16:59:16 +00:00
Files
SakuraMC/sakura-server/minecraft-patches/features/0026-Optimise-check-inside-blocks-and-traverse-blocks.patch

111 lines
6.5 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <kfian294ma4@gmail.com>
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 2304e3e33edfce64b79001d2f70d731da3114d77..d1688f01a9564b5ef4c9e905015a174550cab6ae 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<Entity.Movement> 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,7 +1933,10 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
}
}
- private void checkInsideBlocks(Vec3 vec3, Vec3 vec31, InsideBlockEffectApplier.StepBasedCollector stepBasedCollector, LongSet set) {
+ // Sakura start - optimise check inside blocks
+ private void checkInsideBlocks(Vec3 vec3, Vec3 vec31, InsideBlockEffectApplier.StepBasedCollector stepBasedCollector,
+ LongSet set, 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)) {
@@ -1953,7 +1957,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 353ad0c1db2ee374ac5487539c77b2b067dc7c0a..1cdc68aab80390b5f1fb67c14c0a8aaa64f28250 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 cannon physics
- for (BlockPos blockPos : BlockPos.betweenClosed(boundingBox)) {
+ for (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 (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;
}