From d2c422f13e5226a81b7fe3f8355922beed2a9cfc Mon Sep 17 00:00:00 2001
From: nathat890 <nathat890@outlook.com>
Date: Wed, 14 Nov 2018 14:10:22 +1100
Subject: [PATCH] SPIGOT-4339: Add EntityTransformEvent

Thanks klugemonkey for some contributions in https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/pull-requests/351/overview
---
 nms-patches/EntityMushroomCow.patch           | 25 +++++++++++---
 nms-patches/EntityPig.patch                   | 18 +++++++---
 nms-patches/EntitySlime.patch                 | 16 ++++++---
 nms-patches/EntityVillager.patch              | 24 ++++++++++---
 nms-patches/EntityZombie.patch                | 34 +++++++++++++------
 nms-patches/EntityZombieVillager.patch        | 26 +++++++++++---
 .../craftbukkit/event/CraftEventFactory.java  | 10 ++++++
 7 files changed, 119 insertions(+), 34 deletions(-)

diff --git a/nms-patches/EntityMushroomCow.patch b/nms-patches/EntityMushroomCow.patch
index 0be5ad0793..e39e27813e 100644
--- a/nms-patches/EntityMushroomCow.patch
+++ b/nms-patches/EntityMushroomCow.patch
@@ -1,14 +1,19 @@
 --- a/net/minecraft/server/EntityMushroomCow.java
 +++ b/net/minecraft/server/EntityMushroomCow.java
-@@ -1,6 +1,7 @@
- package net.minecraft.server;
+@@ -2,6 +2,12 @@
  
  import javax.annotation.Nullable;
-+import org.bukkit.event.player.PlayerShearEntityEvent; // CraftBukkit
  
