| 1 | package net.minecraft.world.level.blockscan;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterables;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.function.BiConsumer;
|
| 6 | import java.util.function.BiPredicate;
|
| 7 | import java.util.function.Predicate;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.util.Continuation;
|
| 10 | import net.minecraft.world.level.LevelReader;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 13 |
|
| 14 | public class OrderedBlockMatcher extends BlockMatcher {
|
| 15 | private Iterable<BlockPos> positions;
|
| 16 |
|
| 17 | public OrderedBlockMatcher(final LevelReader level, final Iterable<BlockPos> positions) {
|
| 18 | super(level);
|
| 19 | this.positions = positions;
|
| 20 | }
|
| 21 |
|
| 22 | public OrderedBlockMatcher filterPos(final Predicate<? super BlockPos> predicate) {
|
| 23 | this.positions = Iterables.filter(this.positions, predicate::test);
|
| 24 | return this;
|
| 25 | }
|
| 26 |
|
| 27 | public OrderedBlockMatcher filterState(final Predicate<BlockState> predicate) {
|
| 28 | super.filterState(predicate);
|
| 29 | return this;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public boolean atLeastMatched(final int n) {
|
| 34 | return BlockScanUtils.findBlocksWithCache(this.level, this.positions, this.statePredicate, new BlockStateConsumer() {
|
| 35 | private int count = 0;
|
| 36 |
|
| 37 | @Override
|
| 38 | public Continuation apply(final BlockPos pos, final BlockState state) {
|
| 39 | return Continuation.abortIf(++this.count >= n);
|
| 40 | }
|
| 41 | });
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected boolean anyMatched(final Predicate<BlockState> statePredicate) {
|
| 46 | return BlockScanUtils.findBlocksWithCache(this.level, this.positions, statePredicate, (var0, var1) -> Continuation.ABORT);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void forEach(final BiConsumer<BlockPos, BlockState> consumer) {
|
| 51 | BlockScanUtils.findBlocksWithCache(this.level, this.positions, this.statePredicate, (pos, state) -> {
|
| 52 | consumer.accept(pos, state);
|
| 53 | return Continuation.CONTINUE;
|
| 54 | });
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public boolean forEachUntil(final BlockStateConsumer consumer) {
|
| 59 | return BlockScanUtils.findBlocksWithCache(this.level, this.positions, this.statePredicate, consumer);
|
| 60 | }
|
| 61 |
|
| 62 | public Optional<BlockPos> findFirst() {
|
| 63 | MutableObject<Optional<BlockPos>> result = new MutableObject<>(Optional.empty());
|
| 64 | BlockScanUtils.findBlocksWithCache(this.level, this.positions, this.statePredicate, (pos, var2) -> {
|
| 65 | result.setValue(Optional.of(pos.immutable()));
|
| 66 | return Continuation.ABORT;
|
| 67 | });
|
| 68 | return result.get();
|
| 69 | }
|
| 70 |
|
| 71 | public Optional<BlockPos> findFirst(final BiPredicate<BlockPos, BlockState> predicate) {
|
| 72 | MutableObject<Optional<BlockPos>> result = new MutableObject<>(Optional.empty());
|
| 73 | BlockScanUtils.findBlocksWithCache(this.level, this.positions, this.statePredicate, (pos, state) -> {
|
| 74 | if (predicate.test(pos, state)) {
|
| 75 | result.setValue(Optional.of(pos.immutable()));
|
| 76 | return Continuation.ABORT;
|
| 77 | } else {
|
| 78 | return Continuation.CONTINUE;
|
| 79 | }
|
| 80 | });
|
| 81 | return result.get();
|
| 82 | }
|
| 83 | }
|
| 84 |
|