| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.world.entity.Mob;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.level.block.Block;
|
| 10 | import net.minecraft.world.level.block.Blocks;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.gamerules.GameRules;
|
| 13 |
|
| 14 | public class EatBlockGoal extends Goal {
|
| 15 | private static final int EAT_ANIMATION_TICKS = 40;
|
| 16 | private static final Predicate<BlockState> IS_EDIBLE = state -> state.is(BlockTags.EDIBLE_FOR_SHEEP);
|
| 17 | private final Mob mob;
|
| 18 | private final Level level;
|
| 19 | private int eatAnimationTick;
|
| 20 |
|
| 21 | public EatBlockGoal(final Mob mob) {
|
| 22 | this.mob = mob;
|
| 23 | this.level = mob.level();
|
| 24 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK, Goal.Flag.JUMP));
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public boolean canUse() {
|
| 29 | if (this.mob.getRandom().nextInt(this.adjustedTickDelay(this.mob.isBaby() ? 50 : 1000)) != 0) {
|
| 30 | return false;
|
| 31 | }
|
| 32 |
|
| 33 | BlockPos pos = this.mob.blockPosition();
|
| 34 | return IS_EDIBLE.test(this.level.getBlockState(pos)) ? true : this.level.getBlockState(pos.below()).is(Blocks.GRASS_BLOCK);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void start() {
|
| 39 | this.eatAnimationTick = this.adjustedTickDelay(40);
|
| 40 | this.level.broadcastEntityEvent(this.mob, (byte)10);
|
| 41 | this.mob.getNavigation().stop();
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void stop() {
|
| 46 | this.eatAnimationTick = 0;
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public boolean canContinueToUse() {
|
| 51 | return this.eatAnimationTick > 0;
|
| 52 | }
|
| 53 |
|
| 54 | public int getEatAnimationTick() {
|
| 55 | return this.eatAnimationTick;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void tick() {
|
| 60 | this.eatAnimationTick = Math.max(0, this.eatAnimationTick - 1);
|
| 61 | if (this.eatAnimationTick == this.adjustedTickDelay(4)) {
|
| 62 | BlockPos pos = this.mob.blockPosition();
|
| 63 | if (IS_EDIBLE.test(this.level.getBlockState(pos))) {
|
| 64 | if (getServerLevel(this.level).getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 65 | this.level.destroyBlock(pos, false);
|
| 66 | }
|
| 67 |
|
| 68 | this.mob.ate();
|
| 69 | } else {
|
| 70 | BlockPos below = pos.below();
|
| 71 | if (this.level.getBlockState(below).is(Blocks.GRASS_BLOCK)) {
|
| 72 | if (getServerLevel(this.level).getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 73 | this.level.levelEvent(2001, below, Block.getId(Blocks.GRASS_BLOCK.defaultBlockState()));
|
| 74 | this.level.setBlock(below, Blocks.DIRT.defaultBlockState(), 2);
|
| 75 | }
|
| 76 |
|
| 77 | this.mob.ate();
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 |
|