From 310d8c53aafbfa899f5c2925f77c1859881ff2a0 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sun, 24 Oct 2021 20:29:45 -0700
Subject: [PATCH] Fix issues with mob conversion

---
 .../world/entity/monster/Skeleton.java.patch    | 17 +++++++++++++++--
 .../entity/monster/hoglin/Hoglin.java.patch     | 15 ++++++++++++---
 .../monster/piglin/AbstractPiglin.java.patch    | 13 +++++++++++--
 3 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/paper-server/patches/sources/net/minecraft/world/entity/monster/Skeleton.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/monster/Skeleton.java.patch
index 8be9303de5..d17fea360f 100644
--- a/paper-server/patches/sources/net/minecraft/world/entity/monster/Skeleton.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/entity/monster/Skeleton.java.patch
@@ -1,11 +1,24 @@
 --- a/net/minecraft/world/entity/monster/Skeleton.java
 +++ b/net/minecraft/world/entity/monster/Skeleton.java
-@@ -99,7 +99,7 @@
+@@ -94,12 +94,19 @@
+     }
+ 
+     protected void doFreezeConversion() {
+-        this.convertTo(EntityType.STRAY, ConversionParams.single(this, true, true), (entityskeletonstray) -> {
++        final Stray stray = this.convertTo(EntityType.STRAY, ConversionParams.single(this, true, true), (entityskeletonstray) -> { // Paper - Fix issues with mob conversion; reset conversion time to prevent event spam
+             if (!this.isSilent()) {
                  this.level().levelEvent((Player) null, 1048, this.blockPosition(), 0);
              }
  
 -        });
-+        }, org.bukkit.event.entity.EntityTransformEvent.TransformReason.FROZEN, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.FROZEN); // CraftBukkit - add spawn and transform reasons
++        }, org.bukkit.event.entity.EntityTransformEvent.TransformReason.FROZEN, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.FROZEN);// CraftBukkit - add spawn and transform reasons
++
++        // Paper start - Fix issues with mob conversion; reset conversion time to prevent event spam
++        if (stray == null) {
++            this.conversionTime = 300;
++        }
++        // Paper end - Fix issues with mob conversion
++
      }
  
      @Override
diff --git a/paper-server/patches/sources/net/minecraft/world/entity/monster/hoglin/Hoglin.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/monster/hoglin/Hoglin.java.patch
index 15431f40e0..345420cb17 100644
--- a/paper-server/patches/sources/net/minecraft/world/entity/monster/hoglin/Hoglin.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/entity/monster/hoglin/Hoglin.java.patch
@@ -19,16 +19,25 @@
      }
  
      @Override
-@@ -242,7 +243,7 @@
+@@ -240,9 +241,15 @@
+     }
+ 
      private void finishConversion() {
-         this.convertTo(EntityType.ZOGLIN, ConversionParams.single(this, true, false), (entityzoglin) -> {
+-        this.convertTo(EntityType.ZOGLIN, ConversionParams.single(this, true, false), (entityzoglin) -> {
++        net.minecraft.world.entity.Entity converted = this.convertTo(EntityType.ZOGLIN, ConversionParams.single(this, true, false), (entityzoglin) -> {
              entityzoglin.addEffect(new MobEffectInstance(MobEffects.CONFUSION, 200, 0));
 -        });
 +        }, org.bukkit.event.entity.EntityTransformEvent.TransformReason.PIGLIN_ZOMBIFIED, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.PIGLIN_ZOMBIFIED); // CraftBukkit - add spawn and transform reasons
++
++        // Paper start - Fix issues with mob conversion; reset to prevent event spam
++        if (converted == null) {
++            this.timeInOverworld = 0;
++        }
++        // Paper end - Fix issues with mob conversion
      }
  
      @Override
-@@ -326,7 +327,7 @@
+@@ -326,7 +333,7 @@
  
      @Override
      protected SoundEvent getAmbientSound() {
diff --git a/paper-server/patches/sources/net/minecraft/world/entity/monster/piglin/AbstractPiglin.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/monster/piglin/AbstractPiglin.java.patch
index d2f50d3274..68e189739c 100644
--- a/paper-server/patches/sources/net/minecraft/world/entity/monster/piglin/AbstractPiglin.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/entity/monster/piglin/AbstractPiglin.java.patch
@@ -1,11 +1,20 @@
 --- a/net/minecraft/world/entity/monster/piglin/AbstractPiglin.java
 +++ b/net/minecraft/world/entity/monster/piglin/AbstractPiglin.java
-@@ -102,7 +102,7 @@
+@@ -100,9 +100,15 @@
+     }
+ 
      protected void finishConversion(ServerLevel world) {
-         this.convertTo(EntityType.ZOMBIFIED_PIGLIN, ConversionParams.single(this, true, true), (entitypigzombie) -> {
+-        this.convertTo(EntityType.ZOMBIFIED_PIGLIN, ConversionParams.single(this, true, true), (entitypigzombie) -> {
++        net.minecraft.world.entity.Entity converted = this.convertTo(EntityType.ZOMBIFIED_PIGLIN, ConversionParams.single(this, true, true), (entitypigzombie) -> { // Paper - Fix issues with mob conversion; reset to prevent event spam
              entitypigzombie.addEffect(new MobEffectInstance(MobEffects.CONFUSION, 200, 0));
 -        });
 +        }, org.bukkit.event.entity.EntityTransformEvent.TransformReason.PIGLIN_ZOMBIFIED, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.PIGLIN_ZOMBIFIED); // CraftBukkit - add spawn and transform reasons
++
++        // Paper start - Fix issues with mob conversion; reset to prevent event spam
++        if (converted == null) {
++            this.timeInOverworld = 0;
++        }
++        // Paper end - Fix issues with mob conversion
      }
  
      public boolean isAdult() {