CloneCommands .java

net.minecraft.server.commands.CloneCommands (class): members: CloneBlockEntityInfo, DimensionAndPosition, startPosAndDimension, destPosAndDimension, Mode.

net.minecraft.server.commandsclass322 linesserver13.9 KB

API Surface (2)

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

Fields & Constants (20)

LOGGER
ERROR_OVERLAP
ERROR_AREA_TOO_LARGE
ERROR_FAILED
FILTER_AIR
context
level
fromDimension
beginPos
endPos
destinationPos
strict
filter
source
startPosAndDimension
endPosAndDimension
destPosAndDimension
predicate
mode
blockEntityInfo

String literals (18)

"commands.clone.overlap"
"commands.clone.toobig"
"commands.clone.failed"
"clone"
"from"
"sourceDimension"
"begin"
"targetDimension"
"destination"
"strict"
"replace"
"masked"
"filtered"
"filter"
"force"
"move"
"normal"
"commands.clone.success"

Full Source

CloneCommands.java322 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.brigadier.CommandDispatcher;
5
import com.mojang.brigadier.builder.ArgumentBuilder;
6
import com.mojang.brigadier.context.CommandContext;
7
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
9
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
10
import com.mojang.logging.LogUtils;
11
import java.util.Deque;
12
import java.util.List;
13
import java.util.function.Predicate;
14
import net.minecraft.commands.CommandBuildContext;
15
import net.minecraft.commands.CommandSourceStack;
16
import net.minecraft.commands.Commands;
17
import net.minecraft.commands.arguments.DimensionArgument;
18
import net.minecraft.commands.arguments.blocks.BlockPredicateArgument;
19
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
20
import net.minecraft.core.BlockPos;
21
import net.minecraft.core.component.DataComponentMap;
22
import net.minecraft.nbt.CompoundTag;
23
import net.minecraft.network.chat.Component;
24
import net.minecraft.server.level.ServerLevel;
25
import net.minecraft.util.ProblemReporter;
26
import net.minecraft.world.level.block.Blocks;
27
import net.minecraft.world.level.block.entity.BlockEntity;
28
import net.minecraft.world.level.block.state.BlockState;
29
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
30
import net.minecraft.world.level.gamerules.GameRules;
31
import net.minecraft.world.level.levelgen.structure.BoundingBox;
32
import net.minecraft.world.level.storage.TagValueInput;
33
import net.minecraft.world.level.storage.TagValueOutput;
34
import org.jspecify.annotations.Nullable;
35
import org.slf4j.Logger;
36
37
public class CloneCommands {
38
	private static final Logger LOGGER = LogUtils.getLogger();
39
	private static final SimpleCommandExceptionType ERROR_OVERLAP = new SimpleCommandExceptionType(Component.translatable("commands.clone.overlap"));
40
	private static final Dynamic2CommandExceptionType ERROR_AREA_TOO_LARGE = new Dynamic2CommandExceptionType(
41
		(max, count) -> Component.translatableEscape("commands.clone.toobig", max, count)
42
	);
43
	private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.clone.failed"));
44
	public static final Predicate<BlockInWorld> FILTER_AIR = b -> !b.getState().isAir();
45
46
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
47
		dispatcher.register(
48
			Commands.literal("clone")
49
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
50
				.then(beginEndDestinationAndModeSuffix(context, c -> c.getSource().getLevel()))
51
				.then(
52
					Commands.literal("from")
53
						.then(
54
							Commands.argument("sourceDimension", DimensionArgument.dimension())
55
								.then(beginEndDestinationAndModeSuffix(context, c -> DimensionArgument.getDimension(c, "sourceDimension")))
56
						)
57
				)
58
		);
59
	}
60
61
	private static ArgumentBuilder<CommandSourceStack, ?> beginEndDestinationAndModeSuffix(
62
		final CommandBuildContext context, final InCommandFunction<CommandContext<CommandSourceStack>, ServerLevel> fromDimension
63
	) {
64
		return Commands.argument("begin", BlockPosArgument.blockPos())
65
			.then(
66
				Commands.argument("end", BlockPosArgument.blockPos())
67
					.then(destinationAndStrictSuffix(context, fromDimension, c -> c.getSource().getLevel()))
68
					.then(
69
						Commands.literal("to")
70
							.then(
71
								Commands.argument("targetDimension", DimensionArgument.dimension())
72
									.then(destinationAndStrictSuffix(context, fromDimension, c -> DimensionArgument.getDimension(c, "targetDimension")))
73
							)
74
					)
75
			);
76
	}
