ComparatorBlock .java

net.minecraft.world.level.block.ComparatorBlock (class): members: checkTickOnNeighbor, getInputSignal, shouldTurnOn, MODE, getOutputSignal, getDelay.

net.minecraft.world.level.blockclass195 linescommonextends DiodeBlock7.8 KB

Implements

API Surface (11)

int getDelay(final BlockState state)
BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
int getOutputSignal(final BlockGetter level, final BlockPos pos, final BlockState state)
boolean shouldTurnOn(final Level level, final BlockPos pos, final BlockState state)
int getInputSignal(final Level level, final BlockPos pos, final BlockState state)
InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
void checkTickOnNeighbor(final Level level, final BlockPos pos, final BlockState state)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
boolean triggerEvent(final BlockState state, final Level level, final BlockPos pos, final int b0, final int b1)
BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (8)

MODE
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

ComparatorBlock.java195 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.List;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.sounds.SoundEvents;
8
import net.minecraft.sounds.SoundSource;
9
import net.minecraft.util.RandomSource;
10
import net.minecraft.world.InteractionResult;
11
import net.minecraft.world.entity.decoration.ItemFrame;
12
import net.minecraft.world.entity.player.Player;
13
import net.minecraft.world.level.BlockGetter;
14
import net.minecraft.world.level.Level;
15
import net.minecraft.world.level.LevelReader;
16
import net.minecraft.world.level.ScheduledTickAccess;
17
import net.minecraft.world.level.block.entity.BlockEntity;
18
import net.minecraft.world.level.block.entity.ComparatorBlockEntity;
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.ComparatorMode;
24
import net.minecraft.world.level.block.state.properties.EnumProperty;
25
import net.minecraft.world.phys.AABB;
26
import net.minecraft.world.phys.BlockHitResult;
27
import net.minecraft.world.ticks.TickPriority;
28
import org.jspecify.annotations.Nullable;
29
30
public class ComparatorBlock extends DiodeBlock implements EntityBlock {
31
	public static final EnumProperty<ComparatorMode> MODE = BlockStateProperties.MODE_COMPARATOR;
32
33
	public ComparatorBlock(final BlockBehaviour.Properties properties) {
34
		super(properties);
35
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(POWERED, false).setValue(MODE, ComparatorMode.COMPARE));
36
	}
37
38
	@Override
39
	protected int getDelay(final BlockState state) {
40
		return 2;
41
	}
42
43
	@Override
44
	public BlockState updateShape(
45
		final BlockState state,
46
		final LevelReader level,
47
		final ScheduledTickAccess ticks,
48
		final BlockPos pos,
49
		final Direction directionToNeighbour,
50
		final BlockPos neighbourPos,
51
		final BlockState neighbourState,
52
		final RandomSource random
53
	) {
54
		return directionToNeighbour == Direction.DOWN && !this.canSurviveOn(level, neighbourPos, neighbourState)
55
			? Blocks.AIR.defaultBlockState()
56
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
57
	}
58
59
	@Override
60
	protected int getOutputSignal(final BlockGetter level, final BlockPos pos, final BlockState state) {
61
		return level.getBlockEntity(pos) instanceof ComparatorBlockEntity comparatorBlockEntity ? comparatorBlockEntity.getOutputSignal() : 0;
62
	}
63
64
	private int calculateOutputSignal(final Level level, final BlockPos pos, final BlockState state) {
65
		int inputSignal = this.getInputSignal(level, pos, state);
66
		if (inputSignal == 0) {
67
			return 0;
68
		} else {
69
			int alternateSignal = this.getAlternateSignal(level, pos, state);
70
			if (alternateSignal > inputSignal) {
71
				return 0;
72
			} else {
73
				return state.getValue(MODE) == ComparatorMode.SUBTRACT ? inputSignal - alternateSignal : inputSignal;
74
			}
75
		}
76
	}
77
78
	@Override
79
	protected boolean shouldTurnOn(final Level level, final BlockPos pos, final BlockState state) {
80
		int input = this.getInputSignal(level, pos, state);
81
		if (input == 0) {
82
			return false;
83
		}
84
85
		int sideInput = this.getAlternateSignal(level, pos, state);
86
		return input > sideInput ? true : input == sideInput && state.getValue(MODE) == ComparatorMode.COMPARE;
87
	}
88
89
	@Override
