ResourceArgument .java

net.minecraft.commands.arguments.ResourceArgument (class, 150 lines): implements ArgumentType. members: ERROR_INVALID_RESOURCE_TYPE, getEnchantment, Info.

net.minecraft.commands.argumentsclass150 linescommon6.9 KB

Implements

API Surface (17)

<T> ResourceArgument<T> resource(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> key)
<T> Holder.Reference<T> getResource(final CommandContext<CommandSourceStack> context, final String name, final ResourceKey<Registry<T>> registryKey)
Holder.Reference<Attribute> getAttribute(final CommandContext<CommandSourceStack> context, final String name)
Holder.Reference<EntityType<?>> getSummonableEntityType(final CommandContext<CommandSourceStack> context, final String name)
Holder.Reference<MobEffect> getMobEffect(final CommandContext<CommandSourceStack> context, final String name)
Holder.Reference<Enchantment> getEnchantment(final CommandContext<CommandSourceStack> context, final String name)
Holder.Reference<WorldClock> getClock(final CommandContext<CommandSourceStack> context, final String name)
Holder.Reference<Timeline> getTimeline(final CommandContext<CommandSourceStack> context, final String name)
Holder.Reference<T> parse(final StringReader reader)
<S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder)
Collection<String> getExamples()
void serializeToNetwork(final ResourceArgument.Info<T>.Template template, final FriendlyByteBuf out)
ResourceArgument.Info<T>.Template deserializeFromNetwork(final FriendlyByteBuf in)
void serializeToJson(final ResourceArgument.Info<T>.Template template, final JsonObject out)
ResourceArgument.Info<T>.Template unpack(final ResourceArgument<T> argument)
ResourceArgument<T> instantiate(final CommandBuildContext context)
ArgumentTypeInfo<ResourceArgument<T>, ?> type()

Fields & Constants (7)

EXAMPLES
ERROR_NOT_SUMMONABLE_ENTITY
ERROR_UNKNOWN_RESOURCE
ERROR_INVALID_RESOURCE_TYPE
registryKey
registryLookup
name

String literals (5)

"foo:bar"
"entity.not_summonable"
"argument.resource.not_found"
"argument.resource.invalid_type"
"registry"

Full Source

