Skip to content

Commit

Permalink
Add the option for explosions to damage the explosion cause
Browse files Browse the repository at this point in the history
This intends to give plugin developers more control over explosions created using the World#createExplosion method, specifically by adding the option for explosions to damage the explosion cause (not the default behavior, and previously impossible to do, as far as I know). This is done by overloading existing methods with an extra shouldDamageSource parameter.
  • Loading branch information
EsotericEnderman committed Aug 23, 2024
1 parent 2fdb2e9 commit 0b0f86d
Show file tree
Hide file tree
Showing 26 changed files with 191 additions and 86 deletions.
34 changes: 33 additions & 1 deletion patches/api/0112-Expand-Explosions-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ index 3161eae2fa5f03b7d3a5e9945ab659c15cf568c6..af737017ee397f80c44ee02c6cc60cef
/**
* Returns a list of entities within a bounding box centered around a Location.
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 44a74f15bea60ecd8380520e8faaea41a6c261c5..50c1e4957f66826feb0a2eb04293dbd6b5595700 100644
index 44a74f15bea60ecd8380520e8faaea41a6c261c5..8462745daa02c8102b70e657e1a7569efaa8cac7 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -1424,6 +1424,88 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
Expand Down Expand Up @@ -200,3 +200,35 @@ index 44a74f15bea60ecd8380520e8faaea41a6c261c5..50c1e4957f66826feb0a2eb04293dbd6
/**
* Creates explosion at given coordinates with given power and optionally
* setting blocks on fire or breaking blocks.
@@ -1455,6 +1537,31 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
*/
public boolean createExplosion(@NotNull Location loc, float power, boolean setFire, boolean breakBlocks, @Nullable Entity source);