++// CraftBukkit start
++import org.bukkit.craftbukkit.event.CraftEventFactory;
++import org.bukkit.event.entity.EntityTransformEvent;
++import org.bukkit.event.player.PlayerShearEntityEvent;
++// CraftBukkit end
++
  public class EntityMushroomCow extends EntityCow {
  
-@@ -23,6 +24,14 @@
+     public EntityMushroomCow(World world) {
+@@ -23,6 +29,14 @@
  
              return true;
          } else if (itemstack.getItem() == Items.SHEARS && this.getAge() >= 0) {
@@ -23,3 +28,15 @@
              this.world.addParticle(Particles.u, this.locX, this.locY + (double) (this.length / 2.0F), this.locZ, 0.0D, 0.0D, 0.0D);
              if (!this.world.isClientSide) {
                  this.die();
+@@ -35,6 +49,11 @@
+                     entitycow.setCustomName(this.getCustomName());
+                 }
+ 
++                // CraftBukkit start
++                if (CraftEventFactory.callEntityTransformEvent(this, entitycow, EntityTransformEvent.TransformReason.SHEARED).isCancelled()) {
++                    return false;
++                }
++                // CraftBukkit end
+                 this.world.addEntity(entitycow);
+ 
+                 for (int i = 0; i < 5; ++i) {
diff --git a/nms-patches/EntityPig.patch b/nms-patches/EntityPig.patch
index 0266917607..83c892472f 100644
--- a/nms-patches/EntityPig.patch
+++ b/nms-patches/EntityPig.patch
@@ -1,15 +1,18 @@
 --- a/net/minecraft/server/EntityPig.java
 +++ b/net/minecraft/server/EntityPig.java
-@@ -2,6 +2,8 @@
+@@ -2,6 +2,11 @@
  
  import javax.annotation.Nullable;
  
-+import org.bukkit.craftbukkit.event.CraftEventFactory; // CraftBukkit
++// CraftBukkit start
++import org.bukkit.craftbukkit.event.CraftEventFactory;
++import org.bukkit.event.entity.EntityTransformEvent;
++// CraftBukkit end
 +
  public class EntityPig extends EntityAnimal {
  
      private static final DataWatcherObject<Boolean> bC = DataWatcher.a(EntityPig.class, DataWatcherRegistry.i);
-@@ -118,13 +120,14 @@
+@@ -118,13 +123,14 @@
      }
  
      public void die(DamageSource damagesource) {
@@ -25,7 +28,7 @@
      }
  
      @Nullable
-@@ -149,6 +152,12 @@
+@@ -149,6 +155,12 @@
          if (!this.world.isClientSide && !this.dead) {
              EntityPigZombie entitypigzombie = new EntityPigZombie(this.world);
  
@@ -38,11 +41,16 @@
              entitypigzombie.setSlot(EnumItemSlot.MAINHAND, new ItemStack(Items.GOLDEN_SWORD));
              entitypigzombie.setPositionRotation(this.locX, this.locY, this.locZ, this.yaw, this.pitch);
              entitypigzombie.setNoAI(this.isNoAI());
-@@ -157,7 +166,8 @@
+@@ -157,7 +169,13 @@
                  entitypigzombie.setCustomNameVisible(this.getCustomNameVisible());
              }
  
 -            this.world.addEntity(entitypigzombie);
++            // CraftBukkit start
++            if (CraftEventFactory.callEntityTransformEvent(this, entitypigzombie, EntityTransformEvent.TransformReason.LIGHTNING).isCancelled()) {
++                return;
++            }
++            // CraftBukkit end
 +            // CraftBukkit - added a reason for spawning this creature
 +            this.world.addEntity(entitypigzombie, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.LIGHTNING);
              this.die();
diff --git a/nms-patches/EntitySlime.patch b/nms-patches/EntitySlime.patch
index 0e817f257b..c3edb9da7c 100644
--- a/nms-patches/EntitySlime.patch
+++ b/nms-patches/EntitySlime.patch
@@ -1,16 +1,18 @@
 --- a/net/minecraft/server/EntitySlime.java
 +++ b/net/minecraft/server/EntitySlime.java
-@@ -1,6 +1,9 @@
+@@ -1,6 +1,11 @@
  package net.minecraft.server;
  
  import javax.annotation.Nullable;
 +// CraftBukkit start
++import org.bukkit.craftbukkit.event.CraftEventFactory;
++import org.bukkit.event.entity.EntityTransformEvent;
 +import org.bukkit.event.entity.SlimeSplitEvent;
 +// CraftBukkit end
  
  public class EntitySlime extends EntityInsentient implements IMonster {
  
-@@ -134,7 +137,7 @@
+@@ -134,7 +139,7 @@
      }
  
      public EntityTypes<? extends EntitySlime> P() {
@@ -19,7 +21,7 @@
      }
  
      public void die() {
-@@ -143,6 +146,18 @@
+@@ -143,6 +148,18 @@
          if (!this.world.isClientSide && i > 1 && this.getHealth() <= 0.0F) {
              int j = 2 + this.random.nextInt(3);
  
@@ -38,11 +40,17 @@
              for (int k = 0; k < j; ++k) {
                  float f = ((float) (k % 2) - 0.5F) * (float) i / 4.0F;
                  float f1 = ((float) (k / 2) - 0.5F) * (float) i / 4.0F;
-@@ -158,7 +173,7 @@
+@@ -158,7 +175,13 @@
  
                  entityslime.setSize(i / 2, true);
                  entityslime.setPositionRotation(this.locX + (double) f, this.locY + 0.5D, this.locZ + (double) f1, this.random.nextFloat() * 360.0F, 0.0F);
 -                this.world.addEntity(entityslime);
++
++                // CraftBukkit start
++                if (CraftEventFactory.callEntityTransformEvent(this, entityslime, EntityTransformEvent.TransformReason.SPLIT).isCancelled()) {
++                    continue;
++                }
++                // CraftBukkit end
 +                this.world.addEntity(entityslime, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.SLIME_SPLIT); // CraftBukkit - SpawnReason
              }
          }
diff --git a/nms-patches/EntityVillager.patch b/nms-patches/EntityVillager.patch
index 6cfcdfc52c..7d9705c9d4 100644
--- a/nms-patches/EntityVillager.patch
+++ b/nms-patches/EntityVillager.patch
@@ -1,21 +1,23 @@
 --- a/net/minecraft/server/EntityVillager.java
 +++ b/net/minecraft/server/EntityVillager.java
-@@ -7,6 +7,14 @@
+@@ -7,6 +7,16 @@
  import javax.annotation.Nullable;
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
 +// CraftBukkit start
 +import org.bukkit.Bukkit;
 +import org.bukkit.craftbukkit.entity.CraftVillager;
++import org.bukkit.craftbukkit.event.CraftEventFactory;
 +import org.bukkit.craftbukkit.inventory.CraftMerchantRecipe;
 +import org.bukkit.entity.Villager;
++import org.bukkit.event.entity.EntityTransformEvent;
 +import org.bukkit.event.entity.VillagerAcquireTradeEvent;
 +import org.bukkit.event.entity.VillagerReplenishTradeEvent;
 +// CraftBukkit end
  
  public class EntityVillager extends EntityAgeable implements NPC, IMerchant {
  
-@@ -38,7 +46,7 @@
+@@ -38,7 +48,7 @@
  
      public EntityVillager(World world, int i) {
          super(EntityTypes.VILLAGER, world);
@@ -24,7 +26,7 @@
          this.setProfession(i);
          this.setSize(0.6F, 1.95F);
          ((Navigation) this.getNavigation()).a(true);
-@@ -120,7 +128,14 @@
+@@ -120,7 +130,14 @@
                          MerchantRecipe merchantrecipe = (MerchantRecipe) iterator.next();
  
                          if (merchantrecipe.h()) {
@@ -40,7 +42,7 @@
                          }
                      }
  
-@@ -132,7 +147,7 @@
+@@ -132,7 +149,7 @@
                      }
                  }
  
@@ -49,7 +51,7 @@
              }
          }
  
-@@ -434,7 +449,20 @@
+@@ -434,7 +451,20 @@
                  for (int l = 0; l < k; ++l) {
                      EntityVillager.IMerchantRecipeOption entityvillager_imerchantrecipeoption = aentityvillager_imerchantrecipeoption3[l];
  
@@ -71,3 +73,15 @@
                  }
              }
  
