ResourceOrTagArgument .java

net.minecraft.commands.arguments.ResourceOrTagArgument (class): members: ERROR_INVALID_TAG_TYPE, getResourceOrTag, ResourceResult, resourceOrTag, Info.

net.minecraft.commands.argumentsclass187 linescommon7.6 KB

Implements

API Surface (19)

<T> ResourceOrTagArgument<T> resourceOrTag(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> key)
<T> ResourceOrTagArgument.Result<T> getResourceOrTag(final CommandContext<CommandSourceStack> context, final String name, final ResourceKey<Registry<T>> registryKey)
ResourceOrTagArgument.Result<T> parse(final StringReader reader)
<S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder)
Collection<String> getExamples()
void serializeToNetwork(final ResourceOrTagArgument.Info<T>.Template template, final FriendlyByteBuf out)
ResourceOrTagArgument.Info<T>.Template deserializeFromNetwork(final FriendlyByteBuf in)
void serializeToJson(final ResourceOrTagArgument.Info<T>.Template template, final JsonObject out)
ResourceOrTagArgument.Info<T>.Template unpack(final ResourceOrTagArgument<T> argument)
ResourceOrTagArgument<T> instantiate(final CommandBuildContext context)
ArgumentTypeInfo<ResourceOrTagArgument<T>, ?> type()
Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap()
<E> Optional<ResourceOrTagArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey)
boolean test(final Holder<T> holder)
String asPrintable()
Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap()
<E> Optional<ResourceOrTagArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey)
boolean test(final Holder<T> holder)
String asPrintable()

Fields & Constants (6)

EXAMPLES
ERROR_UNKNOWN_TAG
ERROR_INVALID_TAG_TYPE
registryLookup
registryKey
name

String literals (6)

"foo:bar"
"#skeletons"
"#minecraft:skeletons"
"argument.resource_tag.not_found"
"argument.resource_tag.invalid_type"
"registry"

Full Source

ResourceOrTagArgument.java187 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.arguments;
2
3
import com.google.gson.JsonObject;
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.Dynamic3CommandExceptionType;
10
import com.mojang.brigadier.suggestion.Suggestions;
11
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
12
import com.mojang.datafixers.util.Either;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.Optional;
16
import java.util.concurrent.CompletableFuture;
17
import java.util.function.Predicate;
18
import net.minecraft.commands.CommandBuildContext;
19
import net.minecraft.commands.CommandSourceStack;
20
import net.minecraft.commands.SharedSuggestionProvider;
21
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
22
import net.minecraft.core.Holder;
23
import net.minecraft.core.HolderLookup;
24
import net.minecraft.core.HolderSet;
25
import net.minecraft.core.Registry;
26
import net.minecraft.network.FriendlyByteBuf;
27
import net.minecraft.network.chat.Component;
28
import net.minecraft.resources.Identifier;
29
import net.minecraft.resources.ResourceKey;
30
import net.minecraft.tags.TagKey;
31
32
public class ResourceOrTagArgument<T> implements ArgumentType<ResourceOrTagArgument.Result<T>> {
33
	private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012", "#skeletons", "#minecraft:skeletons");
34
	private static final Dynamic2CommandExceptionType ERROR_UNKNOWN_TAG = new Dynamic2CommandExceptionType(
35
		(id, registry) -> Component.translatableEscape("argument.resource_tag.not_found", id, registry)
36
	);
37
	private static final Dynamic3CommandExceptionType ERROR_INVALID_TAG_TYPE = new Dynamic3CommandExceptionType(
38
		(id, actualRegistry, expectedRegistry) -> Component.translatableEscape("argument.resource_tag.invalid_type", id, actualRegistry, expectedRegistry)
39
	);
40
	private final HolderLookup<T> registryLookup;
41
	private final ResourceKey<? extends Registry<T>> registryKey;
42
43
	public ResourceOrTagArgument(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> registryKey) {
44
		this.registryKey = registryKey;
45
		this.registryLookup = context.lookupOrThrow(registryKey);
46
	}
47
48
	public static <T> ResourceOrTagArgument<T> resourceOrTag(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> key) {
49
		return new ResourceOrTagArgument<>(context, key);
50
	}
51
52
	public static <T> ResourceOrTagArgument.Result<T> getResourceOrTag(
53
		final CommandContext<CommandSourceStack> context, final String name, final ResourceKey<Registry<T>> registryKey
54
	) throws CommandSyntaxException {
55
		ResourceOrTagArgument.Result<?> argument = context.getArgument(name, ResourceOrTagArgument.Result.class);
56
		Optional<ResourceOrTagArgument.Result<T>> value = argument.cast(registryKey);
57
		return value.orElseThrow(() -> argument.unwrap().map(element -> {
58
			ResourceKey<?> elementKey = element.key();
59
			return ResourceArgument.ERROR_INVALID_RESOURCE_TYPE.create(elementKey.identifier(), elementKey.registry(), registryKey.identifier());
60
		}, tag -> {
61
			TagKey<?> tagKey = tag.key();
62
			return ERROR_INVALID_TAG_TYPE.create(tagKey.location(), tagKey.registry(), registryKey.identifier());
63
		}));
64
	}
65
66
	public ResourceOrTagArgument.Result<T> parse(final StringReader reader) throws CommandSyntaxException {
67
		if (reader.canRead() && reader.peek() == '#') {
68
			int cursor = reader.getCursor();
69
70
			try {
71
				reader.skip();
72
				Identifier tagId = Identifier.read(reader);
73
				TagKey<T> tagKey = TagKey.create(this.registryKey, tagId);
74
				HolderSet.Named<T> holderSet = this.registryLookup
75
					.get(tagKey)
76
					.orElseThrow(() -> ERROR_UNKNOWN_TAG.createWithContext(reader, tagId, this.registryKey.identifier()));
77
				return new ResourceOrTagArgument.TagResult<>(holderSet);
78
			} catch (CommandSyntaxException e) {
79
				reader.setCursor(cursor);
80
				throw e;
81
			}
82
		} else {
83
			Identifier resourceId = Identifier.read(reader);
84
			ResourceKey<T> resourceKey = ResourceKey.create(this.registryKey, resourceId);
85
			Holder.Reference<T> holder = this.registryLookup
86
				.get(resourceKey)
87
				.orElseThrow(() -> ResourceArgument.ERROR_UNKNOWN_RESOURCE.createWithContext(reader, resourceId, this.registryKey.identifier()));
88
			return new ResourceOrTagArgument.ResourceResult<>(holder);
89
		}
90
	}
91
92
	@Override
93
	public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
94
		return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ALL);
