9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-27 18:59:06 +00:00
Files
SakuraMC/patches/server/0021-Specialised-Explosions.patch
Samsuik cc574e807d Updated Upstream (Paper 1.20.6)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@abc49bf Begin update to 1.20.5
PaperMC/Paper@f2c45ed do some work
PaperMC/Paper@00c14f2 Move logging patches after adventure
PaperMC/Paper@ef057bb work on Adventure patch
PaperMC/Paper@380c4d2 Add plugin remapping patches
PaperMC/Paper@7cdf804 Work
PaperMC/Paper@dd6ce09 Fix reflection diff and order wrt commodore diff
PaperMC/Paper@598a1cb Apply shadow removal patch
PaperMC/Paper@ffccfd5 Apply timings
PaperMC/Paper@eb41348 33
PaperMC/Paper@9cac5b6 Fix continueServerUpdate with 2-line subjects
PaperMC/Paper@2eeed9e 41
PaperMC/Paper@d07c487 Don't use defaultToolchain method for run tasks
PaperMC/Paper@f2350fe Set mercury sourceCompat
PaperMC/Paper@5cfd92b Fix api compile
PaperMC/Paper@862d004 work on some API stuff
PaperMC/Paper@d6a3c0c fix 2 of our javadoc errors
PaperMC/Paper@487555b update
PaperMC/Paper@0225537 Update upstream
PaperMC/Paper@906f906 Patches!
PaperMC/Paper@8244815 221
PaperMC/Paper@309ebc1 253
PaperMC/Paper@1d7d7e9 301
PaperMC/Paper@9079c90 354
PaperMC/Paper@cc46148 405
PaperMC/Paper@5be858e Patches!
PaperMC/Paper@2d9d0d8 Patches!
PaperMC/Paper@a403a8d Patches!!!!!!!
PaperMC/Paper@21afc0a Patches!!!!!!!
PaperMC/Paper@1de0dc8 666 plus 1
PaperMC/Paper@4e654f2 More patch work
PaperMC/Paper@e832328 Check for existing am session before running continueServerUpdate
PaperMC/Paper@fc5c0da 683
PaperMC/Paper@90095cd 694
PaperMC/Paper@71ae391 more work (721)
PaperMC/Paper@22227cc more more work (757)
PaperMC/Paper@4d9e824 more more work (806)
PaperMC/Paper@9617829 Restore original formatting for MC-252817 patch
PaperMC/Paper@0518559 Update reflection rewriter to 0.0.1 release
PaperMC/Paper@e0a245d Patches!!!!!!!
PaperMC/Paper@1104431 955
PaperMC/Paper@1df7e21 Apply more API-implementing patches
PaperMC/Paper@b733809 Drop conduit api patch (upstream implemented same api with same method signatures)
PaperMC/Paper@c95db4f adventure 4.17.0-SNAPSHOT
PaperMC/Paper@bb1351c some work on adventure
PaperMC/Paper@1444b36 work on adventure
PaperMC/Paper@ce0e78c compile fixes
PaperMC/Paper@25f7c68 compile fixes
PaperMC/Paper@690b1cd compile fixes
PaperMC/Paper@b673721 compile fixes
PaperMC/Paper@6a87d14 dataconverter
PaperMC/Paper@ec9f7e3 starlight
PaperMC/Paper@e9eec78 chunk system
PaperMC/Paper@f6ea373 Patches
PaperMC/Paper@ec4ada8 Apply and move up non-optimization patches
PaperMC/Paper@c9907c6 More patches
PaperMC/Paper@2debcaf More more patches, including hopper optimization patch
PaperMC/Paper@6da0d8c (Almost) all patches applied
PaperMC/Paper@345b5c1 Patches!!!!!!! (we are done)
PaperMC/Paper@4590038 Fix properly resend patches
PaperMC/Paper@457e2c8 pass more stuff
PaperMC/Paper@b8da9ac item meta patch work
PaperMC/Paper@ccf158a update smithing recipe copy data components API
PaperMC/Paper@653f043 readd fix bee aging patch
PaperMC/Paper@bab31b6 Update Enchantment damage increase API
PaperMC/Paper@f4c7639 Un-jankify root user detection
PaperMC/Paper@9c58056 Some more compilation fixes
PaperMC/Paper@b984f66 Missing FQN in CraftPotionBrewer getter
PaperMC/Paper@768befe Remove patch renaming scripts
PaperMC/Paper@04a25c2 Fix WorldCreator#keepSpawnLoaded
PaperMC/Paper@820f999 Fixes for cb pkg ver
PaperMC/Paper@ca9001a compile fixes
PaperMC/Paper@d01f6b2 Fix more compile issues
PaperMC/Paper@8d3759d More more compile fixes
PaperMC/Paper@ed79038 More compile fixes
PaperMC/Paper@2641c02 Updated Upstream (Bukkit/CraftBukkit)
PaperMC/Paper@68aea43 Fixup damage source change
PaperMC/Paper@3062eb2 address my own comments
PaperMC/Paper@e85e1ec fixup sus effect entry patches
PaperMC/Paper@10e6143 fixup adventure's book meta handling
PaperMC/Paper@a244281 Replace VanillaGradle use and update generated API
PaperMC/Paper@31ad90b some more compile fixes
PaperMC/Paper@d2ef062 generator fix
PaperMC/Paper@7c6411b compile fixes
PaperMC/Paper@867eb90 compile fixes
PaperMC/Paper@62b96c5 compile fixes
PaperMC/Paper@c5c2a5f hunk system fixes
PaperMC/Paper@bcdf1f9 fixup loom pattern select event patch
PaperMC/Paper@43d110e fix issue with item default attribute API
PaperMC/Paper@2997fd6 plugin manager updates
PaperMC/Paper@7e9da43 support patch versions in api-version
PaperMC/Paper@813cfbf Fix compile issues
PaperMC/Paper@478a971 Fix light setting in protochunk
PaperMC/Paper@8b28f9f Fix accumulated total ticking chunks not being updated
PaperMC/Paper@00dac83 Drop old decompile fix
PaperMC/Paper@f6fae41 fix chat_type issue
PaperMC/Paper@cc81668 fix some issues on the todo list
PaperMC/Paper@44e68c3 fix some more issues
PaperMC/Paper@1bc278b Update patched spigot decompiler to fix inconsistency based on processing order
PaperMC/Paper@19ac9d4 remove no longer needed patches and fix tests
PaperMC/Paper@e856d17 Update paper plugin check in PluginRemapper
PaperMC/Paper@882ea0a fix adventure translations
PaperMC/Paper@4c53ace fix exact choice recipes
PaperMC/Paper@464cc81 fix custom potion mixes
PaperMC/Paper@ff77f82 paperweight 1.6.0
PaperMC/Paper@6a4974b Remove shadow from build classpath
PaperMC/Paper@c38f96b deprecate our ItemRarity API
PaperMC/Paper@ba8512a fix javadocs for SpawnerEntry$Equipment API
PaperMC/Paper@f1fc1b3 readd old UUID format patch
PaperMC/Paper@5485043 Don't tell people to build the reobf jar
PaperMC/Paper@2f19f0f fix enchantment order issue
PaperMC/Paper@80bd015 fix adventure converters not using RegistryOps
PaperMC/Paper@68e23db add back missing call to profile fill events
PaperMC/Paper@9bac389 more fixes
PaperMC/Paper@81f4405 add back hunk to async SavedData IO
PaperMC/Paper@91b5de6 bunch of fixes and improvements
PaperMC/Paper@f85d423 improve dumpitem and pgive commands
PaperMC/Paper@5731f34 Restore Handle Large Packets Disconnecting Client Patch
PaperMC/Paper@f037f08 Unify plugin remapping debug flags
PaperMC/Paper@c82766d fix item flags
PaperMC/Paper@f933c35 Update DataConverter to 1.20.6-rc1
PaperMC/Paper@55b1a87 fix PotionEffectType#getEffectAttributes
PaperMC/Paper@0b1b1fe fix helmet damage reduction inconsistencies
PaperMC/Paper@e2552ee Start cleaning up plugin remapping patches
PaperMC/Paper@711dbd7 Bump adventure ansi serializer
PaperMC/Paper@af97400 update generator
PaperMC/Paper@b7e148a Move obfhelper/stack deobf diff into original patch
PaperMC/Paper@a390119 Move cb version definition to gradle patch
PaperMC/Paper@20af7a7 remove funny diff
PaperMC/Paper@b3ea907 more ItemFlag fixes
PaperMC/Paper@53d8edd properly check for experimental stuff in generator
PaperMC/Paper@a771940 Revert to vanilla handling of LivingEntity#actuallyHurt (#10463)
PaperMC/Paper@06cae13 Update jdk version and distribution in actions
PaperMC/Paper@dd571d8 Fix bad diff in build gradle file
PaperMC/Paper@d02bb81 proper migration to gamerules for keep spawn loaded distance
PaperMC/Paper@aeb3f37 update paperweight to 1.6.1
PaperMC/Paper@75c22d5 Bump tiny-remapper
PaperMC/Paper@8454786 Bump tiny-remapper (real)
PaperMC/Paper@d99d84a Temporarily comment out adventure jd links
PaperMC/Paper@83767f9 Use JetBrains runtime for run tasks
PaperMC/Paper@4ad6022 improve checking handled tags in itemmeta (#9470)
PaperMC/Paper@8ce62b3 Fix issue with hopppers
PaperMC/Paper@7e3d5b5 bump paperweight to 1.6.2 to fix spurious build failure from scanJar OOM
PaperMC/Paper@d964c15 fix last test issue
PaperMC/Paper@3e90a19 Updated Upstream (Bukkit/CraftBukkit)
PaperMC/Paper@d93c6f9 remove calls to DoNotUse methods
PaperMC/Paper@f187fd6 Only scan `org.bukkit.craftbukkit.inventory` in `MetaHandledTagsTest`
PaperMC/Paper@a3c4dc9 Fix the incorrect codec usage in the Style convert (#10465)
PaperMC/Paper@e16263c Fix firework meta NPE
PaperMC/Paper@306b203 more fixes for ItemMeta relating to components
PaperMC/Paper@1a03cc9 Fix test compilation
PaperMC/Paper@6e31f38 Fix book meta lacking resolved tag from older versions (Fixes #10470)
PaperMC/Paper@37b3891 Fix another missing book meta tag
PaperMC/Paper@c4a1cd1 Fix issue with empty lists in adventure predicates
PaperMC/Paper@9d6e7e3 Don't hard fail on plugin remap error
PaperMC/Paper@4491ac0 respect hard list size limits in ItemMeta
PaperMC/Paper@aed7dca Fix overly strong name validation in player heads
PaperMC/Paper@0f4d328 final missing validation for data components
PaperMC/Paper@52ff3b7 Add itemName components (Resolves #10467) (#10471)
PaperMC/Paper@ba673a3 [ci skip] Feature requests are now in GitHub Discussions (#10606)
PaperMC/Paper@0f3e324 [ci skip] Fix discussion template file name (#10607)
PaperMC/Paper@82a8012 [ci skip] adjust projects workflow for label changes
PaperMC/Paper@61c9c07 Fix CraftMetaBlockState adding extra tags
PaperMC/Paper@7f2d531 Rewrite reflection in library loader jars (#10608)
PaperMC/Paper@05308fc Remap plugin libraries with namespace set to spigot (#10610)
PaperMC/Paper@69a971a Reset component prototype on itemtype change
PaperMC/Paper@f9397e1 Fix incorrect Commodore mapping of sweeping enchant
PaperMC/Paper@8f7ac62 Validate manifest provided mapping namespace (#10612)
PaperMC/Paper@fbe3c00 [ci skip] update projects workflow configuration
PaperMC/Paper@44ee092 1.20.6 (#10623)
PaperMC/Paper@812ccb9 fix spawnChunkRadius gamerule resetting
PaperMC/Paper@25e44bc fix dumpitem command nbt format
PaperMC/Paper@3fdd094 Fix sealed package check in BytecodeModifyingURLClassLoader (#10627)
PaperMC/Paper@937a67a Add initial size cap to component map
PaperMC/Paper@4ea67ab [ci skip] Remove trailing comma in projects action
PaperMC/Paper@8c058f8 Update paperweight to 1.6.3 (#10626)
PaperMC/Paper@fa6013d Fixes beds not exploding in the nether
PaperMC/Paper@3e0eb4a Updated Upstream (CraftBukkit) (#10624)
PaperMC/Paper@4cfd9e2 Expose canReach to mob pathfinding API (#10636)
PaperMC/Paper@b8c454c Add damage check for mending (#10631)
PaperMC/Paper@310b47b Update Java version references, add note on var usages
PaperMC/Paper@faa7aa2 Add GameMode#isInvulnerable (#10633)
PaperMC/Paper@18c201b Add EquipmentSlot#getOppositeHand (#10632)
PaperMC/Paper@544ce20 Expose #hasColor to leather armor (#10638)
PaperMC/Paper@59fe7cc default save-empty-scoreboard-teams to vanilla
PaperMC/Paper@0db72ac Fix can_place_on conversion and size check for item containers
PaperMC/Paper@156675c Fix lodestone compass target conversion
PaperMC/Paper@5632210 add RegistryAccess for managing registries (#10154)
PaperMC/Paper@6fcca46 Add missing step in V3818
PaperMC/Paper@7401313 fix shear drops for wolf and bogged
PaperMC/Paper@7ae2c67 Keep components using single items in creative (#10664)
PaperMC/Paper@b13c7db Add missing experimental field and annotations (#10653)
PaperMC/Paper@9d8d38d Updated Upstream (CraftBukkit) (#10646)
PaperMC/Paper@0e43d3c Added API to get player's proxy address (#10400)
PaperMC/Paper@54c5ecb Add missing SpigotConfig logCommands check (#10659)
PaperMC/Paper@816ea9e [ci skip] Update old particle reference in javadoc (#10652)
PaperMC/Paper@716dfd8 Fix EntityLoadCrossbowEvent (#10645)
PaperMC/Paper@21d91ce Prevent profile lookups when unnecessary (#10651)
PaperMC/Paper@2a6a5fe Add a way to check whether the chest is blocked (#10635)
PaperMC/Paper@ac3a547 Fix BlockStateMeta (#10647)
2024-05-06 02:54:19 +01:00

578 lines
26 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <kfian294ma4@gmail.com>
Date: Fri, 3 May 2024 15:04:31 +0100
Subject: [PATCH] Specialised Explosions
diff --git a/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java b/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java
index 0fd814f1d65c111266a2b20f86561839a4cef755..932f7a0d030d2d4932e6e6d4a5805e9b683cce67 100644
--- a/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java
+++ b/src/main/java/io/papermc/paper/util/maplist/IteratorSafeOrderedReferenceSet.java
@@ -125,6 +125,12 @@ public final class IteratorSafeOrderedReferenceSet<E> {
}
}
+ // Sakura start - add indexOf method
+ public int indexOf(final E element) {
+ return this.indexMap.getInt(element);
+ }
+ // Sakura end - add indexOf method
+
public boolean remove(final E element) {
final int index = this.indexMap.removeInt(element);
if (index >= 0) {
diff --git a/src/main/java/me/samsuik/sakura/explosion/special/SpecialisedExplosion.java b/src/main/java/me/samsuik/sakura/explosion/special/SpecialisedExplosion.java
new file mode 100644
index 0000000000000000000000000000000000000000..688a2bbd0b0b60b9acbdb3e6da0b24efed2bfde9
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/explosion/special/SpecialisedExplosion.java
@@ -0,0 +1,198 @@
+package me.samsuik.sakura.explosion.special;
+
+import io.papermc.paper.util.WorldUtil;
+import net.minecraft.core.BlockPos;
+import net.minecraft.core.Holder;
+import net.minecraft.core.particles.ParticleOptions;
+import net.minecraft.server.level.ServerLevel;
+import net.minecraft.sounds.SoundEvent;
+import net.minecraft.util.Mth;
+import net.minecraft.world.damagesource.DamageSource;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.entity.item.PrimedTnt;
+import net.minecraft.world.level.Explosion;
+import net.minecraft.world.level.ExplosionDamageCalculator;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.phys.AABB;
+import net.minecraft.world.phys.Vec3;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.List;
+import java.util.function.Consumer;
+
+public abstract class SpecialisedExplosion<T extends Entity> extends Explosion {
+ private static final double ENTITY_DISPATCH_DISTANCE = Math.pow(32.0, 2.0);
+
+ protected final ServerLevel level;
+ protected Vec3 position;
+ protected final T cause; // preferred over source
+ private Vec3 impactPosition;
+ protected ExplosionBlockCache[] recentBlockCache;
+ protected final BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
+
+ public SpecialisedExplosion(Level world, T entity, @Nullable DamageSource damageSource, @Nullable ExplosionDamageCalculator behavior, double x, double y, double z, float power, boolean createFire, BlockInteraction destructionType, ParticleOptions particle, ParticleOptions emitterParticle, Holder<SoundEvent> soundEvent) {
+ super(world, entity, damageSource, behavior, x, y, z, power, createFire, destructionType, particle, emitterParticle, soundEvent);
+ this.level = (ServerLevel) world;
+ this.position = new Vec3(x, y, z);
+ this.cause = entity;
+ this.impactPosition = this.position;
+ }
+
+ protected double getExplosionOffset() {
+ return (double) this.cause.getBbHeight() * 0.0625D;
+ }
+
+ protected abstract void startExplosion();
+
+ @Override
+ protected final void clearBlockCache() {
+ super.clearBlockCache();
+ this.recentBlockCache = null;
+ }
+
+ @Override
+ @Deprecated
+ public final void explode() {
+ if (this.radius() < 0.1F) {
+ // (radius < 0.1F) in bukkit is assumed to not be able to find any blocks or entities.
+ for (int i = 1; i < this.cause.getStacked(); ++i) {
+ this.finalizeExplosionAndParticles();
+ }
+ } else {
+ this.recentBlockCache = this.createBlockCache();
+ this.startExplosion(); // search for blocks, impact entities, finalise if necessary
+ this.clearBlockCache();
+ }
+ }
+
+ protected final boolean requiresImpactEntities() {
+ if (this.impactPosition.distanceToSqr(this.position) > ENTITY_DISPATCH_DISTANCE) {
+ this.impactPosition = this.position;
+ return true;
+ }
+ return !this.getToBlow().isEmpty();
+ }
+
+ protected final boolean finalizeExplosionAndParticles() {
+ this.wasCanceled = false;
+ super.finalizeExplosion(false);
+ boolean destroyedBlocks = !this.getToBlow().isEmpty();
+
+ if (!this.interactsWithBlocks()) {
+ this.getToBlow().clear(); // server sends block changes in the explosion packet
+ }
+
+ if (!this.wasCanceled) {
+ this.level.notifyPlayersOfExplosion(this.x, this.y, this.z, this.radius(), this);
+ }
+
+ this.getToBlow().clear();
+ return destroyedBlocks;
+ }
+
+ protected void postExplosion(List<BlockPos> foundBlocks, boolean destroyedBlocks) {
+ // The purpose of this is to make sure papers blockCache doesn't become outdated
+ // by removing stale entries from the block cache map and the recent cache array.
+ if (!foundBlocks.isEmpty() && !destroyedBlocks) {
+ this.markBlocksInCacheAsExplodable(foundBlocks);
+ } else {
+ super.blockCache.clear();
+ }
+
+ java.util.Arrays.fill(this.recentBlockCache, null);
+ }
+
+ protected final void recalculateExplosionPosition() {
+ this.recalculateExplosionPosition(this.cause);
+ }
+
+ protected final void recalculateExplosionPosition(T entity) {
+ this.x = entity.getX();
+ this.y = entity.getY() + this.getExplosionOffset();
+ this.z = entity.getZ();
+ this.position = new Vec3(this.x, this.y, this.z);
+ }
+
+ protected final void forEachEntitySliceInBounds(AABB bb, Consumer<Entity[]> sliceConsumer) {
+ int minSection = WorldUtil.getMinSection(this.level);
+ int maxSection = WorldUtil.getMaxSection(this.level);
+
+ int minChunkX = Mth.floor(bb.minX) >> 4;
+ int minChunkY = Mth.clamp(Mth.floor(bb.minY) >> 4, minSection, maxSection);
+ int minChunkZ = Mth.floor(bb.minZ) >> 4;
+ int maxChunkX = Mth.floor(bb.maxX) >> 4;
+ int maxChunkY = Mth.clamp(Mth.floor(bb.maxY) >> 4, minSection, maxSection);
+ int maxChunkZ = Mth.floor(bb.maxZ) >> 4;
+
+ io.papermc.paper.chunk.system.entity.EntityLookup entityLookup = this.level.getEntityLookup();
+ for (int chunkX = minChunkX; chunkX <= maxChunkX; ++chunkX) {
+ for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; ++chunkZ) {
+ io.papermc.paper.world.ChunkEntitySlices chunk = entityLookup.getChunk(chunkX, chunkZ);
+
+ if (chunk == null) {
+ continue;
+ }
+
+ for (int chunkY = minChunkY; chunkY <= maxChunkY; ++chunkY) {
+ sliceConsumer.accept(chunk.getSectionEntities(chunkY));
+ }
+ }
+ }
+ }
+
+ protected final void impactEntitiesFromPosition(Entity[] entities, Vec3 position, int potential, double radius) {
+ for (int i = 0; i < entities.length; ++i) {
+ Entity entity = entities[i];
+ if (entity == null) break;
+
+ if (entity != source && !entity.ignoreExplosion(this)) {
+ this.impactEntity(entity, position, potential, radius);
+ }
+
+ if (entities[i] != entity) {
+ i--;
+ }
+ }
+ }
+
+ protected final void impactEntity(Entity entity, Vec3 pos, int potential, double radius) {
+ if (entity.isPrimedTNT || entity.isFallingBlock) {
+ this.impactCannonEntity(entity, pos, potential, radius);
+ } else {
+ for (int i = 0; i < potential; ++i) {
+ super.impactEntity(this.recentBlockCache, this.mutablePos, (float) radius, pos, entity);
+ }
+ }
+ }
+
+ protected final void impactCannonEntity(Entity entity, Vec3 pos, int potential, double radius) {
+ double distanceFromBottom = Math.sqrt(entity.distanceToSqr(pos)) / radius;
+
+ if (distanceFromBottom <= 1.0) {
+ double x = entity.getX() - pos.x;
+ double y = (entity instanceof PrimedTnt ? entity.getY() : entity.getEyeY()) - this.y;
+ double z = entity.getZ() - pos.z;
+ double distance = Math.sqrt(x * x + y * y + z * z);
+
+ if (distance != 0.0D) {
+ x /= distance;
+ y /= distance;
+ z /= distance;
+ double density = this.getBlockDensity(pos, entity, this.recentBlockCache, this.mutablePos); // Paper - Optimize explosions // Paper - optimise explosions
+ double exposure = (1.0D - distanceFromBottom) * density;
+
+ x *= exposure;
+ y *= exposure;
+ z *= exposure;
+
+ if (exposure == 0.0) {
+ return;
+ }
+
+ for (int i = 0; i < potential; ++i) {
+ entity.addDeltaMovement(x, y, z);
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/explosion/special/TntExplosion.java b/src/main/java/me/samsuik/sakura/explosion/special/TntExplosion.java
new file mode 100644
index 0000000000000000000000000000000000000000..71e1774a6db949ad6c01f1283a17a89ae6af8b02
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/explosion/special/TntExplosion.java
@@ -0,0 +1,180 @@
+package me.samsuik.sakura.explosion.special;
+
+import io.papermc.paper.util.maplist.IteratorSafeOrderedReferenceSet;
+import it.unimi.dsi.fastutil.objects.ObjectArrayList;
+import me.samsuik.sakura.entity.EntityState;
+import net.minecraft.core.BlockPos;
+import net.minecraft.core.Direction;
+import net.minecraft.core.Holder;
+import net.minecraft.core.particles.ParticleOptions;
+import net.minecraft.sounds.SoundEvent;
+import net.minecraft.util.Mth;
+import net.minecraft.world.damagesource.DamageSource;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.entity.EntityType;
+import net.minecraft.world.entity.item.PrimedTnt;
+import net.minecraft.world.level.ExplosionDamageCalculator;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.phys.AABB;
+import net.minecraft.world.phys.Vec3;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.List;
+
+public final class TntExplosion extends SpecialisedExplosion<PrimedTnt> {
+ private static final int ALL_DIRECTIONS = 0b111;
+ private static final int FOUND_ALL_BLOCKS = ALL_DIRECTIONS + 12;
+
+ private final Vec3 originalPosition;
+ private final List<Vec3> explosions = new ObjectArrayList<>();
+ private AABB bounds;
+ private int wrapped = 0;
+ private int movement = 0;
+
+ public TntExplosion(Level world, PrimedTnt tnt, @Nullable DamageSource damageSource, @Nullable ExplosionDamageCalculator behavior, double x, double y, double z, float power, boolean createFire, BlockInteraction destructionType, ParticleOptions particle, ParticleOptions emitterParticle, Holder<SoundEvent> soundEvent) {
+ super(world, tnt, damageSource, behavior, x, y, z, power, createFire, destructionType, particle, emitterParticle, soundEvent);
+ this.originalPosition = this.position;
+ this.bounds = new AABB(x, y, z, x, y, z);
+ }
+
+ @Override
+ protected void startExplosion() {
+ for (int i = this.calculateExplosionPotential() - 1; i >= 0; --i) {
+ Vec3 previousMomentum = this.cause.entityState().momentum();
+ boolean lastCycle = (i == 0);
+ this.midExplosion(previousMomentum, lastCycle); // search for blocks and impact entities
+
+ if (!lastCycle) {
+ EntityState entityState = this.nextSourceVelocity();
+ List<BlockPos> foundBlocks = new ObjectArrayList<>(this.getToBlow());
+ boolean destroyedBlocks = this.finalizeExplosionAndParticles();
+ this.postExplosion(foundBlocks, destroyedBlocks); // update wrapped, clear recent block cache
+ this.updateExplosionPosition(entityState, destroyedBlocks);
+ }
+ }
+ }
+
+ private void midExplosion(Vec3 previousMomentum, boolean lastCycle) {
+ if (this.wrapped < FOUND_ALL_BLOCKS) {
+ final int blockX = Mth.floor(this.x);
+ final int blockY = Mth.floor(this.y);
+ final int blockZ = Mth.floor(this.z);
+
+ final long key = BlockPos.asLong(blockX, blockY, blockZ);
+
+ final ExplosionBlockCache initialCache = this.getOrCacheExplosionBlock(blockX, blockY, blockZ, key, true);
+
+ if (initialCache.resistance <= (this.radius() * 1.3f) && this.interactsWithBlocks() && this.isRegionUnprotected()) { // Sakura - optimise protected explosions
+ this.searchForBlocks(this.recentBlockCache, initialCache);
+ }
+ }
+
+ if (this.wrapped < ALL_DIRECTIONS) {
+ Vec3 momentum = this.cause.entityState().momentum();
+ for (Direction.Axis axis : Direction.Axis.VALUES) {
+ double current = momentum.get(axis);
+ double previous = previousMomentum.get(axis);
+ if (current == previous || current * previous < 0) {
+ this.wrapped |= 1 << axis.ordinal();
+ }
+ }
+ }
+
+ this.bounds = this.bounds.expand(this.position);
+ this.explosions.add(this.position);
+
+ if (lastCycle || this.requiresImpactEntities()) {
+ this.locateAndImpactEntitiesInBounds();
+ this.bounds = new AABB(this.position, this.position);
+ this.explosions.clear();
+ }
+ }
+
+ @Override
+ protected void postExplosion(List<BlockPos> foundBlocks, boolean destroyedBlocks) {
+ super.postExplosion(foundBlocks, destroyedBlocks);
+ // Update wrapped, this is for tracking swinging and if blocks are found
+ if (this.wrapped >= 7) {
+ if (!destroyedBlocks && this.level.sakuraConfig().cannons.explosion.avoidRedundantBlockSearches) {
+ this.wrapped++;
+ } else {
+ this.wrapped = 7;
+ }
+ }
+ }
+
+ private EntityState nextSourceVelocity() {
+ PrimedTnt tnt = new PrimedTnt(EntityType.TNT, this.level);
+ this.cause.entityState().apply(tnt);
+ this.impactCannonEntity(tnt, this.position, 1, this.radius() * 2.0f);
+ return EntityState.of(tnt);
+ }
+
+ private void updateExplosionPosition(EntityState entityState, boolean destroyedBlocks) {
+ // Ticking is always required after destroying a block.
+ if (destroyedBlocks || !this.position.equals(this.cause.position()) && (this.movement != 1 || !this.originalPosition.equals(this.position))) {
+ entityState.apply(this.cause);
+ this.cause.storeEntityState();
+ this.cause.setFuse(100);
+ this.cause.tick();
+ this.movement++;
+ this.recalculateExplosionPosition();
+ }
+ }
+
+ private int calculateExplosionPotential() {
+ IteratorSafeOrderedReferenceSet<Entity> entities = this.level.entityTickList.entities;
+ int base = this.cause.getStacked();
+ int index = entities.indexOf(this.cause);
+ // iterate over the entityTickList to find entities that are exploding in the same position.
+ while ((index = entities.advanceRawIterator(index)) != -1) {
+ Entity foundEntity = entities.rawGet(index);
+ if (!foundEntity.compareState(this.cause) || !foundEntity.isSafeToMergeInto(this.cause))
+ break;
+ base += foundEntity.getStacked();
+ foundEntity.discard();
+ }
+
+ return base;
+ }
+
+ private void locateAndImpactEntitiesInBounds() {
+ double radius = this.radius() * 2.0f;
+ AABB bb = this.bounds;
+
+ Vec3 center = bb.getCenter();
+ double change = Math.max(bb.getXsize(), Math.max(bb.getYsize(), bb.getZsize()));
+ double maxDistanceSqr = Math.pow(radius + change, 2.0);
+ boolean moved = (change != 0.0);
+
+ this.forEachEntitySliceInBounds(bb.inflate(radius), entities -> {
+ if (moved) {
+ this.impactEntitiesSwinging(entities, center, radius, maxDistanceSqr);
+ } else {
+ this.impactEntitiesFromPosition(entities, this.explosions.get(0), this.explosions.size(), radius);
+ }
+ });
+ }
+
+ private void impactEntitiesSwinging(Entity[] entities, Vec3 center, double radius, double maxDistanceSqr) {
+ for (int i = 0; i < entities.length; ++i) {
+ Entity entity = entities[i];
+ if (entity == null) break;
+
+ if (entity != this.source && !entity.ignoreExplosion(this) && entity.distanceToSqr(center.x, center.y, center.z) <= maxDistanceSqr) {
+ this.impactEntity(entity, radius);
+ }
+
+ if (entities[i] != entity) {
+ i--;
+ }
+ }
+ }
+
+ private void impactEntity(Entity entity, double radius) {
+ //noinspection ForLoopReplaceableByForEach
+ for (int i = 0; i < this.explosions.size(); i++) {
+ this.impactEntity(entity, this.explosions.get(i), 1, radius);
+ }
+ }
+}
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index a9cfdbd7d138763584ee3d2825fe6979241422db..3ca19140d8c7b28f73e80dcdbfd60536d74886ff 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -1964,6 +1964,12 @@ public class ServerLevel extends Level implements WorldGenLevel {
explosion.clearToBlow();
}
+ // Sakura start - specialised explosions
+ this.notifyPlayersOfExplosion(x, y, z, power, explosion);
+ return explosion;
+ }
+ public final void notifyPlayersOfExplosion(double x, double y, double z, float power, Explosion explosion) {
+ // Sakura end - specialised explosions
Iterator iterator = this.players.iterator();
while (iterator.hasNext()) {
@@ -1974,7 +1980,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
}
}
- return explosion;
+ // Sakura - return moved up into explode
}
@Override
diff --git a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java b/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
index 1860ffd3e11601983abbb85962dd9ef56992c9f5..f8e19ca5af5b313e887ce8b428e32a84a26a8927 100644
--- a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
+++ b/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
@@ -84,28 +84,7 @@ public class PrimedTnt extends Entity implements TraceableEntity {
&& (tnt.entityState().fallDistance() == fallDistance
|| tnt.entityState().fallDistance() > 2.5f && fallDistance > 2.5f);
}
-
- @Override
- protected boolean respawnMerged() {
- if (stacked <= 1) return false;
-
- PrimedTnt tnt = new PrimedTnt(EntityType.TNT, level());
-
- while (stacked-- > 1) {
- this.setFuse(100); // Prevent unwanted explosions while ticking
-
- // Cause an explosion to affect this entity
- tnt.setPos(this.position());
- tnt.setDeltaMovement(this.getDeltaMovement());
- this.entityState().apply(this);
- tnt.explode();
- this.storeEntityState();
-
- this.tick();
- }
-
- return true;
- }
+ // Sakura - specialised explosions
// Sakura end
@Override
diff --git a/src/main/java/net/minecraft/world/level/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java
index d7e8dd148098de080c5bc0f76b36207501e1089f..7eb8da4075b4f1706f698a0b48206421bf4353c3 100644
--- a/src/main/java/net/minecraft/world/level/Explosion.java
+++ b/src/main/java/net/minecraft/world/level/Explosion.java
@@ -56,9 +56,11 @@ public class Explosion {
private final Explosion.BlockInteraction blockInteraction;
private final RandomSource random;
private final Level level;
- private final double x;
- private final double y;
- private final double z;
+ // Sakura start - private -> protected
+ protected double x;
+ protected double y;
+ protected double z;
+ // Sakura end - private -> protected
@Nullable
public final Entity source;
private final float radius;
@@ -186,7 +188,7 @@ public class Explosion {
// resistance = (res + 0.3F) * 0.3F;
// so for resistance = 0, we need res = -0.3F
private static final Float ZERO_RESISTANCE = Float.valueOf(-0.3f);
- private it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap<ExplosionBlockCache> blockCache = null;
+ protected it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap<ExplosionBlockCache> blockCache = null; // Sakura - private -> protected
public static final class ExplosionBlockCache {
@@ -213,7 +215,29 @@ public class Explosion {
private long[] chunkPosCache = null;
private net.minecraft.world.level.chunk.LevelChunk[] chunkCache = null;
- private ExplosionBlockCache getOrCacheExplosionBlock(final int x, final int y, final int z,
+ // Sakura start - specialised explosions
+ protected final ExplosionBlockCache[] createBlockCache() {
+ this.blockCache = new it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap<>();
+
+ this.chunkPosCache = new long[CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH];
+ java.util.Arrays.fill(this.chunkPosCache, ChunkPos.INVALID_CHUNK_POS);
+
+ this.chunkCache = new net.minecraft.world.level.chunk.LevelChunk[CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH];
+
+ return new ExplosionBlockCache[BLOCK_EXPLOSION_CACHE_WIDTH * BLOCK_EXPLOSION_CACHE_WIDTH * BLOCK_EXPLOSION_CACHE_WIDTH];
+ }
+
+ protected final void markBlocksInCacheAsExplodable(List<BlockPos> blocks) {
+ for (BlockPos blow : blocks) {
+ ExplosionBlockCache cache = this.blockCache.get(blow.asLong());
+ // May be null if the blockCache is cleared then retrieved from the recent block cache
+ if (cache != null) {
+ cache.shouldExplode = null;
+ }
+ }
+ }
+ // Sakura end - specialised explosions
+ protected final ExplosionBlockCache getOrCacheExplosionBlock(final int x, final int y, final int z, // Sakura - private -> protected
final long key, final boolean calculateResistance) {
ExplosionBlockCache ret = this.blockCache.get(key);
if (ret != null) {
@@ -502,14 +526,7 @@ public class Explosion {
int j;
// Paper start - optimise explosions
- this.blockCache = new it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap<>();
-
- this.chunkPosCache = new long[CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH];
- java.util.Arrays.fill(this.chunkPosCache, ChunkPos.INVALID_CHUNK_POS);
-
- this.chunkCache = new net.minecraft.world.level.chunk.LevelChunk[CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH];
-
- final ExplosionBlockCache[] blockCache = new ExplosionBlockCache[BLOCK_EXPLOSION_CACHE_WIDTH * BLOCK_EXPLOSION_CACHE_WIDTH * BLOCK_EXPLOSION_CACHE_WIDTH];
+ final ExplosionBlockCache[] blockCache = this.createBlockCache();
// use initial cache value that is most likely to be used: the source position
final ExplosionBlockCache initialCache;
{
@@ -1006,7 +1023,7 @@ public class Explosion {
private BlockInteraction() {}
}
// Paper start - Optimize explosions
- private float getBlockDensity(Vec3 vec3d, Entity entity, ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos) { // Paper - optimise explosions
+ protected final float getBlockDensity(Vec3 vec3d, Entity entity, ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos) { // Sakura - private -> protected // Paper - optimise explosions
// Sakura start - replace density cache
float blockDensity = this.level.densityCache.getDensity(vec3d, entity);
if (blockDensity == me.samsuik.sakura.explosion.density.BlockDensityCache.UNKNOWN_DENSITY) {
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index e53f3b508eba942a6d61c1ad678ff0f5793c3f73..4553c252d3dd0a9413319a7a5dd5cce201b0fa72 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -1415,7 +1415,14 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
}
Explosion.BlockInteraction explosion_effect1 = explosion_effect;
- Explosion explosion = new Explosion(this, entity, damageSource, behavior, x, y, z, power, createFire, explosion_effect1, particle, emitterParticle, soundEvent);
+ // Sakura start - specialised explosions
+ final Explosion explosion;
+ if (entity instanceof net.minecraft.world.entity.item.PrimedTnt tnt) {
+ explosion = new me.samsuik.sakura.explosion.special.TntExplosion(this, tnt, damageSource, behavior, x, y, z, power, createFire, explosion_effect1, particle, emitterParticle, soundEvent);
+ } else {
+ explosion = new Explosion(this, entity, damageSource, behavior, x, y, z, power, createFire, explosion_effect1, particle, emitterParticle, soundEvent);
+ }
+ // Sakura end - specialised explosions
explosion.explode();
explosion.finalizeExplosion(particles);