ResourceOrIdArgument .java

net.minecraft.commands.arguments.ResourceOrIdArgument (class, 249 lines): members: ContextFloatProviderArgument, ContextIntProviderArgument, getDialog.

net.minecraft.commands.argumentsclass249 linescommon11.1 KB

Implements

API Surface (19)

<T, O> Grammar<ResourceOrIdArgument.Result<T, O>> createGrammar(final ResourceKey<? extends Registry<T>> registryKey, final DynamicOps<O> ops)
ResourceOrIdArgument.LootTableArgument lootTable(final CommandBuildContext context)
Holder<LootTable> getLootTable(final CommandContext<CommandSourceStack> context, final String name)
ResourceOrIdArgument.LootModifierArgument lootModifier(final CommandBuildContext context)
Holder<LootItemFunction> getLootModifier(final CommandContext<CommandSourceStack> context, final String name)
ResourceOrIdArgument.LootPredicateArgument lootPredicate(final CommandBuildContext context)
Holder<LootItemCondition> getLootPredicate(final CommandContext<CommandSourceStack> context, final String name)
ResourceOrIdArgument.ContextFloatProviderArgument floatProvider(final CommandBuildContext context)
Holder<ContextFloatProvider> getFloatProvider(final CommandContext<CommandSourceStack> context, final String name)
ResourceOrIdArgument.ContextIntProviderArgument intProvider(final CommandBuildContext context)
Holder<ContextIntProvider> getIntProvider(final CommandContext<CommandSourceStack> context, final String name)
ResourceOrIdArgument.FeatureArgument feature(final CommandBuildContext context)
Holder<Feature> getFeature(final CommandContext<CommandSourceStack> context, final String name)
ResourceOrIdArgument.DialogArgument dialog(final CommandBuildContext context)
Holder<Dialog> getDialog(final CommandContext<CommandSourceStack> context, final String name)
<S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder)
Collection<String> getExamples()
Holder<T> parse(final ImmutableStringReader reader, final HolderLookup.Provider lookup, final DynamicOps<O> ops, final Codec<T> codec, final HolderLookup.RegistryLookup<T> elementLookup)
Holder<T> parse(final ImmutableStringReader reader, final HolderLookup.Provider lookup, final DynamicOps<O> ops, final Codec<T> codec, final HolderLookup.RegistryLookup<T> elementLookup)

Fields & Constants (9)

EXAMPLES
ERROR_FAILED_TO_PARSE
ERROR_NO_SUCH_ELEMENT
OPS
registryLookup
elementLookup
codec
grammar
registryKey

String literals (4)

"foo:bar"
"argument.resource_or_id.failed_to_parse"
"argument.resource_or_id.no_such_element"
"result"

Full Source

