MacroFunction .java

net.minecraft.commands.functions.MacroFunction (class): members: PlainTextEntry, MacroEntry, MAX_CACHE_ENTRIES, DECIMAL_FORMAT, instantiate, parameters.

net.minecraft.commands.functionsclass165 linescommonextends ExecutionCommandSource<T>>6.2 KB

Implements

API Surface (6)

Identifier id()
InstantiatedFunction<T> instantiate(final @Nullable CompoundTag arguments, final CommandDispatcher<T> dispatcher)
IntList parameters()
UnboundEntryAction<T> instantiate(final List<String> substitutions, final CommandDispatcher<T> dispatcher, final Identifier functionId)
IntList parameters()
UnboundEntryAction<T> instantiate(final List<String> substitutions, final CommandDispatcher<T> dispatcher, final Identifier functionId)

Fields & Constants (6)

DECIMAL_FORMAT
MAX_CACHE_ENTRIES
parameters
cache
id
entries

String literals (3)

"commands.function.error.missing_arguments"
"commands.function.error.missing_argument"
"commands.function.error.parse"

Full Source

MacroFunction.java165 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.functions;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.StringReader;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import it.unimi.dsi.fastutil.ints.IntList;
7
import it.unimi.dsi.fastutil.ints.IntLists;
8
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
9
import java.text.DecimalFormat;
10
import java.text.DecimalFormatSymbols;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Locale;
14
import net.minecraft.commands.ExecutionCommandSource;
15
import net.minecraft.commands.FunctionInstantiationException;
16
import net.minecraft.commands.execution.UnboundEntryAction;
17
import net.minecraft.nbt.ByteTag;
18
import net.minecraft.nbt.CompoundTag;
19
import net.minecraft.nbt.DoubleTag;
20
import net.minecraft.nbt.FloatTag;
21
import net.minecraft.nbt.LongTag;
22
import net.minecraft.nbt.ShortTag;
23
import net.minecraft.nbt.StringTag;
24
import net.minecraft.nbt.Tag;
25
import net.minecraft.network.chat.Component;
26
import net.minecraft.resources.Identifier;
27
import net.minecraft.util.Util;
28
import org.jspecify.annotations.Nullable;
29
30
public class MacroFunction<T extends ExecutionCommandSource<T>> implements CommandFunction<T> {
31
	private static final DecimalFormat DECIMAL_FORMAT = Util.make(
32
		new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ROOT)), format -> format.setMaximumFractionDigits(15)
33
	);
34
	private static final int MAX_CACHE_ENTRIES = 8;
35
	private final List<String> parameters;
36
	private final Object2ObjectLinkedOpenHashMap<List<String>, InstantiatedFunction<T>> cache = new Object2ObjectLinkedOpenHashMap<>(8, 0.25F);
37
	private final Identifier id;
38
	private final List<MacroFunction.Entry<T>> entries;
39
40
	public MacroFunction(final Identifier id, final List<MacroFunction.Entry<T>> entries, final List<String> parameters) {
41
		this.id = id;
42
		this.entries = entries;
43
		this.parameters = parameters;
44
	}
45
46
	@Override
47
	public Identifier id() {
48
		return this.id;
49
	}
50
51
	@Override
52
	public InstantiatedFunction<T> instantiate(final @Nullable CompoundTag arguments, final CommandDispatcher<T> dispatcher) throws FunctionInstantiationException {
53
		if (arguments == null) {
54
			throw new FunctionInstantiationException(Component.translatable("commands.function.error.missing_arguments", Component.translationArg(this.id())));
55
		}
56
57
		List<String> parameterValues = new ArrayList<>(this.parameters.size());
58
59
		for (String argument : this.parameters) {
60
			Tag argumentValue = arguments.get(argument);
61
			if (argumentValue == null) {
62
				throw new FunctionInstantiationException(Component.translatable("commands.function.error.missing_argument", Component.translationArg(this.id()), argument));
63
			}
64
65
			parameterValues.add(stringify(argumentValue));
66
		}
67
68
		InstantiatedFunction<T> cachedFunction = this.cache.getAndMoveToLast(parameterValues);
69
		if (cachedFunction != null) {
70
			return cachedFunction;
71
		}
72
73
		if (this.cache.size() >= 8) {
74
			this.cache.removeFirst();
75
		}
76
77
		InstantiatedFunction<T> function = this.substituteAndParse(this.parameters, parameterValues, dispatcher);
78
		this.cache.put(parameterValues, function);
79
		return function;
80
	}
