| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.world.InteractionHand;
|
| 6 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 7 | import net.minecraft.world.entity.animal.wolf.Wolf;
|
| 8 | import net.minecraft.world.entity.player.Player;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.item.Items;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class BegGoal extends Goal {
|
| 14 | private final Wolf wolf;
|
| 15 | private @Nullable Player player;
|
| 16 | private final ServerLevel level;
|
| 17 | private final float lookDistance;
|
| 18 | private int lookTime;
|
| 19 | private final TargetingConditions begTargeting;
|
| 20 |
|
| 21 | public BegGoal(final Wolf wolf, final float lookDistance) {
|
| 22 | this.wolf = wolf;
|
| 23 | this.level = getServerLevel(wolf);
|
| 24 | this.lookDistance = lookDistance;
|
| 25 | this.begTargeting = TargetingConditions.forNonCombat().range(lookDistance);
|
| 26 | this.setFlags(EnumSet.of(Goal.Flag.LOOK));
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public boolean canUse() {
|
| 31 | this.player = this.level.getNearestPlayer(this.begTargeting, this.wolf);
|
| 32 | return this.player == null ? false : this.playerHoldingInteresting(this.player);
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public boolean canContinueToUse() {
|
| 37 | if (!this.player.isAlive()) {
|
| 38 | return false;
|
| 39 | } else {
|
| 40 | return this.wolf.distanceToSqr(this.player) > this.lookDistance * this.lookDistance
|
| 41 | ? false
|
| 42 | : this.lookTime > 0 && this.playerHoldingInteresting(this.player);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public void start() {
|
| 48 | this.wolf.setIsInterested(true);
|
| 49 | this.lookTime = this.adjustedTickDelay(40 + this.wolf.getRandom().nextInt(40));
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void stop() {
|
| 54 | this.wolf.setIsInterested(false);
|
| 55 | this.player = null;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void tick() {
|
| 60 | this.wolf.getLookControl().setLookAt(this.player.getX(), this.player.getEyeY(), this.player.getZ(), 10.0F, this.wolf.getMaxHeadXRot());
|
| 61 | this.lookTime--;
|
| 62 | }
|
| 63 |
|
| 64 | private boolean playerHoldingInteresting(final Player player) {
|
| 65 | for (InteractionHand hand : InteractionHand.values()) {
|
| 66 | ItemStack itemStack = player.getItemInHand(hand);
|
| 67 | if (itemStack.is(Items.BONE) || this.wolf.isFood(itemStack)) {
|
| 68 | return true;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return false;
|
| 73 | }
|
| 74 | }
|
| 75 |
|