| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.WeakHashMap;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.particles.DustParticleOptions;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 19 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 20 | import net.minecraft.world.level.redstone.Orientation;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | /**
|
| 24 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 25 | */
|
| 26 | public class RedstoneTorchBlock extends BaseTorchBlock {
|
| 27 | public static final BooleanProperty LIT = BlockStateProperties.LIT;
|
| 28 | private static final Map<BlockGetter, List<RedstoneTorchBlock.Toggle>> RECENT_TOGGLES = new WeakHashMap<>();
|
| 29 | public static final int RECENT_TOGGLE_TIMER = 60;
|
| 30 | public static final int MAX_RECENT_TOGGLES = 8;
|
| 31 | public static final int RESTART_DELAY = 160;
|
| 32 | private static final int TOGGLE_DELAY = 2;
|
| 33 |
|
| 34 | /**
|
| 35 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 36 | */
|
| 37 | public RedstoneTorchBlock(final BlockBehaviour.Properties properties) {
|
| 38 | super(properties);
|
| 39 | this.registerDefaultState(this.stateDefinition.any().setValue(LIT, true));
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 44 | this.notifyNeighbors(level, pos, state);
|
| 45 | }
|
| 46 |
|
| 47 | private void notifyNeighbors(final Level level, final BlockPos pos, final BlockState state) {
|
| 48 | Orientation orientation = this.randomOrientation(level, state);
|
| 49 |
|
| 50 | for (Direction direction : Direction.values()) {
|
| 51 | level.updateNeighborsAt(pos.relative(direction), this, ExperimentalRedstoneUtils.withFront(orientation, direction));
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 57 | if (!movedByPiston) {
|
| 58 | this.notifyNeighbors(level, pos, state);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | protected boolean hasNeighborSignal(final Level level, final BlockPos pos, final BlockState state) {
|
| 63 | return level.hasSignal(pos.below(), Direction.DOWN);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 68 | boolean neighborSignal = this.hasNeighborSignal(level, pos, state);
|
| 69 | List<RedstoneTorchBlock.Toggle> toggles = RECENT_TOGGLES.get(level);
|
| 70 |
|
| 71 | while (toggles != null && !toggles.isEmpty() && level.getGameTime() - toggles.get(0).when > 60L) {
|
| 72 | toggles.remove(0);
|
| 73 | }
|
| 74 |
|
| 75 | if (state.getValue(LIT)) {
|
| 76 | if (neighborSignal) {
|
| 77 | level.setBlockAndUpdate(pos, state.setValue(LIT, false));
|
| 78 | if (isToggledTooFrequently(level, pos, true)) {
|
| 79 | level.levelEvent(1502, pos, 0);
|
| 80 | level.scheduleTick(pos, level.getBlockState(pos).getBlock(), 160);
|
| 81 | }
|
| 82 | }
|
| 83 | } else if (!neighborSignal && !isToggledTooFrequently(level, pos, false)) {
|
| 84 | level.setBlockAndUpdate(pos, state.setValue(LIT, true));
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void neighborChanged(
|
| 90 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 91 | ) {
|
| 92 | if (state.getValue(LIT) == this.hasNeighborSignal(level, pos, state) && !level.getBlockTicks().willTickThisTick(pos, this)) {
|
| 93 | level.scheduleTick(pos, this, 2);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 99 | return direction == Direction.DOWN ? state.getSignal(level, pos, direction) : 0;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected boolean isSignalSource(final BlockState state) {
|
| 104 | return true;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 109 | return state.getValue(LIT) ? 15 : 0;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected int getSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 114 | return Direction.UP != direction ? this.ownSignal(state, level, pos) : 0;
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 119 | if (state.getValue(LIT)) {
|
| 120 | double x = pos.getX() + 0.5 + (random.nextDouble() - 0.5) * 0.2;
|
| 121 | double y = pos.getY() + 0.7 + (random.nextDouble() - 0.5) * 0.2;
|
| 122 | double z = pos.getZ() + 0.5 + (random.nextDouble() - 0.5) * 0.2;
|
| 123 | level.addParticle(DustParticleOptions.REDSTONE, x, y, z, 0.0, 0.0, 0.0);
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 129 | builder.add(LIT);
|
| 130 | }
|
| 131 |
|
| 132 | private static boolean isToggledTooFrequently(final Level level, final BlockPos pos, final boolean add) {
|
| 133 | List<RedstoneTorchBlock.Toggle> toggles = RECENT_TOGGLES.computeIfAbsent(level, k -> Lists.newArrayList());
|
| 134 | if (add) {
|
| 135 | toggles.add(new RedstoneTorchBlock.Toggle(pos.immutable(), level.getGameTime()));
|
| 136 | }
|
| 137 |
|
| 138 | int count = 0;
|
| 139 |
|
| 140 | for (RedstoneTorchBlock.Toggle toggle : toggles) {
|
| 141 | if (toggle.pos.equals(pos)) {
|
| 142 | if (++count >= 8) {
|
| 143 | return true;
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | return false;
|
| 149 | }
|
| 150 |
|
| 151 | protected @Nullable Orientation randomOrientation(final Level level, final BlockState state) {
|
| 152 | return ExperimentalRedstoneUtils.initialOrientation(level, null, Direction.UP);
|
| 153 | }
|
| 154 |
|
| 155 | public static class Toggle {
|
| 156 | private final BlockPos pos;
|
| 157 | private final long when;
|
| 158 |
|
| 159 | public Toggle(final BlockPos pos, final long when) {
|
| 160 | this.pos = pos;
|
| 161 | this.when = when;
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|