ComponentPredicateParser .java

net.minecraft.commands.arguments.item.ComponentPredicateParser (class): members: ComponentLookupRule, PredicateLookupRule, ElementLookupRule, Context.

net.minecraft.commands.arguments.itemclass212 linescommon8.5 KB

API Surface (9)

<T, C, P> Grammar<List<T>> createGrammar(final ComponentPredicateParser.Context<T, C, P> context)
C validateElement(final ImmutableStringReader reader, final Identifier id)
Stream<Identifier> possibleResources()
T validateElement(final ImmutableStringReader reader, final Identifier id)
Stream<Identifier> possibleResources()
P validateElement(final ImmutableStringReader reader, final Identifier id)
Stream<Identifier> possibleResources()
T validateElement(final ImmutableStringReader reader, final Identifier id)
Stream<Identifier> possibleResources()

Fields & Constants (1)

C

String literals (10)

"any_type"
"element_type"
"tag_type"
"conditions"
"alternatives"
"term"
"negation"
"test"
"component_type"
"predicate_type"

Full Source

ComponentPredicateParser.java212 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands.arguments.item;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.ImmutableList.Builder;
5
import com.mojang.brigadier.ImmutableStringReader;
6
import com.mojang.brigadier.StringReader;
7
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8
import com.mojang.serialization.Dynamic;
9
import java.util.List;
10
import java.util.Optional;
11
import java.util.stream.Stream;
12
import net.minecraft.nbt.NbtOps;
13
import net.minecraft.resources.Identifier;
14
import net.minecraft.util.Unit;
15
import net.minecraft.util.Util;
16
import net.minecraft.util.parsing.packrat.Atom;
17
import net.minecraft.util.parsing.packrat.Dictionary;
18
import net.minecraft.util.parsing.packrat.NamedRule;
19
import net.minecraft.util.parsing.packrat.Scope;
20
import net.minecraft.util.parsing.packrat.Term;
21
import net.minecraft.util.parsing.packrat.commands.Grammar;
22
import net.minecraft.util.parsing.packrat.commands.IdentifierParseRule;
23
import net.minecraft.util.parsing.packrat.commands.ResourceLookupRule;
24
import net.minecraft.util.parsing.packrat.commands.StringReaderTerms;
25
import net.minecraft.util.parsing.packrat.commands.TagParseRule;
26
27
public class ComponentPredicateParser {
28
	public static <T, C, P> Grammar<List<T>> createGrammar(final ComponentPredicateParser.Context<T, C, P> context) {
29
		Atom<List<T>> top = Atom.of("top");
30
		Atom<Optional<T>> type = Atom.of("type");
31
		Atom<Unit> anyType = Atom.of("any_type");
32
		Atom<T> elementType = Atom.of("element_type");
33
		Atom<T> tagType = Atom.of("tag_type");
34
		Atom<List<T>> conditions = Atom.of("conditions");
35
		Atom<List<T>> alternatives = Atom.of("alternatives");
36
		Atom<T> term = Atom.of("term");
37
		Atom<T> negation = Atom.of("negation");
38
		Atom<T> test = Atom.of("test");
39
		Atom<C> componentType = Atom.of("component_type");
40
		Atom<P> predicateType = Atom.of("predicate_type");
41
		Atom<Identifier> id = Atom.of("id");
42
		Atom<Dynamic<?>> tag = Atom.of("tag");
43
		Dictionary<StringReader> rules = new Dictionary<>();
44
		NamedRule<StringReader, Identifier> idRule = rules.put(id, IdentifierParseRule.INSTANCE);
45
		NamedRule<StringReader, List<T>> topRule = rules.put(
46
			top,
47
			Term.alternative(
48
				Term.sequence(rules.named(type), StringReaderTerms.character('['), Term.cut(), Term.optional(rules.named(conditions)), StringReaderTerms.character(']')),
49
				rules.named(type)
50
			),
51
			scope -> {
52
				Builder<T> builder = ImmutableList.builder();
53
				scope.getOrThrow(type).ifPresent(builder::add);
54
				List<T> parsedConditions = scope.get(conditions);
55
				if (parsedConditions != null) {
56
					builder.addAll(parsedConditions);
57
				}
58
59
				return builder.build();
60
			}
61
		);
62
		rules.put(
63
			type,
64
			Term.alternative(rules.named(elementType), Term.sequence(StringReaderTerms.character('#'), Term.cut(), rules.named(tagType)), rules.named(anyType)),
65
			scope -> Optional.ofNullable(scope.getAny(elementType, tagType))
66
		);
67
		rules.put(anyType, StringReaderTerms.character('*'), s -> Unit.INSTANCE);
68
		rules.put(elementType, new ComponentPredicateParser.ElementLookupRule<>(idRule, context));
69
		rules.put(tagType, new ComponentPredicateParser.TagLookupRule<>(idRule, context));
70
		rules.put(
71
			conditions, Term.sequence(rules.named(alternatives), Term.optional(Term.sequence(StringReaderTerms.character(','), rules.named(conditions)))), scope -> {
72
				T parsedCondition = context.anyOf(scope.getOrThrow(alternatives));
73
				return Optional.ofNullable(scope.get(conditions)).map(rest -> Util.copyAndAdd(parsedCondition, (List<T>)rest)).orElse(List.of(parsedCondition));
74
			}
75
		);
76
		rules.put(
77
			alternatives, Term.sequence(rules.named(term), Term.optional(Term.sequence(StringReaderTerms.character('|'), rules.named(alternatives)))), scope -> {
78
				T alternative = scope.getOrThrow(term);
79
				return Optional.ofNullable(scope.get(alternatives)).map(rest -> Util.copyAndAdd(alternative, (List<T>)rest)).orElse(List.of(alternative));
80
			}
81
		);
82
		rules.put(
83
			term,
84
			Term.alternative(rules.named(test), Term.sequence(StringReaderTerms.character('!'), rules.named(negation))),
85
			scope -> scope.getAnyOrThrow(test, negation)
86
		);
87
		rules.put(negation, rules.named(test), scope -> context.negate(scope.getOrThrow(test)));
88
		rules.putComplex(
89
			test,
90
			Term.alternative(
91
				Term.sequence(rules.named(componentType), StringReaderTerms.character('='), Term.cut(), rules.named(tag)),
92
				Term.sequence(rules.named(predicateType), StringReaderTerms.character('~'), Term.cut(), rules.named(tag)),
93
				rules.named(componentType)
94
			),
95
			state -> {
96
				Scope scope = state.scope();
97
				P predicate = scope.get(predicateType);
98
99
				try {
100
					if (predicate != null) {
101
						Dynamic<?> value = scope.getOrThrow(tag);
102
						return context.createPredicateTest(state.input(), predicate, value);
103
					} else {
104
						C component = scope.getOrThrow(componentType);
105
						Dynamic<?> value = scope.get(tag);
106
						return value != null ? context.createComponentTest(state.input(), component, value) : context.createComponentTest(state.input(), component);
107
					}
108
				} catch (CommandSyntaxException e) {
109
					state.errorCollector().store(state.mark(), e);
110
					return null;
111
				}
112
			}
113
		);
114
		rules.put(componentType, new ComponentPredicateParser.ComponentLookupRule<>(idRule, context));
115
		rules.put(predicateType, new ComponentPredicateParser.PredicateLookupRule<>(idRule, context));
116
		rules.put(tag, new TagParseRule<>(NbtOps.INSTANCE));
117
		return new Grammar<>(rules, topRule);
118
	}
119
120
	private static class ComponentLookupRule<T, C, P> extends ResourceLookupRule<ComponentPredicateParser.Context<T, C, P>, C> {
121
		private ComponentLookupRule(final NamedRule<StringReader, Identifier> idParser, final ComponentPredicateParser.Context<T, C, P> context) {
122
			super(idParser, context);
123
		}
124
125
		@Override
126
		protected C validateElement(final ImmutableStringReader reader, final Identifier id) throws Exception {
127
			return this.context.lookupComponentType(reader, id);
128
		}
129
130
		@Override
131
		public Stream<Identifier> possibleResources() {
132
			return this.context.listComponentTypes();
133
		}
134
	}
135
136
	public interface Context<T, C, P> {
137
		T forElementType(ImmutableStringReader reader, Identifier id) throws CommandSyntaxException;
138
139
		Stream<Identifier> listElementTypes();
140
141
		T forTagType(ImmutableStringReader reader, Identifier id) throws CommandSyntaxException;
142
143
		Stream<Identifier> listTagTypes();
144
145
		C lookupComponentType(ImmutableStringReader reader, Identifier id) throws CommandSyntaxException;
146
147
		Stream<Identifier> listComponentTypes();
148
149
		T createComponentTest(ImmutableStringReader reader, C componentType, Dynamic<?> value) throws CommandSyntaxException;
150
151
		T createComponentTest(ImmutableStringReader reader, C componentType);
152
153
		P lookupPredicateType(ImmutableStringReader reader, Identifier id) throws CommandSyntaxException;
154
155
		Stream<Identifier> listPredicateTypes();
156
157
		T createPredicateTest(ImmutableStringReader reader, P predicateType, Dynamic<?> value) throws CommandSyntaxException;
158
159
		T negate(T value);
160
161
		T anyOf(List<T> alternatives);
162
	}
163
164
	private static class ElementLookupRule<T, C, P> extends ResourceLookupRule<ComponentPredicateParser.Context<T, C, P>, T> {
165
		private ElementLookupRule(final NamedRule<StringReader, Identifier> idParser, final ComponentPredicateParser.Context<T, C, P> context) {
166
			super(idParser, context);
167
		}
168
169
		@Override
170
		protected T validateElement(final ImmutableStringReader reader, final Identifier id) throws Exception {
171
			return this.context.forElementType(reader, id);
172
		}
173
174
		@Override
175
		public Stream<Identifier> possibleResources() {
176
			return this.context.listElementTypes();
177
		}
178
	}
179
180
	private static class PredicateLookupRule<T, C, P> extends ResourceLookupRule<ComponentPredicateParser.Context<T, C, P>, P> {
181
		private PredicateLookupRule(final NamedRule<StringReader, Identifier> idParser, final ComponentPredicateParser.Context<T, C, P> context) {
182
			super(idParser, context);
183
		}
184
185
		@Override
186
		protected P validateElement(final ImmutableStringReader reader, final Identifier id) throws Exception {
187
			return this.context.lookupPredicateType(reader, id);
188
		}
189
190
		@Override
191
		public Stream<Identifier> possibleResources() {
192
			return this.context.listPredicateTypes();
193
		}
194
	}
195
196
	private static class TagLookupRule<T, C, P> extends ResourceLookupRule<ComponentPredicateParser.Context<T, C, P>, T> {
197
		private TagLookupRule(final NamedRule<StringReader, Identifier> idParser, final ComponentPredicateParser.Context<T, C, P> context) {
198
			super(idParser, context);
199
		}
200
201
		@Override
202
		protected T validateElement(final ImmutableStringReader reader, final Identifier id) throws Exception {
203
			return this.context.forTagType(reader, id);
204
		}
205
206
		@Override
207
		public Stream<Identifier> possibleResources() {
208
			return this.context.listTagTypes();
209
		}
210
	}
211
}
212

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