| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.tags.BlockTags;
|
| 8 | import net.minecraft.world.entity.animal.feline.Cat;
|
| 9 | import net.minecraft.world.level.LevelReader;
|
| 10 | import net.minecraft.world.level.block.BedBlock;
|
| 11 | import net.minecraft.world.level.block.Blocks;
|
| 12 | import net.minecraft.world.level.block.FurnaceBlock;
|
| 13 | import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.properties.BedPart;
|
| 16 | import org.apache.commons.lang3.function.TriFunction;
|
| 17 |
|
| 18 | public class CatSitOnBlockGoal extends MoveToBlockGoal {
|
| 19 | private final Cat cat;
|
| 20 | private static final List<Pair<Predicate<BlockState>, TriFunction<BlockState, LevelReader, BlockPos, Boolean>>> BLOCK_TYPE_VALIDATION = List.of(
|
| 21 | Pair.of(blockState -> blockState.is(Blocks.CHEST), (blockState, level, pos) -> ChestBlockEntity.getOpenCount(level, pos) < 1),
|
| 22 | Pair.of(blockState -> blockState.is(Blocks.FURNACE), (blockState, level, pos) -> blockState.getValue(FurnaceBlock.LIT)),
|
| 23 | Pair.of(
|
| 24 | blockState -> blockState.is(BlockTags.BEDS), (blockState, level, pos) -> blockState.getOptionalValue(BedBlock.PART).map(v -> v != BedPart.HEAD).orElse(true)
|
| 25 | )
|
| 26 | );
|
| 27 |
|
| 28 | public CatSitOnBlockGoal(final Cat cat, final double speedModifier) {
|
| 29 | super(cat, speedModifier, 8);
|
| 30 | this.cat = cat;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public boolean canUse() {
|
| 35 | return this.cat.isTame() && !this.cat.isOrderedToSit() && super.canUse();
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void start() {
|
| 40 | super.start();
|
| 41 | this.cat.setInSittingPose(false);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void stop() {
|
| 46 | super.stop();
|
| 47 | this.cat.setInSittingPose(false);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void tick() {
|
| 52 | super.tick();
|
| 53 | this.cat.setInSittingPose(this.isReachedTarget());
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected boolean isValidTarget(final LevelReader level, final BlockPos pos) {
|
| 58 | if (!level.isEmptyBlock(pos.above())) {
|
| 59 | return false;
|
| 60 | }
|
| 61 |
|
| 62 | BlockState blockState = level.getBlockState(pos);
|
| 63 | if (!blockState.is(BlockTags.CATS_CAN_SIT_ON)) {
|
| 64 | return false;
|
| 65 | }
|
| 66 |
|
| 67 | for (Pair<Predicate<BlockState>, TriFunction<BlockState, LevelReader, BlockPos, Boolean>> blockValidation : BLOCK_TYPE_VALIDATION) {
|
| 68 | if (blockValidation.getFirst().test(blockState)) {
|
| 69 | return blockValidation.getSecond().apply(blockState, level, pos);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return true;
|
| 74 | }
|
| 75 | }
|
| 76 |
|