| 1 | package net.minecraft.world.entity.animal.frog;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.UUID;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvent;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.Pose;
|
| 14 | import net.minecraft.world.entity.ai.behavior.Behavior;
|
| 15 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 16 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 17 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 18 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 19 | import net.minecraft.world.level.pathfinder.Path;
|
| 20 |
|
| 21 | public class ShootTongue extends Behavior<Frog> {
|
| 22 | public static final int TIME_OUT_DURATION = 100;
|
| 23 | public static final int CATCH_ANIMATION_DURATION = 6;
|
| 24 | public static final int TONGUE_ANIMATION_DURATION = 10;
|
| 25 | private static final float EATING_DISTANCE = 1.75F;
|
| 26 | private static final float EATING_MOVEMENT_FACTOR = 0.75F;
|
| 27 | public static final int UNREACHABLE_TONGUE_TARGETS_COOLDOWN_DURATION = 100;
|
| 28 | public static final int MAX_UNREACHBLE_TONGUE_TARGETS_IN_MEMORY = 5;
|
| 29 | private int eatAnimationTimer;
|
| 30 | private int calculatePathCounter;
|
| 31 | private final SoundEvent tongueSound;
|
| 32 | private final SoundEvent eatSound;
|
| 33 | private ShootTongue.State state = ShootTongue.State.DONE;
|
| 34 |
|
| 35 | public ShootTongue(final SoundEvent tongueSound, final SoundEvent eatSound) {
|
| 36 | super(
|
| 37 | ImmutableMap.of(
|
| 38 | MemoryModuleType.WALK_TARGET,
|
| 39 | MemoryStatus.VALUE_ABSENT,
|
| 40 | MemoryModuleType.LOOK_TARGET,
|
| 41 | MemoryStatus.REGISTERED,
|
| 42 | MemoryModuleType.ATTACK_TARGET,
|
| 43 | MemoryStatus.VALUE_PRESENT,
|
| 44 | MemoryModuleType.IS_PANICKING,
|
| 45 | MemoryStatus.VALUE_ABSENT
|
| 46 | ),
|
| 47 | 100
|
| 48 | );
|
| 49 | this.tongueSound = tongueSound;
|
| 50 | this.eatSound = eatSound;
|
| 51 | }
|
| 52 |
|
| 53 | protected boolean checkExtraStartConditions(final ServerLevel level, final Frog body) {
|
| 54 | LivingEntity target = body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).get();
|
| 55 | boolean canPathfindToTarget = this.canPathfindToTarget(body, target);
|
| 56 | if (!canPathfindToTarget) {
|
| 57 | body.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
|
| 58 | this.addUnreachableTargetToMemory(body, target);
|
| 59 | }
|
| 60 |
|
| 61 | return canPathfindToTarget && body.getPose() != Pose.CROAKING && Frog.canEat(target);
|
| 62 | }
|
| 63 |
|
| 64 | protected boolean canStillUse(final ServerLevel level, final Frog body, final long timestamp) {
|
| 65 | return body.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET)
|
| 66 | && this.state != ShootTongue.State.DONE
|
| 67 | && !body.getBrain().hasMemoryValue(MemoryModuleType.IS_PANICKING);
|
| 68 | }
|
| 69 |
|
| 70 | protected void start(final ServerLevel level, final Frog body, final long timestamp) {
|
| 71 | LivingEntity target = body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).get();
|
| 72 | BehaviorUtils.lookAtEntity(body, target);
|
| 73 | body.setTongueTarget(target);
|
| 74 | body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(target.position(), 2.0F, 0));
|
| 75 | this.calculatePathCounter = 10;
|
| 76 | this.state = ShootTongue.State.MOVE_TO_TARGET;
|
| 77 | }
|
| 78 |
|
| 79 | protected void stop(final ServerLevel level, final Frog body, final long timestamp) {
|
| 80 | body.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
|
| 81 | body.eraseTongueTarget();
|
| 82 | body.setPose(Pose.STANDING);
|
| 83 | }
|
| 84 |
|
| 85 | private void eatEntity(final ServerLevel level, final Frog body) {
|
| 86 | level.playSound(null, body, this.eatSound, SoundSource.NEUTRAL, 2.0F, 1.0F);
|
| 87 | Optional<Entity> tongueTarget = body.getTongueTarget();
|
| 88 | if (tongueTarget.isPresent()) {
|
| 89 | Entity target = tongueTarget.get();
|
| 90 | if (target.isAlive()) {
|
| 91 | body.doHurtTarget(level, target);
|
| 92 | if (!target.isAlive()) {
|
| 93 | target.remove(Entity.RemovalReason.KILLED);
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | protected void tick(final ServerLevel level, final Frog body, final long timestamp) {
|
| 100 | LivingEntity target = body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).get();
|
| 101 | body.setTongueTarget(target);
|
| 102 | switch (this.state) {
|
| 103 | case MOVE_TO_TARGET:
|
| 104 | if (target.distanceTo(body) < 1.75F) {
|
| 105 | level.playSound(null, body, this.tongueSound, SoundSource.NEUTRAL, 2.0F, 1.0F);
|
| 106 | body.setPose(Pose.USING_TONGUE);
|
| 107 | target.setDeltaMovement(target.position().vectorTo(body.position()).normalize().scale(0.75));
|
| 108 | this.eatAnimationTimer = 0;
|
| 109 | this.state = ShootTongue.State.CATCH_ANIMATION;
|
| 110 | } else if (this.calculatePathCounter <= 0) {
|
| 111 | body.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(target.position(), 2.0F, 0));
|
| 112 | this.calculatePathCounter = 10;
|
| 113 | } else {
|
| 114 | this.calculatePathCounter--;
|
| 115 | }
|
| 116 | break;
|
| 117 | case CATCH_ANIMATION:
|
| 118 | if (this.eatAnimationTimer++ >= 6) {
|
| 119 | this.state = ShootTongue.State.EAT_ANIMATION;
|
| 120 | this.eatEntity(level, body);
|
| 121 | }
|
| 122 | break;
|
| 123 | case EAT_ANIMATION:
|
| 124 | if (this.eatAnimationTimer >= 10) {
|
| 125 | this.state = ShootTongue.State.DONE;
|
| 126 | } else {
|
| 127 | this.eatAnimationTimer++;
|
| 128 | }
|
| 129 | case DONE:
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | private boolean canPathfindToTarget(final Frog body, final LivingEntity target) {
|
| 134 | Path path = body.getNavigation().createPath(target, 0);
|
| 135 | return path != null && path.getDistToTarget() < 1.75F;
|
| 136 | }
|
| 137 |
|
| 138 | private void addUnreachableTargetToMemory(final Frog body, final LivingEntity entity) {
|
| 139 | List<UUID> unreachableTargets = body.getBrain().getMemory(MemoryModuleType.UNREACHABLE_TONGUE_TARGETS).orElseGet(ArrayList::new);
|
| 140 | boolean shouldAddUnreachableTarget = !unreachableTargets.contains(entity.getUUID());
|
| 141 | if (unreachableTargets.size() == 5 && shouldAddUnreachableTarget) {
|
| 142 | unreachableTargets.remove(0);
|
| 143 | }
|
| 144 |
|
| 145 | if (shouldAddUnreachableTarget) {
|
| 146 | unreachableTargets.add(entity.getUUID());
|
| 147 | }
|
| 148 |
|
| 149 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.UNREACHABLE_TONGUE_TARGETS, unreachableTargets, 100L);
|
| 150 | }
|
| 151 |
|
| 152 | private enum State {
|
| 153 | MOVE_TO_TARGET,
|
| 154 | CATCH_ANIMATION,
|
| 155 | EAT_ANIMATION,
|
| 156 | DONE;
|
| 157 | }
|
| 158 | }
|
| 159 |
|