| 1 | package net.minecraft.world.entity.animal.golem;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableMap;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.function.Consumer;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.minecraft.sounds.SoundEvent;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.tags.BlockTags;
|
| 13 | import net.minecraft.util.valueproviders.UniformInt;
|
| 14 | import net.minecraft.world.Container;
|
| 15 | import net.minecraft.world.entity.EntityTypes;
|
| 16 | import net.minecraft.world.entity.PathfinderMob;
|
| 17 | import net.minecraft.world.entity.ai.ActivityData;
|
| 18 | import net.minecraft.world.entity.ai.behavior.AnimalPanic;
|
| 19 | import net.minecraft.world.entity.ai.behavior.CountDownCooldownTicks;
|
| 20 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 21 | import net.minecraft.world.entity.ai.behavior.InteractWithDoor;
|
| 22 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 23 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 24 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 25 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 26 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
|
| 27 | import net.minecraft.world.entity.ai.behavior.TransportItemsBetweenContainers;
|
| 28 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 29 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 30 | import net.minecraft.world.entity.schedule.Activity;
|
| 31 | import net.minecraft.world.level.block.Blocks;
|
| 32 | import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
| 33 | import net.minecraft.world.level.block.state.BlockState;
|
| 34 | import org.jspecify.annotations.Nullable;
|
| 35 |
|
| 36 | public class CopperGolemAi {
|
| 37 | private static final float SPEED_MULTIPLIER_WHEN_PANICKING = 1.5F;
|
| 38 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 1.0F;
|
| 39 | private static final int TRANSPORT_ITEM_HORIZONTAL_SEARCH_RADIUS = 32;
|
| 40 | private static final int TRANSPORT_ITEM_VERTICAL_SEARCH_RADIUS = 8;
|
| 41 | private static final int TICK_TO_START_ON_REACHED_INTERACTION = 1;
|
| 42 | private static final int TICK_TO_PLAY_ON_REACHED_SOUND = 9;
|
| 43 | private static final Predicate<BlockState> TRANSPORT_ITEM_SOURCE_BLOCK = block -> block.is(BlockTags.COPPER_CHESTS);
|
| 44 | private static final Predicate<BlockState> TRANSPORT_ITEM_DESTINATION_BLOCK = block -> block.is(Blocks.CHEST) || block.is(Blocks.TRAPPED_CHEST);
|
| 45 |
|
| 46 | protected static List<ActivityData<CopperGolem>> getActivities() {
|
| 47 | return List.of(initCoreActivity(), initIdleActivity());
|
| 48 | }
|
| 49 |
|
| 50 | public static void updateActivity(final CopperGolem body) {
|
| 51 | body.getBrain().setActiveActivityToFirstValid(ImmutableList.of(Activity.IDLE));
|
| 52 | }
|
| 53 |
|
| 54 | private static ActivityData<CopperGolem> initCoreActivity() {
|
| 55 | return ActivityData.create(
|
| 56 | Activity.CORE,
|
| 57 | 0,
|
| 58 | ImmutableList.of(
|
| 59 | new AnimalPanic<>(1.5F),
|
| 60 | new LookAtTargetSink(45, 90),
|
| 61 | new MoveToTargetSink(),
|
| 62 | InteractWithDoor.create(),
|
| 63 | new CountDownCooldownTicks(MemoryModuleType.GAZE_COOLDOWN_TICKS),
|
| 64 | new CountDownCooldownTicks(MemoryModuleType.TRANSPORT_ITEMS_COOLDOWN_TICKS)
|
| 65 | )
|
| 66 | );
|
| 67 | }
|
| 68 |
|
| 69 | private static ActivityData<CopperGolem> initIdleActivity() {
|
| 70 | return ActivityData.create(
|
| 71 | Activity.IDLE,
|
| 72 | ImmutableList.of(
|
| 73 | Pair.of(
|
| 74 | 0,
|
| 75 | new TransportItemsBetweenContainers(
|
| 76 | 1.0F, TRANSPORT_ITEM_SOURCE_BLOCK, TRANSPORT_ITEM_DESTINATION_BLOCK, 32, 8, getTargetReachedInteractions(), onTravelling(), shouldQueueForTarget()
|
| 77 | )
|
| 78 | ),
|
| 79 | Pair.of(1, SetEntityLookTargetSometimes.create(EntityTypes.PLAYER, 6.0F, UniformInt.of(40, 80))),
|
| 80 | Pair.of(
|
| 81 | 2,
|
| 82 | new RunOne<>(
|
| 83 | ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, MemoryModuleType.TRANSPORT_ITEMS_COOLDOWN_TICKS, MemoryStatus.VALUE_PRESENT),
|
| 84 | ImmutableList.of(Pair.of(RandomStroll.stroll(1.0F, 2, 2), 1), Pair.of(new DoNothing(30, 60), 1))
|
| 85 | )
|
| 86 | )
|
| 87 | )
|
| 88 | );
|
| 89 | }
|
| 90 |
|
| 91 | private static Map<TransportItemsBetweenContainers.ContainerInteractionState, TransportItemsBetweenContainers.OnTargetReachedInteraction> getTargetReachedInteractions() {
|
| 92 | return Map.of(
|
| 93 | TransportItemsBetweenContainers.ContainerInteractionState.PICKUP_ITEM,
|
| 94 | onReachedTargetInteraction(CopperGolemState.GETTING_ITEM, SoundEvents.COPPER_GOLEM_ITEM_GET),
|
| 95 | TransportItemsBetweenContainers.ContainerInteractionState.PICKUP_NO_ITEM,
|
| 96 | onReachedTargetInteraction(CopperGolemState.GETTING_NO_ITEM, SoundEvents.COPPER_GOLEM_ITEM_NO_GET),
|
| 97 | TransportItemsBetweenContainers.ContainerInteractionState.PLACE_ITEM,
|
| 98 | onReachedTargetInteraction(CopperGolemState.DROPPING_ITEM, SoundEvents.COPPER_GOLEM_ITEM_DROP),
|
| 99 | TransportItemsBetweenContainers.ContainerInteractionState.PLACE_NO_ITEM,
|
| 100 | onReachedTargetInteraction(CopperGolemState.DROPPING_NO_ITEM, SoundEvents.COPPER_GOLEM_ITEM_NO_DROP)
|
| 101 | );
|
| 102 | }
|
| 103 |
|
| 104 | private static TransportItemsBetweenContainers.OnTargetReachedInteraction onReachedTargetInteraction(
|
| 105 | final CopperGolemState state, final @Nullable SoundEvent sound
|
| 106 | ) {
|
| 107 | return (body, target, ticksSinceReachingTarget) -> {
|
| 108 | if (body instanceof CopperGolem copperGolem) {
|
| 109 | Container container = target.container();
|
| 110 | if (ticksSinceReachingTarget == 1) {
|
| 111 | container.startOpen(copperGolem);
|
| 112 | copperGolem.setOpenedChestPos(target.pos());
|
| 113 | copperGolem.setState(state);
|
| 114 | }
|
| 115 |
|
| 116 | if (ticksSinceReachingTarget == 9 && sound != null) {
|
| 117 | copperGolem.playSound(sound);
|
| 118 | }
|
| 119 |
|
| 120 | if (ticksSinceReachingTarget == 60) {
|
| 121 | if (container.getEntitiesWithContainerOpen().contains(body)) {
|
| 122 | container.stopOpen(copperGolem);
|
| 123 | }
|
| 124 |
|
| 125 | copperGolem.clearOpenedChestPos();
|
| 126 | }
|
| 127 | }
|
| 128 | };
|
| 129 | }
|
| 130 |
|
| 131 | private static Consumer<PathfinderMob> onTravelling() {
|
| 132 | return body -> {
|
| 133 | if (body instanceof CopperGolem copperGolem) {
|
| 134 | copperGolem.clearOpenedChestPos();
|
| 135 | copperGolem.setState(CopperGolemState.IDLE);
|
| 136 | }
|
| 137 | };
|
| 138 | }
|
| 139 |
|
| 140 | private static Predicate<TransportItemsBetweenContainers.TransportItemTarget> shouldQueueForTarget() {
|
| 141 | return transportTarget -> transportTarget.blockEntity() instanceof ChestBlockEntity chestBlockEntity
|
| 142 | ? !chestBlockEntity.getEntitiesWithContainerOpen().isEmpty()
|
| 143 | : false;
|
| 144 | }
|
| 145 | }
|
| 146 |
|