95
	}
96
97
	@Override
98
	public Collection<String> getExamples() {
99
		return EXAMPLES;
100
	}
101
102
	public static class Info<T> implements ArgumentTypeInfo<ResourceOrTagArgument<T>, ResourceOrTagArgument.Info<T>.Template> {
103
		public void serializeToNetwork(final ResourceOrTagArgument.Info<T>.Template template, final FriendlyByteBuf out) {
104
			out.writeResourceKey(template.registryKey);
105
		}
106
107
		public ResourceOrTagArgument.Info<T>.Template deserializeFromNetwork(final FriendlyByteBuf in) {
108
			return new ResourceOrTagArgument.Info.Template(in.readRegistryKey());
109
		}
110
111
		public void serializeToJson(final ResourceOrTagArgument.Info<T>.Template template, final JsonObject out) {
112
			out.addProperty("registry", template.registryKey.identifier().toString());
113
		}
114
115
		public ResourceOrTagArgument.Info<T>.Template unpack(final ResourceOrTagArgument<T> argument) {
116
			return new ResourceOrTagArgument.Info.Template(argument.registryKey);
117
		}
118
119
		public final class Template implements ArgumentTypeInfo.Template<ResourceOrTagArgument<T>> {
120
			private final ResourceKey<? extends Registry<T>> registryKey;
121
122
			private Template(final ResourceKey<? extends Registry<T>> registryKey) {
123
				this.registryKey = registryKey;
124
			}
125
126
			public ResourceOrTagArgument<T> instantiate(final CommandBuildContext context) {
127
				return new ResourceOrTagArgument<>(context, this.registryKey);
128
			}
129
130
			@Override
131
			public ArgumentTypeInfo<ResourceOrTagArgument<T>, ?> type() {
132
				return Info.this;
133
			}
134
		}
135
	}
136
137
	private record ResourceResult<T>(Holder.Reference<T> value) implements ResourceOrTagArgument.Result<T> {
138
		@Override
139
		public Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap() {
140
			return Either.left(this.value);
141
		}
142
143
		@Override
144
		public <E> Optional<ResourceOrTagArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey) {
145
			return this.value.key().isFor(registryKey) ? Optional.of((ResourceOrTagArgument.Result<E>)this) : Optional.empty();
146
		}
147
148
		public boolean test(final Holder<T> holder) {
149
			return holder.equals(this.value);
150
		}
151
152
		@Override
153
		public String asPrintable() {
154
			return this.value.key().identifier().toString();
155
		}
156
	}
157
158
	public interface Result<T> extends Predicate<Holder<T>> {
159
		Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap();
160
161
		<E> Optional<ResourceOrTagArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey);
162
163
		String asPrintable();
164
	}
165
166
	private record TagResult<T>(HolderSet.Named<T> tag) implements ResourceOrTagArgument.Result<T> {
167
		@Override
168
		public Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap() {
169
			return Either.right(this.tag);
170
		}
171
172
		@Override
173
		public <E> Optional<ResourceOrTagArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey) {
174
			return this.tag.key().isFor(registryKey) ? Optional.of((ResourceOrTagArgument.Result<E>)this) : Optional.empty();
175
		}
176
177
		public boolean test(final Holder<T> holder) {
178
			return this.tag.contains(holder);
179
		}
180
181
		@Override
182
		public String asPrintable() {
183
			return "#" + this.tag.key().location();
184
		}
185
	}
186
}
187

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