| 1 | package net.minecraft.world.entity.monster.creaking;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableSet;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.util.valueproviders.UniformInt;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.ai.ActivityData;
|
| 12 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 13 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 14 | import net.minecraft.world.entity.ai.behavior.MeleeAttack;
|
| 15 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 16 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 17 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 18 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
|
| 19 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromAttackTargetIfTargetOutOfReach;
|
| 20 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
|
| 21 | import net.minecraft.world.entity.ai.behavior.StartAttacking;
|
| 22 | import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
|
| 23 | import net.minecraft.world.entity.ai.behavior.Swim;
|
| 24 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 25 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 26 | import net.minecraft.world.entity.player.Player;
|
| 27 | import net.minecraft.world.entity.schedule.Activity;
|
| 28 |
|
| 29 | public class CreakingAi {
|
| 30 | private static ActivityData<Creaking> initCoreActivity() {
|
| 31 | return ActivityData.create(Activity.CORE, 0, ImmutableList.of(new Swim<Creaking>(0.8F) {
|
| 32 | protected boolean checkExtraStartConditions(final ServerLevel level, final Creaking body) {
|
| 33 | return body.canMove() && super.checkExtraStartConditions(level, body);
|
| 34 | }
|
| 35 | }, new LookAtTargetSink(45, 90), new MoveToTargetSink()));
|
| 36 | }
|
| 37 |
|
| 38 | private static ActivityData<Creaking> initIdleActivity() {
|
| 39 | return ActivityData.create(
|
| 40 | Activity.IDLE,
|
| 41 | 10,
|
| 42 | ImmutableList.of(
|
| 43 | StartAttacking.create((level, mob) -> mob.isActive(), (level, mob) -> mob.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER)),
|
| 44 | SetEntityLookTargetSometimes.create(8.0F, UniformInt.of(30, 60)),
|
| 45 | new RunOne<>(
|
| 46 | ImmutableList.of(Pair.of(RandomStroll.stroll(0.3F), 2), Pair.of(SetWalkTargetFromLookTarget.create(0.3F, 3), 2), Pair.of(new DoNothing(30, 60), 1))
|
| 47 | )
|
| 48 | )
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | private static ActivityData<Creaking> initFightActivity(final Creaking body) {
|
| 53 | return ActivityData.create(
|
| 54 | Activity.FIGHT,
|
| 55 | 10,
|
| 56 | ImmutableList.of(
|
| 57 | SetWalkTargetFromAttackTargetIfTargetOutOfReach.create(1.0F),
|
| 58 | MeleeAttack.create(Creaking::canMove, 40),
|
| 59 | StopAttackingIfTargetInvalid.create((level, target) -> !isAttackTargetStillReachable(body, target))
|
| 60 | ),
|
| 61 | ImmutableSet.of(Pair.of(MemoryModuleType.ATTACK_TARGET, MemoryStatus.VALUE_PRESENT))
|
| 62 | );
|
| 63 | }
|
| 64 |
|
| 65 | private static boolean isAttackTargetStillReachable(final Creaking creaking, final LivingEntity target) {
|
| 66 | Optional<List<Player>> visibleAttackablePlayers = creaking.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYERS);
|
| 67 | return visibleAttackablePlayers.<Boolean>map(players -> target instanceof Player player && players.contains(player)).orElse(false);
|
| 68 | }
|
| 69 |
|
| 70 | protected static List<ActivityData<Creaking>> getActivities(final Creaking creaking) {
|
| 71 | return List.of(initCoreActivity(), initIdleActivity(), initFightActivity(creaking));
|
| 72 | }
|
| 73 |
|
| 74 | public static void updateActivity(final Creaking creaking) {
|
| 75 | if (!creaking.canMove()) {
|
| 76 | creaking.getBrain().useDefaultActivity();
|
| 77 | } else {
|
| 78 | creaking.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.FIGHT, Activity.IDLE));
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|