--- a/net/minecraft/world/entity/animal/horse/TraderLlama.java
+++ b/net/minecraft/world/entity/animal/horse/TraderLlama.java
@@ -18,8 +18,12 @@
 import net.minecraft.world.entity.player.Player;
 import net.minecraft.world.level.Level;
 import net.minecraft.world.level.ServerLevelAccessor;
+// CraftBukkit start
+import org.bukkit.event.entity.EntityRemoveEvent;
+// CraftBukkit end
 
 public class TraderLlama extends Llama {
+
     private int despawnDelay = 47999;
 
     public TraderLlama(EntityType<? extends TraderLlama> type, Level world) {
@@ -34,7 +38,7 @@
     @Nullable
     @Override
     protected Llama makeNewLlama() {
-        return EntityType.TRADER_LLAMA.create(this.level());
+        return (Llama) EntityType.TRADER_LLAMA.create(this.level());
     }
 
     @Override
@@ -49,12 +53,13 @@
         if (nbt.contains("DespawnDelay", 99)) {
             this.despawnDelay = nbt.getInt("DespawnDelay");
         }
+
     }
 
     @Override
     protected void registerGoals() {
         super.registerGoals();
-        this.goalSelector.addGoal(1, new PanicGoal(this, 2.0));
+        this.goalSelector.addGoal(1, new PanicGoal(this, 2.0D));
         this.targetSelector.addGoal(1, new TraderLlama.TraderLlamaDefendWanderingTraderGoal(this));
     }
 
@@ -65,6 +70,7 @@
     @Override
     protected void doPlayerRide(Player player) {
         Entity entity = this.getLeashHolder();
+
         if (!(entity instanceof WanderingTrader)) {
             super.doPlayerRide(player);
         }
@@ -76,15 +82,17 @@
         if (!this.level().isClientSide) {
             this.maybeDespawn();
         }
+
     }
 
     private void maybeDespawn() {
         if (this.canDespawn()) {
-            this.despawnDelay = this.isLeashedToWanderingTrader() ? ((WanderingTrader)this.getLeashHolder()).getDespawnDelay() - 1 : this.despawnDelay - 1;
+            this.despawnDelay = this.isLeashedToWanderingTrader() ? ((WanderingTrader) this.getLeashHolder()).getDespawnDelay() - 1 : this.despawnDelay - 1;
             if (this.despawnDelay <= 0) {
                 this.dropLeash(true, false);
-                this.discard();
+                this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
             }
+
         }
     }
 
@@ -111,10 +119,11 @@
             entityData = new AgeableMob.AgeableMobGroupData(false);
         }
 
-        return super.finalizeSpawn(world, difficulty, spawnReason, entityData);
+        return super.finalizeSpawn(world, difficulty, spawnReason, (SpawnGroupData) entityData);
     }
 
     protected static class TraderLlamaDefendWanderingTraderGoal extends TargetGoal {
+
         private final Llama llama;
         private LivingEntity ownerLastHurtBy;
         private int timestamp;
@@ -129,21 +138,29 @@
         public boolean canUse() {
             if (!this.llama.isLeashed()) {
                 return false;
-            } else if (!(this.llama.getLeashHolder() instanceof WanderingTrader wanderingTrader)) {
-                return false;
             } else {
-                this.ownerLastHurtBy = wanderingTrader.getLastHurtByMob();
-                int i = wanderingTrader.getLastHurtByMobTimestamp();
-                return i != this.timestamp && this.canAttack(this.ownerLastHurtBy, TargetingConditions.DEFAULT);
+                Entity entity = this.llama.getLeashHolder();
+
+                if (!(entity instanceof WanderingTrader)) {
+                    return false;
+                } else {
+                    WanderingTrader entityvillagertrader = (WanderingTrader) entity;
+
+                    this.ownerLastHurtBy = entityvillagertrader.getLastHurtByMob();
+                    int i = entityvillagertrader.getLastHurtByMobTimestamp();
+
+                    return i != this.timestamp && this.canAttack(this.ownerLastHurtBy, TargetingConditions.DEFAULT);
+                }
             }
         }
 
         @Override
         public void start() {
-            this.mob.setTarget(this.ownerLastHurtBy);
+            this.mob.setTarget(this.ownerLastHurtBy, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_ATTACKED_OWNER, true); // CraftBukkit
             Entity entity = this.llama.getLeashHolder();
+
             if (entity instanceof WanderingTrader) {
-                this.timestamp = ((WanderingTrader)entity).getLastHurtByMobTimestamp();
+                this.timestamp = ((WanderingTrader) entity).getLastHurtByMobTimestamp();
             }
 
             super.start();