81
82
	private static String stringify(final Tag tag) {
83
		return switch (tag) {
84
			case FloatTag(float value) -> DECIMAL_FORMAT.format(value);
85
			case DoubleTag(double value) -> DECIMAL_FORMAT.format(value);
86
			case ByteTag(byte value) -> String.valueOf(value);
87
			case ShortTag(short value) -> String.valueOf(value);
88
			case LongTag(long value) -> String.valueOf(value);
89
			case StringTag(String value) -> value;
90
			default -> tag.toString();
91
		};
92
	}
93
94
	private static void lookupValues(final List<String> values, final IntList indicesToSelect, final List<String> selectedValuesOutput) {
95
		selectedValuesOutput.clear();
96
		indicesToSelect.forEach(index -> selectedValuesOutput.add(values.get(index)));
97
	}
98
99
	private InstantiatedFunction<T> substituteAndParse(final List<String> keys, final List<String> values, final CommandDispatcher<T> dispatcher) throws FunctionInstantiationException {
100
		List<UnboundEntryAction<T>> newEntries = new ArrayList<>(this.entries.size());
101
		List<String> entryArguments = new ArrayList<>(values.size());
102
103
		for (MacroFunction.Entry<T> entry : this.entries) {
104
			lookupValues(values, entry.parameters(), entryArguments);
105
			newEntries.add(entry.instantiate(entryArguments, dispatcher, this.id));
106
		}
107
108
		return new PlainTextFunction<>(this.id().withPath(id -> id + "/" + keys.hashCode()), newEntries);
109
	}
110
111
	interface Entry<T> {
112
		IntList parameters();
113
114
		UnboundEntryAction<T> instantiate(List<String> substitutions, CommandDispatcher<T> dispatcher, Identifier funtionId) throws FunctionInstantiationException;
115
	}
116
117
	static class MacroEntry<T extends ExecutionCommandSource<T>> implements MacroFunction.Entry<T> {
118
		private final StringTemplate template;
119
		private final IntList parameters;
120
		private final T compilationContext;
121
122
		public MacroEntry(final StringTemplate template, final IntList parameters, final T compilationContext) {
123
			this.template = template;
124
			this.parameters = parameters;
125
			this.compilationContext = compilationContext;
126
		}
127
128
		@Override
129
		public IntList parameters() {
130
			return this.parameters;
131
		}
132
133
		@Override
134
		public UnboundEntryAction<T> instantiate(final List<String> substitutions, final CommandDispatcher<T> dispatcher, final Identifier functionId) throws FunctionInstantiationException {
135
			String command = this.template.substitute(substitutions);
136
137
			try {
138
				return CommandFunction.parseCommand(dispatcher, this.compilationContext, new StringReader(command));
139
			} catch (CommandSyntaxException e) {
140
				throw new FunctionInstantiationException(
141
					Component.translatable("commands.function.error.parse", Component.translationArg(functionId), command, e.getMessage())
142
				);
143
			}
144
		}
145
	}
146
147
	static class PlainTextEntry<T> implements MacroFunction.Entry<T> {
148
		private final UnboundEntryAction<T> compiledAction;
149
150
		public PlainTextEntry(final UnboundEntryAction<T> compiledAction) {
151
			this.compiledAction = compiledAction;
152
		}
153
154
		@Override
155
		public IntList parameters() {
156
			return IntLists.emptyList();
157
		}
158
159
		@Override
160
		public UnboundEntryAction<T> instantiate(final List<String> substitutions, final CommandDispatcher<T> dispatcher, final Identifier functionId) {
161
			return this.compiledAction;
162
		}
163
	}
164
}
165

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