| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Map.Entry;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.core.particles.DustParticleOptions;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.util.ARGB;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.world.InteractionResult;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.flag.FeatureFlags;
|
| 19 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 20 | import net.minecraft.world.level.BlockGetter;
|
| 21 | import net.minecraft.world.level.Level;
|
| 22 | import net.minecraft.world.level.LevelAccessor;
|
| 23 | import net.minecraft.world.level.LevelReader;
|
| 24 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 25 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 26 | import net.minecraft.world.level.block.state.BlockState;
|
| 27 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 28 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 29 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 30 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 31 | import net.minecraft.world.level.block.state.properties.RedstoneSide;
|
| 32 | import net.minecraft.world.level.redstone.DefaultRedstoneWireEvaluator;
|
| 33 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 34 | import net.minecraft.world.level.redstone.ExperimentalRedstoneWireEvaluator;
|
| 35 | import net.minecraft.world.level.redstone.Orientation;
|
| 36 | import net.minecraft.world.level.redstone.RedstoneWireEvaluator;
|
| 37 | import net.minecraft.world.phys.BlockHitResult;
|
| 38 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 39 | import net.minecraft.world.phys.shapes.Shapes;
|
| 40 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 41 | import org.jspecify.annotations.Nullable;
|
| 42 |
|
| 43 | public class RedstoneWireBlock extends Block {
|
| 44 | public static final EnumProperty<RedstoneSide> NORTH = BlockStateProperties.NORTH_REDSTONE;
|
| 45 | public static final EnumProperty<RedstoneSide> EAST = BlockStateProperties.EAST_REDSTONE;
|
| 46 | public static final EnumProperty<RedstoneSide> SOUTH = BlockStateProperties.SOUTH_REDSTONE;
|
| 47 | public static final EnumProperty<RedstoneSide> WEST = BlockStateProperties.WEST_REDSTONE;
|
| 48 | public static final IntegerProperty POWER = BlockStateProperties.POWER;
|
| 49 | public static final Map<Direction, EnumProperty<RedstoneSide>> PROPERTY_BY_DIRECTION = ImmutableMap.copyOf(
|
| 50 | Maps.newEnumMap(Map.of(Direction.NORTH, NORTH, Direction.EAST, EAST, Direction.SOUTH, SOUTH, Direction.WEST, WEST))
|
| 51 | );
|
| 52 | private static final int[] COLORS = Util.make(new int[16], list -> {
|
| 53 | for (int i = 0; i <= 15; i++) {
|
| 54 | float power = i / 15.0F;
|
| 55 | float red = power * 0.6F + (power > 0.0F ? 0.4F : 0.3F);
|
| 56 | float green = Mth.clamp(power * power * 0.7F - 0.5F, 0.0F, 1.0F);
|
| 57 | float blue = Mth.clamp(power * power * 0.6F - 0.7F, 0.0F, 1.0F);
|
| 58 | list[i] = ARGB.colorFromFloat(1.0F, red, green, blue);
|
| 59 | }
|
| 60 | });
|
| 61 | private static final float PARTICLE_DENSITY = 0.2F;
|
| 62 | private final Function<BlockState, VoxelShape> shapes;
|
| 63 | private final BlockState crossState;
|
| 64 | private final RedstoneWireEvaluator evaluator = new DefaultRedstoneWireEvaluator(this);
|
| 65 | private boolean shouldSignal = true;
|
| 66 |
|
| 67 | public RedstoneWireBlock(final BlockBehaviour.Properties properties) {
|
| 68 | super(properties);
|
| 69 | this.registerDefaultState(
|
| 70 | this.stateDefinition
|
| 71 | .any()
|
| 72 | .setValue(NORTH, RedstoneSide.NONE)
|
| 73 | .setValue(EAST, RedstoneSide.NONE)
|
| 74 | .setValue(SOUTH, RedstoneSide.NONE)
|
| 75 | .setValue(WEST, RedstoneSide.NONE)
|
| 76 | .setValue(POWER, 0)
|
| 77 | );
|
| 78 | this.shapes = this.makeShapes();
|
| 79 | this.crossState = this.defaultBlockState()
|
| 80 | .setValue(NORTH, RedstoneSide.SIDE)
|
| 81 | .setValue(EAST, RedstoneSide.SIDE)
|
| 82 | .setValue(SOUTH, RedstoneSide.SIDE)
|
| 83 | .setValue(WEST, RedstoneSide.SIDE);
|
| 84 | }
|
| 85 |
|
| 86 | private Function<BlockState, VoxelShape> makeShapes() {
|
| 87 | int height = 1;
|
| 88 | int width = 10;
|
| 89 | VoxelShape dot = Block.column(10.0, 0.0, 1.0);
|
| 90 | Map<Direction, VoxelShape> floor = Shapes.rotateHorizontal(Block.boxZ(10.0, 0.0, 1.0, 0.0, 8.0));
|
| 91 | Map<Direction, VoxelShape> up = Shapes.rotateHorizontal(Block.boxZ(10.0, 16.0, 0.0, 1.0));
|
| 92 | return this.getShapeForEachState(state -> {
|
| 93 | VoxelShape shape = dot;
|
| 94 |
|
| 95 | for (Entry<Direction, EnumProperty<RedstoneSide>> entry : PROPERTY_BY_DIRECTION.entrySet()) {
|
| 96 | shape = switch ((RedstoneSide)state.getValue(entry.getValue())) {
|
| 97 | case UP -> Shapes.or(shape, floor.get(entry.getKey()), up.get(entry.getKey()));
|
| 98 | case SIDE -> Shapes.or(shape, floor.get(entry.getKey()));
|
| 99 | case NONE -> shape;
|
| 100 | };
|
| 101 | }
|
| 102 |
|
| 103 | return shape;
|
| 104 | }, POWER);
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 109 | return this.shapes.apply(state);
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 114 | return this.getConnectionState(context.getLevel(), this.crossState, context.getClickedPos());
|
| 115 | }
|
| 116 |
|
| 117 | private BlockState getConnectionState(final BlockGetter level, BlockState state, final BlockPos pos) {
|
| 118 | boolean wasDot = isDot(state);
|
| 119 | state = this.getMissingConnections(level, this.defaultBlockState().setValue(POWER, state.getValue(POWER)), pos);
|
| 120 | if (wasDot && isDot(state)) {
|
| 121 | return state;
|
| 122 | }
|
| 123 |
|
| 124 | boolean north = state.getValue(NORTH).isConnected();
|
| 125 | boolean south = state.getValue(SOUTH).isConnected();
|
| 126 | boolean east = state.getValue(EAST).isConnected();
|
| 127 | boolean west = state.getValue(WEST).isConnected();
|
| 128 | boolean northSouthEmpty = !north && !south;
|
| 129 | boolean eastWestEmpty = !east && !west;
|
| 130 | if (!west && northSouthEmpty) {
|
| 131 | state = state.setValue(WEST, RedstoneSide.SIDE);
|
| 132 | }
|
| 133 |
|
| 134 | if (!east && northSouthEmpty) {
|
| 135 | state = state.setValue(EAST, RedstoneSide.SIDE);
|
| 136 | }
|
| 137 |
|
| 138 | if (!north && eastWestEmpty) {
|
| 139 | state = state.setValue(NORTH, RedstoneSide.SIDE);
|
| 140 | }
|
| 141 |
|
| 142 | if (!south && eastWestEmpty) {
|
| 143 | state = state.setValue(SOUTH, RedstoneSide.SIDE);
|
| 144 | }
|
| 145 |
|
| 146 | return state;
|
| 147 | }
|
| 148 |
|
| 149 | private BlockState getMissingConnections(final BlockGetter level, BlockState state, final BlockPos pos) {
|
| 150 | boolean canConnectUp = !level.getBlockState(pos.above()).isRedstoneConductor(level, pos);
|
| 151 |
|
| 152 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 153 | if (!state.getValue(PROPERTY_BY_DIRECTION.get(direction)).isConnected()) {
|
| 154 | RedstoneSide sideConnection = this.getConnectingSide(level, pos, direction, canConnectUp);
|
| 155 | state = state.setValue(PROPERTY_BY_DIRECTION.get(direction), sideConnection);
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | return state;
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | protected BlockState updateShape(
|
| 164 | final BlockState state,
|
| 165 | final LevelReader level,
|
| 166 | final ScheduledTickAccess ticks,
|
| 167 | final BlockPos pos,
|
| 168 | final Direction directionToNeighbour,
|
| 169 | final BlockPos neighbourPos,
|
| 170 | final BlockState neighbourState,
|
| 171 | final RandomSource random
|
| 172 | ) {
|
| 173 | if (directionToNeighbour == Direction.DOWN) {
|
| 174 | return !this.canSurviveOn(level, neighbourPos, neighbourState) ? Blocks.AIR.defaultBlockState() : state;
|
| 175 | }
|
| 176 |
|
| 177 | if (directionToNeighbour == Direction.UP) {
|
| 178 | return this.getConnectionState(level, state, pos);
|
| 179 | }
|
| 180 |
|
| 181 | RedstoneSide sideConnection = this.getConnectingSide(level, pos, directionToNeighbour);
|
| 182 | return sideConnection.isConnected() == state.getValue(PROPERTY_BY_DIRECTION.get(directionToNeighbour)).isConnected() && !isCross(state)
|
| 183 | ? state.setValue(PROPERTY_BY_DIRECTION.get(directionToNeighbour), sideConnection)
|
| 184 | : this.getConnectionState(
|
| 185 | level, this.crossState.setValue(POWER, state.getValue(POWER)).setValue(PROPERTY_BY_DIRECTION.get(directionToNeighbour), sideConnection), pos
|
| 186 | );
|
| 187 | }
|
| 188 |
|
| 189 | private static boolean isCross(final BlockState state) {
|
| 190 | return state.getValue(NORTH).isConnected() && state.getValue(SOUTH).isConnected() && state.getValue(EAST).isConnected() && state.getValue(WEST).isConnected();
|
| 191 | }
|
| 192 |
|
| 193 | private static boolean isDot(final BlockState state) {
|
| 194 | return !state.getValue(NORTH).isConnected()
|
| 195 | && !state.getValue(SOUTH).isConnected()
|
| 196 | && !state.getValue(EAST).isConnected()
|
| 197 | && !state.getValue(WEST).isConnected();
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | protected void updateIndirectNeighbourShapes(
|
| 202 | final BlockState state, final LevelAccessor level, final BlockPos pos, final @Block.UpdateFlags int updateFlags, final int updateLimit
|
| 203 | ) {
|
| 204 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 205 |
|
| 206 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 207 | RedstoneSide value = state.getValue(PROPERTY_BY_DIRECTION.get(direction));
|
| 208 | if (value != RedstoneSide.NONE && !level.getBlockState(blockPos.setWithOffset(pos, direction)).is(this)) {
|
| 209 | blockPos.move(Direction.DOWN);
|
| 210 | BlockState blockStateDown = level.getBlockState(blockPos);
|
| 211 | if (blockStateDown.is(this)) {
|
| 212 | BlockPos neighborPos = blockPos.relative(direction.getOpposite());
|
| 213 | level.neighborShapeChanged(direction.getOpposite(), blockPos, neighborPos, level.getBlockState(neighborPos), updateFlags, updateLimit);
|
| 214 | }
|
| 215 |
|
| 216 | blockPos.setWithOffset(pos, direction).move(Direction.UP);
|
| 217 | BlockState blockStateUp = level.getBlockState(blockPos);
|
| 218 | if (blockStateUp.is(this)) {
|
| 219 | BlockPos neighborPos = blockPos.relative(direction.getOpposite());
|
| 220 | level.neighborShapeChanged(direction.getOpposite(), blockPos, neighborPos, level.getBlockState(neighborPos), updateFlags, updateLimit);
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | private RedstoneSide getConnectingSide(final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 227 | return this.getConnectingSide(level, pos, direction, !level.getBlockState(pos.above()).isRedstoneConductor(level, pos));
|
| 228 | }
|
| 229 |
|
| 230 | private RedstoneSide getConnectingSide(final BlockGetter level, final BlockPos pos, final Direction direction, final boolean canConnectUp) {
|
| 231 | BlockPos relativePos = pos.relative(direction);
|
| 232 | BlockState relativeState = level.getBlockState(relativePos);
|
| 233 | if (canConnectUp) {
|
| 234 | boolean isPlaceableAbove = relativeState.getBlock() instanceof TrapDoorBlock || this.canSurviveOn(level, relativePos, relativeState);
|
| 235 | if (isPlaceableAbove && shouldConnectTo(level, relativePos.above())) {
|
| 236 | if (relativeState.isFaceSturdy(level, relativePos, direction.getOpposite())) {
|
| 237 | return RedstoneSide.UP;
|
| 238 | }
|
| 239 |
|
| 240 | return RedstoneSide.SIDE;
|
| 241 | }
|
| 242 | }
|
| 243 |
|
| 244 | return !shouldConnectTo(relativeState, level, relativePos, direction)
|
| 245 | && (relativeState.isRedstoneConductor(level, relativePos) || !shouldConnectTo(level, relativePos.below()))
|
| 246 | ? RedstoneSide.NONE
|
| 247 | : RedstoneSide.SIDE;
|
| 248 | }
|
| 249 |
|
| 250 | @Override
|
| 251 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 252 | BlockPos below = pos.below();
|
| 253 | BlockState belowState = level.getBlockState(below);
|
| 254 | return this.canSurviveOn(level, below, belowState);
|
| 255 | }
|
| 256 |
|
| 257 | private boolean canSurviveOn(final BlockGetter level, final BlockPos relativePos, final BlockState relativeState) {
|
| 258 | return relativeState.isFaceSturdy(level, relativePos, Direction.UP) || relativeState.is(Blocks.HOPPER);
|
| 259 | }
|
| 260 |
|
| 261 | private void updatePowerStrength(
|
| 262 | final Level level, final BlockPos pos, final BlockState state, final @Nullable Orientation orientation, final boolean shapeUpdateWiresAroundInitialPosition
|
| 263 | ) {
|
| 264 | if (useExperimentalEvaluator(level)) {
|
| 265 | new ExperimentalRedstoneWireEvaluator(this).updatePowerStrength(level, pos, state, orientation, shapeUpdateWiresAroundInitialPosition);
|
| 266 | } else {
|
| 267 | this.evaluator.updatePowerStrength(level, pos, state, orientation, shapeUpdateWiresAroundInitialPosition);
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | public int getBlockSignal(final Level level, final BlockPos pos) {
|
| 272 | this.shouldSignal = false;
|
| 273 | int blockSignal = level.getBestNeighborSignal(pos);
|
| 274 | this.shouldSignal = true;
|
| 275 | return blockSignal;
|
| 276 | }
|
| 277 |
|
| 278 | private void checkCornerChangeAt(final Level level, final BlockPos pos) {
|
| 279 | if (level.getBlockState(pos).is(this)) {
|
| 280 | level.updateNeighborsAt(pos, this);
|
| 281 |
|
| 282 | for (Direction direction : Direction.values()) {
|
| 283 | level.updateNeighborsAt(pos.relative(direction), this);
|
| 284 | }
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 290 | if (!oldState.is(state.getBlock()) && !level.isClientSide()) {
|
| 291 | this.updatePowerStrength(level, pos, state, null, true);
|
| 292 |
|
| 293 | for (Direction direction : Direction.Plane.VERTICAL) {
|
| 294 | level.updateNeighborsAt(pos.relative(direction), this);
|
| 295 | }
|
| 296 |
|
| 297 | this.updateNeighborsOfNeighboringWires(level, pos);
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | @Override
|
| 302 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 303 | if (!movedByPiston) {
|
| 304 | for (Direction direction : Direction.values()) {
|
| 305 | level.updateNeighborsAt(pos.relative(direction), this);
|
| 306 | }
|
| 307 |
|
| 308 | this.updatePowerStrength(level, pos, state, null, false);
|
| 309 | this.updateNeighborsOfNeighboringWires(level, pos);
|
| 310 | }
|
| 311 | }
|
| 312 |
|
| 313 | private void updateNeighborsOfNeighboringWires(final Level level, final BlockPos pos) {
|
| 314 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 315 | this.checkCornerChangeAt(level, pos.relative(direction));
|
| 316 | }
|
| 317 |
|
| 318 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 319 | BlockPos target = pos.relative(direction);
|
| 320 | if (level.getBlockState(target).isRedstoneConductor(level, target)) {
|
| 321 | this.checkCornerChangeAt(level, target.above());
|
| 322 | } else {
|
| 323 | this.checkCornerChangeAt(level, target.below());
|
| 324 | }
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | @Override
|
| 329 | protected void neighborChanged(
|
| 330 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 331 | ) {
|
| 332 | if (!level.isClientSide()) {
|
| 333 | if (block != this || !useExperimentalEvaluator(level)) {
|
| 334 | if (state.canSurvive(level, pos)) {
|
| 335 | this.updatePowerStrength(level, pos, state, orientation, false);
|
| 336 | } else {
|
| 337 | dropResources(state, level, pos);
|
| 338 | level.removeBlock(pos, false);
|
| 339 | }
|
| 340 | }
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | private static boolean useExperimentalEvaluator(final Level level) {
|
| 345 | return level.enabledFeatures().contains(FeatureFlags.REDSTONE_EXPERIMENTS);
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 350 | return !this.shouldSignal ? 0 : state.getSignal(level, pos, direction);
|
| 351 | }
|
| 352 |
|
| 353 | @Override
|
| 354 | protected int getSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 355 | if (this.shouldSignal && direction != Direction.DOWN) {
|
| 356 | int power = this.ownSignal(state, level, pos);
|
| 357 | if (power == 0) {
|
| 358 | return 0;
|
| 359 | } else {
|
| 360 | return direction != Direction.UP && !this.getConnectionState(level, state, pos).getValue(PROPERTY_BY_DIRECTION.get(direction.getOpposite())).isConnected()
|
| 361 | ? 0
|
| 362 | : power;
|
| 363 | }
|
| 364 | } else {
|
| 365 | return 0;
|
| 366 | }
|
| 367 | }
|
| 368 |
|
| 369 | @Override
|
| 370 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 371 | return state.getValue(POWER);
|
| 372 | }
|
| 373 |
|
| 374 | @Override
|
| 375 | protected boolean shouldRedstoneWireConnectTo(final BlockState state, final BlockGetter level, final BlockPos pos, final @Nullable Direction direction) {
|
| 376 | return true;
|
| 377 | }
|
| 378 |
|
| 379 | protected static boolean shouldConnectTo(final BlockGetter level, final BlockPos pos) {
|
| 380 | return shouldConnectTo(level.getBlockState(pos), level, pos, null);
|
| 381 | }
|
| 382 |
|
| 383 | protected static boolean shouldConnectTo(final BlockState state, final BlockGetter level, final BlockPos pos, final @Nullable Direction direction) {
|
| 384 | return state.shouldRedstoneWireConnectTo(level, pos, direction);
|
| 385 | }
|
| 386 |
|
| 387 | @Override
|
| 388 | protected boolean isSignalSource(final BlockState state) {
|
| 389 | return this.shouldSignal;
|
| 390 | }
|
| 391 |
|
| 392 | public static int getColorForPower(final int power) {
|
| 393 | return COLORS[power];
|
| 394 | }
|
| 395 |
|
| 396 | private static void spawnParticlesAlongLine(
|
| 397 | final Level level,
|
| 398 | final RandomSource random,
|
| 399 | final BlockPos pos,
|
| 400 | final int color,
|
| 401 | final Direction side,
|
| 402 | final Direction along,
|
| 403 | final float from,
|
| 404 | final float to
|
| 405 | ) {
|
| 406 | float span = to - from;
|
| 407 | if (!(random.nextFloat() >= 0.2F * span)) {
|
| 408 | float sideOfBlock = 0.4375F;
|
| 409 | float positionOnLine = from + span * random.nextFloat();
|
| 410 | double x = 0.5 + 0.4375F * side.getStepX() + positionOnLine * along.getStepX();
|
| 411 | double y = 0.5 + 0.4375F * side.getStepY() + positionOnLine * along.getStepY();
|
| 412 | double z = 0.5 + 0.4375F * side.getStepZ() + positionOnLine * along.getStepZ();
|
| 413 | level.addParticle(new DustParticleOptions(color, 1.0F), pos.getX() + x, pos.getY() + y, pos.getZ() + z, 0.0, 0.0, 0.0);
|
| 414 | }
|
| 415 | }
|
| 416 |
|
| 417 | @Override
|
| 418 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 419 | int power = state.getValue(POWER);
|
| 420 | if (power != 0) {
|
| 421 | for (Direction horizontal : Direction.Plane.HORIZONTAL) {
|
| 422 | RedstoneSide connection = state.getValue(PROPERTY_BY_DIRECTION.get(horizontal));
|
| 423 | switch (connection) {
|
| 424 | case UP:
|
| 425 | spawnParticlesAlongLine(level, random, pos, COLORS[power], horizontal, Direction.UP, -0.5F, 0.5F);
|
| 426 | case SIDE:
|
| 427 | spawnParticlesAlongLine(level, random, pos, COLORS[power], Direction.DOWN, horizontal, 0.0F, 0.5F);
|
| 428 | break;
|
| 429 | case NONE:
|
| 430 | default:
|
| 431 | spawnParticlesAlongLine(level, random, pos, COLORS[power], Direction.DOWN, horizontal, 0.0F, 0.3F);
|
| 432 | }
|
| 433 | }
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | @Override
|
| 438 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 439 | return switch (rotation) {
|
| 440 | case CLOCKWISE_180 -> (BlockState)state.setValue(NORTH, state.getValue(SOUTH))
|
| 441 | .setValue(EAST, state.getValue(WEST))
|
| 442 | .setValue(SOUTH, state.getValue(NORTH))
|
| 443 | .setValue(WEST, state.getValue(EAST));
|
| 444 | case COUNTERCLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(EAST))
|
| 445 | .setValue(EAST, state.getValue(SOUTH))
|
| 446 | .setValue(SOUTH, state.getValue(WEST))
|
| 447 | .setValue(WEST, state.getValue(NORTH));
|
| 448 | case CLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(WEST))
|
| 449 | .setValue(EAST, state.getValue(NORTH))
|
| 450 | .setValue(SOUTH, state.getValue(EAST))
|
| 451 | .setValue(WEST, state.getValue(SOUTH));
|
| 452 | default -> state;
|
| 453 | };
|
| 454 | }
|
| 455 |
|
| 456 | @Override
|
| 457 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 458 | switch (mirror) {
|
| 459 | case LEFT_RIGHT:
|
| 460 | return state.setValue(NORTH, state.getValue(SOUTH)).setValue(SOUTH, state.getValue(NORTH));
|
| 461 | case FRONT_BACK:
|
| 462 | return state.setValue(EAST, state.getValue(WEST)).setValue(WEST, state.getValue(EAST));
|
| 463 | default:
|
| 464 | return super.mirror(state, mirror);
|
| 465 | }
|
| 466 | }
|
| 467 |
|
| 468 | @Override
|
| 469 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 470 | builder.add(NORTH, EAST, SOUTH, WEST, POWER);
|
| 471 | }
|
| 472 |
|
| 473 | @Override
|
| 474 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 475 | if (!player.getAbilities().mayBuild) {
|
| 476 | return InteractionResult.PASS;
|
| 477 | }
|
| 478 |
|
| 479 | if (isCross(state) || isDot(state)) {
|
| 480 | BlockState newState = isCross(state) ? this.defaultBlockState() : this.crossState;
|
| 481 | newState = newState.setValue(POWER, state.getValue(POWER));
|
| 482 | newState = this.getConnectionState(level, newState, pos);
|
| 483 | if (newState != state) {
|
| 484 | level.setBlockAndUpdate(pos, newState);
|
| 485 | this.updatesOnShapeChange(level, pos, state, newState);
|
| 486 | return InteractionResult.SUCCESS;
|
| 487 | }
|
| 488 | }
|
| 489 |
|
| 490 | return InteractionResult.PASS;
|
| 491 | }
|
| 492 |
|
| 493 | private void updatesOnShapeChange(final Level level, final BlockPos pos, final BlockState oldState, final BlockState newState) {
|
| 494 | Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, null, Direction.UP);
|
| 495 |
|
| 496 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 497 | BlockPos relativePos = pos.relative(direction);
|
| 498 | if (oldState.getValue(PROPERTY_BY_DIRECTION.get(direction)).isConnected() != newState.getValue(PROPERTY_BY_DIRECTION.get(direction)).isConnected()
|
| 499 | && level.getBlockState(relativePos).isRedstoneConductor(level, relativePos)) {
|
| 500 | level.updateNeighborsAtExceptFromFacing(
|
| 501 | relativePos, newState.getBlock(), direction.getOpposite(), ExperimentalRedstoneUtils.withFront(orientation, direction)
|
| 502 | );
|
| 503 | }
|
| 504 | }
|
| 505 | }
|
| 506 | }
|
| 507 |
|