ClientboundCommandsPacket 0x10

ClientboundCommandsPacket: Minecraft 26.3 packet (0x10 Play clientbound). Wire name 'commands', handled by ClientPacketListener.handleCommands().

Minecraft 26.3PlayClientboundnet.minecraft.network.protocol.game

Protocol slots (1)

IDdecStateDirectionRegistry
0x1016PLAYClientboundGamePacketTypes.CLIENTBOUND_COMMANDS: "commands"

Packet ids are positional: the id is the index of the addPacket call in the protocol builder (see ProtocolInfoBuilder.listPackets). Ids change between game versions.

Wire fields (0)

FieldType
No wire fields (marker packet).

Stream codec

static final StreamCodec<FriendlyByteBuf, ClientboundCommandsPacket.Entry> ENTRY_STREAM_CODEC = StreamCodec.of( (output, entry) -> entry.write(output), ClientboundCommandsPacket::readNode );

Modern packets serialize through StreamCodecs instead of read/write methods.

Handling

Resolved on the ClientGamePacketListener packet-listener interface by void handleCommands(ClientboundCommandsPacket clientboundcommandspacket). The implementation in the game is ClientPacketListener.

Full Source

ClientboundCommandsPacket.java341 lines · Minecraft 26.3 · verbatimSource page ↗
1
package net.minecraft.network.protocol.game;
2
3
import com.mojang.brigadier.arguments.ArgumentType;
4
import com.mojang.brigadier.builder.ArgumentBuilder;
5
import com.mojang.brigadier.tree.ArgumentCommandNode;
6
import com.mojang.brigadier.tree.CommandNode;
7
import com.mojang.brigadier.tree.LiteralCommandNode;
8
import com.mojang.brigadier.tree.RootCommandNode;
9
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
10
import it.unimi.dsi.fastutil.ints.IntSet;
11
import it.unimi.dsi.fastutil.ints.IntSets;
12
import it.unimi.dsi.fastutil.objects.Object2IntMap;
13
import it.unimi.dsi.fastutil.objects.Object2IntMaps;
14
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
15
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
16
import java.util.ArrayDeque;
17
import java.util.List;
18
import java.util.Queue;
19
import java.util.function.BiPredicate;
20
import net.minecraft.commands.CommandBuildContext;
21
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
22
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
23
import net.minecraft.core.registries.BuiltInRegistries;
24
import net.minecraft.network.FriendlyByteBuf;
25
import net.minecraft.network.codec.ByteBufCodecs;
26
import net.minecraft.network.codec.StreamCodec;
27
import net.minecraft.network.protocol.Packet;
28
import net.minecraft.network.protocol.PacketType;
29
import net.minecraft.resources.Identifier;
30
import org.jspecify.annotations.Nullable;
31
32
public class ClientboundCommandsPacket implements Packet<ClientGamePacketListener> {
33
	private static final StreamCodec<FriendlyByteBuf, ClientboundCommandsPacket.Entry> ENTRY_STREAM_CODEC = StreamCodec.of(
34
		(output, entry) -> entry.write(output), ClientboundCommandsPacket::readNode
35
	);
36
	public static final StreamCodec<FriendlyByteBuf, ClientboundCommandsPacket> STREAM_CODEC = StreamCodec.composite(
37
		ENTRY_STREAM_CODEC.apply(ByteBufCodecs.list()), p -> p.entries, ByteBufCodecs.VAR_INT, p -> p.rootIndex, ClientboundCommandsPacket::new
38
	);
39
	private static final byte MASK_TYPE = 3;
40
	private static final byte FLAG_EXECUTABLE = 4;
41
	private static final byte FLAG_REDIRECT = 8;
42
	private static final byte FLAG_CUSTOM_SUGGESTIONS = 16;
43
	private static final byte FLAG_RESTRICTED = 32;
44
	private static final byte TYPE_ROOT = 0;
45
	private static final byte TYPE_LITERAL = 1;
46
	private static final byte TYPE_ARGUMENT = 2;
47
	private final int rootIndex;
48
	private final List<ClientboundCommandsPacket.Entry> entries;
49
50
	public <S> ClientboundCommandsPacket(final RootCommandNode<S> root, final ClientboundCommandsPacket.NodeInspector<S> inspector) {
51
		Object2IntMap<CommandNode<S>> nodeToId = enumerateNodes(root);
52
		this.entries = createEntries(nodeToId, inspector);
53
		this.rootIndex = nodeToId.getInt(root);
54
	}
55
56
	private ClientboundCommandsPacket(final List<ClientboundCommandsPacket.Entry> entries, final int rootIndex) {
57
		this.entries = entries;
58
		this.rootIndex = rootIndex;
59
		validateEntries(this.entries);
60
	}
61
62
	private static void validateEntries(final List<ClientboundCommandsPacket.Entry> entries, final BiPredicate<ClientboundCommandsPacket.Entry, IntSet> validator) {
63
		IntSet elementsToCheck = new IntOpenHashSet(IntSets.fromTo(0, entries.size()));
64
65
		while (!elementsToCheck.isEmpty()) {
66
			boolean worked = elementsToCheck.removeIf(index -> validator.test(entries.get(index), elementsToCheck));
67
			if (!worked) {
68
				throw new IllegalStateException("Server sent an impossible command tree");
69
			}
70
		}
71
	}
72
73
	private static void validateEntries(final List<ClientboundCommandsPacket.Entry> entries) {
74
		validateEntries(entries, ClientboundCommandsPacket.Entry::canBuild);
75
		validateEntries(entries, ClientboundCommandsPacket.Entry::canResolve);
76
	}
77
78
	private static <S> Object2IntMap<CommandNode<S>> enumerateNodes(final RootCommandNode<S> root) {
79
		Object2IntMap<CommandNode<S>> nodeToId = new Object2IntOpenHashMap<>();
80
		Queue<CommandNode<S>> queue = new ArrayDeque<>();
81
		queue.add(root);
82
83
		CommandNode<S> node;
84
		while ((node = queue.poll()) != null) {
85
			if (!nodeToId.containsKey(node)) {
86
				int id = nodeToId.size();
87
				nodeToId.put(node, id);
88
				queue.addAll(node.getChildren());
89
				if (node.getRedirect() != null) {
90
					queue.add(node.getRedirect());
91
				}
92
			}
93
		}
94
95
		return nodeToId;
96
	}
97
98
	private static <S> List<ClientboundCommandsPacket.Entry> createEntries(
99
		final Object2IntMap<CommandNode<S>> nodeToId, final ClientboundCommandsPacket.NodeInspector<S> inspector
100
	) {
101
		ObjectArrayList<ClientboundCommandsPacket.Entry> result = new ObjectArrayList<>(nodeToId.size());
102
		result.size(nodeToId.size());
103
104
		for (Object2IntMap.Entry<CommandNode<S>> entry : Object2IntMaps.fastIterable(nodeToId)) {
105
			result.set(entry.getIntValue(), createEntry(entry.getKey(), inspector, nodeToId));
106
		}
107
108
		return result;
109
	}
110
111
	private static ClientboundCommandsPacket.Entry readNode(final FriendlyByteBuf input) {
112
		byte flags = input.readByte();
113
		int[] children = input.readVarIntArray();
114
		int redirect = (flags & 8) != 0 ? input.readVarInt() : 0;
115
		ClientboundCommandsPacket.NodeStub stub = read(input, flags);
116
		return new ClientboundCommandsPacket.Entry(stub, flags, redirect, children);
117
	}
118
119
	private static ClientboundCommandsPacket.@Nullable NodeStub read(final FriendlyByteBuf input, final byte flags) {
120
		int type = flags & 3;
121
		if (type == 2) {
122
			String name = input.readUtf();
123
			int id = input.readVarInt();
124
			ArgumentTypeInfo<?, ?> argumentType = BuiltInRegistries.COMMAND_ARGUMENT_TYPE.byId(id);
125
			if (argumentType == null) {
126
				return null;
127
			}
128
129
			ArgumentTypeInfo.Template<?> argument = argumentType.deserializeFromNetwork(input);
130
			Identifier suggestionId = (flags & 16) != 0 ? input.readIdentifier() : null;
131
			return new ClientboundCommandsPacket.ArgumentNodeStub(name, argument, suggestionId);
132
		} else if (type == 1) {
133
			String id = input.readUtf();
134
			return new ClientboundCommandsPacket.LiteralNodeStub(id);
135
		} else {
136
			return null;
137
		}
138
	}
139
140
	private static <S> ClientboundCommandsPacket.Entry createEntry(
141
		final CommandNode<S> node, final ClientboundCommandsPacket.NodeInspector<S> inspector, final Object2IntMap<CommandNode<S>> ids
142
	) {
143
		int flags = 0;
144
		int redirect;
145
		if (node.getRedirect() != null) {
146
			flags |= 8;
147
			redirect = ids.getInt(node.getRedirect());
148
		} else {
149
			redirect = 0;
150
		}
151
152
		if (inspector.isExecutable(node)) {
153
			flags |= 4;
154
		}
155
156
		if (inspector.isRestricted(node)) {
157
			flags |= 32;
158
		}
159
160
		ClientboundCommandsPacket.NodeStub nodeStub;
161
		switch (node) {
162
			case RootCommandNode<S> ignored:
163
				flags |= 0;
164
				nodeStub = null;
165
				break;
166
			case ArgumentCommandNode<S, ?> arg:
167
				Identifier suggestionId = inspector.suggestionId(arg);
168
				nodeStub = new ClientboundCommandsPacket.ArgumentNodeStub(arg.getName(), ArgumentTypeInfos.unpack(arg.getType()), suggestionId);
169
				flags |= 2;
170
				if (suggestionId != null) {
171
					flags |= 16;
172
				}
173
				break;
174
			case LiteralCommandNode<S> literal:
175
				nodeStub = new ClientboundCommandsPacket.LiteralNodeStub(literal.getLiteral());
176
				flags |= 1;
177
				break;
178
			default:
179
				throw new UnsupportedOperationException("Unknown node type " + node);
180
		}
181
182
		int[] childrenIds = node.getChildren().stream().mapToInt(ids::getInt).toArray();
183
		return new ClientboundCommandsPacket.Entry(nodeStub, flags, redirect, childrenIds);
184
	}
185
186
	@Override
187
	public PacketType<ClientboundCommandsPacket> type() {
188
		return GamePacketTypes.CLIENTBOUND_COMMANDS;
189
	}
190
191
	public void handle(final ClientGamePacketListener listener) {
192
		listener.handleCommands(this);
193
	}
194
195
	public <S> RootCommandNode<S> getRoot(final CommandBuildContext context, final ClientboundCommandsPacket.NodeBuilder<S> builder) {
196
		return (RootCommandNode<S>)new ClientboundCommandsPacket.NodeResolver<>(context, builder, this.entries).resolve(this.rootIndex);
197
	}
198
199
	private record ArgumentNodeStub(String id, ArgumentTypeInfo.Template<?> argumentType, @Nullable Identifier suggestionId)
200
		implements ClientboundCommandsPacket.NodeStub {
201
		@Override
202
		public <S> ArgumentBuilder<S, ?> build(final CommandBuildContext context, final ClientboundCommandsPacket.NodeBuilder<S> builder) {
203
			ArgumentType<?> type = this.argumentType.instantiate(context);
204
			return builder.createArgument(this.id, type, this.suggestionId);
205
		}
206
207
		@Override
208
		public void write(final FriendlyByteBuf output) {
209
			output.writeUtf(this.id);
210
			serializeCap(output, this.argumentType);
211
			if (this.suggestionId != null) {
212
				output.writeIdentifier(this.suggestionId);
213
			}
214
		}
215
216
		private static <A extends ArgumentType<?>> void serializeCap(final FriendlyByteBuf output, final ArgumentTypeInfo.Template<A> argumentType) {
217
			serializeCap(output, argumentType.type(), argumentType);
218
		}
219
220
		private static <A extends ArgumentType<?>, T extends ArgumentTypeInfo.Template<A>> void serializeCap(
221
			final FriendlyByteBuf output, final ArgumentTypeInfo<A, T> info, final ArgumentTypeInfo.Template<A> argumentType
222
		) {
223
			output.writeVarInt(BuiltInRegistries.COMMAND_ARGUMENT_TYPE.getId(info));
224
			info.serializeToNetwork((T)argumentType, output);
225
		}
226
	}
227
228
	private record Entry(ClientboundCommandsPacket.@Nullable NodeStub stub, int flags, int redirect, int[] children) {
229
		public void write(final FriendlyByteBuf output) {
230
			output.writeByte(this.flags);
231
			output.writeVarIntArray(this.children);
232
			if ((this.flags & 8) != 0) {
233
				output.writeVarInt(this.redirect);
234
			}
235
236
			if (this.stub != null) {
237
				this.stub.write(output);
238
			}
239
		}
240
241
		public boolean canBuild(final IntSet unbuiltNodes) {
242
			return (this.flags & 8) != 0 ? !unbuiltNodes.contains(this.redirect) : true;
243
		}
244
245
		public boolean canResolve(final IntSet unresolvedNodes) {
246
			for (int child : this.children) {
247
				if (unresolvedNodes.contains(child)) {
248
					return false;
249
				}
250
			}
251
252
			return true;
253
		}
254
	}
255
256
	private record LiteralNodeStub(String id) implements ClientboundCommandsPacket.NodeStub {
257
		@Override
258
		public <S> ArgumentBuilder<S, ?> build(final CommandBuildContext context, final ClientboundCommandsPacket.NodeBuilder<S> builder) {
259
			return builder.createLiteral(this.id);
260
		}
261
262
		@Override
263
		public void write(final FriendlyByteBuf output) {
264
			output.writeUtf(this.id);
265
		}
266
	}
267
268
	public interface NodeBuilder<S> {
269
		ArgumentBuilder<S, ?> createLiteral(String id);
270
271
		ArgumentBuilder<S, ?> createArgument(String id, ArgumentType<?> argumentType, @Nullable Identifier suggestionId);
272
273
		ArgumentBuilder<S, ?> configure(ArgumentBuilder<S, ?> input, boolean executable, boolean restricted);
274
	}
275
276
	public interface NodeInspector<S> {
277
		@Nullable Identifier suggestionId(ArgumentCommandNode<S, ?> node);
278
279
		boolean isExecutable(CommandNode<S> node);
280
281
		boolean isRestricted(CommandNode<S> node);
282
	}
283
284
	private static class NodeResolver<S> {
285
		private final CommandBuildContext context;
286
		private final ClientboundCommandsPacket.NodeBuilder<S> builder;
287
		private final List<ClientboundCommandsPacket.Entry> entries;
288
		private final List<CommandNode<S>> nodes;
289
290
		private NodeResolver(
291
			final CommandBuildContext context, final ClientboundCommandsPacket.NodeBuilder<S> builder, final List<ClientboundCommandsPacket.Entry> entries
292
		) {
293
			this.context = context;
294
			this.builder = builder;
295
			this.entries = entries;
296
			ObjectArrayList<CommandNode<S>> nodes = new ObjectArrayList<>();
297
			nodes.size(entries.size());
298
			this.nodes = nodes;
299
		}
300
301
		public CommandNode<S> resolve(final int index) {
302
			CommandNode<S> currentNode = this.nodes.get(index);
303
			if (currentNode != null) {
304
				return currentNode;
305
			}
306
307
			ClientboundCommandsPacket.Entry entry = this.entries.get(index);
308
			CommandNode<S> result;
309
			if (entry.stub == null) {
310
				result = new RootCommandNode<>();
311
			} else {
312
				ArgumentBuilder<S, ?> resultBuilder = entry.stub.build(this.context, this.builder);
313
				if ((entry.flags & 8) != 0) {
314
					resultBuilder.redirect(this.resolve(entry.redirect));
315
				}
316
317
				boolean isExecutable = (entry.flags & 4) != 0;
318
				boolean isRestricted = (entry.flags & 32) != 0;
319
				result = this.builder.configure(resultBuilder, isExecutable, isRestricted).build();
320
			}
321
322
			this.nodes.set(index, result);
323
324
			for (int childId : entry.children) {
325
				CommandNode<S> child = this.resolve(childId);
326
				if (!(child instanceof RootCommandNode)) {
327
					result.addChild(child);
328
				}
329
			}
330
331
			return result;
332
		}
333
	}
334
335
	private interface NodeStub {
336
		<S> ArgumentBuilder<S, ?> build(CommandBuildContext context, ClientboundCommandsPacket.NodeBuilder<S> builder);
337
338
		void write(FriendlyByteBuf output);
339
	}
340
}
341