From 472983f42346f87cffb7098715aba7c09733558a Mon Sep 17 00:00:00 2001 From: AlphaBlend Date: Sun, 16 Oct 2016 23:19:30 -0700 Subject: [PATCH] Add EntityZapEvent --- .../world/entity/ConversionParams.java.patch | 14 +++++++ .../net/minecraft/world/entity/Mob.java.patch | 41 ++++++++++++------- .../world/entity/npc/Villager.java.patch | 21 ++++++++-- .../craftbukkit/event/CraftEventFactory.java | 8 ++++ 4 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 paper-server/patches/sources/net/minecraft/world/entity/ConversionParams.java.patch diff --git a/paper-server/patches/sources/net/minecraft/world/entity/ConversionParams.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/ConversionParams.java.patch new file mode 100644 index 0000000000..3673cd3667 --- /dev/null +++ b/paper-server/patches/sources/net/minecraft/world/entity/ConversionParams.java.patch @@ -0,0 +1,14 @@ +--- a/net/minecraft/world/entity/ConversionParams.java ++++ b/net/minecraft/world/entity/ConversionParams.java +@@ -12,4 +12,11 @@ + public interface AfterConversion { + void finalizeConversion(T convertedEntity); + } ++ ++ // Paper start - entity zap event - allow conversion to be cancelled during finalization ++ @FunctionalInterface ++ public interface CancellingAfterConversion { ++ boolean finalizeConversionOrCancel(final T convertedEntity); ++ } ++ // Paper start - entity zap event - allow conversion to be cancelled during finalization + } diff --git a/paper-server/patches/sources/net/minecraft/world/entity/Mob.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/Mob.java.patch index 43fb144bfd..533625a978 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/Mob.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/Mob.java.patch @@ -26,15 +26,15 @@ public GoalSelector targetSelector; @Nullable private LivingEntity target; -@@ -132,6 +144,8 @@ +@@ -131,6 +143,8 @@ + private Leashable.LeashData leashData; private BlockPos restrictCenter; private float restrictRadius; - -+ public boolean aware = true; // CraftBukkit + ++ public boolean aware = true; // CraftBukkit + protected Mob(EntityType type, Level world) { super(type, world); - this.handItems = NonNullList.withSize(2, ItemStack.EMPTY); @@ -160,6 +174,12 @@ } @@ -48,7 +48,7 @@ protected void registerGoals() {} public static AttributeSupplier.Builder createMobAttributes() { -@@ -264,11 +284,42 @@ +@@ -264,13 +284,44 @@ @Nullable protected final LivingEntity getTargetFromBrain() { @@ -60,8 +60,8 @@ - this.target = target; + // CraftBukkit start - fire event + this.setTarget(target, EntityTargetEvent.TargetReason.UNKNOWN, true); -+ } -+ + } + + public boolean setTarget(LivingEntity entityliving, EntityTargetEvent.TargetReason reason, boolean fireEvent) { + if (this.getTarget() == entityliving) return false; + if (fireEvent) { @@ -90,9 +90,11 @@ + this.target = entityliving; + return true; + // CraftBukkit end - } - ++ } ++ @Override + public boolean canAttackType(EntityType type) { + return type != EntityType.GHAST; @@ -399,6 +450,12 @@ return null; } @@ -276,7 +278,7 @@ optional.ifPresent((entityinsentient) -> { this.onOffspringSpawnedFromEgg(player, entityinsentient); -@@ -1389,28 +1490,45 @@ +@@ -1389,28 +1490,51 @@ return this.restrictRadius != -1.0F; } @@ -288,6 +290,12 @@ + + @Nullable + public T convertTo(EntityType entitytypes, ConversionParams conversionparams, EntitySpawnReason entityspawnreason, ConversionParams.AfterConversion conversionparams_a, EntityTransformEvent.TransformReason transformReason, CreatureSpawnEvent.SpawnReason spawnReason) { ++ // Paper start - entity zap event - allow cancellation of conversion post creation ++ return this.convertTo(entitytypes, conversionparams, entityspawnreason, e -> { conversionparams_a.finalizeConversion(e); return true; }, transformReason, spawnReason); ++ } ++ @Nullable ++ public T convertTo(EntityType entitytypes, ConversionParams conversionparams, EntitySpawnReason entityspawnreason, ConversionParams.CancellingAfterConversion conversionparams_a, EntityTransformEvent.TransformReason transformReason, CreatureSpawnEvent.SpawnReason spawnReason) { ++ // Paper end - entity zap event - allow cancellation of conversion post creation + // CraftBukkit end if (this.isRemoved()) { return null; @@ -301,7 +309,7 @@ - context.type().convert(this, t0, context); - finalizer.finalizeConversion(t0); + conversionparams.type().convert(this, t0, conversionparams); -+ conversionparams_a.finalizeConversion(t0); ++ if (!conversionparams_a.finalizeConversionOrCancel(t0)) return null; // Paper - entity zap event - return null if conversion was cancelled Level world = this.level(); + // CraftBukkit start @@ -328,7 +336,7 @@ } return t0; -@@ -1420,10 +1538,17 @@ +@@ -1420,10 +1544,22 @@ @Nullable public T convertTo(EntityType entityType, ConversionParams context, ConversionParams.AfterConversion finalizer) { @@ -339,6 +347,11 @@ @Nullable + public T convertTo(EntityType entitytypes, ConversionParams conversionparams, ConversionParams.AfterConversion conversionparams_a, EntityTransformEvent.TransformReason transformReason, CreatureSpawnEvent.SpawnReason spawnReason) { ++ // Paper start - entity zap event - allow cancellation of conversion post creation ++ return this.convertTo(entitytypes, conversionparams, e -> { conversionparams_a.finalizeConversion(e); return true; }, transformReason, spawnReason); ++ } ++ public T convertTo(EntityType entitytypes, ConversionParams conversionparams, ConversionParams.CancellingAfterConversion conversionparams_a, EntityTransformEvent.TransformReason transformReason, CreatureSpawnEvent.SpawnReason spawnReason) { ++ // Paper start - entity zap event - allow cancellation of conversion post creation + return this.convertTo(entitytypes, conversionparams, EntitySpawnReason.CONVERSION, conversionparams_a, transformReason, spawnReason); + // CraftBukkit end + } @@ -347,7 +360,7 @@ @Override public Leashable.LeashData getLeashData() { return this.leashData; -@@ -1458,6 +1583,7 @@ +@@ -1458,6 +1594,7 @@ boolean flag1 = super.startRiding(entity, force); if (flag1 && this.isLeashed()) { @@ -355,7 +368,7 @@ this.dropLeash(); } -@@ -1542,7 +1668,7 @@ +@@ -1542,7 +1679,7 @@ if (f1 > 0.0F && target instanceof LivingEntity) { entityliving = (LivingEntity) target; diff --git a/paper-server/patches/sources/net/minecraft/world/entity/npc/Villager.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/npc/Villager.java.patch index 792919030c..50d1a6008e 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/npc/Villager.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/npc/Villager.java.patch @@ -109,16 +109,29 @@ Entity entity = damageSource.getEntity(); if (entity != null) { -@@ -808,7 +839,7 @@ +@@ -803,12 +834,19 @@ + @Override + public void thunderHit(ServerLevel world, LightningBolt lightning) { + if (world.getDifficulty() != Difficulty.PEACEFUL) { +- Villager.LOGGER.info("Villager {} was struck by lightning {}.", this, lightning); ++ // Paper - Add EntityZapEvent; move log down, event can cancel + Witch entitywitch = (Witch) this.convertTo(EntityType.WITCH, ConversionParams.single(this, false, false), (entitywitch1) -> { ++ // Paper start - Add EntityZapEvent ++ if (org.bukkit.craftbukkit.event.CraftEventFactory.callEntityZapEvent(this, lightning, entitywitch1).isCancelled()) { ++ return false; ++ } ++ if (org.spigotmc.SpigotConfig.logVillagerDeaths) Villager.LOGGER.info("Villager {} was struck by lightning {}.", this, lightning); // Move down ++ // Paper end - Add EntityZapEvent entitywitch1.finalizeSpawn(world, world.getCurrentDifficultyAt(entitywitch1.blockPosition()), EntitySpawnReason.CONVERSION, (SpawnGroupData) null); entitywitch1.setPersistenceRequired(); this.releaseAllPois(); - }); ++ return true; // Paper start - Add EntityZapEvent + }, EntityTransformEvent.TransformReason.LIGHTNING, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.LIGHTNING); // CraftBukkit if (entitywitch == null) { super.thunderHit(world, lightning); -@@ -906,7 +937,7 @@ +@@ -906,7 +944,7 @@ }).limit(5L).toList(); if (list1.size() >= requiredCount) { @@ -127,7 +140,7 @@ list.forEach(GolemSensor::golemDetected); } } -@@ -963,7 +994,7 @@ +@@ -963,7 +1001,7 @@ @Override public void startSleeping(BlockPos pos) { super.startSleeping(pos); @@ -136,7 +149,7 @@ this.brain.eraseMemory(MemoryModuleType.WALK_TARGET); this.brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE); } -@@ -971,7 +1002,7 @@ +@@ -971,7 +1009,7 @@ @Override public void stopSleeping() { super.stopSleeping(); 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 9100ea65e8..40c298cf44 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 @@ -1215,6 +1215,14 @@ public class CraftEventFactory { return !event.isCancelled(); } + // Paper start + public static com.destroystokyo.paper.event.entity.EntityZapEvent callEntityZapEvent(Entity entity, Entity lightning, Entity changedEntity) { + com.destroystokyo.paper.event.entity.EntityZapEvent event = new com.destroystokyo.paper.event.entity.EntityZapEvent(entity.getBukkitEntity(), (LightningStrike) lightning.getBukkitEntity(), changedEntity.getBukkitEntity()); + entity.getBukkitEntity().getServer().getPluginManager().callEvent(event); + return event; + } + // Paper end + public static boolean callEntityChangeBlockEvent(Entity entity, BlockPos position, net.minecraft.world.level.block.state.BlockState newBlock) { return CraftEventFactory.callEntityChangeBlockEvent(entity, position, newBlock, false); }