+ // Paper start - Add the option for explosions to damage the explosion cause
+ /**
+ * Creates explosion at given coordinates with given power and optionally
+ * setting blocks on fire, breaking blocks, or damaging the explosion
+ * cause.
+ * <p>
+ * Note that if a non-null {@code source} Entity is provided and {@code
+ * breakBlocks} is {@code true}, the value of {@code breakBlocks} will be
+ * ignored if {@link GameRule#MOB_GRIEFING} is {@code false} in the world
+ * in which the explosion occurs. In other words, the mob griefing gamerule
+ * will take priority over {@code breakBlocks} if explosions are not allowed.
+ *
+ * @param x The x component of the location to blow up
+ * @param y The y component of the location to blow up
+ * @param z The z component of the location to blow up
+ * @param power The power of explosion, where 4F is TNT
+ * @param setFire Whether or not to set blocks on fire
+ * @param breakBlocks Whether or not to have blocks be destroyed
+ * @param source the source entity, used for tracking damage
+ * @param shouldDamageSource Whether or not to damage the {@code source} entity
+ * @return false if explosion was canceled, otherwise true
+ */
+ public boolean createExplosion(double x, double y, double z, float power, boolean setFire, boolean breakBlocks, @Nullable Entity source, boolean shouldDamageSource);
+ // Paper end - Add the option for explosions to damage the explosion cause
+
/**
* Gets the current PVP setting for this world.
*
12 changes: 6 additions & 6 deletions patches/api/0166-Fix-Spigot-annotation-mistakes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ index e455eb21abf121dc6ff10ff8a13dd06f67096a8f..bbc01e7c192ae6689c301670047ff114
return origin;
}
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index fcdc5d83621acff5f9210585455be1ea50abb77c..216995288f6b8b407ef8240411b5ed4713379a7a 100644
index 51302cac3e18897ba476e209f8d4a8c7e9d86aa0..d7c8d145e497efe1273ecddf506d28e243255ee3 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -418,9 +418,8 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
Expand All @@ -632,15 +632,15 @@ index fcdc5d83621acff5f9210585455be1ea50abb77c..216995288f6b8b407ef8240411b5ed47
public boolean refreshChunk(int x, int z);

/**
@@ -3797,6 +3796,7 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -3822,6 +3821,7 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
StructureSearchResult locateNearestStructure(@NotNull Location origin, @NotNull Structure structure, int radius, boolean findUnexplored);

// Spigot start
+ @Deprecated(forRemoval = true) // Paper
public class Spigot {

/**
@@ -3830,7 +3830,11 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -3855,7 +3855,11 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
}
}

Expand All @@ -652,7 +652,7 @@ index fcdc5d83621acff5f9210585455be1ea50abb77c..216995288f6b8b407ef8240411b5ed47
Spigot spigot();
// Spigot end

@@ -4048,9 +4052,9 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -4073,9 +4077,9 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
* Gets the dimension ID of this environment
*
* @return dimension ID
Expand All @@ -664,7 +664,7 @@ index fcdc5d83621acff5f9210585455be1ea50abb77c..216995288f6b8b407ef8240411b5ed47
public int getId() {
return id;
}
@@ -4060,9 +4064,9 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -4085,9 +4089,9 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
*
* @param id The ID of the environment
* @return The environment
Expand Down Expand Up @@ -890,7 +890,7 @@ index bafef53c1d449135f1300c8c8fbb06f482ba67e1..f50aaddf8582be55fd4860ad374d8f22
+@Deprecated(forRemoval = true) // Paper
public interface LingeringPotion extends ThrownPotion { }
diff --git a/src/main/java/org/bukkit/entity/LivingEntity.java b/src/main/java/org/bukkit/entity/LivingEntity.java
index b5ea7b60b47f056553a1cec766c57e0f75735633..ec35111df4b38fd55cc34f4baedebcf39c7fc92b 100644
index 3f1b74af137868e502792c65ccd7ca74f3c3cb8c..c89ffb0f98dccd015e80e299142252fed3ece4a8 100644
--- a/src/main/java/org/bukkit/entity/LivingEntity.java
+++ b/src/main/java/org/bukkit/entity/LivingEntity.java
@@ -716,7 +716,9 @@ public interface LivingEntity extends Attributable, Damageable, ProjectileSource
Expand Down
4 changes: 2 additions & 2 deletions patches/api/0260-More-World-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] More World API


diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 216995288f6b8b407ef8240411b5ed4713379a7a..d3fc033aba36c5fd99846e9200ed0071fddd6045 100644
index d7c8d145e497efe1273ecddf506d28e243255ee3..feeaa13906893f7a9f7ffd97141891ca97f89e3c 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -3795,6 +3795,72 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -3820,6 +3820,72 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@Nullable
StructureSearchResult locateNearestStructure(@NotNull Location origin, @NotNull Structure structure, int radius, boolean findUnexplored);

Expand Down
8 changes: 4 additions & 4 deletions patches/api/0348-Expand-FallingBlock-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Subject: [PATCH] Expand FallingBlock API
Co-authored-by: Lukas Planz <[email protected]>

diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 8e9ab00503167799c6c929d00e48c07cb328848c..907906e15c9250fea385e49f10d3c248236fd004 100644
index de9709c923a376e8bb3f969202ff90341dbb655a..30568eaf1dde1f55ca7388c08c5040b0b98a1467 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -2228,8 +2228,10 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -2253,8 +2253,10 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
* @return The spawned {@link FallingBlock} instance
* @throws IllegalArgumentException if {@link Location} or {@link
* MaterialData} are null or {@link Material} of the {@link MaterialData} is not a block
Expand All @@ -24,7 +24,7 @@ index 8e9ab00503167799c6c929d00e48c07cb328848c..907906e15c9250fea385e49f10d3c248
public FallingBlock spawnFallingBlock(@NotNull Location location, @NotNull MaterialData data) throws IllegalArgumentException;

/**
@@ -2242,8 +2244,10 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -2267,8 +2269,10 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
* @return The spawned {@link FallingBlock} instance
* @throws IllegalArgumentException if {@link Location} or {@link
* BlockData} are null
Expand All @@ -35,7 +35,7 @@ index 8e9ab00503167799c6c929d00e48c07cb328848c..907906e15c9250fea385e49f10d3c248
public FallingBlock spawnFallingBlock(@NotNull Location location, @NotNull BlockData data) throws IllegalArgumentException;

/**
@@ -2260,7 +2264,7 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -2285,7 +2289,7 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
* @return The spawned {@link FallingBlock} instance
* @throws IllegalArgumentException if {@link Location} or {@link
* Material} are null or {@link Material} is not a block
Expand Down
4 changes: 2 additions & 2 deletions patches/api/0458-More-Raid-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ index 983a8c20a06d2b509602b27f49c090598b8ecc42..fa98599e3eee37bf68f0e9813497c718
+ // Paper end
}
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index ecc2d486cfec79cce27a947dfeed4853575a594d..d8a23aa0d898ca3360757721e38ddb97387f7d21 100644
index da3d2f7519ecfbbfa5eeeddc5f00ec7c4490bffb..45f71a36f1e3788998a2c740bb811d8473cec99a 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -4111,6 +4111,17 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@@ -4136,6 +4136,17 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
@Nullable
public Raid locateNearestRaid(@NotNull Location location, int radius);

Expand Down
79 changes: 76 additions & 3 deletions patches/server/0203-Expand-Explosions-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,86 @@ Subject: [PATCH] Expand Explosions API

Add Entity as a Source capability, and add more API choices, and on Location.

diff --git a/src/main/java/net/minecraft/world/level/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java
index b216140a8be65e210250358af8daf49344850f20..f286b17f2f9133a9a2d4e088d1cf67d67996a5ae 100644
--- a/src/main/java/net/minecraft/world/level/Explosion.java
+++ b/src/main/java/net/minecraft/world/level/Explosion.java
@@ -160,7 +160,13 @@ public class Explosion {
return new Vec3(this.x, this.y, this.z);
}

+ // Paper start - Add the option for explosions to damage the explosion cause
public void explode() {
+ this.explode(false);
+ }
+ // Paper end - Add the option for explosions to damage the explosion cause
+
+ public void explode(boolean shouldDamageCause) { // Paper - Add the option for explosions to damage the explosion cause
// CraftBukkit start
if (this.radius < 0.1F) {
return;
@@ -227,7 +233,7 @@ public class Explosion {
int i1 = Mth.floor(this.y + (double) f2 + 1.0D);
int j1 = Mth.floor(this.z - (double) f2 - 1.0D);
int k1 = Mth.floor(this.z + (double) f2 + 1.0D);
- List<Entity> list = this.level.getEntities(this.source, new AABB((double) i, (double) l, (double) j1, (double) j, (double) i1, (double) k1), (com.google.common.base.Predicate<Entity>) entity -> entity.isAlive() && !entity.isSpectator()); // Paper - Fix lag from explosions processing dead entities
+ List<Entity> list = this.level.getEntities(shouldDamageCause ? null : this.source, new AABB((double) i, (double) l, (double) j1, (double) j, (double) i1, (double) k1), (com.google.common.base.Predicate<Entity>) entity -> entity.isAlive() && !entity.isSpectator()); // Paper - Fix lag from explosions processing dead entities // Paper - Add the option for explosions to damage the explosion cause
Vec3 vec3d = new Vec3(this.x, this.y, this.z);
Iterator iterator = list.iterator();

diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 6944c0b0cfcde9fa4dd78742aee3e3b87d679abf..e44020460d056368ecc0bd0d9273512bb692484f 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -797,7 +797,13 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
return this.explode(entity, damageSource, behavior, x, y, z, power, createFire, explosionSourceType, true, particle, emitterParticle, soundEvent);
}

+ // Paper start - Add the option for explosions to damage the explosion cause
public Explosion explode(@Nullable Entity entity, @Nullable DamageSource damageSource, @Nullable ExplosionDamageCalculator behavior, double x, double y, double z, float power, boolean createFire, Level.ExplosionInteraction explosionSourceType, boolean particles, ParticleOptions particle, ParticleOptions emitterParticle, Holder<SoundEvent> soundEvent) {
+ return this.explode(entity, damageSource, behavior, x, y, z, power, createFire, explosionSourceType, particles, particle, emitterParticle, soundEvent, false);
+ }
+ // Paper end - Add the option for explosions to damage the explosion cause
+
+ public Explosion explode(@Nullable Entity entity, @Nullable DamageSource damageSource, @Nullable ExplosionDamageCalculator behavior, double x, double y, double z, float power, boolean createFire, Level.ExplosionInteraction explosionSourceType, boolean particles, ParticleOptions particle, ParticleOptions emitterParticle, Holder<SoundEvent> soundEvent, boolean shouldDamageSource) { // Paper end - Add the option for explosions to damage the explosion cause
Explosion.BlockInteraction explosion_effect;

switch (explosionSourceType.ordinal()) {
@@ -828,7 +834,7 @@ 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);

- explosion.explode();
+ explosion.explode(shouldDamageSource);
explosion.finalizeExplosion(particles);
return explosion;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 3c00eaf52ab04d1396f226cc074d9dd013c57027..b2a1f7e6576757d02a0369c42d4526ca3ac3d775 100644
index 3c00eaf52ab04d1396f226cc074d9dd013c57027..eaee15027eea607a9743b3e25daaf98f3f0ad555 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -789,6 +789,12 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@@ -776,8 +776,15 @@ public class CraftWorld extends CraftRegionAccessor implements World {
return this.createExplosion(x, y, z, power, setFire, breakBlocks, null);
}

+ // Paper start - Add the option for explosions to damage the explosion cause
@Override
public boolean createExplosion(double x, double y, double z, float power, boolean setFire, boolean breakBlocks, Entity source) {
+ return this.createExplosion(x, y, z, power, setFire, breakBlocks, source, false);
+ }
+ // Paper end - Add the option for explosions to damage the explosion cause
+
+ @Override
+ public boolean createExplosion(double x, double y, double z, float power, boolean setFire, boolean breakBlocks, Entity source, boolean shouldDamageSource) { // Paper - Add the option for explosions to damage the explosion cause
net.minecraft.world.level.Level.ExplosionInteraction explosionType;
if (!breakBlocks) {
explosionType = net.minecraft.world.level.Level.ExplosionInteraction.NONE; // Don't break blocks
@@ -787,8 +794,14 @@ public class CraftWorld extends CraftRegionAccessor implements World {
explosionType = net.minecraft.world.level.Level.ExplosionInteraction.MOB; // Respect mobGriefing gamerule
}

return !this.world.explode(source == null ? null : ((CraftEntity) source).getHandle(), x, y, z, power, setFire, explosionType).wasCanceled;
- return !this.world.explode(source == null ? null : ((CraftEntity) source).getHandle(), x, y, z, power, setFire, explosionType).wasCanceled;
+ return !this.world.explode(source == null ? null : ((CraftEntity) source).getHandle(), net.minecraft.world.level.Explosion.getDefaultDamageSource(this.getHandle(), source == null ? null : ((CraftEntity) source).getHandle()), (net.minecraft.world.level.ExplosionDamageCalculator) null, x, y, z, power, setFire, explosionType, true, net.minecraft.core.particles.ParticleTypes.EXPLOSION, net.minecraft.core.particles.ParticleTypes.EXPLOSION_EMITTER, net.minecraft.sounds.SoundEvents.GENERIC_EXPLODE).wasCanceled; // Paper - Add the option for explosions to damage the explosion cause
}
+ // Paper start
+ @Override
Expand Down
4 changes: 2 additions & 2 deletions patches/server/0207-Implement-World.getEntity-UUID-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] Implement World.getEntity(UUID) API


diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index b2a1f7e6576757d02a0369c42d4526ca3ac3d775..281fa67cb5c95e6016c7220c0ef912bbfd28cd9f 100644
index b02c9430df5600301a97bf1dd8929f824718272a..33d559c4b3997c08a5a3b2fa185d702c73a7ee96 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -1125,6 +1125,15 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@@ -1132,6 +1132,15 @@ public class CraftWorld extends CraftRegionAccessor implements World {
return list;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Optimize to check if the captured list even has values in it, and also to
just do a get call since the value can never be null.

diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 8e2acb3c6f815b5b1d3237a2f4e0b5f3683d2c60..5a3a89c568d42a2adbc2b6e2631fd4b70e54f0bf 100644
index d4d624ff381ba13ec5d7514630360f1e7e2304b1..f915b44eb6bbddd297a02d1eebf39a43d819f9d5 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -912,9 +912,12 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
@@ -918,9 +918,12 @@ public abstract class Level implements LevelAccessor, AutoCloseable {

@Nullable
public BlockEntity getBlockEntity(BlockPos blockposition, boolean validate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] Optimize call to getFluid for explosions


diff --git a/src/main/java/net/minecraft/world/level/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java
index 512d79b66fed3d1bef645c3ecb59bda032c81d15..1350c8df69b4ffcf9daa73549e496627db8bc6f7 100644
index f286b17f2f9133a9a2d4e088d1cf67d67996a5ae..995518dd3a1005bce196bae228a23b73fcc6485b 100644
--- a/src/main/java/net/minecraft/world/level/Explosion.java
+++ b/src/main/java/net/minecraft/world/level/Explosion.java
@@ -193,7 +193,7 @@ public class Explosion {
@@ -199,7 +199,7 @@ public class Explosion {
for (float f1 = 0.3F; f > 0.0F; f -= 0.22500001F) {
BlockPos blockposition = BlockPos.containing(d4, d5, d6);
BlockState iblockdata = this.level.getBlockState(blockposition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ index 1b3f1b06155e114cd5ab76a406c1b4e745ef58c6..52e8664f8d0368daed43e349aa67e3b2
for (SpawnCategory spawnCategory : SpawnCategory.values()) {
if (CraftSpawnCategory.isValidForLimits(spawnCategory)) {
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 20bb365b188c7081123db87186f0e1a999758817..a381688de78f5a78980b660f9765f839413a6c65 100644
index 3f251c77cc27849d2b20cb4a2731626e0e58e5f6..b636c375cc227c0c074056d5f00627fe7c975f0c 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -1165,7 +1165,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@@ -1172,7 +1172,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {

@Override
public void setDifficulty(Difficulty difficulty) {
Expand Down
Loading

0 comments on commit 0b0f86d

Please sign in to comment.