SetBlockCommand .java

net.minecraft.server.commands.SetBlockCommand (class): members: Mode, ERROR_FAILED, mode, predicate, block, source, register, pos. strings: "setblock".

net.minecraft.server.commandsclass117 linesserver4.0 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)

Fields & Constants (6)

ERROR_FAILED
source
pos
block
mode
predicate

String literals (8)

"commands.setblock.failed"
"setblock"
"block"
"destroy"
"keep"
"replace"
"strict"
"commands.setblock.success"

Full Source

SetBlockCommand.java117 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
6
import java.util.function.Predicate;
7
import net.minecraft.commands.CommandBuildContext;
8
import net.minecraft.commands.CommandSourceStack;
9
import net.minecraft.commands.Commands;
10
import net.minecraft.commands.arguments.blocks.BlockInput;
11
import net.minecraft.commands.arguments.blocks.BlockStateArgument;
12
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
13
import net.minecraft.core.BlockPos;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.server.level.ServerLevel;
16
import net.minecraft.world.level.block.state.BlockState;
17
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
18
import org.jspecify.annotations.Nullable;
19
20
public class SetBlockCommand {
21
	private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.setblock.failed"));
22
23
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
24
		Predicate<BlockInWorld> filter = b -> b.getLevel().isEmptyBlock(b.getPos());
25
		dispatcher.register(
26
			Commands.literal("setblock")
27
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
28
				.then(
29
					Commands.argument("pos", BlockPosArgument.blockPos())
30
						.then(
31
							Commands.argument("block", BlockStateArgument.block(context))
32
								.executes(
33
									c -> setBlock(
34
										c.getSource(), BlockPosArgument.getLoadedBlockPos(c, "pos"), BlockStateArgument.getBlock(c, "block"), SetBlockCommand.Mode.REPLACE, null, false
35
									)
36
								)
37
								.then(
38
									Commands.literal("destroy")
39
										.executes(
40
											c -> setBlock(
41
												c.getSource(), BlockPosArgument.getLoadedBlockPos(c, "pos"), BlockStateArgument.getBlock(c, "block"), SetBlockCommand.Mode.DESTROY, null, false
42
											)
43
										)
44
								)
45
								.then(
46
									Commands.literal("keep")
47
										.executes(
48
											c -> setBlock(
49
												c.getSource(), BlockPosArgument.getLoadedBlockPos(c, "pos"), BlockStateArgument.getBlock(c, "block"), SetBlockCommand.Mode.REPLACE, filter, false
50
											)
51
										)
52
								)
53
								.then(
54
									Commands.literal("replace")
55
										.executes(
56
											c -> setBlock(
57
												c.getSource(), BlockPosArgument.getLoadedBlockPos(c, "pos"), BlockStateArgument.getBlock(c, "block"), SetBlockCommand.Mode.REPLACE, null, false
58
											)
59
										)
60
								)
61
								.then(
62
									Commands.literal("strict")
63
										.executes(
64
											c -> setBlock(
65
												c.getSource(), BlockPosArgument.getLoadedBlockPos(c, "pos"), BlockStateArgument.getBlock(c, "block"), SetBlockCommand.Mode.REPLACE, null, true
66
											)
67
										)
68
								)
69
						)
70
				)
71
		);
72
	}
73
74
	private static int setBlock(
75
		final CommandSourceStack source,
76
		final BlockPos pos,
77
		final BlockInput block,
78
		final SetBlockCommand.Mode mode,
79
		final @Nullable Predicate<BlockInWorld> predicate,
80
		final boolean strict
81
	) throws CommandSyntaxException {
82
		ServerLevel level = source.getLevel();
83
		if (level.isDebug()) {
84
			throw ERROR_FAILED.create();
85
		}
86
87
		if (predicate != null && !predicate.test(new BlockInWorld(level, pos, true))) {
88
			throw ERROR_FAILED.create();
89
		}
90
91
		boolean placeNeeded;
92
		if (mode == SetBlockCommand.Mode.DESTROY) {
93
			level.destroyBlock(pos, true);
94
			placeNeeded = !block.getState().isAir() || !level.getBlockState(pos).isAir();
95
		} else {
96
			placeNeeded = true;
97
		}
98
99
		BlockState oldState = level.getBlockState(pos);
100
		if (placeNeeded && !block.place(level, pos, 2 | (strict ? 816 : 256))) {
101
			throw ERROR_FAILED.create();
102
		}
103
104
		if (!strict) {
105
			level.updateNeighboursOnBlockSet(pos, oldState);
106
		}
107
108
		source.sendSuccess(() -> Component.translatable("commands.setblock.success", pos.getX(), pos.getY(), pos.getZ()), true);
109
		return 1;
110
	}
111
112
	public enum Mode {
113
		REPLACE,
114
		DESTROY;
115
	}
116
}
117

Decompiled Minecraft 26.3 source of SetBlockCommand (net/minecraft/server/commands/SetBlockCommand.java) with Mojang official mappings, stored verbatim on the site.