ForceLoadCommand .java

net.minecraft.server.commands.ForceLoadCommand (class): members: ERROR_TOO_MANY_CHUNKS, ERROR_NONE_REMOVED, ERROR_NOT_TICKING, ERROR_ALL_ADDED, register.

net.minecraft.server.commandsclass184 linesserver8.2 KB

API Surface (3)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)
ChunkPos min()
ChunkPos max()

Fields & Constants (7)

MAX_CHUNK_LIMIT
ERROR_TOO_MANY_CHUNKS
ERROR_NOT_TICKING
ERROR_ALL_ADDED
ERROR_NONE_REMOVED
RESPONSE_ADD
RESPONSE_REMOVE

String literals (17)

"commands.forceload.toobig"
"commands.forceload.query.failure"
"commands.forceload.added.failure"
"commands.forceload.removed.failure"
"commands.forceload.added.single"
"commands.forceload.added.multiple"
"commands.forceload.removed.single"
"commands.forceload.removed.multiple"
"forceload"
"from"
"remove"
"query"
"commands.forceload.query.success"
"commands.forceload.list.single"
"commands.forceload.list.multiple"
"commands.forceload.added.none"
"commands.forceload.removed.all"

Full Source

ForceLoadCommand.java184 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.google.common.base.Joiner;
4
import com.mojang.brigadier.CommandDispatcher;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
7
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
8
import it.unimi.dsi.fastutil.longs.LongSet;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
12
import net.minecraft.commands.arguments.coordinates.ColumnPosArgument;
13
import net.minecraft.core.SectionPos;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.resources.ResourceKey;
16
import net.minecraft.server.level.ColumnPos;
17
import net.minecraft.server.level.ServerLevel;
18
import net.minecraft.world.level.ChunkPos;
19
import net.minecraft.world.level.Level;
20
21
public class ForceLoadCommand {
22
	private static final int MAX_CHUNK_LIMIT = 256;
23
	private static final Dynamic2CommandExceptionType ERROR_TOO_MANY_CHUNKS = new Dynamic2CommandExceptionType(
24
		(max, amount) -> Component.translatableEscape("commands.forceload.toobig", max, amount)
25
	);
26
	private static final Dynamic2CommandExceptionType ERROR_NOT_TICKING = new Dynamic2CommandExceptionType(
27
		(pos, dimension) -> Component.translatableEscape("commands.forceload.query.failure", pos, dimension)
28
	);
29
	private static final SimpleCommandExceptionType ERROR_ALL_ADDED = new SimpleCommandExceptionType(Component.translatable("commands.forceload.added.failure"));
30
	private static final SimpleCommandExceptionType ERROR_NONE_REMOVED = new SimpleCommandExceptionType(
31
		Component.translatable("commands.forceload.removed.failure")
32
	);
33
	private static final CommandResponseTracker.MessagesWithArg<ChunkPos, ForceLoadCommand.ForceLoadParams> RESPONSE_ADD = CommandResponseTracker.messages(
34
		ERROR_ALL_ADDED,
35
		(chunkPos, var1, params) -> Component.translatable(
36
			"commands.forceload.added.single", Component.translationArg(chunkPos), Component.translationArg(params.dimension().identifier())
37
		),
38
		(chunkCount, var1, params) -> Component.translatable(
39
			"commands.forceload.added.multiple",
40
			chunkCount,
41
			Component.translationArg(params.dimension().identifier()),
42
			Component.translationArg(params.min()),
43
			Component.translationArg(params.max())
44
		)
45
	);
46
	private static final CommandResponseTracker.MessagesWithArg<ChunkPos, ForceLoadCommand.ForceLoadParams> RESPONSE_REMOVE = CommandResponseTracker.messages(
47
		ERROR_NONE_REMOVED,
48
		(chunkPos, var1, params) -> Component.translatable(
49
			"commands.forceload.removed.single", Component.translationArg(chunkPos), Component.translationArg(params.dimension().identifier())
50
		),
51
		(chunkCount, var1, params) -> Component.translatable(
52
			"commands.forceload.removed.multiple",
53
			chunkCount,
54
			Component.translationArg(params.dimension().identifier()),
55
			Component.translationArg(params.min()),
56
			Component.translationArg(params.max())
57
		)
58
	);
59
60
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
61
		dispatcher.register(
62
			Commands.literal("forceload")
63
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
64
				.then(
65
					Commands.literal("add")
66
						.then(
67
							Commands.argument("from", ColumnPosArgument.columnPos())
68
								.executes(c -> changeForceLoad(c.getSource(), ColumnPosArgument.getColumnPos(c, "from"), ColumnPosArgument.getColumnPos(c, "from"), true))
69
								.then(
70
									Commands.argument("to", ColumnPosArgument.columnPos())
71
										.executes(c -> changeForceLoad(c.getSource(), ColumnPosArgument.getColumnPos(c, "from"), ColumnPosArgument.getColumnPos(c, "to"), true))
72
								)
73
						)
74
				)
75
				.then(
76
					Commands.literal("remove")
77
						.then(
78
							Commands.argument("from", ColumnPosArgument.columnPos())
79
								.executes(c -> changeForceLoad(c.getSource(), ColumnPosArgument.getColumnPos(c, "from"), ColumnPosArgument.getColumnPos(c, "from"), false))
80
								.then(
81
									Commands.argument("to", ColumnPosArgument.columnPos())
82
										.executes(c -> changeForceLoad(c.getSource(), ColumnPosArgument.getColumnPos(c, "from"), ColumnPosArgument.getColumnPos(c, "to"), false))
83
								)
84
						)
85
						.then(Commands.literal("all").executes(c -> removeAll(c.getSource())))
86
				)
87
				.then(
88
					Commands.literal("query")
89
						.executes(c -> listForceLoad(c.getSource()))
90
						.then(Commands.argument("pos", ColumnPosArgument.columnPos()).executes(c -> queryForceLoad(c.getSource(), ColumnPosArgument.getColumnPos(c, "pos"))))
91
				)
92
		);
93
	}
