PaperMC/paper-server/patches/sources/net/minecraft/world/entity/LightningBolt.java.patch

160 lines
8 KiB
Diff

--- a/net/minecraft/world/entity/LightningBolt.java
+++ b/net/minecraft/world/entity/LightningBolt.java
@@ -30,6 +30,10 @@
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
+// CraftBukkit start
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.event.entity.EntityRemoveEvent;
+// CraftBukkit end
public class LightningBolt extends Entity {
@@ -44,6 +48,7 @@
private ServerPlayer cause;
private final Set<Entity> hitEntities = Sets.newHashSet();
private int blocksSetOnFire;
+ public boolean isEffect; // Paper - Properly handle lightning effects api
public LightningBolt(EntityType<? extends LightningBolt> type, Level world) {
super(type, world);
@@ -82,7 +87,7 @@
@Override
public void tick() {
super.tick();
- if (this.life == 2) {
+ if (!this.isEffect && this.life == 2) { // Paper - Properly handle lightning effects api
if (this.level().isClientSide()) {
this.level().playLocalSound(this.getX(), this.getY(), this.getZ(), SoundEvents.LIGHTNING_BOLT_THUNDER, SoundSource.WEATHER, 10000.0F, 0.8F + this.random.nextFloat() * 0.2F, false);
this.level().playLocalSound(this.getX(), this.getY(), this.getZ(), SoundEvents.LIGHTNING_BOLT_IMPACT, SoundSource.WEATHER, 2.0F, 0.5F + this.random.nextFloat() * 0.2F, false);
@@ -94,7 +99,7 @@
}
this.powerLightningRod();
- LightningBolt.clearCopperOnLightningStrike(this.level(), this.getStrikePosition());
+ LightningBolt.clearCopperOnLightningStrike(this.level(), this.getStrikePosition(), this); // Paper - Call EntityChangeBlockEvent
this.gameEvent(GameEvent.LIGHTNING_STRIKE);
}
}
@@ -120,7 +125,7 @@
}
}
- this.discard();
+ this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
} else if (this.life < -this.random.nextInt(10)) {
--this.flashes;
this.life = 1;
@@ -129,7 +134,7 @@
}
}
- if (this.life >= 0) {
+ if (this.life >= 0 && !this.isEffect) { // CraftBukkit - add !this.visualOnly // Paper - Properly handle lightning effects api
if (!(this.level() instanceof ServerLevel)) {
this.level().setSkyFlashTime(2);
} else if (!this.visualOnly) {
@@ -158,7 +163,7 @@
}
private void spawnFire(int spreadAttempts) {
- if (!this.visualOnly) {
+ if (!this.visualOnly && !this.isEffect) { // Paper - Properly handle lightning effects api
Level world = this.level();
if (world instanceof ServerLevel) {
@@ -169,8 +174,12 @@
BlockState iblockdata = BaseFireBlock.getState(this.level(), blockposition);
if (this.level().getBlockState(blockposition).isAir() && iblockdata.canSurvive(this.level(), blockposition)) {
- this.level().setBlockAndUpdate(blockposition, iblockdata);
- ++this.blocksSetOnFire;
+ // CraftBukkit start - add "!visualOnly"
+ if (!this.visualOnly && !CraftEventFactory.callBlockIgniteEvent(this.level(), blockposition, this).isCancelled()) {
+ this.level().setBlockAndUpdate(blockposition, iblockdata);
+ ++this.blocksSetOnFire;
+ }
+ // CraftBukkit end
}
for (int j = 0; j < spreadAttempts; ++j) {
@@ -178,8 +187,12 @@
iblockdata = BaseFireBlock.getState(this.level(), blockposition1);
if (this.level().getBlockState(blockposition1).isAir() && iblockdata.canSurvive(this.level(), blockposition1)) {
- this.level().setBlockAndUpdate(blockposition1, iblockdata);
- ++this.blocksSetOnFire;
+ // CraftBukkit start - add "!visualOnly"
+ if (!this.visualOnly && !CraftEventFactory.callBlockIgniteEvent(this.level(), blockposition1, this).isCancelled()) {
+ this.level().setBlockAndUpdate(blockposition1, iblockdata);
+ ++this.blocksSetOnFire;
+ }
+ // CraftBukkit end
}
}
@@ -190,7 +203,7 @@
}
- private static void clearCopperOnLightningStrike(Level world, BlockPos pos) {
+ private static void clearCopperOnLightningStrike(Level world, BlockPos pos, Entity lightning) { // Paper - Call EntityChangeBlockEvent
BlockState iblockdata = world.getBlockState(pos);
BlockPos blockposition1;
BlockState iblockdata1;
@@ -204,24 +217,29 @@
}
if (iblockdata1.getBlock() instanceof WeatheringCopper) {
- world.setBlockAndUpdate(blockposition1, WeatheringCopper.getFirst(world.getBlockState(blockposition1)));
+ // Paper start - Call EntityChangeBlockEvent
+ BlockState newBlock = WeatheringCopper.getFirst(world.getBlockState(blockposition1));
+ if (CraftEventFactory.callEntityChangeBlockEvent(lightning, blockposition1, newBlock)) {
+ world.setBlockAndUpdate(blockposition1, newBlock);
+ }
+ // Paper end - Call EntityChangeBlockEvent
BlockPos.MutableBlockPos blockposition_mutableblockposition = pos.mutable();
int i = world.random.nextInt(3) + 3;
for (int j = 0; j < i; ++j) {
int k = world.random.nextInt(8) + 1;
- LightningBolt.randomWalkCleaningCopper(world, blockposition1, blockposition_mutableblockposition, k);
+ LightningBolt.randomWalkCleaningCopper(world, blockposition1, blockposition_mutableblockposition, k, lightning); // Paper - transmit LightningBolt instance to call EntityChangeBlockEvent
}
}
}
- private static void randomWalkCleaningCopper(Level world, BlockPos pos, BlockPos.MutableBlockPos mutablePos, int count) {
+ private static void randomWalkCleaningCopper(Level world, BlockPos pos, BlockPos.MutableBlockPos mutablePos, int count, Entity lightning) { // Paper - transmit LightningBolt instance to call EntityChangeBlockEvent
mutablePos.set(pos);
for (int j = 0; j < count; ++j) {
- Optional<BlockPos> optional = LightningBolt.randomStepCleaningCopper(world, mutablePos);
+ Optional<BlockPos> optional = LightningBolt.randomStepCleaningCopper(world, mutablePos, lightning); // Paper - transmit LightningBolt instance to call EntityChangeBlockEvent
if (optional.isEmpty()) {
break;
@@ -232,7 +250,7 @@
}
- private static Optional<BlockPos> randomStepCleaningCopper(Level world, BlockPos pos) {
+ private static Optional<BlockPos> randomStepCleaningCopper(Level world, BlockPos pos, Entity lightning) { // Paper - transmit LightningBolt instance to call EntityChangeBlockEvent
Iterator iterator = BlockPos.randomInCube(world.random, 10, pos, 1).iterator();
BlockPos blockposition1;
@@ -247,8 +265,10 @@
iblockdata = world.getBlockState(blockposition1);
} while (!(iblockdata.getBlock() instanceof WeatheringCopper));
+ BlockPos blockposition1Final = blockposition1; // CraftBukkit - decompile error
WeatheringCopper.getPrevious(iblockdata).ifPresent((iblockdata1) -> {
- world.setBlockAndUpdate(blockposition1, iblockdata1);
+ if (CraftEventFactory.callEntityChangeBlockEvent(lightning, blockposition1Final, iblockdata1)) // Paper - call EntityChangeBlockEvent
+ world.setBlockAndUpdate(blockposition1Final, iblockdata1); // CraftBukkit - decompile error
});
world.levelEvent(3002, blockposition1, -1);
return Optional.of(blockposition1);