| 1 | package net.minecraft.world.level.redstone;
|
| 2 |
|
| 3 | import java.util.Locale;
|
| 4 | import net.minecraft.CrashReport;
|
| 5 | import net.minecraft.CrashReportCategory;
|
| 6 | import net.minecraft.ReportedException;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.LevelAccessor;
|
| 12 | import net.minecraft.world.level.block.Block;
|
| 13 | import net.minecraft.world.level.block.Blocks;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public interface NeighborUpdater {
|
| 18 | Direction[] UPDATE_ORDER = new Direction[]{Direction.WEST, Direction.EAST, Direction.DOWN, Direction.UP, Direction.NORTH, Direction.SOUTH};
|
| 19 |
|
| 20 | void shapeUpdate(Direction direction, BlockState neighborState, BlockPos pos, BlockPos neighborPos, @Block.UpdateFlags int updateFlags, int updateLimit);
|
| 21 |
|
| 22 | void neighborChanged(BlockPos pos, Block changedBlock, @Nullable Orientation orientation);
|
| 23 |
|
| 24 | void neighborChanged(BlockState state, BlockPos pos, Block changedBlock, @Nullable Orientation orientation, boolean movedByPiston);
|
| 25 |
|
| 26 | default void updateNeighborsAtExceptFromFacing(
|
| 27 | final BlockPos pos, final Block block, final @Nullable Direction skipDirection, final @Nullable Orientation orientation
|
| 28 | ) {
|
| 29 | for (Direction direction : UPDATE_ORDER) {
|
| 30 | if (direction != skipDirection) {
|
| 31 | this.neighborChanged(pos.relative(direction), block, null);
|
| 32 | }
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | static void executeShapeUpdate(
|
| 37 | final LevelAccessor level,
|
| 38 | final Direction direction,
|
| 39 | final BlockPos pos,
|
| 40 | final BlockPos neighborPos,
|
| 41 | final BlockState neighborState,
|
| 42 | final @Block.UpdateFlags int updateFlags,
|
| 43 | final int updateLimit
|
| 44 | ) {
|
| 45 | BlockState currentState = level.getBlockState(pos);
|
| 46 | if ((updateFlags & 128) == 0 || !currentState.is(Blocks.REDSTONE_WIRE)) {
|
| 47 | try {
|
| 48 | BlockState newState = currentState.updateShape(level, level, pos, direction, neighborPos, neighborState, level.getRandom());
|
| 49 | Block.updateOrDestroy(currentState, newState, level, pos, updateFlags, updateLimit);
|
| 50 | } catch (Throwable t) {
|
| 51 | CrashReport report = CrashReport.forThrowable(t, "Exception while updating neighbour shapes");
|
| 52 | CrashReportCategory ownCategory = report.addCategory("Block being updated");
|
| 53 | CrashReportCategory.populateBlockDetails(ownCategory, level, pos, currentState);
|
| 54 | CrashReportCategory neighborCategory = report.addCategory("Neighbor block");
|
| 55 | CrashReportCategory.populateBlockDetails(neighborCategory, level, neighborPos, neighborState);
|
| 56 | throw new ReportedException(report);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | static void executeUpdate(
|
| 62 | final Level level, final BlockState state, final BlockPos pos, final Block changedBlock, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 63 | ) {
|
| 64 | try {
|
| 65 | state.handleNeighborChanged(level, pos, changedBlock, orientation, movedByPiston);
|
| 66 | } catch (Throwable t) {
|
| 67 | CrashReport report = CrashReport.forThrowable(t, "Exception while updating neighbours");
|
| 68 | CrashReportCategory category = report.addCategory("Block being updated");
|
| 69 | category.setDetail(
|
| 70 | "Source block type",
|
| 71 | () -> {
|
| 72 | try {
|
| 73 | return String.format(
|
| 74 | Locale.ROOT,
|
| 75 | "ID #%s (%s // %s)",
|
| 76 | BuiltInRegistries.BLOCK.getKey(changedBlock),
|
| 77 | changedBlock.getDescriptionId(),
|
| 78 | changedBlock.getClass().getCanonicalName()
|
| 79 | );
|
| 80 | } catch (Throwable ignored) {
|
| 81 | return "ID #" + BuiltInRegistries.BLOCK.getKey(changedBlock);
|
| 82 | }
|
| 83 | }
|
| 84 | );
|
| 85 | CrashReportCategory.populateBlockDetails(category, level, pos, state);
|
| 86 | throw new ReportedException(report);
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 |
|