ResourceOrIdArgument.java249 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.arguments;
2
3
import com.mojang.brigadier.ImmutableStringReader;
4
import com.mojang.brigadier.StringReader;
5
import com.mojang.brigadier.arguments.ArgumentType;
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.DynamicCommandExceptionType;
10
import com.mojang.brigadier.suggestion.Suggestions;
11
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
12
import com.mojang.serialization.Codec;
13
import com.mojang.serialization.DynamicOps;
14
import java.util.Collection;
15
import java.util.List;
16
import java.util.Optional;
17
import java.util.concurrent.CompletableFuture;
18
import net.minecraft.commands.CommandBuildContext;
19
import net.minecraft.commands.CommandSourceStack;
20
import net.minecraft.commands.SharedSuggestionProvider;
21
import net.minecraft.core.Holder;
22
import net.minecraft.core.HolderLookup;
23
import net.minecraft.core.Registry;
24
import net.minecraft.core.registries.Registries;
25
import net.minecraft.nbt.NbtOps;
26
import net.minecraft.nbt.SnbtGrammar;
27
import net.minecraft.nbt.Tag;
28
import net.minecraft.network.chat.Component;
29
import net.minecraft.resources.Identifier;
30
import net.minecraft.resources.ResourceKey;
31
import net.minecraft.server.dialog.Dialog;
32
import net.minecraft.util.parsing.packrat.Atom;
33
import net.minecraft.util.parsing.packrat.Dictionary;
34
import net.minecraft.util.parsing.packrat.NamedRule;
35
import net.minecraft.util.parsing.packrat.Term;
36
import net.minecraft.util.parsing.packrat.commands.Grammar;
37
import net.minecraft.util.parsing.packrat.commands.IdentifierParseRule;
38
import net.minecraft.world.level.levelgen.feature.Feature;
39
import net.minecraft.world.level.storage.loot.LootTable;
40
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
41
import net.minecraft.world.level.storage.loot.functions.LootItemFunctions;
42
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
43
import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProvider;
44
import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProviders;
45
import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
46
import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProviders;
47
import org.jspecify.annotations.Nullable;
48
49
public class ResourceOrIdArgument<T> implements ArgumentType<Holder<T>> {
50
	static final Collection<String> EXAMPLES = List.of("foo", "foo:bar", "012", "{}", "true");
51
	public static final DynamicCommandExceptionType ERROR_FAILED_TO_PARSE = new DynamicCommandExceptionType(
52
		error -> Component.translatableEscape("argument.resource_or_id.failed_to_parse", error)
53
	);
54
	public static final Dynamic2CommandExceptionType ERROR_NO_SUCH_ELEMENT = new Dynamic2CommandExceptionType(
55
		(id, registry) -> Component.translatableEscape("argument.resource_or_id.no_such_element", id, registry)
56
	);
57
	public static final DynamicOps<Tag> OPS = NbtOps.INSTANCE;
58
	private final HolderLookup.Provider registryLookup;
59
	private final Optional<? extends HolderLookup.RegistryLookup<T>> elementLookup;
60
	private final Codec<T> codec;
61
	private final Grammar<ResourceOrIdArgument.Result<T, Tag>> grammar;
62
	private final ResourceKey<? extends Registry<T>> registryKey;
63
64
	protected ResourceOrIdArgument(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> registryKey, final Codec<T> codec) {
65
		this.registryLookup = context;
66
		this.elementLookup = context.lookup(registryKey);
67
		this.registryKey = registryKey;
68
		this.codec = codec;
69
		this.grammar = createGrammar(registryKey, OPS);
70
	}
71
72
	public static <T, O> Grammar<ResourceOrIdArgument.Result<T, O>> createGrammar(final ResourceKey<? extends Registry<T>> registryKey, final DynamicOps<O> ops) {
73
		Grammar<O> inlineValueGrammar = SnbtGrammar.createParser(ops);
74
		Dictionary<StringReader> rules = new Dictionary<>();
75
		Atom<ResourceOrIdArgument.Result<T, O>> result = Atom.of("result");
76
		Atom<Identifier> id = Atom.of("id");
77
		Atom<O> value = Atom.of("value");
78
		rules.put(id, IdentifierParseRule.INSTANCE);
79
		rules.put(value, inlineValueGrammar.top().value());
80
		NamedRule<StringReader, ResourceOrIdArgument.Result<T, O>> topRule = rules.put(result, Term.alternative(rules.named(id), rules.named(value)), scope -> {
81
			Identifier parsedId = scope.get(id);
82
			if (parsedId != null) {
83
				return new ResourceOrIdArgument.ReferenceResult<>(ResourceKey.create(registryKey, parsedId));
84
			}
85
86
			O parsedInline = scope.getOrThrow(value);
87
			return new ResourceOrIdArgument.InlineResult<>(parsedInline);
88
		});
89
		return new Grammar<>(rules, topRule);
90
	}
91
92
	public static ResourceOrIdArgument.LootTableArgument lootTable(final CommandBuildContext context) {
93
		return new ResourceOrIdArgument.LootTableArgument(context);
94
	}
95
96
	public static Holder<LootTable> getLootTable(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
97
		return getResource(context, name);
98
	}
99
100
	public static ResourceOrIdArgument.LootModifierArgument lootModifier(final CommandBuildContext context) {
101
		return new ResourceOrIdArgument.LootModifierArgument(context);
102
	}
103
104
	public static Holder<LootItemFunction> getLootModifier(final CommandContext<CommandSourceStack> context, final String name) {
105
		return getResource(context, name);
106
	}
107
108
	public static ResourceOrIdArgument.LootPredicateArgument lootPredicate(final CommandBuildContext context) {
109
		return new ResourceOrIdArgument.LootPredicateArgument(context);
110
	}
111
112
	public static Holder<LootItemCondition> getLootPredicate(final CommandContext<CommandSourceStack> context, final String name) {
113
		return getResource(context, name);
114
	}
115
116
	public static ResourceOrIdArgument.ContextFloatProviderArgument floatProvider(final CommandBuildContext context) {
117
		return new ResourceOrIdArgument.ContextFloatProviderArgument(context);
118
	}
119
120
	public static Holder<ContextFloatProvider> getFloatProvider(final CommandContext<CommandSourceStack> context, final String name) {
121
		return getResource(context, name);
122
	}
123
124
	public static ResourceOrIdArgument.ContextIntProviderArgument intProvider(final CommandBuildContext context) {
125
		return new ResourceOrIdArgument.ContextIntProviderArgument(context);
126
	}
127
128
	public static Holder<ContextIntProvider> getIntProvider(final CommandContext<CommandSourceStack> context, final String name) {
129
		return getResource(context, name);
130
	}
131
132
	public static ResourceOrIdArgument.FeatureArgument feature(final CommandBuildContext context) {
133
		return new ResourceOrIdArgument.FeatureArgument(context);
134
	}
135
136
	public static Holder<Feature> getFeature(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
137
		return getResource(context, name);
138
	}
139
140
	public static ResourceOrIdArgument.DialogArgument dialog(final CommandBuildContext context) {
141
		return new ResourceOrIdArgument.DialogArgument(context);
142
	}
143
144
	public static Holder<Dialog> getDialog(final CommandContext<CommandSourceStack> context, final String name) {
145
		return getResource(context, name);
146
	}
147
148
	private static <T> Holder<T> getResource(final CommandContext<CommandSourceStack> context, final String name) {
149
		return context.getArgument(name, Holder.class);
150
	}
151
152
	public @Nullable Holder<T> parse(final StringReader reader) throws CommandSyntaxException {
153
		return this.parse(reader, this.grammar, OPS);
154
	}
155
156
	private <O> @Nullable Holder<T> parse(final StringReader reader, final Grammar<ResourceOrIdArgument.Result<T, O>> grammar, final DynamicOps<O> ops) throws CommandSyntaxException {
157
		ResourceOrIdArgument.Result<T, O> contents = grammar.parseForCommands(reader);
158
		return this.elementLookup.isEmpty()
159
			? null
160
			: contents.parse(reader, this.registryLookup, ops, this.codec, (HolderLookup.RegistryLookup<T>)this.elementLookup.get());
161
	}
162
163
	@Override
164
	public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
165
		return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ELEMENTS);
