| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.function.BiPredicate;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.tags.TagKey;
|
| 13 | import net.minecraft.util.valueproviders.UniformInt;
|
| 14 | import net.minecraft.world.entity.Mob;
|
| 15 | import net.minecraft.world.level.block.Block;
|
| 16 |
|
| 17 | public class LongJumpToPreferredBlock<E extends Mob> extends LongJumpToRandomPos<E> {
|
| 18 | private final TagKey<Block> preferredBlockTag;
|
| 19 | private final float preferredBlocksChance;
|
| 20 | private final List<LongJumpToRandomPos.PossibleJump> notPrefferedJumpCandidates = new ArrayList<>();
|
| 21 | private boolean currentlyWantingPreferredOnes;
|
| 22 |
|
| 23 | public LongJumpToPreferredBlock(
|
| 24 | final UniformInt timeBetweenLongJumps,
|
| 25 | final int maxLongJumpHeight,
|
| 26 | final int maxLongJumpWidth,
|
| 27 | final float maxJumpVelocity,
|
| 28 | final Function<E, SoundEvent> getJumpSound,
|
| 29 | final TagKey<Block> preferredBlockTag,
|
| 30 | final float preferredBlocksChance,
|
| 31 | final BiPredicate<E, BlockPos> acceptableLandingSpot
|
| 32 | ) {
|
| 33 | super(timeBetweenLongJumps, maxLongJumpHeight, maxLongJumpWidth, maxJumpVelocity, getJumpSound, acceptableLandingSpot);
|
| 34 | this.preferredBlockTag = preferredBlockTag;
|
| 35 | this.preferredBlocksChance = preferredBlocksChance;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected void start(final ServerLevel level, final E body, final long timestamp) {
|
| 40 | super.start(level, body, timestamp);
|
| 41 | this.notPrefferedJumpCandidates.clear();
|
| 42 | this.currentlyWantingPreferredOnes = body.getRandom().nextFloat() < this.preferredBlocksChance;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected Optional<LongJumpToRandomPos.PossibleJump> getJumpCandidate(final ServerLevel level) {
|
| 47 | if (!this.currentlyWantingPreferredOnes) {
|
| 48 | return super.getJumpCandidate(level);
|
| 49 | }
|
| 50 |
|
| 51 | BlockPos.MutableBlockPos testPos = new BlockPos.MutableBlockPos();
|
| 52 |
|
| 53 | while (!this.jumpCandidates.isEmpty()) {
|
| 54 | Optional<LongJumpToRandomPos.PossibleJump> jumpCandidate = super.getJumpCandidate(level);
|
| 55 | if (jumpCandidate.isPresent()) {
|
| 56 | LongJumpToRandomPos.PossibleJump possibleJump = jumpCandidate.get();
|
| 57 | if (level.getBlockState(testPos.setWithOffset(possibleJump.targetPos(), Direction.DOWN)).is(this.preferredBlockTag)) {
|
| 58 | return jumpCandidate;
|
| 59 | }
|
| 60 |
|
| 61 | this.notPrefferedJumpCandidates.add(possibleJump);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return !this.notPrefferedJumpCandidates.isEmpty() ? Optional.of(this.notPrefferedJumpCandidates.remove(0)) : Optional.empty();
|
| 66 | }
|
| 67 | }
|
| 68 |
|