| 1 | package net.minecraft.world.entity.animal.equine;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.world.DifficultyInstance;
|
| 5 | import net.minecraft.world.entity.AgeableMob;
|
| 6 | import net.minecraft.world.entity.Entity;
|
| 7 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 8 | import net.minecraft.world.entity.EntityType;
|
| 9 | import net.minecraft.world.entity.EntityTypes;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.SpawnGroupData;
|
| 12 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 13 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 14 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 15 | import net.minecraft.world.entity.ai.goal.target.TargetGoal;
|
| 16 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 17 | import net.minecraft.world.entity.monster.illager.AbstractIllager;
|
| 18 | import net.minecraft.world.entity.monster.zombie.Zombie;
|
| 19 | import net.minecraft.world.entity.npc.wanderingtrader.WanderingTrader;
|
| 20 | import net.minecraft.world.entity.player.Player;
|
| 21 | import net.minecraft.world.level.Level;
|
| 22 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 23 | import net.minecraft.world.level.storage.ValueInput;
|
| 24 | import net.minecraft.world.level.storage.ValueOutput;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class TraderLlama extends Llama {
|
| 28 | private static final int DEFAULT_DESPAWN_DELAY = 47999;
|
| 29 | private int despawnDelay = 47999;
|
| 30 |
|
| 31 | public TraderLlama(final EntityType<? extends TraderLlama> type, final Level level) {
|
| 32 | super(type, level);
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public boolean isTraderLlama() {
|
| 37 | return true;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected @Nullable Llama makeNewLlama() {
|
| 42 | TraderLlama baby = EntityTypes.TRADER_LLAMA.create(this.level(), EntitySpawnReason.BREEDING);
|
| 43 | if (baby != null) {
|
| 44 | baby.setPersistenceRequired();
|
| 45 | }
|
| 46 |
|
| 47 | return baby;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 52 | super.addAdditionalSaveData(output);
|
| 53 | output.putInt("DespawnDelay", this.despawnDelay);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 58 | super.readAdditionalSaveData(input);
|
| 59 | this.despawnDelay = input.getIntOr("DespawnDelay", 47999);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | protected void registerGoals() {
|
| 64 | super.registerGoals();
|
| 65 | this.goalSelector.addGoal(1, new PanicGoal(this, 2.0));
|
| 66 | this.targetSelector.addGoal(1, new TraderLlama.TraderLlamaDefendWanderingTraderGoal(this));
|
| 67 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Zombie.class, true, (target, level) -> !target.is(EntityTypes.ZOMBIFIED_PIGLIN)));
|
| 68 | this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, AbstractIllager.class, true));
|
| 69 | }
|
| 70 |
|
| 71 | public void setDespawnDelay(final int despawnDelay) {
|
| 72 | this.despawnDelay = despawnDelay;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void doPlayerRide(final Player player) {
|
| 77 | Entity leashHolder = this.getLeashHolder();
|
| 78 | if (!(leashHolder instanceof WanderingTrader)) {
|
| 79 | super.doPlayerRide(player);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void aiStep() {
|
| 85 | super.aiStep();
|
| 86 | if (!this.level().isClientSide()) {
|
| 87 | this.maybeDespawn();
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private void maybeDespawn() {
|
| 92 | if (this.canDespawn()) {
|
| 93 | this.despawnDelay = this.isLeashedToWanderingTrader() ? ((WanderingTrader)this.getLeashHolder()).getDespawnDelay() - 1 : this.despawnDelay - 1;
|
| 94 | if (this.despawnDelay <= 0) {
|
| 95 | this.removeLeash();
|
| 96 | this.discard();
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | private boolean canDespawn() {
|
| 102 | return !this.isTamed()
|
| 103 | && !this.isLeashedToSomethingOtherThanTheWanderingTrader()
|
| 104 | && !this.hasExactlyOnePlayerPassenger()
|
| 105 | && !this.isAgeLocked()
|
| 106 | && !this.isPersistenceRequired();
|
| 107 | }
|
| 108 |
|
| 109 | private boolean isLeashedToWanderingTrader() {
|
| 110 | return this.getLeashHolder() instanceof WanderingTrader;
|
| 111 | }
|
| 112 |
|
| 113 | private boolean isLeashedToSomethingOtherThanTheWanderingTrader() {
|
| 114 | return this.isLeashed() && !this.isLeashedToWanderingTrader();
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 119 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 120 | ) {
|
| 121 | if (spawnReason == EntitySpawnReason.EVENT) {
|
| 122 | this.setAge(0);
|
| 123 | }
|
| 124 |
|
| 125 | if (groupData == null) {
|
| 126 | groupData = new AgeableMob.AgeableMobGroupData(false);
|
| 127 | }
|
| 128 |
|
| 129 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 130 | }
|
| 131 |
|
| 132 | protected static class TraderLlamaDefendWanderingTraderGoal extends TargetGoal {
|
| 133 | private final Llama llama;
|
| 134 | private LivingEntity ownerLastHurtBy;
|
| 135 | private int timestamp;
|
| 136 |
|
| 137 | public TraderLlamaDefendWanderingTraderGoal(final Llama tameAnimal) {
|
| 138 | super(tameAnimal, false);
|
| 139 | this.llama = tameAnimal;
|
| 140 | this.setFlags(EnumSet.of(Goal.Flag.TARGET));
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public boolean canUse() {
|
| 145 | if (!this.llama.isLeashed()) {
|
| 146 | return false;
|
| 147 | } else if (!(this.llama.getLeashHolder() instanceof WanderingTrader owner)) {
|
| 148 | return false;
|
| 149 | } else {
|
| 150 | this.ownerLastHurtBy = owner.getLastHurtByMob();
|
| 151 | int timeStamp = owner.getLastHurtByMobTimestamp();
|
| 152 | return timeStamp != this.timestamp && this.canAttack(this.ownerLastHurtBy, TargetingConditions.DEFAULT);
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public void start() {
|
| 158 | this.mob.setTarget(this.ownerLastHurtBy);
|
| 159 | if (this.llama.getLeashHolder() instanceof WanderingTrader wanderingTrader) {
|
| 160 | this.timestamp = wanderingTrader.getLastHurtByMobTimestamp();
|
| 161 | }
|
| 162 |
|
| 163 | super.start();
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|