| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.base.MoreObjects;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 15 | import net.minecraft.world.level.BlockGetter;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.LevelReader;
|
| 18 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 19 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 22 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 23 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 24 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 25 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 26 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 27 | import net.minecraft.world.level.redstone.Orientation;
|
| 28 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 29 | import net.minecraft.world.phys.shapes.Shapes;
|
| 30 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public class TripWireHookBlock extends Block {
|
| 34 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 35 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 36 | public static final BooleanProperty ATTACHED = BlockStateProperties.ATTACHED;
|
| 37 | protected static final int WIRE_DIST_MIN = 1;
|
| 38 | protected static final int WIRE_DIST_MAX = 42;
|
| 39 | private static final int RECHECK_PERIOD = 10;
|
| 40 | private static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateHorizontal(Block.boxZ(6.0, 0.0, 10.0, 10.0, 16.0));
|
| 41 |
|
| 42 | public TripWireHookBlock(final BlockBehaviour.Properties properties) {
|
| 43 | super(properties);
|
| 44 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(POWERED, false).setValue(ATTACHED, false));
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 49 | return SHAPES.get(state.getValue(FACING));
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 54 | Direction direction = state.getValue(FACING);
|
| 55 | BlockPos relative = pos.relative(direction.getOpposite());
|
| 56 | BlockState blockState = level.getBlockState(relative);
|
| 57 | return direction.getAxis().isHorizontal() && blockState.isFaceSturdy(level, relative, direction);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected BlockState updateShape(
|
| 62 | final BlockState state,
|
| 63 | final LevelReader level,
|
| 64 | final ScheduledTickAccess ticks,
|
| 65 | final BlockPos pos,
|
| 66 | final Direction directionToNeighbour,
|
| 67 | final BlockPos neighbourPos,
|
| 68 | final BlockState neighbourState,
|
| 69 | final RandomSource random
|
| 70 | ) {
|
| 71 | return directionToNeighbour.getOpposite() == state.getValue(FACING) && !state.canSurvive(level, pos)
|
| 72 | ? Blocks.AIR.defaultBlockState()
|
| 73 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 78 | BlockState state = this.defaultBlockState().setValue(POWERED, false).setValue(ATTACHED, false);
|
| 79 | LevelReader level = context.getLevel();
|
| 80 | BlockPos pos = context.getClickedPos();
|
| 81 | Direction[] directions = context.getNearestLookingDirections();
|
| 82 |
|
| 83 | for (Direction direction : directions) {
|
| 84 | if (direction.getAxis().isHorizontal()) {
|
| 85 | Direction facing = direction.getOpposite();
|
| 86 | state = state.setValue(FACING, facing);
|
| 87 | if (state.canSurvive(level, pos)) {
|
| 88 | return state;
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | return null;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 98 | calculateState(level, pos, state, false, false, -1, null);
|
| 99 | }
|
| 100 |
|
| 101 | public static void calculateState(
|
| 102 | final Level level,
|
| 103 | final BlockPos pos,
|
| 104 | final BlockState state,
|
| 105 | final boolean isBeingDestroyed,
|
| 106 | final boolean canUpdate,
|
| 107 | final int wireSource,
|
| 108 | final @Nullable BlockState wireSourceState
|
| 109 | ) {
|
| 110 | Optional<Direction> facingOptional = state.getOptionalValue(FACING);
|
| 111 | if (facingOptional.isPresent()) {
|
| 112 | Direction direction = facingOptional.get();
|
| 113 | boolean wasAttached = state.getOptionalValue(ATTACHED).orElse(false);
|
| 114 | boolean wasPowered = state.getOptionalValue(POWERED).orElse(false);
|
| 115 | Block block = state.getBlock();
|
| 116 | boolean attached = !isBeingDestroyed;
|
| 117 | boolean powered = false;
|
| 118 | int receiverPos = 0;
|
| 119 | BlockState[] wireStates = new BlockState[42];
|
| 120 |
|
| 121 | for (int i = 1; i < 42; i++) {
|
| 122 | BlockPos testPos = pos.relative(direction, i);
|
| 123 | BlockState wireState = level.getBlockState(testPos);
|
| 124 | if (wireState.is(Blocks.TRIPWIRE_HOOK)) {
|
| 125 | if (wireState.getValue(FACING) == direction.getOpposite()) {
|
| 126 | receiverPos = i;
|
| 127 | }
|
| 128 | break;
|
| 129 | }
|
| 130 |
|
| 131 | if (!wireState.is(Blocks.TRIPWIRE) && i != wireSource) {
|
| 132 | wireStates[i] = null;
|
| 133 | attached = false;
|
| 134 | } else {
|
| 135 | if (i == wireSource) {
|
| 136 | wireState = MoreObjects.firstNonNull(wireSourceState, wireState);
|
| 137 | }
|
| 138 |
|
| 139 | boolean wireArmed = !wireState.getValue(TripWireBlock.DISARMED);
|
| 140 | boolean wirePowered = wireState.getValue(TripWireBlock.POWERED);
|
| 141 | powered |= wireArmed && wirePowered;
|
| 142 | wireStates[i] = wireState;
|
| 143 | if (i == wireSource) {
|
| 144 | level.scheduleTick(pos, block, 10);
|
| 145 | attached &= wireArmed;
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | attached &= receiverPos > 1;
|
| 151 | powered &= attached;
|
| 152 | BlockState newState = block.defaultBlockState().trySetValue(ATTACHED, attached).trySetValue(POWERED, powered);
|
| 153 | if (receiverPos > 0) {
|
| 154 | BlockPos testPos = pos.relative(direction, receiverPos);
|
| 155 | Direction opposite = direction.getOpposite();
|
| 156 | level.setBlockAndUpdate(testPos, newState.setValue(FACING, opposite));
|
| 157 | notifyNeighbors(block, level, testPos, opposite);
|
| 158 | if (!level.getBlockState(pos).is(Blocks.TRIPWIRE_HOOK)) {
|
| 159 | onRemoved(newState, level, pos);
|
| 160 | return;
|
| 161 | }
|
| 162 |
|
| 163 | emitState(level, testPos, attached, powered, wasAttached, wasPowered);
|
| 164 | }
|
| 165 |
|
| 166 | emitState(level, pos, attached, powered, wasAttached, wasPowered);
|
| 167 | if (!isBeingDestroyed) {
|
| 168 | level.setBlockAndUpdate(pos, newState.setValue(FACING, direction));
|
| 169 | if (canUpdate) {
|
| 170 | notifyNeighbors(block, level, pos, direction);
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | if (wasAttached != attached) {
|
| 175 | for (int i = 1; i < receiverPos; i++) {
|
| 176 | BlockPos testPos = pos.relative(direction, i);
|
| 177 | BlockState wireData = wireStates[i];
|
| 178 | if (wireData != null) {
|
| 179 | BlockState testPosState = level.getBlockState(testPos);
|
| 180 | if (testPosState.is(Blocks.TRIPWIRE) || testPosState.is(Blocks.TRIPWIRE_HOOK)) {
|
| 181 | level.setBlockAndUpdate(testPos, wireData.trySetValue(ATTACHED, attached));
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 191 | calculateState(level, pos, state, false, true, -1, null);
|
| 192 | }
|
| 193 |
|
| 194 | private static void emitState(
|
| 195 | final Level level, final BlockPos pos, final boolean attached, final boolean powered, final boolean wasAttached, final boolean wasPowered
|
| 196 | ) {
|
| 197 | if (powered && !wasPowered) {
|
| 198 | level.playSound(null, pos, SoundEvents.TRIPWIRE_CLICK_ON, SoundSource.BLOCKS, 0.4F, 0.6F);
|
| 199 | level.gameEvent(null, GameEvent.BLOCK_ACTIVATE, pos);
|
| 200 | } else if (!powered && wasPowered) {
|
| 201 | level.playSound(null, pos, SoundEvents.TRIPWIRE_CLICK_OFF, SoundSource.BLOCKS, 0.4F, 0.5F);
|
| 202 | level.gameEvent(null, GameEvent.BLOCK_DEACTIVATE, pos);
|
| 203 | } else if (attached && !wasAttached) {
|
| 204 | level.playSound(null, pos, SoundEvents.TRIPWIRE_ATTACH, SoundSource.BLOCKS, 0.4F, 0.7F);
|
| 205 | level.gameEvent(null, GameEvent.BLOCK_ATTACH, pos);
|
| 206 | } else if (!attached && wasAttached) {
|
| 207 | level.playSound(null, pos, SoundEvents.TRIPWIRE_DETACH, SoundSource.BLOCKS, 0.4F, 1.2F / (level.getRandom().nextFloat() * 0.2F + 0.9F));
|
| 208 | level.gameEvent(null, GameEvent.BLOCK_DETACH, pos);
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | private static void notifyNeighbors(final Block block, final Level level, final BlockPos pos, final Direction direction) {
|
| 213 | Direction front = direction.getOpposite();
|
| 214 | Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, front, Direction.UP);
|
| 215 | level.updateNeighborsAt(pos, block, orientation);
|
| 216 | level.updateNeighborsAt(pos.relative(front), block, orientation);
|
| 217 | }
|
| 218 |
|
| 219 | @Override
|
| 220 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 221 | if (!movedByPiston) {
|
| 222 | onRemoved(state, level, pos);
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | private static void onRemoved(final BlockState state, final Level level, final BlockPos pos) {
|
| 227 | boolean attached = state.getValue(ATTACHED);
|
| 228 | boolean powered = state.getValue(POWERED);
|
| 229 | if (attached || powered) {
|
| 230 | calculateState(level, pos, state, true, false, -1, null);
|
| 231 | }
|
| 232 |
|
| 233 | if (powered) {
|
| 234 | notifyNeighbors(state.getBlock(), level, pos, state.getValue(FACING));
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 240 | return state.getValue(POWERED) ? 15 : 0;
|
| 241 | }
|
| 242 |
|
| 243 | @Override
|
| 244 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 245 | if (!state.getValue(POWERED)) {
|
| 246 | return 0;
|
| 247 | } else {
|
| 248 | return state.getValue(FACING) == direction ? 15 : 0;
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | protected boolean isSignalSource(final BlockState state) {
|
| 254 | return true;
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 259 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 260 | }
|
| 261 |
|
| 262 | @Override
|
| 263 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 264 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 265 | }
|
| 266 |
|
| 267 | @Override
|
| 268 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 269 | builder.add(FACING, POWERED, ATTACHED);
|
| 270 | }
|
| 271 | }
|
| 272 |
|