ResourceArgument.java150 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.exceptions.DynamicCommandExceptionType;
11
import com.mojang.brigadier.suggestion.Suggestions;
12
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.concurrent.CompletableFuture;
16
import net.minecraft.commands.CommandBuildContext;
17
import net.minecraft.commands.CommandSourceStack;
18
import net.minecraft.commands.SharedSuggestionProvider;
19
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
20
import net.minecraft.core.Holder;
21
import net.minecraft.core.HolderLookup;
22
import net.minecraft.core.Registry;
23
import net.minecraft.core.registries.Registries;
24
import net.minecraft.network.FriendlyByteBuf;
25
import net.minecraft.network.chat.Component;
26
import net.minecraft.resources.Identifier;
27
import net.minecraft.resources.ResourceKey;
28
import net.minecraft.world.clock.WorldClock;
29
import net.minecraft.world.effect.MobEffect;
30
import net.minecraft.world.entity.EntityType;
31
import net.minecraft.world.entity.ai.attributes.Attribute;
32
import net.minecraft.world.item.enchantment.Enchantment;
33
import net.minecraft.world.timeline.Timeline;
34
35
public class ResourceArgument<T> implements ArgumentType<Holder.Reference<T>> {
36
	private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012");
37
	private static final DynamicCommandExceptionType ERROR_NOT_SUMMONABLE_ENTITY = new DynamicCommandExceptionType(
38
		value -> Component.translatableEscape("entity.not_summonable", value)
39
	);
40
	public static final Dynamic2CommandExceptionType ERROR_UNKNOWN_RESOURCE = new Dynamic2CommandExceptionType(
41
		(id, registry) -> Component.translatableEscape("argument.resource.not_found", id, registry)
42
	);
43
	public static final Dynamic3CommandExceptionType ERROR_INVALID_RESOURCE_TYPE = new Dynamic3CommandExceptionType(
44
		(id, actualRegistry, expectedRegistry) -> Component.translatableEscape("argument.resource.invalid_type", id, actualRegistry, expectedRegistry)
45
	);
46
	private final ResourceKey<? extends Registry<T>> registryKey;
47
	private final HolderLookup<T> registryLookup;
48
49
	public ResourceArgument(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> registryKey) {
50
		this.registryKey = registryKey;
51
		this.registryLookup = context.lookupOrThrow(registryKey);
52
	}
53
54
	public static <T> ResourceArgument<T> resource(final CommandBuildContext context, final ResourceKey<? extends Registry<T>> key) {
55
		return new ResourceArgument<>(context, key);
56
	}
57
58
	public static <T> Holder.Reference<T> getResource(
59
		final CommandContext<CommandSourceStack> context, final String name, final ResourceKey<Registry<T>> registryKey
60
	) throws CommandSyntaxException {
61
		Holder.Reference<T> argument = context.getArgument(name, Holder.Reference.class);
62
		ResourceKey<?> argumentKey = argument.key();
63
		if (argumentKey.isFor(registryKey)) {
64
			return argument;
65
		} else {
66
			throw ERROR_INVALID_RESOURCE_TYPE.create(argumentKey.identifier(), argumentKey.registry(), registryKey.identifier());
67
		}
68
	}
69
70
	public static Holder.Reference<Attribute> getAttribute(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
71
		return getResource(context, name, Registries.ATTRIBUTE);
72
	}
73
74
	public static Holder.Reference<EntityType<?>> getSummonableEntityType(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
75
		Holder.Reference<EntityType<?>> result = getResource(context, name, Registries.ENTITY_TYPE);
76
		if (!result.value().canSummon()) {
77
			throw ERROR_NOT_SUMMONABLE_ENTITY.create(result.key().identifier().toString());
78
		} else {
79
			return result;
80
		}
81
	}
82
83
	public static Holder.Reference<MobEffect> getMobEffect(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
84
		return getResource(context, name, Registries.MOB_EFFECT);
85
	}
86
87
	public static Holder.Reference<Enchantment> getEnchantment(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
88
		return getResource(context, name, Registries.ENCHANTMENT);
89
	}
90
91
	public static Holder.Reference<WorldClock> getClock(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
92
		return getResource(context, name, Registries.WORLD_CLOCK);
93
	}
94
95
	public static Holder.Reference<Timeline> getTimeline(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
96
		return getResource(context, name, Registries.TIMELINE);
97
	}
98
99
	public Holder.Reference<T> parse(final StringReader reader) throws CommandSyntaxException {
100
		Identifier resourceId = Identifier.read(reader);
101
		ResourceKey<T> keyInRegistry = ResourceKey.create(this.registryKey, resourceId);
102
		return this.registryLookup.get(keyInRegistry).orElseThrow(() -> ERROR_UNKNOWN_RESOURCE.createWithContext(reader, resourceId, this.registryKey.identifier()));
103
	}
104
105
	@Override
106
	public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
107
		return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ELEMENTS);
108
	}
109
110
	@Override
111
	public Collection<String> getExamples() {
112
		return EXAMPLES;
113
	}
114
115
	public static class Info<T> implements ArgumentTypeInfo<ResourceArgument<T>, ResourceArgument.Info<T>.Template> {
116
		public void serializeToNetwork(final ResourceArgument.Info<T>.Template template, final FriendlyByteBuf out) {
117
			out.writeResourceKey(template.registryKey);
118
		}
119
120
		public ResourceArgument.Info<T>.Template deserializeFromNetwork(final FriendlyByteBuf in) {
121
			return new ResourceArgument.Info.Template(in.readRegistryKey());
122
		}
123
124
		public void serializeToJson(final ResourceArgument.Info<T>.Template template, final JsonObject out) {
125
			out.addProperty("registry", template.registryKey.identifier().toString());
126
		}
127
128
		public ResourceArgument.Info<T>.Template unpack(final ResourceArgument<T> argument) {
129
			return new ResourceArgument.Info.Template(argument.registryKey);
130
		}
131
132
		public final class Template implements ArgumentTypeInfo.Template<ResourceArgument<T>> {
133
			private final ResourceKey<? extends Registry<T>> registryKey;
134
135
			private Template(final ResourceKey<? extends Registry<T>> registryKey) {
136
				this.registryKey = registryKey;
137
			}
138
139
			public ResourceArgument<T> instantiate(final CommandBuildContext context) {
140
				return new ResourceArgument<>(context, this.registryKey);
141
			}
142
143
			@Override
144
			public ArgumentTypeInfo<ResourceArgument<T>, ?> type() {
145
				return Info.this;
146
			}
147
		}
148
	}
149
}
150

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