| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.function.BiPredicate;
|
| 4 | import java.util.function.Function;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.world.level.LevelAccessor;
|
| 8 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 9 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 10 | import net.minecraft.world.level.block.state.BlockState;
|
| 11 | import net.minecraft.world.level.block.state.properties.Property;
|
| 12 |
|
| 13 | public class DoubleBlockCombiner {
|
| 14 | public static <S extends BlockEntity> DoubleBlockCombiner.NeighborCombineResult<S> combineWithNeigbour(
|
| 15 | final BlockEntityType<S> entityType,
|
| 16 | final Function<BlockState, DoubleBlockCombiner.BlockType> typeResolver,
|
| 17 | final Function<BlockState, Direction> connectionResolver,
|
| 18 | final Property<Direction> facingProperty,
|
| 19 | final BlockState state,
|
| 20 | final LevelAccessor level,
|
| 21 | final BlockPos pos,
|
| 22 | final BiPredicate<LevelAccessor, BlockPos> blockedChecker
|
| 23 | ) {
|
| 24 | S blockEntity = entityType.getBlockEntity(level, pos);
|
| 25 | if (blockEntity == null) {
|
| 26 | return DoubleBlockCombiner.Combiner::acceptNone;
|
| 27 | }
|
| 28 |
|
| 29 | if (blockedChecker.test(level, pos)) {
|
| 30 | return DoubleBlockCombiner.Combiner::acceptNone;
|
| 31 | }
|
| 32 |
|
| 33 | DoubleBlockCombiner.BlockType type = typeResolver.apply(state);
|
| 34 | boolean single = type == DoubleBlockCombiner.BlockType.SINGLE;
|
| 35 | boolean isFirst = type == DoubleBlockCombiner.BlockType.FIRST;
|
| 36 | if (single) {
|
| 37 | return new DoubleBlockCombiner.NeighborCombineResult.Single<>(blockEntity);
|
| 38 | }
|
| 39 |
|
| 40 | BlockPos neighborPos = pos.relative(connectionResolver.apply(state));
|
| 41 | BlockState neighbourState = level.getBlockState(neighborPos);
|
| 42 | if (neighbourState.is(state.getBlock())) {
|
| 43 | DoubleBlockCombiner.BlockType neighbourType = typeResolver.apply(neighbourState);
|
| 44 | if (neighbourType != DoubleBlockCombiner.BlockType.SINGLE
|
| 45 | && type != neighbourType
|
| 46 | && neighbourState.getValue(facingProperty) == state.getValue(facingProperty)) {
|
| 47 | if (blockedChecker.test(level, neighborPos)) {
|
| 48 | return DoubleBlockCombiner.Combiner::acceptNone;
|
| 49 | }
|
| 50 |
|
| 51 | S neighbour = entityType.getBlockEntity(level, neighborPos);
|
| 52 | if (neighbour != null) {
|
| 53 | S first = isFirst ? blockEntity : neighbour;
|
| 54 | S second = isFirst ? neighbour : blockEntity;
|
| 55 | return new DoubleBlockCombiner.NeighborCombineResult.Double<>(first, second);
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | return new DoubleBlockCombiner.NeighborCombineResult.Single<>(blockEntity);
|
| 61 | }
|
| 62 |
|
| 63 | public enum BlockType {
|
| 64 | SINGLE,
|
| 65 | FIRST,
|
| 66 | SECOND;
|
| 67 | }
|
| 68 |
|
| 69 | public interface Combiner<S, T> {
|
| 70 | T acceptDouble(S first, S second);
|
| 71 |
|
| 72 | T acceptSingle(S single);
|
| 73 |
|
| 74 | T acceptNone();
|
| 75 | }
|
| 76 |
|
| 77 | public interface NeighborCombineResult<S> {
|
| 78 | <T> T apply(DoubleBlockCombiner.Combiner<? super S, T> callback);
|
| 79 |
|
| 80 | final class Double<S> implements DoubleBlockCombiner.NeighborCombineResult<S> {
|
| 81 | private final S first;
|
| 82 | private final S second;
|
| 83 |
|
| 84 | public Double(final S first, final S second) {
|
| 85 | this.first = first;
|
| 86 | this.second = second;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public <T> T apply(final DoubleBlockCombiner.Combiner<? super S, T> callback) {
|
| 91 | return callback.acceptDouble(this.first, this.second);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | final class Single<S> implements DoubleBlockCombiner.NeighborCombineResult<S> {
|
| 96 | private final S single;
|
| 97 |
|
| 98 | public Single(final S single) {
|
| 99 | this.single = single;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public <T> T apply(final DoubleBlockCombiner.Combiner<? super S, T> callback) {
|
| 104 | return callback.acceptSingle(this.single);
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|