| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.function.Function;
|
| 6 | import java.util.function.Predicate;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.world.entity.LivingEntity;
|
| 9 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 10 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public class GoAndGiveItemsToTarget<E extends LivingEntity> extends Behavior<E> {
|
| 14 | private static final int CLOSE_ENOUGH_DISTANCE_TO_TARGET = 3;
|
| 15 | private final Function<LivingEntity, Optional<PositionTracker>> targetPositionGetter;
|
| 16 | private final float speedModifier;
|
| 17 | private final GoAndGiveItemsToTarget.ItemThrower<E> itemThrower;
|
| 18 | private final MemoryModuleType<Integer> cooldownMemory;
|
| 19 | private final int cooldownDuration;
|
| 20 | private final Predicate<E> hasItemPredicate;
|
| 21 |
|
| 22 | public GoAndGiveItemsToTarget(
|
| 23 | final Function<LivingEntity, Optional<PositionTracker>> targetPositionGetter,
|
| 24 | final float speedModifier,
|
| 25 | final int timeoutDuration,
|
| 26 | final GoAndGiveItemsToTarget.ItemThrower<E> itemThrower,
|
| 27 | final MemoryModuleType<Integer> cooldownMemory,
|
| 28 | final int cooldownDuration,
|
| 29 | final Predicate<E> hasItemPredicate
|
| 30 | ) {
|
| 31 | super(
|
| 32 | Map.of(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, MemoryModuleType.WALK_TARGET, MemoryStatus.REGISTERED, cooldownMemory, MemoryStatus.REGISTERED),
|
| 33 | timeoutDuration
|
| 34 | );
|
| 35 | this.targetPositionGetter = targetPositionGetter;
|
| 36 | this.speedModifier = speedModifier;
|
| 37 | this.itemThrower = itemThrower;
|
| 38 | this.cooldownMemory = cooldownMemory;
|
| 39 | this.cooldownDuration = cooldownDuration;
|
| 40 | this.hasItemPredicate = hasItemPredicate;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected boolean checkExtraStartConditions(final ServerLevel level, final E body) {
|
| 45 | return this.canThrowItemToTarget(body);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected boolean canStillUse(final ServerLevel level, final E body, final long timestamp) {
|
| 50 | return this.canThrowItemToTarget(body);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected void start(final ServerLevel level, final E body, final long timestamp) {
|
| 55 | this.targetPositionGetter.apply(body).ifPresent(positionTracker -> BehaviorUtils.setWalkAndLookTargetMemories(body, positionTracker, this.speedModifier, 3));
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void tick(final ServerLevel level, final E body, final long timestamp) {
|
| 60 | Optional<PositionTracker> targetPosition = this.targetPositionGetter.apply(body);
|
| 61 | if (!targetPosition.isEmpty()) {
|
| 62 | PositionTracker depositTarget = targetPosition.get();
|
| 63 | Vec3 depositPosition = depositTarget.currentPosition();
|
| 64 | double distanceToTarget = depositPosition.distanceTo(body.getEyePosition());
|
| 65 | if (distanceToTarget < 3.0) {
|
| 66 | this.itemThrower.throwItem(level, body, depositTarget.currentPosition());
|
| 67 | body.getBrain().setMemory(this.cooldownMemory, this.cooldownDuration);
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | private boolean canThrowItemToTarget(final E body) {
|
| 73 | return this.hasItemPredicate.test(body) && this.targetPositionGetter.apply(body).isPresent();
|
| 74 | }
|
| 75 |
|
| 76 | @FunctionalInterface
|
| 77 | public interface ItemThrower<E> {
|
| 78 | void throwItem(ServerLevel level, E thrower, Vec3 targetPos);
|
| 79 | }
|
| 80 | }
|
| 81 |
|