94
95
	private static int queryForceLoad(final CommandSourceStack source, final ColumnPos pos) throws CommandSyntaxException {
96
		ChunkPos chunkPos = pos.toChunkPos();
97
		ServerLevel level = source.getLevel();
98
		ResourceKey<Level> dimension = level.dimension();
99
		boolean result = level.getForceLoadedChunks().contains(chunkPos.pack());
100
		if (result) {
101
			source.sendSuccess(
102
				() -> Component.translatable("commands.forceload.query.success", Component.translationArg(chunkPos), Component.translationArg(dimension.identifier())),
103
				false
104
			);
105
			return 1;
106
		} else {
107
			throw ERROR_NOT_TICKING.create(chunkPos, dimension.identifier());
108
		}
109
	}
110
111
	private static int listForceLoad(final CommandSourceStack source) {
112
		ServerLevel level = source.getLevel();
113
		ResourceKey<Level> dimension = level.dimension();
114
		LongSet forcedChunks = level.getForceLoadedChunks();
115
		int chunkCount = forcedChunks.size();
116
		if (chunkCount > 0) {
117
			String chunkList = Joiner.on(", ").join(forcedChunks.longStream().sorted().mapToObj(ChunkPos::unpack).map(ChunkPos::toString).iterator());
118
			if (chunkCount == 1) {
119
				source.sendSuccess(() -> Component.translatable("commands.forceload.list.single", Component.translationArg(dimension.identifier()), chunkList), false);
120
			} else {
121
				source.sendSuccess(
122
					() -> Component.translatable("commands.forceload.list.multiple", chunkCount, Component.translationArg(dimension.identifier()), chunkList), false
123
				);
124
			}
125
		} else {
126
			source.sendFailure(Component.translatable("commands.forceload.added.none", Component.translationArg(dimension.identifier())));
127
		}
128
129
		return chunkCount;
130
	}
131
132
	private static int removeAll(final CommandSourceStack source) {
133
		ServerLevel level = source.getLevel();
134
		ResourceKey<Level> dimension = level.dimension();
135
		LongSet forcedChunks = level.getForceLoadedChunks();
136
		forcedChunks.forEach(chunk -> level.setChunkForced(ChunkPos.getX(chunk), ChunkPos.getZ(chunk), false));
137
		source.sendSuccess(() -> Component.translatable("commands.forceload.removed.all", Component.translationArg(dimension.identifier())), true);
138
		return 0;
139
	}
140
141
	private static int changeForceLoad(final CommandSourceStack source, final ColumnPos from, final ColumnPos to, final boolean add) throws CommandSyntaxException {
142
		int minX = Math.min(from.x(), to.x());
143
		int minZ = Math.min(from.z(), to.z());
144
		int maxX = Math.max(from.x(), to.x());
145
		int maxZ = Math.max(from.z(), to.z());
146
		if (minX >= -30000000 && minZ >= -30000000 && maxX < 30000000 && maxZ < 30000000) {
147
			int minChunkX = SectionPos.blockToSectionCoord(minX);
148
			int minChunkZ = SectionPos.blockToSectionCoord(minZ);
149
			int maxChunkX = SectionPos.blockToSectionCoord(maxX);
150
			int maxChunkZ = SectionPos.blockToSectionCoord(maxZ);
151
			long chunkCount = (maxChunkX - minChunkX + 1L) * (maxChunkZ - minChunkZ + 1L);
152
			if (chunkCount > 256L) {
153
				throw ERROR_TOO_MANY_CHUNKS.create(256, chunkCount);
154
			}
155
156
			CommandResponseTracker<ChunkPos> tracker = CommandResponseTracker.create();
157
			ServerLevel level = source.getLevel();
158
			ResourceKey<Level> dimension = level.dimension();
159
160
			for (int x = minChunkX; x <= maxChunkX; x++) {
161
				for (int z = minChunkZ; z <= maxChunkZ; z++) {
162
					tracker.track(new ChunkPos(x, z), level.setChunkForced(x, z, add));
163
				}
164
			}
165
166
			return tracker.sendFeedback(
167
				source, true, add ? RESPONSE_ADD : RESPONSE_REMOVE, new ForceLoadCommand.ForceLoadParams(dimension, minChunkX, minChunkZ, maxChunkX, maxChunkZ)
168
			);
169
		} else {
170
			throw BlockPosArgument.ERROR_OUT_OF_WORLD.create();
171
		}
172
	}
173
174
	private record ForceLoadParams(ResourceKey<Level> dimension, int minChunkX, int minChunkZ, int maxChunkX, int maxChunkZ) {
175
		public ChunkPos min() {
176
			return new ChunkPos(this.minChunkX, this.minChunkZ);
177
		}
178
179
		public ChunkPos max() {
180
			return new ChunkPos(this.maxChunkX, this.maxChunkZ);
181
		}
182
	}
183
}
184

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