From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Fri, 18 Oct 2019 22:50:12 -0500
Subject: [PATCH] Llama API


diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java b/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
index e037d618955de9a213a9cd90752b29d189faace4..34dec0a94840a8865f1d80857ec54a1022ae5b84 100644
--- a/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
@@ -22,6 +22,7 @@ public class LlamaFollowCaravanGoal extends Goal {
 
     @Override
     public boolean canUse() {
+        if (!this.llama.shouldJoinCaravan) return false; // Purpur
         if (!this.llama.isLeashed() && !this.llama.inCaravan()) {
             List<Entity> list = this.llama.level.getEntities(this.llama, this.llama.getBoundingBox().inflate(9.0D, 4.0D, 9.0D), (entity) -> {
                 EntityType<?> entityType = entity.getType();
@@ -71,6 +72,7 @@ public class LlamaFollowCaravanGoal extends Goal {
 
     @Override
     public boolean canContinueToUse() {
+        if (!this.llama.shouldJoinCaravan) return false; // Purpur
         if (this.llama.inCaravan() && this.llama.getCaravanHead().isAlive() && this.firstIsLeashed(this.llama, 0)) {
             double d = this.llama.distanceToSqr(this.llama.getCaravanHead());
             if (d > 676.0D) {
diff --git a/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java b/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java
index d664be25c2c2c527a44fa939e68e84d60effc144..5a0b9cfdd0b8d79b10f16c4a3ea1eed746c8887d 100644
--- a/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java
+++ b/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java
@@ -65,6 +65,7 @@ public class Llama extends AbstractChestedHorse implements RangedAttackMob {
     private Llama caravanHead;
     @Nullable
     private Llama caravanTail;
+    public boolean shouldJoinCaravan = true; // Purpur
 
     public Llama(EntityType<? extends Llama> type, Level world) {
         super(type, world);
@@ -149,7 +150,7 @@ public class Llama extends AbstractChestedHorse implements RangedAttackMob {
         if (!this.inventory.getItem(1).isEmpty()) {
             nbt.put("DecorItem", this.inventory.getItem(1).save(new CompoundTag()));
         }
-
+        nbt.putBoolean("Purpur.ShouldJoinCaravan", shouldJoinCaravan); // Purpur
     }
 
     @Override
@@ -161,6 +162,12 @@ public class Llama extends AbstractChestedHorse implements RangedAttackMob {
             this.inventory.setItem(1, ItemStack.of(nbt.getCompound("DecorItem")));
         }
 
+        // Purpur start
+        if (nbt.contains("Purpur.ShouldJoinCaravan")) {
+            this.shouldJoinCaravan = nbt.getBoolean("Purpur.ShouldJoinCaravan");
+        }
+        // Purpur end
+
         this.updateContainerEquipment();
     }
 
@@ -482,6 +489,7 @@ public class Llama extends AbstractChestedHorse implements RangedAttackMob {
 
     public void leaveCaravan() {
         if (this.caravanHead != null) {
+            new net.pl3x.purpur.event.entity.LlamaLeaveCaravanEvent((org.bukkit.entity.Llama) getBukkitEntity()).callEvent(); // Purpur
             this.caravanHead.caravanTail = null;
         }
 
@@ -489,6 +497,7 @@ public class Llama extends AbstractChestedHorse implements RangedAttackMob {
     }
 
     public void joinCaravan(Llama llama) {
+        if (!shouldJoinCaravan || !new net.pl3x.purpur.event.entity.LlamaJoinCaravanEvent((org.bukkit.entity.Llama) getBukkitEntity(), (org.bukkit.entity.Llama) llama.getBukkitEntity()).callEvent()) return; // Purpur
         this.caravanHead = llama;
         this.caravanHead.caravanTail = this;
     }
@@ -506,6 +515,13 @@ public class Llama extends AbstractChestedHorse implements RangedAttackMob {
         return this.caravanHead;
     }
 
+    // Purpur start
+    @Nullable
+    public Llama getCaravanTail() {
+        return this.caravanTail;
+    }
+    // Purpur end
+
     @Override
     protected double followLeashSpeed() {
         return 2.0D;
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java
index 6ad12711a82d7be42ba41c0428779f86536fd900..4dd47cac823a63056985079e75588dd3d81f0af4 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java
@@ -64,4 +64,46 @@ public class CraftLlama extends CraftChestedHorse implements Llama, com.destroys
     public EntityType getType() {
         return EntityType.LLAMA;
     }
+
+    // Purpur start
+    @Override
+    public boolean shouldJoinCaravan() {
+        return getHandle().shouldJoinCaravan;
+    }
+
+    @Override
+    public void setShouldJoinCaravan(boolean shouldJoinCaravan) {
+        getHandle().shouldJoinCaravan = shouldJoinCaravan;
+    }
+
+    @Override
+    public boolean inCaravan() {
+        return getHandle().inCaravan();
+    }
+
+    @Override
+    public void joinCaravan(@org.jetbrains.annotations.NotNull Llama llama) {
+        getHandle().joinCaravan(((CraftLlama) llama).getHandle());
+    }
+
+    @Override
+    public void leaveCaravan() {
+        getHandle().leaveCaravan();
+    }
+
+    @Override
+    public boolean hasCaravanTail() {
+        return getHandle().hasCaravanTail();
+    }
+
+    @Override
+    public Llama getCaravanHead() {
+        return getHandle().getCaravanHead() == null ? null : (Llama) getHandle().getCaravanHead().getBukkitEntity();
+    }
+
+    @Override
+    public Llama getCaravanTail() {
+        return getHandle().getCaravanTail() == null ? null : (Llama) getHandle().getCaravanTail().getBukkitEntity();
+    }
+    // Purpur end
 }
