| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.ImmutableMap.Builder;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.function.Function;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.Util;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.PathfinderMob;
|
| 12 | import net.minecraft.world.entity.ai.Brain;
|
| 13 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 14 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 15 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 16 | import net.minecraft.world.entity.player.Player;
|
| 17 |
|
| 18 | public class FollowTemptation extends Behavior<PathfinderMob> {
|
| 19 | public static final int TEMPTATION_COOLDOWN = 100;
|
| 20 | public static final double DEFAULT_CLOSE_ENOUGH_DIST = 2.5;
|
| 21 | public static final double BACKED_UP_CLOSE_ENOUGH_DIST = 3.5;
|
| 22 | private final Function<LivingEntity, Float> speedModifier;
|
| 23 | private final Function<LivingEntity, Double> closeEnoughDistance;
|
| 24 | private final boolean lookInTheEyes;
|
| 25 |
|
| 26 | public FollowTemptation(final Function<LivingEntity, Float> speedModifier) {
|
| 27 | this(speedModifier, entity -> 2.5);
|
| 28 | }
|
| 29 |
|
| 30 | public FollowTemptation(final Function<LivingEntity, Float> speedModifier, final Function<LivingEntity, Double> closeEnoughDistance) {
|
| 31 | this(speedModifier, closeEnoughDistance, false);
|
| 32 | }
|
| 33 |
|
| 34 | public FollowTemptation(
|
| 35 | final Function<LivingEntity, Float> speedModifier, final Function<LivingEntity, Double> closeEnoughDistance, final boolean lookInTheEyes
|
| 36 | ) {
|
| 37 | super(Util.make(() -> {
|
| 38 | Builder<MemoryModuleType<?>, MemoryStatus> builder = ImmutableMap.builder();
|
| 39 | builder.put(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED);
|
| 40 | builder.put(MemoryModuleType.WALK_TARGET, MemoryStatus.REGISTERED);
|
| 41 | builder.put(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS, MemoryStatus.VALUE_ABSENT);
|
| 42 | builder.put(MemoryModuleType.TEMPTING_PLAYER, MemoryStatus.VALUE_PRESENT);
|
| 43 | builder.put(MemoryModuleType.BREED_TARGET, MemoryStatus.VALUE_ABSENT);
|
| 44 | builder.put(MemoryModuleType.IS_PANICKING, MemoryStatus.VALUE_ABSENT);
|
| 45 | return builder.build();
|
| 46 | }));
|
| 47 | this.speedModifier = speedModifier;
|
| 48 | this.closeEnoughDistance = closeEnoughDistance;
|
| 49 | this.lookInTheEyes = lookInTheEyes;
|
| 50 | }
|
| 51 |
|
| 52 | protected float getSpeedModifier(final PathfinderMob body) {
|
| 53 | return this.speedModifier.apply(body);
|
| 54 | }
|
| 55 |
|
| 56 | private Optional<Player> getTemptingPlayer(final PathfinderMob body) {
|
| 57 | return body.getBrain().getMemory(MemoryModuleType.TEMPTING_PLAYER);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected boolean timedOut(final long timestamp) {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | protected boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 66 | return this.getTemptingPlayer(body).isPresent()
|
| 67 | && !body.getBrain().hasMemoryValue(MemoryModuleType.BREED_TARGET)
|
| 68 | && !body.getBrain().hasMemoryValue(MemoryModuleType.IS_PANICKING);
|
| 69 | }
|
| 70 |
|
| 71 | protected void stop(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 72 | Brain<?> brain = body.getBrain();
|
| 73 | brain.setMemory(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS, 100);
|
| 74 | brain.eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 75 | brain.eraseMemory(MemoryModuleType.LOOK_TARGET);
|
| 76 | }
|
| 77 |
|
| 78 | protected void tick(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 79 | Player player = this.getTemptingPlayer(body).get();
|
| 80 | Brain<?> brain = body.getBrain();
|
| 81 | brain.setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(player, true));
|
| 82 | double closeEnough = this.closeEnoughDistance.apply(body);
|
| 83 | if (body.distanceToSqr(player) < Mth.square(closeEnough)) {
|
| 84 | brain.eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 85 | } else {
|
| 86 | brain.setMemory(
|
| 87 | MemoryModuleType.WALK_TARGET, new WalkTarget(new EntityTracker(player, this.lookInTheEyes, this.lookInTheEyes), this.getSpeedModifier(body), 2)
|
| 88 | );
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|