77
78
	private static CloneCommands.DimensionAndPosition getLoadedDimensionAndPosition(
79
		final CommandContext<CommandSourceStack> context, final ServerLevel level, final String positionArgument
80
	) throws CommandSyntaxException {
81
		BlockPos blockPos = BlockPosArgument.getLoadedBlockPos(context, level, positionArgument);
82
		return new CloneCommands.DimensionAndPosition(level, blockPos);
83
	}
84
85
	private static ArgumentBuilder<CommandSourceStack, ?> destinationAndStrictSuffix(
86
		final CommandBuildContext context,
87
		final InCommandFunction<CommandContext<CommandSourceStack>, ServerLevel> fromDimension,
88
		final InCommandFunction<CommandContext<CommandSourceStack>, ServerLevel> toDimension
89
	) {
90
		InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> beginPos = c -> getLoadedDimensionAndPosition(
91
			c, fromDimension.apply(c), "begin"
92
		);
93
		InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> endPos = c -> getLoadedDimensionAndPosition(
94
			c, fromDimension.apply(c), "end"
95
		);
96
		InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> destinationPos = c -> getLoadedDimensionAndPosition(
97
			c, toDimension.apply(c), "destination"
98
		);
99
		return modeSuffix(context, beginPos, endPos, destinationPos, false, Commands.argument("destination", BlockPosArgument.blockPos()))
100
			.then(modeSuffix(context, beginPos, endPos, destinationPos, true, Commands.literal("strict")));
101
	}
102
103
	private static ArgumentBuilder<CommandSourceStack, ?> modeSuffix(
104
		final CommandBuildContext context,
105
		final InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> beginPos,
106
		final InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> endPos,
107
		final InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> destinationPos,
108
		final boolean strict,
109
		final ArgumentBuilder<CommandSourceStack, ?> builder
110
	) {
111
		return builder.executes(c -> clone(c.getSource(), beginPos.apply(c), endPos.apply(c), destinationPos.apply(c), b -> true, CloneCommands.Mode.NORMAL, strict))
112
			.then(wrapWithCloneMode(beginPos, endPos, destinationPos, c -> b -> true, strict, Commands.literal("replace")))
113
			.then(wrapWithCloneMode(beginPos, endPos, destinationPos, c -> FILTER_AIR, strict, Commands.literal("masked")))
114
			.then(
115
				Commands.literal("filtered")
116
					.then(
117
						wrapWithCloneMode(
118
							beginPos,
119
							endPos,
120
							destinationPos,
121
							c -> BlockPredicateArgument.getBlockPredicate(c, "filter"),
122
							strict,
123
							Commands.argument("filter", BlockPredicateArgument.blockPredicate(context))
124
						)
125
					)
126
			);
127
	}
128
129
	private static ArgumentBuilder<CommandSourceStack, ?> wrapWithCloneMode(
130
		final InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> beginPos,
131
		final InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> endPos,
132
		final InCommandFunction<CommandContext<CommandSourceStack>, CloneCommands.DimensionAndPosition> destinationPos,
133
		final InCommandFunction<CommandContext<CommandSourceStack>, Predicate<BlockInWorld>> filter,
134
		final boolean strict,
135
		final ArgumentBuilder<CommandSourceStack, ?> builder
136
	) {
137
		return builder.executes(
138
				c -> clone(c.getSource(), beginPos.apply(c), endPos.apply(c), destinationPos.apply(c), filter.apply(c), CloneCommands.Mode.NORMAL, strict)
139
			)
140
			.then(
141
				Commands.literal("force")
142
					.executes(c -> clone(c.getSource(), beginPos.apply(c), endPos.apply(c), destinationPos.apply(c), filter.apply(c), CloneCommands.Mode.FORCE, strict))
143
			)
144
			.then(
145
				Commands.literal("move")
146
					.executes(c -> clone(c.getSource(), beginPos.apply(c), endPos.apply(c), destinationPos.apply(c), filter.apply(c), CloneCommands.Mode.MOVE, strict))
147
			)
148
			.then(
149
				Commands.literal("normal")
150
					.executes(c -> clone(c.getSource(), beginPos.apply(c), endPos.apply(c), destinationPos.apply(c), filter.apply(c), CloneCommands.Mode.NORMAL, strict))
151
			);
152
	}
