| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.world.level.block.Blocks;
|
| 6 | import net.minecraft.world.level.block.DiodeBlock;
|
| 7 | import net.minecraft.world.level.block.RedstoneWireBlock;
|
| 8 | import net.minecraft.world.level.block.state.BlockState;
|
| 9 |
|
| 10 | public interface SignalGetter extends BlockGetter {
|
| 11 | Direction[] DIRECTIONS = Direction.values();
|
| 12 |
|
| 13 | default int getDirectSignal(final BlockPos pos, final Direction direction) {
|
| 14 | return this.getBlockState(pos).getDirectSignal(this, pos, direction);
|
| 15 | }
|
| 16 |
|
| 17 | default int getDirectSignalTo(final BlockPos pos) {
|
| 18 | int result = 0;
|
| 19 | result = Math.max(result, this.getDirectSignal(pos.below(), Direction.DOWN));
|
| 20 | if (result >= 15) {
|
| 21 | return result;
|
| 22 | }
|
| 23 |
|
| 24 | result = Math.max(result, this.getDirectSignal(pos.above(), Direction.UP));
|
| 25 | if (result >= 15) {
|
| 26 | return result;
|
| 27 | }
|
| 28 |
|
| 29 | result = Math.max(result, this.getDirectSignal(pos.north(), Direction.NORTH));
|
| 30 | if (result >= 15) {
|
| 31 | return result;
|
| 32 | }
|
| 33 |
|
| 34 | result = Math.max(result, this.getDirectSignal(pos.south(), Direction.SOUTH));
|
| 35 | if (result >= 15) {
|
| 36 | return result;
|
| 37 | }
|
| 38 |
|
| 39 | result = Math.max(result, this.getDirectSignal(pos.west(), Direction.WEST));
|
| 40 | if (result >= 15) {
|
| 41 | return result;
|
| 42 | }
|
| 43 |
|
| 44 | result = Math.max(result, this.getDirectSignal(pos.east(), Direction.EAST));
|
| 45 | return result >= 15 ? result : result;
|
| 46 | }
|
| 47 |
|
| 48 | default int getControlInputSignal(final BlockPos pos, final Direction direction, final boolean onlyDiodes) {
|
| 49 | BlockState blockState = this.getBlockState(pos);
|
| 50 | if (onlyDiodes) {
|
| 51 | return DiodeBlock.isDiode(blockState) ? this.getDirectSignal(pos, direction) : 0;
|
| 52 | } else if (blockState.is(Blocks.REDSTONE_BLOCK)) {
|
| 53 | return 15;
|
| 54 | } else if (blockState.is(Blocks.REDSTONE_WIRE)) {
|
| 55 | return blockState.getValue(RedstoneWireBlock.POWER);
|
| 56 | } else {
|
| 57 | return blockState.isSignalSource() ? this.getDirectSignal(pos, direction) : 0;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | default boolean hasSignal(final BlockPos pos, final Direction direction) {
|
| 62 | return this.getSignal(pos, direction) > 0;
|
| 63 | }
|
| 64 |
|
| 65 | default int getSignal(final BlockPos pos, final Direction direction) {
|
| 66 | BlockState state = this.getBlockState(pos);
|
| 67 | int signal = state.getSignal(this, pos, direction);
|
| 68 | return state.isRedstoneConductor(this, pos) ? Math.max(signal, this.getDirectSignalTo(pos)) : signal;
|
| 69 | }
|
| 70 |
|
| 71 | default int getBestOwnOrNeighbourSignal(final BlockPos pos) {
|
| 72 | BlockState blockState = this.getBlockState(pos);
|
| 73 | return Math.max(this.getBestNeighborSignal(pos), blockState.isSignalSource() ? blockState.getOwnSignal(this, pos) : 0);
|
| 74 | }
|
| 75 |
|
| 76 | default boolean hasNeighborSignal(final BlockPos blockPos) {
|
| 77 | if (this.getSignal(blockPos.below(), Direction.DOWN) > 0) {
|
| 78 | return true;
|
| 79 | } else if (this.getSignal(blockPos.above(), Direction.UP) > 0) {
|
| 80 | return true;
|
| 81 | } else if (this.getSignal(blockPos.north(), Direction.NORTH) > 0) {
|
| 82 | return true;
|
| 83 | } else if (this.getSignal(blockPos.south(), Direction.SOUTH) > 0) {
|
| 84 | return true;
|
| 85 | } else {
|
| 86 | return this.getSignal(blockPos.west(), Direction.WEST) > 0 ? true : this.getSignal(blockPos.east(), Direction.EAST) > 0;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | default int getBestNeighborSignal(final BlockPos pos) {
|
| 91 | int best = 0;
|
| 92 |
|
| 93 | for (Direction direction : DIRECTIONS) {
|
| 94 | int signal = this.getSignal(pos.relative(direction), direction);
|
| 95 | if (signal >= 15) {
|
| 96 | return 15;
|
| 97 | }
|
| 98 |
|
| 99 | if (signal > best) {
|
| 100 | best = signal;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | return best;
|
| 105 | }
|
| 106 | }
|
| 107 |
|