9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-22 16:29:16 +00:00
Files
SakuraMC/patches/server/0017-Replace-explosion-density-cache.patch
Samsuik ca5ff29a0d Updated Upstream (Paper)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@691d452 Fix bundled spark permission check (#11355)
PaperMC/Paper@012c527 Update Velocity natives (#11347)
PaperMC/Paper@953e6e9 Fire BlockExpEvent on grindstone use (#11346)
PaperMC/Paper@10f5879 Change condition check order of entity tracking Y (#11348)
PaperMC/Paper@805a974 Improve console completion with brig suggestions (#9251)
PaperMC/Paper@e0021b1 Fix allowSpiderWorldBorderClimbing world config (#11321)
PaperMC/Paper@3db4758 Check dead flag in isAlive() (#11330)
PaperMC/Paper@21f125f Revert velocity natives to 3.1.2 (#11368)
PaperMC/Paper@0e82527 Fix NPE while trying to respawn an already disconnected player (#11353)
PaperMC/Paper@5d91bef Fix shulkerbox loot table replenish (#11366)
PaperMC/Paper@a8e6a93 Deprecate for removal all OldEnum-related methods (#11371)
PaperMC/Paper@925c3b9 Add FeatureFlag API (#8952)
PaperMC/Paper@426f992 Enchantment is data-driven, so not FeatureDependant (#11377)
PaperMC/Paper@1ba1be7 Update Velocity natives again
PaperMC/Paper@7632de5 Tag Lifecycle Events (#10993)
PaperMC/Paper@b09eaf2 Add Item serialization as json api (#11235)
PaperMC/Paper@971a7a5 Add Decorated Pot Cracked API (#11365)
PaperMC/Paper@61fe23c deprecate isEnabledByFeature in Item/BlockType
PaperMC/Paper@e945cfe Fix PaperServerListPingEvent#getPlayerSample not being populated or used (#11387)
PaperMC/Paper@4ff58c4 Update spark
PaperMC/Paper@d1a72ea Updated Upstream (Bukkit/CraftBukkit/Spigot) (#11405)
PaperMC/Paper@0a53f1d Set default drop behavior for player deaths (#11380)
PaperMC/Paper@951e7dd Fix TrialSpawner forgetting assigned mob when placed by player (#11381)
PaperMC/Paper@13a2395 Fix enable-player-collisions playing sounds when set to false (#11390)
PaperMC/Paper@1348e44 Prevent NPE when serializing unresolved profile (#11407)
PaperMC/Paper@2aaf436 Validate slot in PlayerInventory#setSlot (#11399)
PaperMC/Paper@5c82955 Only mark decorations dirty if a removal actually occurs (#11413)
PaperMC/Paper@c5a1066 Remove wall-time / unused skip tick protection (#11412)
2024-09-22 20:38:14 +01:00

286 lines
15 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/src/main/java/me/samsuik/sakura/explosion/density/BlockDensityCache.java b/src/main/java/me/samsuik/sakura/explosion/density/BlockDensityCache.java
new file mode 100644
index 0000000000000000000000000000000000000000..35454e122e87892099226ba8fd8d444664be9037
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/explosion/density/BlockDensityCache.java
@@ -0,0 +1,62 @@
+package me.samsuik.sakura.explosion.density;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import net.minecraft.util.Mth;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.phys.Vec3;
+
+/**
+ * This is a replacement for papers explosion density cache to be more lenient and efficient.
+ */
+public final class BlockDensityCache {
+ public static final float UNKNOWN_DENSITY = -1.0f;
+
+ private final Int2ObjectOpenHashMap<DensityData> densityDataMap = new Int2ObjectOpenHashMap<>();
+ private DensityData data;
+ private int key;
+ private boolean knownSource;
+
+ public float getDensity(Vec3 explosion, Entity entity) {
+ int key = getKey(explosion, entity);
+ DensityData data = this.densityDataMap.get(key);
+
+ if (data != null && data.hasPosition(explosion, entity.getBoundingBox())) {
+ return data.density();
+ } else {
+ this.knownSource = data != null && data.complete() && data.isExplosionPosition(explosion);
+ this.data = data;
+ this.key = key;
+ return UNKNOWN_DENSITY;
+ }
+ }
+
+ public float getKnownDensity(Vec3 point) {
+ if (this.knownSource && this.data.isKnownPosition(point)) {
+ return this.data.density();
+ } else {
+ return UNKNOWN_DENSITY;
+ }
+ }
+
+ public void putDensity(Vec3 explosion, Entity entity, float density) {
+ if (this.data == null || !this.data.complete()) {
+ this.densityDataMap.put(this.key, new DensityData(explosion, entity, density));
+ } else if (this.data.density() == density) {
+ this.data.expand(explosion, entity);
+ }
+ }
+
+ public void invalidate() {
+ this.densityDataMap.clear();
+ }
+
+ private static int getKey(Vec3 explosion, Entity entity) {
+ int key = Mth.floor(explosion.x());
+ key = 31 * key + Mth.floor(explosion.y());
+ key = 31 * key + Mth.floor(explosion.z());
+ key = 31 * key + entity.getBlockX();
+ key = 31 * key + entity.getBlockY();
+ key = 31 * key + entity.getBlockZ();
+ return key;
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/explosion/density/DensityData.java b/src/main/java/me/samsuik/sakura/explosion/density/DensityData.java
new file mode 100644
index 0000000000000000000000000000000000000000..d7e24638f07f243502004970ab4ce646cbe1e436
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/explosion/density/DensityData.java
@@ -0,0 +1,47 @@
+package me.samsuik.sakura.explosion.density;
+
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.phys.AABB;
+import net.minecraft.world.phys.Vec3;
+
+public final class DensityData {
+ private AABB source;
+ private AABB known;
+ private AABB entity;
+ private final float density;
+ private final boolean complete;
+
+ public DensityData(Vec3 explosion, Entity entity, float density) {
+ this.source = new AABB(explosion, explosion);
+ this.known = new AABB(entity.position(), entity.position());
+ this.entity = entity.getBoundingBox();
+ this.density = density;
+ this.complete = Math.abs(density - 0.5f) == 0.5f;
+ }
+
+ public float density() {
+ return this.density;
+ }
+
+ public boolean complete() {
+ return this.complete;
+ }
+
+ public boolean hasPosition(Vec3 explosion, AABB entity) {
+ return this.isExplosionPosition(explosion) && this.entity.isAABBInBounds(entity);
+ }
+
+ public boolean isKnownPosition(Vec3 point) {
+ return this.entity.isVec3InBounds(point);
+ }
+
+ public boolean isExplosionPosition(Vec3 explosion) {
+ return this.source.isVec3InBounds(explosion);
+ }
+
+ public void expand(Vec3 explosion, Entity entity) {
+ this.source = this.source.expand(explosion);
+ this.known = this.known.expand(entity.position());
+ this.entity = this.entity.minmax(entity.getBoundingBox());
+ }
+}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 012bec9a32acc26f65c8efbfde341b0d15a4e822..8041737aa751bec1c51ee3d9dacd6dfb2b845265 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1823,6 +1823,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
worldserver.explosionDensityCache.clear(); // Paper - Optimize explosions
worldserver.localConfig().expire(currentTick); // Sakura - add local config
worldserver.mergeHandler.expire(currentTick); // Sakura - merge cannon entities
+ worldserver.densityCache.invalidate(); // Sakura - explosion density cache
}
this.isIteratingOverLevels = false; // Paper - Throw exception on world create while being ticked
diff --git a/src/main/java/net/minecraft/world/level/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java
index 80bd249e9572fd0c9ebf53c4731ed50a65290c74..fd1d9e0a8b55378c9cd06369d1850f97b63ab7d7 100644
--- a/src/main/java/net/minecraft/world/level/Explosion.java
+++ b/src/main/java/net/minecraft/world/level/Explosion.java
@@ -305,7 +305,12 @@ public class 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;
}
}
@@ -411,7 +416,16 @@ public class Explosion {
double d10 = Mth.lerp(d7, axisalignedbb.minZ, axisalignedbb.maxZ);
Vec3 vec3d1 = new Vec3(d8 + d3, d9, d10 + d4);
- if (entity.level().clip(new ClipContext(vec3d1, source, 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(vec3d1);
+ 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(vec3d1, source, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity)).getType();
+ }
+ if (hitResult == HitResult.Type.MISS) {
+ // Sakura end - replace density cache
++i;
}
@@ -801,6 +815,11 @@ public class Explosion {
});
}
+ // Sakura start - explosion density cache
+ if (!this.toBlow.isEmpty() && !this.level.paperConfig().environment.optimizeExplosions) {
+ this.level.densityCache.invalidate();
+ }
+ // Sakura end - explosion density cache
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
@@ -929,14 +948,12 @@ public class Explosion {
}
// Paper start - Optimize explosions
private float getBlockDensity(Vec3 vec3d, Entity entity, ca.spottedleaf.moonrise.patches.collisions.ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos) { // Paper - optimise collisions
- if (!this.level.paperConfig().environment.optimizeExplosions) {
- return this.getSeenFraction(vec3d, entity, blockCache, blockPos); // Paper - optimise collisions
- }
- CacheKey key = new CacheKey(this, entity.getBoundingBox());
- Float blockDensity = this.level.explosionDensityCache.get(key);
- if (blockDensity == null) {
- blockDensity = this.getSeenFraction(vec3d, entity, blockCache, blockPos); // Paper - optimise collisions
- this.level.explosionDensityCache.put(key, blockDensity);
+ // Sakura start - replace density cache
+ float blockDensity = this.level.densityCache.getDensity(vec3d, entity);
+ if (blockDensity == me.samsuik.sakura.explosion.density.BlockDensityCache.UNKNOWN_DENSITY) {
+ blockDensity = this.getSeenFraction(vec3d, entity, blockCache, blockPos); // Paper - optimise explosions;
+ this.level.densityCache.putDensity(vec3d, entity, blockDensity);
+ // Sakura end - replace density cache
}
return blockDensity;
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 186f1be4aca5295ca260f3808d332cb5ed77bc7e..8e12d473bfd7dad4dfda8bba503f475151da0ad1 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -696,6 +696,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl
}
// Paper end - optimise random ticking
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(); // Sakura - explosion density cache
protected Level(WritableLevelData worlddatamutable, ResourceKey<Level> resourcekey, RegistryAccess iregistrycustom, Holder<DimensionType> holder, Supplier<ProfilerFiller> supplier, boolean flag, boolean flag1, long i, int j, org.bukkit.generator.ChunkGenerator gen, org.bukkit.generator.BiomeProvider biomeProvider, org.bukkit.World.Environment env, java.util.function.Function<org.spigotmc.SpigotWorldConfig, io.papermc.paper.configuration.WorldConfiguration> paperWorldConfigCreator, Supplier<me.samsuik.sakura.configuration.WorldConfiguration> sakuraWorldConfigCreator, java.util.concurrent.Executor executor) { // Sakura - sakura configuration files// Paper - create paper world config & Anti-Xray
this.spigotConfig = new org.spigotmc.SpigotWorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData) worlddatamutable).getLevelName()); // Spigot
diff --git a/src/main/java/net/minecraft/world/level/block/BasePressurePlateBlock.java b/src/main/java/net/minecraft/world/level/block/BasePressurePlateBlock.java
index 1aa36b456b1c635d3184aeab70d1d84266e22c4b..afb76b13f404a12a5f5b9ffe72756d31290a9a85 100644
--- a/src/main/java/net/minecraft/world/level/block/BasePressurePlateBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/BasePressurePlateBlock.java
@@ -109,6 +109,11 @@ public abstract class BasePressurePlateBlock extends Block {
if (output != j) {
BlockState iblockdata1 = this.setSignalForState(state, j);
+ // Sakura start - explosion density cache
+ if (!world.paperConfig().environment.optimizeExplosions) {
+ world.densityCache.invalidate();
+ }
+ // Sakura end - explosion density cache
world.setBlock(pos, iblockdata1, 2);
this.updateNeighbours(world, pos);
world.setBlocksDirty(pos, state, iblockdata1);
diff --git a/src/main/java/net/minecraft/world/level/block/TripWireHookBlock.java b/src/main/java/net/minecraft/world/level/block/TripWireHookBlock.java
index 76aca266d3f3222502ff4c196228f08fcd88c5f8..b965d1b38a0a3f08531060039e46913dff3bfd0e 100644
--- a/src/main/java/net/minecraft/world/level/block/TripWireHookBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/TripWireHookBlock.java
@@ -173,6 +173,11 @@ public class TripWireHookBlock extends Block {
blockposition1 = pos.relative(enumdirection, j);
Direction enumdirection1 = enumdirection.getOpposite();
+ // Sakura start - explosion density cache
+ if (!world.paperConfig().environment.optimizeExplosions) {
+ world.densityCache.invalidate();
+ }
+ // Sakura end - explosion density cache
world.setBlock(blockposition1, (BlockState) iblockdata3.setValue(TripWireHookBlock.FACING, enumdirection1), 3);
TripWireHookBlock.notifyNeighbors(block, world, blockposition1, enumdirection1);
TripWireHookBlock.emitState(world, blockposition1, flag4, flag5, flag2, flag3);
diff --git a/src/main/java/net/minecraft/world/phys/AABB.java b/src/main/java/net/minecraft/world/phys/AABB.java
index 29123f3a2f211c08d1a9ccf62ca9bc9822f90111..a63bd7945eef572615b1aa4a4914fa385b25cad2 100644
--- a/src/main/java/net/minecraft/world/phys/AABB.java
+++ b/src/main/java/net/minecraft/world/phys/AABB.java
@@ -508,4 +508,28 @@ public class AABB {
public static AABB ofSize(Vec3 center, double dx, double dy, double dz) {
return new AABB(center.x - dx / 2.0, center.y - dy / 2.0, center.z - dz / 2.0, center.x + dx / 2.0, center.y + dy / 2.0, center.z + dz / 2.0);
}
+
+ // Sakura start - explosion density cache
+ public final boolean isAABBInBounds(AABB bb) {
+ return this.minX <= bb.minX && this.maxX >= bb.maxX
+ && this.minY <= bb.minY && this.maxY >= bb.maxY
+ && this.minZ <= bb.minZ && this.maxZ >= bb.maxZ;
+ }
+
+ public final boolean isVec3InBounds(Vec3 p) {
+ return this.minX <= p.x && this.maxX >= p.x
+ && this.minY <= p.y && this.maxY >= p.y
+ && this.minZ <= p.z && this.maxZ >= p.z;
+ }
+
+ public final AABB expand(Vec3 pos) {
+ double minX = Math.min(this.minX, pos.x);
+ double minY = Math.min(this.minY, pos.y);
+ double minZ = Math.min(this.minZ, pos.z);
+ double maxX = Math.max(this.maxX, pos.x);
+ double maxY = Math.max(this.maxY, pos.y);
+ double maxZ = Math.max(this.maxZ, pos.z);
+ return new AABB(minX, minY, minZ, maxX, maxY, maxZ);
+ }
+ // Sakura end - explosion density cache
}