| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.world.entity.Entity;
|
| 6 | import net.minecraft.world.entity.EntityTypes;
|
| 7 | import net.minecraft.world.entity.animal.equine.Llama;
|
| 8 | import net.minecraft.world.entity.decoration.LeashFenceKnotEntity;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 |
|
| 11 | public class LlamaFollowCaravanGoal extends Goal {
|
| 12 | public final Llama llama;
|
| 13 | private double speedModifier;
|
| 14 | private static final int CARAVAN_LIMIT = 8;
|
| 15 | private int distCheckCounter;
|
| 16 |
|
| 17 | public LlamaFollowCaravanGoal(final Llama llama, final double speedModifier) {
|
| 18 | this.llama = llama;
|
| 19 | this.speedModifier = speedModifier;
|
| 20 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public boolean canUse() {
|
| 25 | if (!this.llama.isLeashed() && !this.llama.inCaravan()) {
|
| 26 | List<Entity> llamas = this.llama
|
| 27 | .level()
|
| 28 | .getEntities(this.llama, this.llama.getBoundingBox().inflate(9.0, 4.0, 9.0), e -> e.is(EntityTypes.LLAMA) || e.is(EntityTypes.TRADER_LLAMA));
|
| 29 | Llama closest = null;
|
| 30 | double closestDistSquare = Double.MAX_VALUE;
|
| 31 |
|
| 32 | for (Entity entity : llamas) {
|
| 33 | Llama candidate = (Llama)entity;
|
| 34 | if (candidate.inCaravan() && !candidate.hasCaravanTail()) {
|
| 35 | double distSquare = this.llama.distanceToSqr(candidate);
|
| 36 | if (!(distSquare > closestDistSquare)) {
|
| 37 | closestDistSquare = distSquare;
|
| 38 | closest = candidate;
|
| 39 | }
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | if (closest == null) {
|
| 44 | for (Entity entity : llamas) {
|
| 45 | Llama candidate = (Llama)entity;
|
| 46 | if (candidate.isLeashed() && !candidate.hasCaravanTail()) {
|
| 47 | double distSquare = this.llama.distanceToSqr(candidate);
|
| 48 | if (!(distSquare > closestDistSquare)) {
|
| 49 | closestDistSquare = distSquare;
|
| 50 | closest = candidate;
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | if (closest == null) {
|
| 57 | return false;
|
| 58 | }
|
| 59 |
|
| 60 | if (closestDistSquare < 4.0) {
|
| 61 | return false;
|
| 62 | }
|
| 63 |
|
| 64 | if (!closest.isLeashed() && !this.firstIsLeashed(closest, 1)) {
|
| 65 | return false;
|
| 66 | }
|
| 67 |
|
| 68 | this.llama.joinCaravan(closest);
|
| 69 | return true;
|
| 70 | } else {
|
| 71 | return false;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public boolean canContinueToUse() {
|
| 77 | if (this.llama.inCaravan() && this.llama.getCaravanHead().isAlive() && this.firstIsLeashed(this.llama, 0)) {
|
| 78 | double distSqr = this.llama.distanceToSqr(this.llama.getCaravanHead());
|
| 79 | if (distSqr > 676.0) {
|
| 80 | if (this.speedModifier <= 3.0) {
|
| 81 | this.speedModifier *= 1.2;
|
| 82 | this.distCheckCounter = reducedTickDelay(40);
|
| 83 | return true;
|
| 84 | }
|
| 85 |
|
| 86 | if (this.distCheckCounter == 0) {
|
| 87 | return false;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | if (this.distCheckCounter > 0) {
|
| 92 | this.distCheckCounter--;
|
| 93 | }
|
| 94 |
|
| 95 | return true;
|
| 96 | } else {
|
| 97 | return false;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public void stop() {
|
| 103 | this.llama.leaveCaravan();
|
| 104 | this.speedModifier = 2.1;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public void tick() {
|
| 109 | if (this.llama.inCaravan()) {
|
| 110 | if (!(this.llama.getLeashHolder() instanceof LeashFenceKnotEntity)) {
|
| 111 | Llama follows = this.llama.getCaravanHead();
|
| 112 | double distanceTo = this.llama.distanceTo(follows);
|
| 113 | float wantedDistance = 2.0F;
|
| 114 | Vec3 delta = new Vec3(follows.getX() - this.llama.getX(), follows.getY() - this.llama.getY(), follows.getZ() - this.llama.getZ())
|
| 115 | .normalize()
|
| 116 | .scale(Math.max(distanceTo - 2.0, 0.0));
|
| 117 | this.llama.getNavigation().moveTo(this.llama.getX() + delta.x, this.llama.getY() + delta.y, this.llama.getZ() + delta.z, this.speedModifier);
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | private boolean firstIsLeashed(final Llama currentMob, int counter) {
|
| 123 | if (counter > 8) {
|
| 124 | return false;
|
| 125 | } else if (currentMob.inCaravan()) {
|
| 126 | return currentMob.getCaravanHead().isLeashed() ? true : this.firstIsLeashed(currentMob.getCaravanHead(), ++counter);
|
| 127 | } else {
|
| 128 | return false;
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 |
|