CommandFunction .java

net.minecraft.commands.functions.CommandFunction (interface): members: shouldConcatenateNextLine, checkCommandLineLength, compilationContext, parseCommand.

net.minecraft.commands.functionsinterface109 linescommon4.0 KB

API Surface (0)

No public/protected methods detected (fields or constants only).

Fields & Constants (1)

compilationContext

String literals (8)

"Line continuation at end of file"
"Unknown or invalid command '"
"' on line "
" (did you mean '"
"'? Do not use a preceding forwards slash.)"
"Whilst parsing command on line "
"Command too long: "
" characters, contents: "

Full Source

CommandFunction.java109 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.functions;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.ParseResults;
5
import com.mojang.brigadier.StringReader;
6
import com.mojang.brigadier.context.ContextChain;
7
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8
import java.util.List;
9
import java.util.Optional;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.commands.ExecutionCommandSource;
12
import net.minecraft.commands.FunctionInstantiationException;
13
import net.minecraft.commands.execution.UnboundEntryAction;
14
import net.minecraft.commands.execution.tasks.BuildContexts;
15
import net.minecraft.nbt.CompoundTag;
16
import net.minecraft.resources.Identifier;
17
import org.jspecify.annotations.Nullable;
18
19
public interface CommandFunction<T> {
20
	Identifier id();
21
22
	InstantiatedFunction<T> instantiate(@Nullable CompoundTag arguments, CommandDispatcher<T> dispatcher) throws FunctionInstantiationException;
23
24
	private static boolean shouldConcatenateNextLine(final CharSequence line) {
25
		int length = line.length();
26
		return length > 0 && line.charAt(length - 1) == '\\';
27
	}
28
29
	static <T extends ExecutionCommandSource<T>> CommandFunction<T> fromLines(
30
		final Identifier id, final CommandDispatcher<T> dispatcher, final T compilationContext, final List<String> lines
31
	) {
32
		FunctionBuilder<T> functionBuilder = new FunctionBuilder<>();
33
34
		for (int i = 0; i < lines.size(); i++) {
35
			int lineNumber = i + 1;
36
			String inputLine = lines.get(i).trim();
37
			String line;
38
			if (shouldConcatenateNextLine(inputLine)) {
39
				StringBuilder builder = new StringBuilder(inputLine);
40
41
				do {
42
					if (++i == lines.size()) {
43
						throw new IllegalArgumentException("Line continuation at end of file");
44
					}
45
46
					builder.deleteCharAt(builder.length() - 1);
47
					String innerLine = lines.get(i).trim();
48
					builder.append(innerLine);
49
					checkCommandLineLength(builder);
50
				} while (shouldConcatenateNextLine(builder));
51
52
				line = builder.toString();
53
			} else {
54
				line = inputLine;
55
			}
56
57
			checkCommandLineLength(line);
58
			StringReader input = new StringReader(line);
59
			if (input.canRead() && input.peek() != '#') {
60
				if (input.peek() == '/') {
61
					input.skip();
62
					if (input.peek() == '/') {
63
						throw new IllegalArgumentException(
64
							"Unknown or invalid command '" + line + "' on line " + lineNumber + " (if you intended to make a comment, use '#' not '//')"
65
						);
66
					}
67
68
					String name = input.readUnquotedString();
69
					throw new IllegalArgumentException(
70
						"Unknown or invalid command '" + line + "' on line " + lineNumber + " (did you mean '" + name + "'? Do not use a preceding forwards slash.)"
71
					);
72
				}
73
74
				if (input.peek() == '$') {
75
					functionBuilder.addMacro(line.substring(1), lineNumber, compilationContext);
76
				} else {
77
					try {
78
						functionBuilder.addCommand(parseCommand(dispatcher, compilationContext, input));
79
					} catch (CommandSyntaxException e) {
80
						throw new IllegalArgumentException("Whilst parsing command on line " + lineNumber + ": " + e.getMessage());
81
					}
82
				}
83
			}
84
		}
85
86
		return functionBuilder.build(id);
87
	}
88
89
	static void checkCommandLineLength(final CharSequence line) {
90
		if (line.length() > 2000000) {
91
			CharSequence truncated = line.subSequence(0, Math.min(512, 2000000));
92
			throw new IllegalStateException("Command too long: " + line.length() + " characters, contents: " + truncated + "...");
93
		}
94
	}
95
96
	static <T extends ExecutionCommandSource<T>> UnboundEntryAction<T> parseCommand(
97
		final CommandDispatcher<T> dispatcher, final T compilationContext, final StringReader input
98
	) throws CommandSyntaxException {
99
		ParseResults<T> parse = dispatcher.parse(input, compilationContext);
100
		Commands.validateParseResults(parse);
101
		Optional<ContextChain<T>> commandChain = ContextChain.tryFlatten(parse.getContext().build(input.getString()));
102
		if (commandChain.isEmpty()) {
103
			throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader());
104
		} else {
105
			return new BuildContexts.Unbound<>(input.getString(), commandChain.get());
106
		}
107
	}
108
}
109

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