166
	}
167
168
	@Override
169
	public Collection<String> getExamples() {
170
		return EXAMPLES;
171
	}
172
173
	public static class ContextFloatProviderArgument extends ResourceOrIdArgument<ContextFloatProvider> {
174
		protected ContextFloatProviderArgument(final CommandBuildContext context) {
175
			super(context, Registries.CONTEXT_FLOAT_PROVIDER, ContextFloatProviders.DIRECT_CODEC);
176
		}
177
	}
178
179
	public static class ContextIntProviderArgument extends ResourceOrIdArgument<ContextIntProvider> {
180
		protected ContextIntProviderArgument(final CommandBuildContext context) {
181
			super(context, Registries.CONTEXT_INT_PROVIDER, ContextIntProviders.DIRECT_CODEC);
182
		}
183
	}
184
185
	public static class DialogArgument extends ResourceOrIdArgument<Dialog> {
186
		protected DialogArgument(final CommandBuildContext context) {
187
			super(context, Registries.DIALOG, Dialog.DIRECT_CODEC);
188
		}
189
	}
190
191
	public static class FeatureArgument extends ResourceOrIdArgument<Feature> {
192
		protected FeatureArgument(final CommandBuildContext context) {
193
			super(context, Registries.FEATURE, Feature.DIRECT_CODEC);
194
		}
195
	}
196
197
	public record InlineResult<T, O>(O value) implements ResourceOrIdArgument.Result<T, O> {
198
		@Override
199
		public Holder<T> parse(
200
			final ImmutableStringReader reader,
201
			final HolderLookup.Provider lookup,
202
			final DynamicOps<O> ops,
203
			final Codec<T> codec,
204
			final HolderLookup.RegistryLookup<T> elementLookup
205
		) throws CommandSyntaxException {
206
			return Holder.direct(
207
				codec.parse(lookup.createSerializationContext(ops), this.value)
208
					.getOrThrow(msg -> ResourceOrIdArgument.ERROR_FAILED_TO_PARSE.createWithContext(reader, msg))
209
			);
210
		}
211
	}
212
213
	public static class LootModifierArgument extends ResourceOrIdArgument<LootItemFunction> {
214
		protected LootModifierArgument(final CommandBuildContext context) {
215
			super(context, Registries.ITEM_MODIFIER, LootItemFunctions.DIRECT_CODEC);
216
		}
217
	}
218
219
	public static class LootPredicateArgument extends ResourceOrIdArgument<LootItemCondition> {
220
		protected LootPredicateArgument(final CommandBuildContext context) {
221
			super(context, Registries.PREDICATE, LootItemCondition.DIRECT_CODEC);
222
		}
223
	}
224
225
	public static class LootTableArgument extends ResourceOrIdArgument<LootTable> {
226
		protected LootTableArgument(final CommandBuildContext context) {
227
			super(context, Registries.LOOT_TABLE, LootTable.DIRECT_CODEC);
228
		}
229
	}
230
231
	public record ReferenceResult<T, O>(ResourceKey<T> key) implements ResourceOrIdArgument.Result<T, O> {
232
		@Override
233
		public Holder<T> parse(
234
			final ImmutableStringReader reader,
235
			final HolderLookup.Provider lookup,
236
			final DynamicOps<O> ops,
237
			final Codec<T> codec,
238
			final HolderLookup.RegistryLookup<T> elementLookup
239
		) throws CommandSyntaxException {
240
			return elementLookup.get(this.key)
241
				.orElseThrow(() -> ResourceOrIdArgument.ERROR_NO_SUCH_ELEMENT.createWithContext(reader, this.key.identifier(), this.key.registry()));
242
		}
243
	}
244
245
	public sealed interface Result<T, O> permits ResourceOrIdArgument.InlineResult, ResourceOrIdArgument.ReferenceResult {
246
		Holder<T> parse(ImmutableStringReader reader, HolderLookup.Provider lookup, DynamicOps<O> ops, Codec<T> codec, HolderLookup.RegistryLookup<T> elementLookup) throws CommandSyntaxException;
247
	}
248
}
249

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