+@@ -574,6 +604,11 @@
+                 entitywitch.setCustomNameVisible(this.getCustomNameVisible());
+             }
+ 
++            // CraftBukkit start
++            if (CraftEventFactory.callEntityTransformEvent(this, entitywitch, EntityTransformEvent.TransformReason.LIGHTNING).isCancelled()) {
++                return;
++            }
++            // CraftBukkit end
+             this.world.addEntity(entitywitch);
+             this.die();
+         }
diff --git a/nms-patches/EntityZombie.patch b/nms-patches/EntityZombie.patch
index 4a6dc06cc9..e0c799e0fe 100644
--- a/nms-patches/EntityZombie.patch
+++ b/nms-patches/EntityZombie.patch
@@ -1,20 +1,22 @@
 --- a/net/minecraft/server/EntityZombie.java
 +++ b/net/minecraft/server/EntityZombie.java
-@@ -6,6 +6,13 @@
+@@ -6,6 +6,15 @@
  import java.util.UUID;
  import javax.annotation.Nullable;
  
 +// CraftBukkit start
++import org.bukkit.craftbukkit.event.CraftEventFactory;
 +import org.bukkit.event.entity.CreatureSpawnEvent;
 +import org.bukkit.event.entity.EntityCombustByEntityEvent;
 +import org.bukkit.event.entity.EntityCombustEvent;
 +import org.bukkit.event.entity.EntityTargetEvent;
