| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.Container;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.EntitySelector;
|
| 12 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 13 | import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
| 14 | import net.minecraft.world.entity.vehicle.minecart.MinecartCommandBlock;
|
| 15 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 21 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 22 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 23 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 24 | import net.minecraft.world.level.block.state.properties.Property;
|
| 25 | import net.minecraft.world.level.block.state.properties.RailShape;
|
| 26 | import net.minecraft.world.phys.AABB;
|
| 27 |
|
| 28 | public class DetectorRailBlock extends BaseRailBlock {
|
| 29 | public static final EnumProperty<RailShape> SHAPE = BlockStateProperties.RAIL_SHAPE_STRAIGHT;
|
| 30 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 31 | private static final int PRESSED_CHECK_PERIOD = 20;
|
| 32 |
|
| 33 | public DetectorRailBlock(final BlockBehaviour.Properties properties) {
|
| 34 | super(true, properties);
|
| 35 | this.registerDefaultState(this.stateDefinition.any().setValue(POWERED, false).setValue(SHAPE, RailShape.NORTH_SOUTH).setValue(WATERLOGGED, false));
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected boolean isSignalSource(final BlockState state) {
|
| 40 | return true;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 45 | return state.getValue(POWERED) ? 15 : 0;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected void entityInside(
|
| 50 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 51 | ) {
|
| 52 | if (!level.isClientSide()) {
|
| 53 | if (!state.getValue(POWERED)) {
|
| 54 | this.checkPressed(level, pos, state);
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 61 | if (state.getValue(POWERED)) {
|
| 62 | this.checkPressed(level, pos, state);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 68 | if (!state.getValue(POWERED)) {
|
| 69 | return 0;
|
| 70 | } else {
|
| 71 | return direction == Direction.UP ? 15 : 0;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private void checkPressed(final Level level, final BlockPos pos, final BlockState state) {
|
| 76 | if (this.canSurvive(state, level, pos) && level.getBlockState(pos).is(this)) {
|
| 77 | boolean wasPressed = state.getValue(POWERED);
|
| 78 | boolean shouldBePressed = false;
|
| 79 | List<AbstractMinecart> entities = this.getInteractingMinecartOfType(level, pos, AbstractMinecart.class, e -> true);
|
| 80 | if (!entities.isEmpty()) {
|
| 81 | shouldBePressed = true;
|
| 82 | }
|
| 83 |
|
| 84 | if (shouldBePressed && !wasPressed) {
|
| 85 | BlockState newState = state.setValue(POWERED, true);
|
| 86 | level.setBlockAndUpdate(pos, newState);
|
| 87 | this.updatePowerToConnected(level, pos, newState, true);
|
| 88 | level.updateNeighborsAt(pos, this);
|
| 89 | level.updateNeighborsAt(pos.below(), this);
|
| 90 | level.setBlocksDirty(pos, state, newState);
|
| 91 | }
|
| 92 |
|
| 93 | if (!shouldBePressed && wasPressed) {
|
| 94 | BlockState newState = state.setValue(POWERED, false);
|
| 95 | level.setBlockAndUpdate(pos, newState);
|
| 96 | this.updatePowerToConnected(level, pos, newState, false);
|
| 97 | level.updateNeighborsAt(pos, this);
|
| 98 | level.updateNeighborsAt(pos.below(), this);
|
| 99 | level.setBlocksDirty(pos, state, newState);
|
| 100 | }
|
| 101 |
|
| 102 | if (shouldBePressed) {
|
| 103 | level.scheduleTick(pos, this, 20);
|
| 104 | }
|
| 105 |
|
| 106 | level.updateNeighbourForOutputSignal(pos, this);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | protected void updatePowerToConnected(final Level level, final BlockPos pos, final BlockState state, final boolean powered) {
|
| 111 | RailState rail = new RailState(level, pos, state);
|
| 112 |
|
| 113 | for (BlockPos connectionPos : rail.getConnections()) {
|
| 114 | BlockState connectionState = level.getBlockState(connectionPos);
|
| 115 | level.neighborChanged(connectionState, connectionPos, connectionState.getBlock(), null, false);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 121 | if (!oldState.is(state.getBlock())) {
|
| 122 | BlockState updatedState = this.updateState(state, level, pos, movedByPiston);
|
| 123 | this.checkPressed(level, pos, updatedState);
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public Property<RailShape> getShapeProperty() {
|
| 129 | return SHAPE;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 134 | return true;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 139 | if (state.getValue(POWERED)) {
|
| 140 | List<MinecartCommandBlock> commandBlocks = this.getInteractingMinecartOfType(level, pos, MinecartCommandBlock.class, e -> true);
|
| 141 | if (!commandBlocks.isEmpty()) {
|
| 142 | return commandBlocks.get(0).getCommandBlock().getSuccessCount();
|
| 143 | }
|
| 144 |
|
| 145 | List<AbstractMinecart> entities = this.getInteractingMinecartOfType(level, pos, AbstractMinecart.class, EntitySelector.CONTAINER_ENTITY_SELECTOR);
|
| 146 | if (!entities.isEmpty()) {
|
| 147 | return AbstractContainerMenu.getRedstoneSignalFromContainer((Container)entities.get(0));
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | return 0;
|
| 152 | }
|
| 153 |
|
| 154 | private <T extends AbstractMinecart> List<T> getInteractingMinecartOfType(
|
| 155 | final Level level, final BlockPos pos, final Class<T> type, final Predicate<Entity> containerEntitySelector
|
| 156 | ) {
|
| 157 | return level.getEntitiesOfClass(type, this.getSearchBB(pos), containerEntitySelector);
|
| 158 | }
|
| 159 |
|
| 160 | private AABB getSearchBB(final BlockPos pos) {
|
| 161 | double b = 0.2;
|
| 162 | return new AABB(pos.getX() + 0.2, pos.getY(), pos.getZ() + 0.2, pos.getX() + 1 - 0.2, pos.getY() + 1 - 0.2, pos.getZ() + 1 - 0.2);
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 167 | RailShape currentShape = state.getValue(SHAPE);
|
| 168 | RailShape newShape = this.rotate(currentShape, rotation);
|
| 169 | return state.setValue(SHAPE, newShape);
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 174 | RailShape currentShape = state.getValue(SHAPE);
|
| 175 | RailShape newShape = this.mirror(currentShape, mirror);
|
| 176 | return state.setValue(SHAPE, newShape);
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 181 | builder.add(SHAPE, POWERED, WATERLOGGED);
|
| 182 | }
|
| 183 | }
|
| 184 |
|