| 1 | package net.minecraft.world.entity.animal.allay;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.UUID;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.GlobalPos;
|
| 12 | import net.minecraft.server.level.ServerLevel;
|
| 13 | import net.minecraft.server.level.ServerPlayer;
|
| 14 | import net.minecraft.sounds.SoundEvents;
|
| 15 | import net.minecraft.sounds.SoundSource;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import net.minecraft.util.valueproviders.UniformInt;
|
| 18 | import net.minecraft.world.entity.LivingEntity;
|
| 19 | import net.minecraft.world.entity.ai.ActivityData;
|
| 20 | import net.minecraft.world.entity.ai.Brain;
|
| 21 | import net.minecraft.world.entity.ai.behavior.AnimalPanic;
|
| 22 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 23 | import net.minecraft.world.entity.ai.behavior.BlockPosTracker;
|
| 24 | import net.minecraft.world.entity.ai.behavior.CountDownCooldownTicks;
|
| 25 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 26 | import net.minecraft.world.entity.ai.behavior.EntityTracker;
|
| 27 | import net.minecraft.world.entity.ai.behavior.GoAndGiveItemsToTarget;
|
| 28 | import net.minecraft.world.entity.ai.behavior.GoToWantedItem;
|
| 29 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 30 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 31 | import net.minecraft.world.entity.ai.behavior.PositionTracker;
|
| 32 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 33 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 34 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
|
| 35 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
|
| 36 | import net.minecraft.world.entity.ai.behavior.StayCloseToTarget;
|
| 37 | import net.minecraft.world.entity.ai.behavior.Swim;
|
| 38 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 39 | import net.minecraft.world.entity.schedule.Activity;
|
| 40 | import net.minecraft.world.item.ItemStack;
|
| 41 | import net.minecraft.world.level.Level;
|
| 42 | import net.minecraft.world.level.block.Blocks;
|
| 43 | import net.minecraft.world.phys.Vec3;
|
| 44 |
|
| 45 | public class AllayAi {
|
| 46 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 1.0F;
|
| 47 | private static final float SPEED_MULTIPLIER_WHEN_FOLLOWING_DEPOSIT_TARGET = 2.25F;
|
| 48 | private static final float SPEED_MULTIPLIER_WHEN_RETRIEVING_ITEM = 1.75F;
|
| 49 | private static final float SPEED_MULTIPLIER_WHEN_PANICKING = 2.5F;
|
| 50 | private static final int CLOSE_ENOUGH_TO_TARGET = 4;
|
| 51 | private static final int TOO_FAR_FROM_TARGET = 16;
|
| 52 | private static final int MAX_LOOK_DISTANCE = 6;
|
| 53 | private static final int MIN_WAIT_DURATION = 30;
|
| 54 | private static final int MAX_WAIT_DURATION = 60;
|
| 55 | private static final int TIME_TO_FORGET_NOTEBLOCK = 600;
|
| 56 | private static final int DISTANCE_TO_WANTED_ITEM = 32;
|
| 57 | private static final int GIVE_ITEM_TIMEOUT_DURATION = 20;
|
| 58 | private static final Vec3 THROW_VELOCITY = new Vec3(0.2F, 0.3F, 0.2F);
|
| 59 | private static final int ITEM_PICKUP_COOLDOWN_DURATION = 60;
|
| 60 |
|
| 61 | protected static List<ActivityData<Allay>> getActivities() {
|
| 62 | return List.of(initCoreActivity(), initIdleActivity());
|
| 63 | }
|
| 64 |
|
| 65 | private static ActivityData<Allay> initCoreActivity() {
|
| 66 | return ActivityData.create(
|
| 67 | Activity.CORE,
|
| 68 | 0,
|
| 69 | ImmutableList.of(
|
| 70 | new Swim<>(0.8F),
|
| 71 | new AnimalPanic(2.5F),
|
| 72 | new LookAtTargetSink(45, 90),
|
| 73 | new MoveToTargetSink(),
|
| 74 | new CountDownCooldownTicks(MemoryModuleType.LIKED_NOTEBLOCK_COOLDOWN_TICKS),
|
| 75 | new CountDownCooldownTicks(MemoryModuleType.ITEM_PICKUP_COOLDOWN_TICKS)
|
| 76 | )
|
| 77 | );
|
| 78 | }
|
| 79 |
|
| 80 | private static ActivityData<Allay> initIdleActivity() {
|
| 81 | return ActivityData.create(
|
| 82 | Activity.IDLE,
|
| 83 | 0,
|
| 84 | ImmutableList.of(
|
| 85 | GoToWantedItem.create(mob -> true, 1.75F, true, 32),
|
| 86 | new GoAndGiveItemsToTarget<>(
|
| 87 | AllayAi::getItemDepositPosition, 2.25F, 20, AllayAi::throwItem, MemoryModuleType.ITEM_PICKUP_COOLDOWN_TICKS, 60, AllayAi::hasItemToThrow
|
| 88 | ),
|
| 89 | StayCloseToTarget.create(AllayAi::getItemDepositPosition, Predicate.not(AllayAi::hasWantedItem), 4, 16, 2.25F),
|
| 90 | SetEntityLookTargetSometimes.create(6.0F, UniformInt.of(30, 60)),
|
| 91 | new RunOne(ImmutableList.of(Pair.of(RandomStroll.fly(1.0F), 2), Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 2), Pair.of(new DoNothing(30, 60), 1)))
|
| 92 | )
|
| 93 | );
|
| 94 | }
|
| 95 |
|
| 96 | private static boolean hasItemToThrow(final Allay allay) {
|
| 97 | return !allay.getInventory().isEmpty();
|
| 98 | }
|
| 99 |
|
| 100 | public static void updateActivity(final Allay body) {
|
| 101 | body.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.IDLE));
|
| 102 | }
|
| 103 |
|
| 104 | public static void hearNoteblock(final LivingEntity allay, final BlockPos pos) {
|
| 105 | Brain<?> brain = allay.getBrain();
|
| 106 | GlobalPos globalPos = GlobalPos.of(allay.level().dimension(), pos);
|
| 107 | Optional<GlobalPos> likedNoteblockPos = brain.getMemory(MemoryModuleType.LIKED_NOTEBLOCK_POSITION);
|
| 108 | if (likedNoteblockPos.isEmpty()) {
|
| 109 | brain.setMemory(MemoryModuleType.LIKED_NOTEBLOCK_POSITION, globalPos);
|
| 110 | brain.setMemory(MemoryModuleType.LIKED_NOTEBLOCK_COOLDOWN_TICKS, 600);
|
| 111 | } else if (likedNoteblockPos.get().equals(globalPos)) {
|
| 112 | brain.setMemory(MemoryModuleType.LIKED_NOTEBLOCK_COOLDOWN_TICKS, 600);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | private static Optional<PositionTracker> getItemDepositPosition(final LivingEntity allay) {
|
| 117 | Brain<?> brain = allay.getBrain();
|
| 118 | Optional<GlobalPos> likedNoteblockPos = brain.getMemory(MemoryModuleType.LIKED_NOTEBLOCK_POSITION);
|
| 119 | if (likedNoteblockPos.isPresent()) {
|
| 120 | GlobalPos position = likedNoteblockPos.get();
|
| 121 | if (shouldDepositItemsAtLikedNoteblock(allay, brain, position)) {
|
| 122 | return Optional.of(new BlockPosTracker(position.pos().above()));
|
| 123 | }
|
| 124 |
|
| 125 | brain.eraseMemory(MemoryModuleType.LIKED_NOTEBLOCK_POSITION);
|
| 126 | }
|
| 127 |
|
| 128 | return getLikedPlayerPositionTracker(allay);
|
| 129 | }
|
| 130 |
|
| 131 | private static boolean hasWantedItem(final LivingEntity allay) {
|
| 132 | Brain<?> brain = allay.getBrain();
|
| 133 | return brain.hasMemoryValue(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM);
|
| 134 | }
|
| 135 |
|
| 136 | private static boolean shouldDepositItemsAtLikedNoteblock(final LivingEntity allay, final Brain<?> brain, final GlobalPos likedNoteblockPos) {
|
| 137 | Optional<Integer> likedNoteblockCooldown = brain.getMemory(MemoryModuleType.LIKED_NOTEBLOCK_COOLDOWN_TICKS);
|
| 138 | Level level = allay.level();
|
| 139 | return likedNoteblockPos.isCloseEnough(level.dimension(), allay.blockPosition(), 1024)
|
| 140 | && level.getBlockState(likedNoteblockPos.pos()).is(Blocks.NOTE_BLOCK)
|
| 141 | && likedNoteblockCooldown.isPresent();
|
| 142 | }
|
| 143 |
|
| 144 | private static Optional<PositionTracker> getLikedPlayerPositionTracker(final LivingEntity allay) {
|
| 145 | return getLikedPlayer(allay).map(serverPlayer -> new EntityTracker(serverPlayer, true));
|
| 146 | }
|
| 147 |
|
| 148 | public static Optional<ServerPlayer> getLikedPlayer(final LivingEntity allay) {
|
| 149 | Level level = allay.level();
|
| 150 | if (!level.isClientSide() && level instanceof ServerLevel serverLevel) {
|
| 151 | Optional<UUID> likedPlayer = allay.getBrain().getMemory(MemoryModuleType.LIKED_PLAYER);
|
| 152 | if (likedPlayer.isPresent()) {
|
| 153 | if (serverLevel.getEntity(likedPlayer.get()) instanceof ServerPlayer serverPlayer
|
| 154 | && (serverPlayer.gameMode.isSurvival() || serverPlayer.gameMode.isCreative())
|
| 155 | && serverPlayer.closerThan(allay, 64.0)) {
|
| 156 | return Optional.of(serverPlayer);
|
| 157 | }
|
| 158 |
|
| 159 | return Optional.empty();
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | return Optional.empty();
|
| 164 | }
|
| 165 |
|
| 166 | private static void throwItem(final ServerLevel level, final Allay thrower, final Vec3 targetPos) {
|
| 167 | ItemStack item = thrower.getInventory().removeItem(0, 1);
|
| 168 | if (!item.isEmpty()) {
|
| 169 | BehaviorUtils.throwItem(thrower, item, targetPos.add(0.0, 1.0, 0.0), THROW_VELOCITY, 0.2F);
|
| 170 | getLikedPlayer(thrower).ifPresent(player -> CriteriaTriggers.ALLAY_DROP_ITEM_ON_BLOCK.trigger(player, BlockPos.containing(targetPos).below(), item));
|
| 171 | if (level.getGameTime() % 7L == 0L && level.getRandom().nextDouble() < 0.9) {
|
| 172 | float pitch = Util.getRandom(Allay.THROW_SOUND_PITCHES, level.getRandom());
|
| 173 | level.playSound(null, thrower, SoundEvents.ALLAY_THROW, SoundSource.NEUTRAL, 1.0F, pitch);
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 | }
|
| 178 |
|