++import org.bukkit.event.entity.EntityTransformEvent;
 +// CraftBukkit end
 +
  public class EntityZombie extends EntityMonster {
  
      protected static final IAttribute c = (new AttributeRanged((IAttribute) null, "zombie.spawnReinforcements", 0.0D, 0.0D, 1.0D)).a("Spawn Reinforcements Chance");
-@@ -21,6 +28,7 @@
+@@ -21,6 +30,7 @@
      private int bJ;
      private float bK;
      private float bL;
@@ -22,7 +24,7 @@
  
      public EntityZombie(EntityTypes<?> entitytypes, World world) {
          super(entitytypes, world);
-@@ -143,8 +151,12 @@
+@@ -143,8 +153,12 @@
  
      public void tick() {
          if (!this.world.isClientSide) {
@@ -37,7 +39,7 @@
                  if (this.bJ < 0) {
                      this.dE();
                  }
-@@ -182,7 +194,14 @@
+@@ -182,7 +196,14 @@
              }
  
              if (flag) {
@@ -53,16 +55,21 @@
              }
          }
  
-@@ -221,7 +240,7 @@
+@@ -221,7 +242,12 @@
                  entityzombie.setCustomNameVisible(this.getCustomNameVisible());
              }
  
 -            this.world.addEntity(entityzombie);
++            // CraftBukkit start
++            if (CraftEventFactory.callEntityTransformEvent(this, entityzombie, EntityTransformEvent.TransformReason.DROWNED).isCancelled()) {
++                return;
++            }
++            // CraftBukkit end
 +            this.world.addEntity(entityzombie, CreatureSpawnEvent.SpawnReason.DROWNED); // CraftBukkit - added spawn reason
              this.die();
          }
      }
-@@ -252,8 +271,8 @@
+@@ -252,8 +278,8 @@
                      if (this.world.getType(new BlockPosition(i1, j1 - 1, k1)).q() && this.world.getLightLevel(new BlockPosition(i1, j1, k1)) < 10) {
                          entityzombie.setPosition((double) i1, (double) j1, (double) k1);
                          if (!this.world.isPlayerNearby((double) i1, (double) j1, (double) k1, 7.0D) && this.world.a_(entityzombie, entityzombie.getBoundingBox()) && this.world.getCubes(entityzombie, entityzombie.getBoundingBox()) && !this.world.containsLiquid(entityzombie.getBoundingBox())) {
@@ -73,7 +80,7 @@
                              entityzombie.prepare(this.world.getDamageScaler(new BlockPosition(entityzombie)), (GroupDataEntity) null, (NBTTagCompound) null);
                              this.getAttributeInstance(EntityZombie.c).b(new AttributeModifier("Zombie reinforcement caller charge", -0.05000000074505806D, 0));
                              entityzombie.getAttributeInstance(EntityZombie.c).b(new AttributeModifier("Zombie reinforcement callee charge", -0.05000000074505806D, 0));
-@@ -276,7 +295,14 @@
+@@ -276,7 +302,14 @@
              float f = this.world.getDamageScaler(new BlockPosition(this)).b();
  
              if (this.getItemInMainHand().isEmpty() && this.isBurning() && this.random.nextFloat() < f * 0.3F) {
@@ -89,16 +96,21 @@
              }
          }
  
-@@ -372,7 +398,7 @@
+@@ -372,7 +405,12 @@
                  entityzombievillager.setCustomNameVisible(entityvillager.getCustomNameVisible());
              }
  
 -            this.world.addEntity(entityzombievillager);
++            // CraftBukkit start
++            if (CraftEventFactory.callEntityTransformEvent(this, entityzombievillager, EntityTransformEvent.TransformReason.INFECTION).isCancelled()) {
++                return;
++            }
++            // CraftBukkit end
 +            this.world.addEntity(entityzombievillager, CreatureSpawnEvent.SpawnReason.INFECTION); // CraftBukkit - add SpawnReason
              this.world.a((EntityHuman) null, 1026, new BlockPosition(this), 0);
          }
  
-@@ -422,7 +448,7 @@
+@@ -422,7 +460,7 @@
                      entitychicken1.setPositionRotation(this.locX, this.locY, this.locZ, this.yaw, 0.0F);
                      entitychicken1.prepare(difficultydamagescaler, (GroupDataEntity) null, (NBTTagCompound) null);
                      entitychicken1.s(true);