153
154
	private static int clone(
155
		final CommandSourceStack source,
156
		final CloneCommands.DimensionAndPosition startPosAndDimension,
157
		final CloneCommands.DimensionAndPosition endPosAndDimension,
158
		final CloneCommands.DimensionAndPosition destPosAndDimension,
159
		final Predicate<BlockInWorld> predicate,
160
		final CloneCommands.Mode mode,
161
		final boolean strict
162
	) throws CommandSyntaxException {
163
		BlockPos startPos = startPosAndDimension.position();
164
		BlockPos endPos = endPosAndDimension.position();
165
		BoundingBox from = BoundingBox.fromCorners(startPos, endPos);
166
		BlockPos destPos = destPosAndDimension.position();
167
		BlockPos destEndPos = destPos.offset(from.getLength());
168
		BoundingBox destination = BoundingBox.fromCorners(destPos, destEndPos);
169
		ServerLevel fromDimension = startPosAndDimension.dimension();
170
		ServerLevel toDimension = destPosAndDimension.dimension();
171
		if (!mode.canOverlap() && fromDimension == toDimension && destination.intersects(from)) {
172
			throw ERROR_OVERLAP.create();
173
		}
174
175
		long area = (long)from.getXSpan() * from.getYSpan() * from.getZSpan();
176
		int limit = source.getLevel().getGameRules().get(GameRules.MAX_BLOCK_MODIFICATIONS);
177
		if (area > limit) {
178
			throw ERROR_AREA_TOO_LARGE.create(limit, area);
179
		}
180
181
		if (!fromDimension.hasChunksAt(startPos, endPos) || !toDimension.hasChunksAt(destPos, destEndPos)) {
182
			throw BlockPosArgument.ERROR_NOT_LOADED.create();
183
		}
184
185
		if (toDimension.isDebug()) {
186
			throw ERROR_FAILED.create();
187
		}
188
189
		List<CloneCommands.CloneBlockInfo> solidList = Lists.newArrayList();
190
		List<CloneCommands.CloneBlockInfo> blockEntitiesList = Lists.newArrayList();
191
		List<CloneCommands.CloneBlockInfo> otherBlocksList = Lists.newArrayList();
192
		Deque<BlockPos> clearBlocksList = Lists.newLinkedList();
193
		int count = 0;
194
		ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER);
