CommandBlock .java

net.minecraft.world.level.block.CommandBlock (class): members: CONDITIONAL, automatic, setPlacedBy, hasAnalogOutputSignal, getAnalogOutputSignal, FACING.

net.minecraft.world.level.blockclass209 linescommonextends BaseEntityBlock8.0 KB

Implements

API Surface (11)

BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
boolean hasAnalogOutputSignal(final BlockState state)
int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction)
void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
BlockState getStateForPlacement(final BlockPlaceContext context)

Fields & Constants (5)

LOGGER
FACING
CONDITIONAL
automatic
orientation

Full Source

CommandBlock.java209 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import com.mojang.logging.LogUtils;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.component.DataComponents;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.util.StringUtil;
10
import net.minecraft.world.InteractionResult;
11
import net.minecraft.world.entity.LivingEntity;
12
import net.minecraft.world.entity.player.Player;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.item.context.BlockPlaceContext;
15
import net.minecraft.world.level.BaseCommandBlock;
16
import net.minecraft.world.level.Level;
17
import net.minecraft.world.level.block.entity.BlockEntity;
18
import net.minecraft.world.level.block.entity.CommandBlockEntity;
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.BooleanProperty;
24
import net.minecraft.world.level.block.state.properties.EnumProperty;
25
import net.minecraft.world.level.gamerules.GameRules;
26
import net.minecraft.world.level.redstone.Orientation;
27
import net.minecraft.world.phys.BlockHitResult;
28
import org.jspecify.annotations.Nullable;
29
import org.slf4j.Logger;
30
31
public class CommandBlock extends BaseEntityBlock implements GameMasterBlock {
32
	private static final Logger LOGGER = LogUtils.getLogger();
33
	public static final EnumProperty<Direction> FACING = DirectionalBlock.FACING;
34
	public static final BooleanProperty CONDITIONAL = BlockStateProperties.CONDITIONAL;
35
	private final boolean automatic;
36
37
	public CommandBlock(final boolean automatic, final BlockBehaviour.Properties properties) {
38
		super(properties);
39
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(CONDITIONAL, false));
40
		this.automatic = automatic;
41
	}
42
43
	@Override
44
	public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
45
		CommandBlockEntity blockEntity = new CommandBlockEntity(worldPosition, blockState);
46
		blockEntity.setAutomatic(this.automatic);
47
		return blockEntity;
48
	}
49
50
	@Override
51
	protected void neighborChanged(
52
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
53
	) {
54
		if (!level.isClientSide()) {
55
			if (level.getBlockEntity(pos) instanceof CommandBlockEntity commandBlock) {
56
				this.setPoweredAndUpdate(level, pos, commandBlock, level.hasNeighborSignal(pos));
57
			}
58
		}
59
	}
60
61
	private void setPoweredAndUpdate(final Level level, final BlockPos pos, final CommandBlockEntity commandBlock, final boolean isPowered) {
62
		boolean wasPowered = commandBlock.isPowered();
63
		if (isPowered != wasPowered) {
64
			commandBlock.setPowered(isPowered);
65
			if (isPowered) {
66
				if (commandBlock.isAutomatic() || commandBlock.getMode() == CommandBlockEntity.Mode.SEQUENCE) {
67
					return;
68
				}
69
70
				commandBlock.markConditionMet();
71
				level.scheduleTick(pos, this, 1);
72
			}
73
		}
74
	}
75
76
	@Override
77
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
78
		if (level.getBlockEntity(pos) instanceof CommandBlockEntity commandBlock) {
79
			BaseCommandBlock baseCommandBlock = commandBlock.getCommandBlock();
80
			boolean commandSet = !StringUtil.isNullOrEmpty(baseCommandBlock.getCommand());
81
			CommandBlockEntity.Mode mode = commandBlock.getMode();
82
			boolean wasConditionMet = commandBlock.wasConditionMet();
83
			if (mode == CommandBlockEntity.Mode.AUTO) {
84
				commandBlock.markConditionMet();
85
				if (wasConditionMet) {
86
					this.execute(state, level, pos, baseCommandBlock, commandSet);
87
				} else if (commandBlock.isConditional()) {
88
					baseCommandBlock.setSuccessCount(0);
89
				}
90
91
				if (commandBlock.isPowered() || commandBlock.isAutomatic()) {
92
					level.scheduleTick(pos, this, 1);
93
				}
94
			} else if (mode == CommandBlockEntity.Mode.REDSTONE) {
95
				if (wasConditionMet) {
96
					this.execute(state, level, pos, baseCommandBlock, commandSet);
97
				} else if (commandBlock.isConditional()) {
98
					baseCommandBlock.setSuccessCount(0);
99
				}
100
			}
101
102
			level.updateNeighbourForOutputSignal(pos, this);
103
		}
