ParticleArgument .java

net.minecraft.commands.arguments.ParticleArgument (class): members: ERROR_UNKNOWN_PARTICLE, ERROR_INVALID_OPTIONS, ParticleOptions, VALUE_PARSER, particle.

net.minecraft.commands.argumentsclass90 linescommon4.0 KB

Implements

API Surface (6)

ParticleArgument particle(final CommandBuildContext context)
ParticleOptions getParticle(final CommandContext<CommandSourceStack> context, final String name)
ParticleOptions parse(final StringReader reader)
Collection<String> getExamples()
ParticleOptions readParticle(final StringReader reader, final HolderLookup.Provider registries)
<S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder)

Fields & Constants (7)

EXAMPLES
ERROR_UNKNOWN_PARTICLE
ERROR_INVALID_OPTIONS
VALUE_PARSER
registries
ParticleOptions
type

String literals (4)

"foo:bar"
"particle{foo:bar}"
"particle.notFound"
"particle.invalidOptions"

Full Source

ParticleArgument.java90 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.arguments;
2
3
import com.mojang.brigadier.StringReader;
4
import com.mojang.brigadier.arguments.ArgumentType;
5
import com.mojang.brigadier.context.CommandContext;
6
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
8
import com.mojang.brigadier.suggestion.Suggestions;
9
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
10
import java.util.Arrays;
11
import java.util.Collection;
12
import java.util.concurrent.CompletableFuture;
13
import net.minecraft.commands.CommandBuildContext;
14
import net.minecraft.commands.CommandSourceStack;
15
import net.minecraft.commands.SharedSuggestionProvider;
16
import net.minecraft.core.HolderLookup;
17
import net.minecraft.core.particles.ParticleOptions;
18
import net.minecraft.core.particles.ParticleType;
19
import net.minecraft.core.registries.Registries;
20
import net.minecraft.nbt.NbtOps;
21
import net.minecraft.nbt.TagParser;
22
import net.minecraft.network.chat.Component;
23
import net.minecraft.resources.Identifier;
24
import net.minecraft.resources.RegistryOps;
25
import net.minecraft.resources.ResourceKey;
26
27
public class ParticleArgument implements ArgumentType<ParticleOptions> {
28
	private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "particle{foo:bar}");
29
	public static final DynamicCommandExceptionType ERROR_UNKNOWN_PARTICLE = new DynamicCommandExceptionType(
30
		value -> Component.translatableEscape("particle.notFound", value)
31
	);
32
	public static final DynamicCommandExceptionType ERROR_INVALID_OPTIONS = new DynamicCommandExceptionType(
33
		message -> Component.translatableEscape("particle.invalidOptions", message)
34
	);
35
	private final HolderLookup.Provider registries;
36
	private static final TagParser<?> VALUE_PARSER = TagParser.create(NbtOps.INSTANCE);
37
38
	public ParticleArgument(final CommandBuildContext context) {
39
		this.registries = context;
40
	}
41
42
	public static ParticleArgument particle(final CommandBuildContext context) {
43
		return new ParticleArgument(context);
44
	}
45
46
	public static ParticleOptions getParticle(final CommandContext<CommandSourceStack> context, final String name) {
47
		return context.getArgument(name, ParticleOptions.class);
48
	}
49
50
	public ParticleOptions parse(final StringReader reader) throws CommandSyntaxException {
51
		return readParticle(reader, this.registries);
52
	}
53
54
	@Override
55
	public Collection<String> getExamples() {
56
		return EXAMPLES;
57
	}
58
59
	public static ParticleOptions readParticle(final StringReader reader, final HolderLookup.Provider registries) throws CommandSyntaxException {
60
		ParticleType<?> type = readParticleType(reader, registries.lookupOrThrow(Registries.PARTICLE_TYPE));
61
		return readParticle(VALUE_PARSER, reader, (ParticleType<ParticleOptions>)type, registries);
62
	}
63
64
	private static ParticleType<?> readParticleType(final StringReader reader, final HolderLookup<ParticleType<?>> particles) throws CommandSyntaxException {
65
		Identifier id = Identifier.read(reader);
66
		ResourceKey<ParticleType<?>> key = ResourceKey.create(Registries.PARTICLE_TYPE, id);
67
		return particles.get(key).orElseThrow(() -> ERROR_UNKNOWN_PARTICLE.createWithContext(reader, id)).value();
68
	}
69
70
	private static <T extends ParticleOptions, O> T readParticle(
71
		final TagParser<O> parser, final StringReader reader, final ParticleType<T> type, final HolderLookup.Provider registries
72
	) throws CommandSyntaxException {
73
		RegistryOps<O> ops = registries.createSerializationContext(parser.getOps());
74
		O extraData;
75
		if (reader.canRead() && reader.peek() == '{') {
76
			extraData = parser.parseAsArgument(reader);
77
		} else {
78
			extraData = ops.emptyMap();
79
		}
80
81
		return type.codec().codec().parse(ops, extraData).getOrThrow(ERROR_INVALID_OPTIONS::create);
82
	}
83
84
	@Override
85
	public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
86
		HolderLookup.RegistryLookup<ParticleType<?>> particles = this.registries.lookupOrThrow(Registries.PARTICLE_TYPE);
87
		return SharedSuggestionProvider.suggestResource(particles.listElementIds().map(ResourceKey::identifier), builder);
88
	}
89
}
90

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