9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2026-01-04 15:31:43 +00:00
Files
SakuraMC/sakura-server/minecraft-patches/features/0009-Replace-explosion-density-cache.patch
2025-12-10 10:30:35 +00:00

167 lines
9.9 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <kfian294ma4@gmail.com>
Date: Mon, 22 Apr 2024 23:01:26 +0100
Subject: [PATCH] Replace explosion density cache
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
index a42a0672d56e32baaa594c9c1b9db0e7487e7d59..60fc3ff0b7ed04a2f71d14658783d150dd92a680 100644
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -717,6 +717,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
this.getCraftServer().addWorld(this.getWorld()); // CraftBukkit
this.levelTickScheduler.repeatingTask(this.explosionPositions::clear, 0); // Sakura - client visibility settings
this.levelTickScheduler.repeatingTask(this.mergeHandler::expire, 200); // Sakura - merge cannon entities
+ this.levelTickScheduler.repeatingTask(this.densityCache::expire, 0); // Sakura - explosion density cache
}
// Paper start
diff --git a/net/minecraft/world/level/Level.java b/net/minecraft/world/level/Level.java
index 294f9148a54c5e0bd00e2ebe3943f216f582ff00..d501e9cca821c20f6ce2366538d95b794c81ef00 100644
--- a/net/minecraft/world/level/Level.java
+++ b/net/minecraft/world/level/Level.java
@@ -835,6 +835,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl
public final me.samsuik.sakura.block_change.BlockStateChangeTracker blockStateChangeTracker = new me.samsuik.sakura.block_change.BlockStateChangeTracker(this);
// Sakura end - track block changes and tick scheduler
public final me.samsuik.sakura.entity.merge.EntityMergeHandler mergeHandler = new me.samsuik.sakura.entity.merge.EntityMergeHandler(); // Sakura - merge cannon entities
+ public final me.samsuik.sakura.explosion.density.BlockDensityCache densityCache = new me.samsuik.sakura.explosion.density.BlockDensityCache(this); // Sakura - optimise explosion density cache
protected Level(
WritableLevelData levelData,
diff --git a/net/minecraft/world/level/ServerExplosion.java b/net/minecraft/world/level/ServerExplosion.java
index d93a6ed05a09655704e70a7d7ea75ac44e5942e1..950ac55b64ae86a22cfdad6746460283cd6d35aa 100644
--- a/net/minecraft/world/level/ServerExplosion.java
+++ b/net/minecraft/world/level/ServerExplosion.java
@@ -296,7 +296,12 @@ public class ServerExplosion implements Explosion {
Math.fma(dz, diffZ, offZ)
);
- if (!this.clipsAnything(from, source, context, blockCache, blockPos)) {
+ // Sakura start - replace density cache
+ final float density = this.level.densityCache.getKnownDensity(from);
+ if (density != me.samsuik.sakura.explosion.density.BlockDensityCache.UNKNOWN_DENSITY) {
+ missedRays += (int) density;
+ } else if (!this.clipsAnything(from, source, context, blockCache, blockPos)) {
+ // Sakura end - replace density cache
++missedRays;
}
}
@@ -354,8 +359,16 @@ public class ServerExplosion implements Explosion {
double d9 = Mth.lerp(d6, boundingBox.minY, boundingBox.maxY);
double d10 = Mth.lerp(d7, boundingBox.minZ, boundingBox.maxZ);
Vec3 vec3 = new Vec3(d8 + d3, d9, d10 + d4);
- if (entity.level().clip(new ClipContext(vec3, explosionVector, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity)).getType()
- == HitResult.Type.MISS) {
+ // Sakura start - replace density cache
+ final net.minecraft.world.phys.HitResult.Type hitResult;
+ final float density = entity.level().densityCache.getKnownDensity(vec3);
+ if (density != me.samsuik.sakura.explosion.density.BlockDensityCache.UNKNOWN_DENSITY) {
+ hitResult = density != 0.0f ? net.minecraft.world.phys.HitResult.Type.MISS : net.minecraft.world.phys.HitResult.Type.BLOCK;
+ } else {
+ hitResult = entity.level().clip(new ClipContext(vec3, explosionVector, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity)).getType();
+ }
+ if (hitResult == HitResult.Type.MISS) {
+ // Sakura end - replace density cache
i++;
}
@@ -597,6 +610,11 @@ public class ServerExplosion implements Explosion {
return;
}
// CraftBukkit end
+ // Sakura start - explosion density cache
+ if (!blocks.isEmpty() && !this.level.paperConfig().environment.optimizeExplosions) {
+ this.level.densityCache.invalidate();
+ }
+ // Sakura end - explosion density cache
for (BlockPos blockPos : blocks) {
// CraftBukkit start - TNTPrimeEvent
@@ -765,14 +783,12 @@ public class ServerExplosion implements Explosion {
// Paper start - Optimize explosions
protected float getBlockDensity(Vec3 vec3d, Entity entity) {
- if (!this.level.paperConfig().environment.optimizeExplosions) {
- return this.getSeenFraction(vec3d, entity, this.directMappedBlockCache, this.mutablePos); // Paper - collision optimisations
- }
- CacheKey key = new CacheKey(this, entity.getBoundingBox());
- Float blockDensity = this.level.explosionDensityCache.get(key);
- if (blockDensity == null) {
+ // Sakura start - replace density cache
+ float blockDensity = this.level.densityCache.getBlockDensity(vec3d, entity);
+ if (blockDensity == me.samsuik.sakura.explosion.density.BlockDensityCache.UNKNOWN_DENSITY) {
blockDensity = this.getSeenFraction(vec3d, entity, this.directMappedBlockCache, this.mutablePos); // Paper - collision optimisations
- this.level.explosionDensityCache.put(key, blockDensity);
+ this.level.densityCache.putDensity(vec3d, entity, blockDensity);
+ // Sakura end - replace density cache
}
return blockDensity;
diff --git a/net/minecraft/world/level/block/BasePressurePlateBlock.java b/net/minecraft/world/level/block/BasePressurePlateBlock.java
index 6e6d5deea89be952f9ab2743340a165ed114827a..2af0ea3bda6688ac51124129f0e463c38c667376 100644
--- a/net/minecraft/world/level/block/BasePressurePlateBlock.java
+++ b/net/minecraft/world/level/block/BasePressurePlateBlock.java
@@ -107,6 +107,11 @@ public abstract class BasePressurePlateBlock extends Block {
// CraftBukkit end
if (currentSignal != signalStrength) {
BlockState blockState = this.setSignalForState(state, signalStrength);
+ // Sakura start - explosion density cache
+ if (!level.paperConfig().environment.optimizeExplosions) {
+ level.densityCache.invalidate();
+ }
+ // Sakura end - explosion density cache
level.setBlock(pos, blockState, Block.UPDATE_CLIENTS);
this.updateNeighbours(level, pos);
level.setBlocksDirty(pos, state, blockState);
diff --git a/net/minecraft/world/level/block/TripWireHookBlock.java b/net/minecraft/world/level/block/TripWireHookBlock.java
index 9d53b18c530f19c333dbf3e3ef8beeb8c5c49418..b4ee0485467301cf5e1659cd27efe50362375d59 100644
--- a/net/minecraft/world/level/block/TripWireHookBlock.java
+++ b/net/minecraft/world/level/block/TripWireHookBlock.java
@@ -168,6 +168,11 @@ public class TripWireHookBlock extends Block {
if (!cancelledReceiverHook) { // always trigger two events even when the first hook current change is cancelled
// Paper end - Call BlockRedstoneEvent
Direction opposite = direction.getOpposite();
+ // Sakura start - explosion density cache
+ if (!level.paperConfig().environment.optimizeExplosions) {
+ level.densityCache.invalidate();
+ }
+ // Sakura end - explosion density cache
level.setBlock(blockPosx, blockState1.setValue(FACING, opposite), Block.UPDATE_ALL);
notifyNeighbors(block, level, blockPosx, opposite);
emitState(level, blockPosx, flag2, flag3, flag, flag1);
diff --git a/net/minecraft/world/phys/AABB.java b/net/minecraft/world/phys/AABB.java
index cffb3b7d480f72cdb9ed535f362fa60f30597523..c2aa62ad0485f02ba0e65a9cf882e2cdf7be460b 100644
--- a/net/minecraft/world/phys/AABB.java
+++ b/net/minecraft/world/phys/AABB.java
@@ -19,6 +19,30 @@ public class AABB {
public final double maxY;
public final double maxZ;
+ // Sakura start - explosion density cache
+ public final boolean containsInclusive(final AABB other) {
+ return this.minX <= other.minX && this.maxX >= other.maxX
+ && this.minY <= other.minY && this.maxY >= other.maxY
+ && this.minZ <= other.minZ && this.maxZ >= other.maxZ;
+ }
+
+ public final boolean containsInclusive(final Vec3 pos) {
+ return this.minX <= pos.x && this.maxX >= pos.x
+ && this.minY <= pos.y && this.maxY >= pos.y
+ && this.minZ <= pos.z && this.maxZ >= pos.z;
+ }
+
+ public final AABB expand(final Vec3 pos) {
+ final double minX = Math.min(this.minX, pos.x);
+ final double minY = Math.min(this.minY, pos.y);
+ final double minZ = Math.min(this.minZ, pos.z);
+ final double maxX = Math.max(this.maxX, pos.x);
+ final double maxY = Math.max(this.maxY, pos.y);
+ final double maxZ = Math.max(this.maxZ, pos.z);
+ return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
+ }
+ // Sakura end - explosion density cache
+
public AABB(double x1, double y1, double z1, double x2, double y2, double z2) {
this.minX = Math.min(x1, x2);
this.minY = Math.min(y1, y2);