104
	}
105
106
	private void execute(final BlockState state, final ServerLevel level, final BlockPos pos, final BaseCommandBlock baseCommandBlock, final boolean commandSet) {
107
		if (commandSet) {
108
			baseCommandBlock.performCommand(level);
109
		} else {
110
			baseCommandBlock.setSuccessCount(0);
111
		}
112
113
		executeChain(level, pos, state.getValue(FACING));
114
	}
115
116
	@Override
117
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
118
		if (level.getBlockEntity(pos) instanceof CommandBlockEntity commandBlockEntity && player.canUseGameMasterBlocks()) {
119
			player.openCommandBlock(commandBlockEntity);
120
			return InteractionResult.SUCCESS;
121
		} else {
122
			return InteractionResult.PASS;
123
		}
124
	}
125
126
	@Override
127
	protected boolean hasAnalogOutputSignal(final BlockState state) {
128
		return true;
129
	}
130
131
	@Override
132
	protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
133
		return level.getBlockEntity(pos) instanceof CommandBlockEntity commandBlockEntity ? commandBlockEntity.getCommandBlock().getSuccessCount() : 0;
134
	}
135
136
	@Override
137
	public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
138
		if (level.getBlockEntity(pos) instanceof CommandBlockEntity commandBlockEntity) {
139
			BaseCommandBlock commandBlock = commandBlockEntity.getCommandBlock();
140
			if (level instanceof ServerLevel serverLevel) {
141
				if (!itemStack.has(DataComponents.BLOCK_ENTITY_DATA)) {
142
					commandBlock.setTrackOutput(serverLevel.getGameRules().get(GameRules.SEND_COMMAND_FEEDBACK));
143
					commandBlockEntity.setAutomatic(this.automatic);
144
				}
145
146
				boolean hasNeighborSignal = level.hasNeighborSignal(pos);
147
				this.setPoweredAndUpdate(level, pos, commandBlockEntity, hasNeighborSignal);
148
			}
149
		}
150
	}
151
152
	@Override
153
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
154
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
155
	}
156
157
	@Override
158
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
159
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
160
	}
161
162
	@Override
163
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
164
		builder.add(FACING, CONDITIONAL);
165
	}
166
167
	@Override
168
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
169
		return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite());
170
	}
171
172
	private static void executeChain(final ServerLevel level, final BlockPos blockPos, Direction direction) {
173
		BlockPos.MutableBlockPos pos = blockPos.mutable();
174
		GameRules gameRules = level.getGameRules();
175
		int maxIterations = gameRules.get(GameRules.MAX_COMMAND_SEQUENCE_LENGTH);
176
177
		while (maxIterations-- > 0) {
178
			pos.move(direction);
179
			BlockState state = level.getBlockState(pos);
180
			Block block = state.getBlock();
181
			if (!state.is(Blocks.CHAIN_COMMAND_BLOCK)
182
				|| !(level.getBlockEntity(pos) instanceof CommandBlockEntity commandBlock)
183
				|| commandBlock.getMode() != CommandBlockEntity.Mode.SEQUENCE) {
184
				break;
185
			}
186
187
			if (commandBlock.isPowered() || commandBlock.isAutomatic()) {
188
				BaseCommandBlock baseCommandBlock = commandBlock.getCommandBlock();
189
				if (commandBlock.markConditionMet()) {
190
					if (!baseCommandBlock.performCommand(level)) {
191
						break;
192
					}
193
194
					level.updateNeighbourForOutputSignal(pos, block);
195
				} else if (commandBlock.isConditional()) {
196
					baseCommandBlock.setSuccessCount(0);
197
				}
198
			}
199
200
			direction = state.getValue(FACING);
201
		}
202
203
		if (maxIterations <= 0) {
204
			int limit = Math.max(gameRules.get(GameRules.MAX_COMMAND_SEQUENCE_LENGTH), 0);
205
			LOGGER.warn("Command Block chain tried to execute more than {} steps!", limit);
206
		}
207
	}
208
}
209

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