195
196
		try {
197
			BlockPos offset = new BlockPos(destination.minX() - from.minX(), destination.minY() - from.minY(), destination.minZ() - from.minZ());
198
199
			for (int z = from.minZ(); z <= from.maxZ(); z++) {
200
				for (int y = from.minY(); y <= from.maxY(); y++) {
201
					for (int x = from.minX(); x <= from.maxX(); x++) {
202
						BlockPos sourcePos = new BlockPos(x, y, z);
203
						BlockPos destinationPos = sourcePos.offset(offset);
204
						BlockInWorld block = new BlockInWorld(fromDimension, sourcePos, false);
205
						BlockState blockState = block.getState();
206
						if (predicate.test(block)) {
207
							BlockEntity blockEntity = fromDimension.getBlockEntity(sourcePos);
208
							if (blockEntity != null) {
209
								TagValueOutput output = TagValueOutput.createWithContext(reporter.forChild(blockEntity.problemPath()), source.registryAccess());
210
								blockEntity.saveCustomOnly(output);
211
								CloneCommands.CloneBlockEntityInfo blockEntityInfo = new CloneCommands.CloneBlockEntityInfo(output.buildResult(), blockEntity.components());
212
								blockEntitiesList.add(new CloneCommands.CloneBlockInfo(destinationPos, blockState, blockEntityInfo, toDimension.getBlockState(destinationPos)));
213
								clearBlocksList.addLast(sourcePos);
214
							} else if (!blockState.isSolidRender() && !blockState.isCollisionShapeFullBlock(fromDimension, sourcePos)) {
215
								otherBlocksList.add(new CloneCommands.CloneBlockInfo(destinationPos, blockState, null, toDimension.getBlockState(destinationPos)));
216
								clearBlocksList.addFirst(sourcePos);
217
							} else {
218
								solidList.add(new CloneCommands.CloneBlockInfo(destinationPos, blockState, null, toDimension.getBlockState(destinationPos)));
219
								clearBlocksList.addLast(sourcePos);
220
							}
221
						}
222
					}
223
				}
224
			}
225
226
			int defaultUpdateFlags = 2 | (strict ? 816 : 0);
227
			if (mode == CloneCommands.Mode.MOVE) {
228
				for (BlockPos pos : clearBlocksList) {
229
					fromDimension.setBlock(pos, Blocks.BARRIER.defaultBlockState(), defaultUpdateFlags | 816);
230
				}
231
232
				int standardUpdateFlags = strict ? defaultUpdateFlags : 3;
233
234
				for (BlockPos pos : clearBlocksList) {
235
					fromDimension.setBlock(pos, Blocks.AIR.defaultBlockState(), standardUpdateFlags);
236
				}
237
			}
238
239
			List<CloneCommands.CloneBlockInfo> blockInfoList = Lists.newArrayList();
240
			blockInfoList.addAll(solidList);
241
			blockInfoList.addAll(blockEntitiesList);
242
			blockInfoList.addAll(otherBlocksList);
243
			List<CloneCommands.CloneBlockInfo> reverse = Lists.reverse(blockInfoList);
244
245
			for (CloneCommands.CloneBlockInfo cloneInfo : reverse) {
246
				toDimension.setBlock(cloneInfo.pos, Blocks.BARRIER.defaultBlockState(), defaultUpdateFlags | 816);
247
			}
248
249
			for (CloneCommands.CloneBlockInfo cloneInfo : blockInfoList) {
250
				if (toDimension.setBlock(cloneInfo.pos, cloneInfo.state, defaultUpdateFlags)) {
251
					count++;
252
				}
253
			}
254
255
			for (CloneCommands.CloneBlockInfo cloneInfo : blockEntitiesList) {
256
				BlockEntity newBlockEntity = toDimension.getBlockEntity(cloneInfo.pos);
257
				if (cloneInfo.blockEntityInfo != null && newBlockEntity != null) {
258
					newBlockEntity.loadCustomOnly(
259
						TagValueInput.create(reporter.forChild(newBlockEntity.problemPath()), toDimension.registryAccess(), cloneInfo.blockEntityInfo.tag)
260
					);
261
					newBlockEntity.setComponents(cloneInfo.blockEntityInfo.components);
262
					newBlockEntity.setChanged();
263
				}
264
265
				toDimension.setBlock(cloneInfo.pos, cloneInfo.state, defaultUpdateFlags);
266
			}
267
268
			if (!strict) {
269
				for (CloneCommands.CloneBlockInfo cloneInfo : reverse) {
270
					toDimension.updateNeighboursOnBlockSet(cloneInfo.pos, cloneInfo.previousStateAtDestination);
271
				}
272
			}
273
274
			toDimension.getBlockTicks().copyAreaFrom(fromDimension.getBlockTicks(), from, offset);
275
		} catch (Throwable var36) {
276
			try {
277
				reporter.close();
278
			} catch (Throwable var35) {
279
				var36.addSuppressed(var35);
280
			}
281
282
			throw var36;
283
		}
284
285
		reporter.close();
286
		if (count == 0) {
287
			throw ERROR_FAILED.create();
288
		}
289
290
		int finalCount = count;
291
		source.sendSuccess(() -> Component.translatable("commands.clone.success", finalCount), true);
292
		return count;
293
	}
294
295
	private record CloneBlockEntityInfo(CompoundTag tag, DataComponentMap components) {
296
	}
297
298
	private record CloneBlockInfo(
299
		BlockPos pos, BlockState state, CloneCommands.@Nullable CloneBlockEntityInfo blockEntityInfo, BlockState previousStateAtDestination
300
	) {
301
	}
302
303
	private record DimensionAndPosition(ServerLevel dimension, BlockPos position) {
304
	}
305
306
	private enum Mode {
307
		FORCE(true),
308
		MOVE(true),
309
		NORMAL(false);
310
311
		private final boolean canOverlap;
312
313
		Mode(final boolean canOverlap) {
314
			this.canOverlap = canOverlap;
315
		}
316
317
		public boolean canOverlap() {
318
			return this.canOverlap;
319
		}
320
	}
321
}
322

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