90
	protected int getInputSignal(final Level level, final BlockPos pos, final BlockState state) {
91
		int resultSignal = super.getInputSignal(level, pos, state);
92
		Direction direction = state.getValue(FACING);
93
		BlockPos targetPos = pos.relative(direction);
94
		BlockState targetState = level.getBlockState(targetPos);
95
		if (targetState.hasAnalogOutputSignal()) {
96
			resultSignal = targetState.getAnalogOutputSignal(level, targetPos, direction.getOpposite());
97
		} else if (resultSignal < 15 && targetState.isRedstoneConductor(level, targetPos)) {
98
			targetPos = targetPos.relative(direction);
99
			targetState = level.getBlockState(targetPos);
100
			ItemFrame itemFrame = this.getItemFrame(level, direction, targetPos);
101
			int itemFrameOrBlockSignal = Math.max(
102
				itemFrame == null ? Integer.MIN_VALUE : itemFrame.getAnalogOutput(),
103
				targetState.hasAnalogOutputSignal() ? targetState.getAnalogOutputSignal(level, targetPos, direction.getOpposite()) : Integer.MIN_VALUE
104
			);
105
			if (itemFrameOrBlockSignal != Integer.MIN_VALUE) {
106
				resultSignal = itemFrameOrBlockSignal;
107
			}
108
		}
109
110
		return resultSignal;
111
	}
112
113
	private @Nullable ItemFrame getItemFrame(final Level level, final Direction direction, final BlockPos tPos) {
114
		List<ItemFrame> itemFrames = level.getEntitiesOfClass(
115
			ItemFrame.class,
116
			new AABB(tPos.getX(), tPos.getY(), tPos.getZ(), tPos.getX() + 1, tPos.getY() + 1, tPos.getZ() + 1),
117
			entity -> entity.getDirection() == direction
118
		);
119
		return itemFrames.size() == 1 ? itemFrames.get(0) : null;
120
	}
121
122
	@Override
123
	protected InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
124
		if (!player.getAbilities().mayBuild) {
125
			return InteractionResult.PASS;
126
		}
127
128
		state = state.cycle(MODE);
129
		float pitch = state.getValue(MODE) == ComparatorMode.SUBTRACT ? 0.55F : 0.5F;
130
		level.playSound(player, pos, SoundEvents.COMPARATOR_CLICK, SoundSource.BLOCKS, 0.3F, pitch);
131
		level.setBlock(pos, state, 2);
132
		if (level.getBlockState(pos).is(this)) {
133
			this.refreshOutputState(level, pos, state);
134
		}
135
136
		return InteractionResult.SUCCESS;
137
	}
138
139
	@Override
140
	protected void checkTickOnNeighbor(final Level level, final BlockPos pos, final BlockState state) {
141
		if (!level.getBlockTicks().willTickThisTick(pos, this)) {
142
			int outputValue = this.calculateOutputSignal(level, pos, state);
143
			int oldValue = level.getBlockEntity(pos) instanceof ComparatorBlockEntity comparatorBlockEntity ? comparatorBlockEntity.getOutputSignal() : 0;
144
			if (outputValue != oldValue || state.getValue(POWERED) != this.shouldTurnOn(level, pos, state)) {
145
				TickPriority priority = this.shouldPrioritize(level, pos, state) ? TickPriority.HIGH : TickPriority.NORMAL;
146
				level.scheduleTick(pos, this, 2, priority);
147
			}
148
		}
149
	}
150
151
	private void refreshOutputState(final Level level, final BlockPos pos, final BlockState state) {
152
		int outputValue = this.calculateOutputSignal(level, pos, state);
153
		BlockEntity blockEntity = level.getBlockEntity(pos);
154
		int oldValue = 0;
155
		if (blockEntity instanceof ComparatorBlockEntity comparatorBlockEntity) {
156
			oldValue = comparatorBlockEntity.getOutputSignal();
157
			comparatorBlockEntity.setOutputSignal(outputValue);
158
		}
159
160
		if (oldValue != outputValue || state.getValue(MODE) == ComparatorMode.COMPARE) {
161
			boolean sourceOn = this.shouldTurnOn(level, pos, state);
162
			boolean isOn = state.getValue(POWERED);
163
			if (isOn && !sourceOn) {
164
				level.setBlock(pos, state.setValue(POWERED, false), 2);
165
			} else if (!isOn && sourceOn) {
166
				level.setBlock(pos, state.setValue(POWERED, true), 2);
167
			}
168
169
			this.updateNeighborsInFront(level, pos, state);
170
		}
171
	}
172
173
	@Override
174
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
175
		this.refreshOutputState(level, pos, state);
176
	}
177
178
	@Override
179
	protected boolean triggerEvent(final BlockState state, final Level level, final BlockPos pos, final int b0, final int b1) {
180
		super.triggerEvent(state, level, pos, b0, b1);
181
		BlockEntity blockEntity = level.getBlockEntity(pos);
182
		return blockEntity != null && blockEntity.triggerEvent(b0, b1);
183
	}
184
185
	@Override
186
	public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
187
		return new ComparatorBlockEntity(worldPosition, blockState);
188
	}
189
190
	@Override
191
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
192
		builder.add(FACING, MODE, POWERED);
193
	}
194
}
195

Decompiled Minecraft 26.3 source of ComparatorBlock (net/minecraft/world/level/block/ComparatorBlock.java) with Mojang official mappings, stored verbatim on the site.