From e797fdd58affad1d116b8534bd3859130c4a817b Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Tue, 13 Jul 2021 19:16:36 +1000 Subject: [PATCH] #886: Add new Causes for LightningStrikeEvent By: Doc --- .../java/org/bukkit/craftbukkit/CraftWorld.java | 5 +++-- .../craftbukkit/event/CraftEventFactory.java | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index dfaffdc67c..c0225ec9ce 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -258,6 +258,7 @@ import org.bukkit.entity.minecart.PoweredMinecart; import org.bukkit.entity.minecart.SpawnerMinecart; import org.bukkit.entity.minecart.StorageMinecart; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.bukkit.event.weather.LightningStrikeEvent; import org.bukkit.event.world.SpawnChangeEvent; import org.bukkit.event.world.TimeSkipEvent; import org.bukkit.generator.BlockPopulator; @@ -674,7 +675,7 @@ public class CraftWorld implements World { public LightningStrike strikeLightning(Location loc) { EntityLightning lightning = EntityTypes.LIGHTNING_BOLT.a(world); lightning.teleportAndSync(loc.getX(), loc.getY(), loc.getZ()); - world.strikeLightning(lightning); + world.strikeLightning(lightning, LightningStrikeEvent.Cause.CUSTOM); return (LightningStrike) lightning.getBukkitEntity(); } @@ -683,7 +684,7 @@ public class CraftWorld implements World { EntityLightning lightning = EntityTypes.LIGHTNING_BOLT.a(world); lightning.teleportAndSync(loc.getX(), loc.getY(), loc.getZ()); lightning.setEffect(true); - world.strikeLightning(lightning); + world.strikeLightning(lightning, LightningStrikeEvent.Cause.CUSTOM); return (LightningStrike) lightning.getBukkitEntity(); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index b856486c20..1be8babf3c 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -594,7 +594,22 @@ public class CraftEventFactory { } else if (entity.getBukkitEntity() instanceof org.bukkit.entity.Vehicle) { event = CraftEventFactory.callVehicleCreateEvent(entity); } else if (entity.getBukkitEntity() instanceof org.bukkit.entity.LightningStrike) { - LightningStrikeEvent.Cause cause = (spawnReason == SpawnReason.COMMAND ? LightningStrikeEvent.Cause.COMMAND : LightningStrikeEvent.Cause.UNKNOWN); + LightningStrikeEvent.Cause cause = LightningStrikeEvent.Cause.UNKNOWN; + switch (spawnReason) { + case COMMAND: + cause = LightningStrikeEvent.Cause.COMMAND; + break; + case CUSTOM: + cause = LightningStrikeEvent.Cause.CUSTOM; + break; + case SPAWNER: + cause = LightningStrikeEvent.Cause.SPAWNER; + break; + } + // This event is called in nms-patches for common causes like Weather, Trap or Trident (SpawnReason.DEFAULT) then can ignore this cases for avoid two calls to this event + if (cause == LightningStrikeEvent.Cause.UNKNOWN && spawnReason == SpawnReason.DEFAULT) { + return true; + } event = CraftEventFactory.callLightningStrikeEvent((LightningStrike) entity.getBukkitEntity(), cause); } else if (!(entity instanceof EntityPlayer)) { event = CraftEventFactory.callEntitySpawnEvent(entity);