| 1 | package net.minecraft.world.level.redstone;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 5 | import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
|
| 7 | import java.util.ArrayDeque;
|
| 8 | import java.util.Deque;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.util.debug.DebugSubscriptions;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.block.RedstoneWireBlock;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 17 | import net.minecraft.world.level.block.state.properties.RedstoneSide;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public class ExperimentalRedstoneWireEvaluator extends RedstoneWireEvaluator {
|
| 21 | private final Deque<BlockPos> wiresToTurnOff = new ArrayDeque<>();
|
| 22 | private final Deque<BlockPos> wiresToTurnOn = new ArrayDeque<>();
|
| 23 | private final Object2IntMap<BlockPos> updatedWires = new Object2IntLinkedOpenHashMap<>();
|
| 24 |
|
| 25 | public ExperimentalRedstoneWireEvaluator(final RedstoneWireBlock wireBlock) {
|
| 26 | super(wireBlock);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public void updatePowerStrength(
|
| 31 | final Level level,
|
| 32 | final BlockPos initialPos,
|
| 33 | final BlockState ignored,
|
| 34 | final @Nullable Orientation orientation,
|
| 35 | final boolean shapeUpdateWiresAroundInitialPosition
|
| 36 | ) {
|
| 37 | Orientation initialOrientation = getInitialOrientation(level, orientation);
|
| 38 | this.calculateCurrentChanges(level, initialPos, initialOrientation);
|
| 39 | ObjectIterator<Entry<BlockPos>> iterator = this.updatedWires.object2IntEntrySet().iterator();
|
| 40 |
|
| 41 | for (boolean initialWire = true; iterator.hasNext(); initialWire = false) {
|
| 42 | Entry<BlockPos> next = iterator.next();
|
| 43 | BlockPos pos = next.getKey();
|
| 44 | int packed = next.getIntValue();
|
| 45 | int newLevel = unpackPower(packed);
|
| 46 | BlockState state = level.getBlockState(pos);
|
| 47 | if (state.is(this.wireBlock) && !state.getValue(RedstoneWireBlock.POWER).equals(newLevel)) {
|
| 48 | int updateFlags = 2;
|
| 49 | if (!shapeUpdateWiresAroundInitialPosition || !initialWire) {
|
| 50 | updateFlags |= 128;
|
| 51 | }
|
| 52 |
|
| 53 | level.setBlock(pos, state.setValue(RedstoneWireBlock.POWER, newLevel), updateFlags);
|
| 54 | } else {
|
| 55 | iterator.remove();
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | this.causeNeighborUpdates(level);
|
| 60 | }
|
| 61 |
|
| 62 | private void causeNeighborUpdates(final Level level) {
|
| 63 | this.updatedWires.forEach((wirePos, packed) -> {
|
| 64 | Orientation orientation = unpackOrientation(packed);
|
| 65 | BlockState state = level.getBlockState(wirePos);
|
| 66 |
|
| 67 | for (Direction neighborDirection : orientation.getDirections()) {
|
| 68 | if (isConnected(state, neighborDirection)) {
|
| 69 | BlockPos neighborPos = wirePos.relative(neighborDirection);
|
| 70 | BlockState neighborState = level.getBlockState(neighborPos);
|
| 71 | Orientation neighborOrientation = orientation.withFrontPreserveUp(neighborDirection);
|
| 72 | level.neighborChanged(neighborState, neighborPos, this.wireBlock, neighborOrientation, false);
|
| 73 | if (neighborState.isRedstoneConductor(level, neighborPos)) {
|
| 74 | for (Direction direction : neighborOrientation.getDirections()) {
|
| 75 | if (direction != neighborDirection.getOpposite()) {
|
| 76 | level.neighborChanged(neighborPos.relative(direction), this.wireBlock, neighborOrientation.withFrontPreserveUp(direction));
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 | });
|
| 83 | if (level instanceof ServerLevel serverLevel && serverLevel.debugSynchronizers().hasAnySubscriberFor(DebugSubscriptions.REDSTONE_WIRE_ORIENTATIONS)) {
|
| 84 | this.updatedWires
|
| 85 | .forEach(
|
| 86 | (wirePos, packed) -> serverLevel.debugSynchronizers().sendBlockValue(wirePos, DebugSubscriptions.REDSTONE_WIRE_ORIENTATIONS, unpackOrientation(packed))
|
| 87 | );
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private static boolean isConnected(final BlockState state, final Direction direction) {
|
| 92 | EnumProperty<RedstoneSide> property = RedstoneWireBlock.PROPERTY_BY_DIRECTION.get(direction);
|
| 93 | return property == null ? direction == Direction.DOWN : state.getValue(property).isConnected();
|
| 94 | }
|
| 95 |
|
| 96 | private static Orientation getInitialOrientation(final Level level, final @Nullable Orientation incomingOrigination) {
|
| 97 | Orientation orientation;
|
| 98 | if (incomingOrigination != null) {
|
| 99 | orientation = incomingOrigination;
|
| 100 | } else {
|
| 101 | orientation = Orientation.random(level.getRandom());
|
| 102 | }
|
| 103 |
|
| 104 | return orientation.withUp(Direction.UP).withSideBias(Orientation.SideBias.LEFT);
|
| 105 | }
|
| 106 |
|
| 107 | private void calculateCurrentChanges(final Level level, final BlockPos initialPosition, final Orientation initialOrientation) {
|
| 108 | BlockState initialState = level.getBlockState(initialPosition);
|
| 109 | if (initialState.is(this.wireBlock)) {
|
| 110 | this.setPower(initialPosition, initialState.getValue(RedstoneWireBlock.POWER), initialOrientation);
|
| 111 | this.wiresToTurnOff.add(initialPosition);
|
| 112 | } else {
|
| 113 | this.propagateChangeToNeighbors(level, initialPosition, 0, initialOrientation, true);
|
| 114 | }
|
| 115 |
|
| 116 | while (!this.wiresToTurnOff.isEmpty()) {
|
| 117 | BlockPos pos = this.wiresToTurnOff.removeFirst();
|
| 118 | int packed = this.updatedWires.getInt(pos);
|
| 119 | Orientation orientation = unpackOrientation(packed);
|
| 120 | int oldPower = unpackPower(packed);
|
| 121 | int blockPower = this.getBlockSignal(level, pos);
|
| 122 | int wirePower = this.getIncomingWireSignal(level, pos);
|
| 123 | int newPower = Math.max(blockPower, wirePower);
|
| 124 | int powerToSet;
|
| 125 | if (newPower < oldPower) {
|
| 126 | if (blockPower > 0 && !this.wiresToTurnOn.contains(pos)) {
|
| 127 | this.wiresToTurnOn.add(pos);
|
| 128 | }
|
| 129 |
|
| 130 | powerToSet = 0;
|
| 131 | } else {
|
| 132 | powerToSet = newPower;
|
| 133 | }
|
| 134 |
|
| 135 | if (powerToSet != oldPower) {
|
| 136 | this.setPower(pos, powerToSet, orientation);
|
| 137 | }
|
| 138 |
|
| 139 | this.propagateChangeToNeighbors(level, pos, powerToSet, orientation, oldPower > newPower);
|
| 140 | }
|
| 141 |
|
| 142 | while (!this.wiresToTurnOn.isEmpty()) {
|
| 143 | BlockPos pos = this.wiresToTurnOn.removeFirst();
|
| 144 | int packed = this.updatedWires.getInt(pos);
|
| 145 | int oldPower = unpackPower(packed);
|
| 146 | int blockPower = this.getBlockSignal(level, pos);
|
| 147 | int wirePower = this.getIncomingWireSignal(level, pos);
|
| 148 | int newPower = Math.max(blockPower, wirePower);
|
| 149 | Orientation orientation = unpackOrientation(packed);
|
| 150 | if (newPower > oldPower) {
|
| 151 | this.setPower(pos, newPower, orientation);
|
| 152 | } else if (newPower < oldPower) {
|
| 153 | throw new IllegalStateException("Turning off wire while trying to turn it on. Should not happen.");
|
| 154 | }
|
| 155 |
|
| 156 | this.propagateChangeToNeighbors(level, pos, newPower, orientation, false);
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | private static int packOrientationAndPower(final Orientation orientation, final int power) {
|
| 161 | return orientation.getIndex() << 4 | power;
|
| 162 | }
|
| 163 |
|
| 164 | private static Orientation unpackOrientation(final int packed) {
|
| 165 | return Orientation.fromIndex(packed >> 4);
|
| 166 | }
|
| 167 |
|
| 168 | private static int unpackPower(final int packed) {
|
| 169 | return packed & 15;
|
| 170 | }
|
| 171 |
|
| 172 | private void setPower(final BlockPos pos, final int newPower, final Orientation orientation) {
|
| 173 | this.updatedWires
|
| 174 | .compute(
|
| 175 | pos, (key, packed) -> packed == null ? packOrientationAndPower(orientation, newPower) : packOrientationAndPower(unpackOrientation(packed), newPower)
|
| 176 | );
|
| 177 | }
|
| 178 |
|
| 179 | private void propagateChangeToNeighbors(
|
| 180 | final Level level, final BlockPos pos, final int newPower, final Orientation orientation, final boolean allowTurningOff
|
| 181 | ) {
|
| 182 | for (Direction directionHorizontal : orientation.getHorizontalDirections()) {
|
| 183 | BlockPos offsetPos = pos.relative(directionHorizontal);
|
| 184 | this.enqueueNeighborWire(level, offsetPos, newPower, orientation.withFront(directionHorizontal), allowTurningOff);
|
| 185 | }
|
| 186 |
|
| 187 | for (Direction directionVertical : orientation.getVerticalDirections()) {
|
| 188 | BlockPos offsetPos = pos.relative(directionVertical);
|
| 189 | boolean solidBlock = level.getBlockState(offsetPos).isRedstoneConductor(level, offsetPos);
|
| 190 |
|
| 191 | for (Direction directionHorizontal : orientation.getHorizontalDirections()) {
|
| 192 | BlockPos neighbor = pos.relative(directionHorizontal);
|
| 193 | if (directionVertical == Direction.UP && !solidBlock) {
|
| 194 | BlockPos neighborWire = offsetPos.relative(directionHorizontal);
|
| 195 | this.enqueueNeighborWire(level, neighborWire, newPower, orientation.withFront(directionHorizontal), allowTurningOff);
|
| 196 | } else if (directionVertical == Direction.DOWN && !level.getBlockState(neighbor).isRedstoneConductor(level, neighbor)) {
|
| 197 | BlockPos neighborWire = offsetPos.relative(directionHorizontal);
|
| 198 | this.enqueueNeighborWire(level, neighborWire, newPower, orientation.withFront(directionHorizontal), allowTurningOff);
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | private void enqueueNeighborWire(final Level level, final BlockPos pos, final int newFromPower, final Orientation orientation, final boolean allowTurningOff) {
|
| 205 | BlockState state = level.getBlockState(pos);
|
| 206 | if (state.is(this.wireBlock)) {
|
| 207 | int toPower = this.getWireSignal(pos, state);
|
| 208 | if (toPower < newFromPower - 1 && !this.wiresToTurnOn.contains(pos)) {
|
| 209 | this.wiresToTurnOn.add(pos);
|
| 210 | this.setPower(pos, toPower, orientation);
|
| 211 | }
|
| 212 |
|
| 213 | if (allowTurningOff && toPower > newFromPower && !this.wiresToTurnOff.contains(pos)) {
|
| 214 | this.wiresToTurnOff.add(pos);
|
| 215 | this.setPower(pos, toPower, orientation);
|
| 216 | }
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | protected int getWireSignal(final BlockPos pos, final BlockState state) {
|
| 222 | int packed = this.updatedWires.getOrDefault(pos, -1);
|
| 223 | return packed != -1 ? unpackPower(packed) : super.getWireSignal(pos, state);
|
| 224 | }
|
| 225 | }
|
| 226 |
|