@@ -107,7 +119,7 @@
                      this.startRiding(entitychicken1);
                  }
              }
-@@ -495,7 +521,7 @@
+@@ -495,7 +533,7 @@
      }
  
      public void die(DamageSource damagesource) {
@@ -116,7 +128,7 @@
          if (damagesource.getEntity() instanceof EntityCreeper) {
              EntityCreeper entitycreeper = (EntityCreeper) damagesource.getEntity();
  
-@@ -508,6 +534,7 @@
+@@ -508,6 +546,7 @@
                  }
              }
          }
diff --git a/nms-patches/EntityZombieVillager.patch b/nms-patches/EntityZombieVillager.patch
index adbd9da466..895bf78958 100644
--- a/nms-patches/EntityZombieVillager.patch
+++ b/nms-patches/EntityZombieVillager.patch
@@ -1,6 +1,17 @@
 --- a/net/minecraft/server/EntityZombieVillager.java
 +++ b/net/minecraft/server/EntityZombieVillager.java
-@@ -9,6 +9,7 @@
+@@ -2,6 +2,10 @@
+ 
+ import java.util.UUID;
+ import javax.annotation.Nullable;
++// CraftBukkit start
++import org.bukkit.craftbukkit.event.CraftEventFactory;
++import org.bukkit.event.entity.EntityTransformEvent;
++// CraftBukkit end
+ 
+ public class EntityZombieVillager extends EntityZombie {
+ 
+@@ -9,6 +13,7 @@
      private static final DataWatcherObject<Integer> b = DataWatcher.a(EntityZombieVillager.class, DataWatcherRegistry.b);
      private int conversionTime;
      private UUID bD;
@@ -8,7 +19,7 @@
  
      public EntityZombieVillager(World world) {
          super(EntityTypes.ZOMBIE_VILLAGER, world);
-@@ -54,8 +55,13 @@
+@@ -54,8 +59,13 @@
      }
  
      public void tick() {
@@ -23,7 +34,7 @@
  
              this.conversionTime -= i;
              if (this.conversionTime <= 0) {
-@@ -100,8 +106,10 @@
+@@ -100,8 +110,10 @@
          this.bD = uuid;
          this.conversionTime = i;
          this.getDataWatcher().set(EntityZombieVillager.a, Boolean.valueOf(true));
@@ -36,16 +47,21 @@
          this.world.broadcastEntityEffect(this, (byte) 16);
      }
  
-@@ -123,7 +131,7 @@
+@@ -123,7 +135,12 @@
              entityvillager.setCustomNameVisible(this.getCustomNameVisible());
          }
  
 -        this.world.addEntity(entityvillager);
++        // CraftBukkit start
++        if (CraftEventFactory.callEntityTransformEvent(this, entityvillager, EntityTransformEvent.TransformReason.CURED).isCancelled()) {
++            return;
++        }
++        // CraftBukkit end
 +        this.world.addEntity(entityvillager, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.CURED); // CraftBukkit - add SpawnReason
          if (this.bD != null) {
              EntityHuman entityhuman = this.world.b(this.bD);
  
-@@ -132,7 +140,7 @@
+@@ -132,7 +149,7 @@
              }
          }
  
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 328322c159..aca3214785 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -300,6 +300,16 @@ public class CraftEventFactory {
         return event;
     }
 
+    /**
+     * EntityTransformEvent
+     */
+    public static EntityTransformEvent callEntityTransformEvent(EntityLiving original, EntityLiving converted, EntityTransformEvent.TransformReason convertType) {
+        EntityTransformEvent event = new EntityTransformEvent(original.getBukkitEntity(), converted.getBukkitEntity(), convertType);
+        Bukkit.getPluginManager().callEvent(event);
+
+        return event;
+    }
+
     /**
      * EntityShootBowEvent
      */