/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.common.collect.ImmutableList * com.google.common.collect.ImmutableList$Builder * com.mojang.brigadier.ImmutableStringReader * com.mojang.brigadier.StringReader * com.mojang.brigadier.exceptions.CommandSyntaxException * com.mojang.serialization.Dynamic */ package net.minecraft.commands.arguments.item; import com.google.common.collect.ImmutableList; import com.mojang.brigadier.ImmutableStringReader; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.serialization.Dynamic; import java.util.List; import java.util.Optional; import java.util.stream.Stream; import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.Tag; import net.minecraft.resources.Identifier; import net.minecraft.util.Unit; import net.minecraft.util.Util; import net.minecraft.util.parsing.packrat.Atom; import net.minecraft.util.parsing.packrat.Dictionary; import net.minecraft.util.parsing.packrat.NamedRule; import net.minecraft.util.parsing.packrat.Scope; import net.minecraft.util.parsing.packrat.Term; import net.minecraft.util.parsing.packrat.commands.Grammar; import net.minecraft.util.parsing.packrat.commands.IdentifierParseRule; import net.minecraft.util.parsing.packrat.commands.ResourceLookupRule; import net.minecraft.util.parsing.packrat.commands.StringReaderTerms; import net.minecraft.util.parsing.packrat.commands.TagParseRule; public class ComponentPredicateParser { public static Grammar> createGrammar(Context context) { Atom top = Atom.of("top"); Atom type = Atom.of("type"); Atom anyType = Atom.of("any_type"); Atom elementType = Atom.of("element_type"); Atom tagType = Atom.of("tag_type"); Atom conditions = Atom.of("conditions"); Atom alternatives = Atom.of("alternatives"); Atom term = Atom.of("term"); Atom negation = Atom.of("negation"); Atom test = Atom.of("test"); Atom componentType = Atom.of("component_type"); Atom predicateType = Atom.of("predicate_type"); Atom id = Atom.of("id"); Atom tag = Atom.of("tag"); Dictionary rules = new Dictionary(); NamedRule idRule = rules.put(id, IdentifierParseRule.INSTANCE); NamedRule topRule = rules.put(top, Term.alternative(Term.sequence(rules.named(type), StringReaderTerms.character('['), Term.cut(), Term.optional(rules.named(conditions)), StringReaderTerms.character(']')), rules.named(type)), scope -> { ImmutableList.Builder builder = ImmutableList.builder(); ((Optional)scope.getOrThrow(type)).ifPresent(arg_0 -> ((ImmutableList.Builder)builder).add(arg_0)); List parsedConditions = (List)scope.get(conditions); if (parsedConditions != null) { builder.addAll((Iterable)parsedConditions); } return builder.build(); }); rules.put(type, Term.alternative(rules.named(elementType), Term.sequence(StringReaderTerms.character('#'), Term.cut(), rules.named(tagType)), rules.named(anyType)), scope -> Optional.ofNullable(scope.getAny(elementType, tagType))); rules.put(anyType, StringReaderTerms.character('*'), s -> Unit.INSTANCE); rules.put(elementType, new ElementLookupRule(idRule, context)); rules.put(tagType, new TagLookupRule(idRule, context)); rules.put(conditions, Term.sequence(rules.named(alternatives), Term.optional(Term.sequence(StringReaderTerms.character(','), rules.named(conditions)))), scope -> { Object parsedCondition = context.anyOf((List)scope.getOrThrow(alternatives)); return Optional.ofNullable((List)scope.get(conditions)).map(rest -> Util.copyAndAdd(parsedCondition, rest)).orElse(List.of(parsedCondition)); }); rules.put(alternatives, Term.sequence(rules.named(term), Term.optional(Term.sequence(StringReaderTerms.character('|'), rules.named(alternatives)))), scope -> { Object alternative = scope.getOrThrow(term); return Optional.ofNullable((List)scope.get(alternatives)).map(rest -> Util.copyAndAdd(alternative, rest)).orElse(List.of(alternative)); }); rules.put(term, Term.alternative(rules.named(test), Term.sequence(StringReaderTerms.character('!'), rules.named(negation))), scope -> scope.getAnyOrThrow(test, negation)); rules.put(negation, rules.named(test), scope -> context.negate(scope.getOrThrow(test))); rules.putComplex(test, Term.alternative(Term.sequence(rules.named(componentType), StringReaderTerms.character('='), Term.cut(), rules.named(tag)), Term.sequence(rules.named(predicateType), StringReaderTerms.character('~'), Term.cut(), rules.named(tag)), rules.named(componentType)), state -> { Scope scope = state.scope(); Object predicate = scope.get(predicateType); try { if (predicate != null) { Dynamic value = (Dynamic)scope.getOrThrow(tag); return context.createPredicateTest((ImmutableStringReader)state.input(), predicate, value); } Object component = scope.getOrThrow(componentType); Dynamic value = (Dynamic)scope.get(tag); return value != null ? context.createComponentTest((ImmutableStringReader)state.input(), component, value) : context.createComponentTest((ImmutableStringReader)state.input(), component); } catch (CommandSyntaxException e) { state.errorCollector().store(state.mark(), (Object)e); return null; } }); rules.put(componentType, new ComponentLookupRule(idRule, context)); rules.put(predicateType, new PredicateLookupRule(idRule, context)); rules.put(tag, new TagParseRule(NbtOps.INSTANCE)); return new Grammar>(rules, topRule); } private static class ElementLookupRule extends ResourceLookupRule, T> { private ElementLookupRule(NamedRule idParser, Context context) { super(idParser, context); } @Override protected T validateElement(ImmutableStringReader reader, Identifier id) throws Exception { return ((Context)this.context).forElementType(reader, id); } @Override public Stream possibleResources() { return ((Context)this.context).listElementTypes(); } } public static interface Context { public T forElementType(ImmutableStringReader var1, Identifier var2) throws CommandSyntaxException; public Stream listElementTypes(); public T forTagType(ImmutableStringReader var1, Identifier var2) throws CommandSyntaxException; public Stream listTagTypes(); public C lookupComponentType(ImmutableStringReader var1, Identifier var2) throws CommandSyntaxException; public Stream listComponentTypes(); public T createComponentTest(ImmutableStringReader var1, C var2, Dynamic var3) throws CommandSyntaxException; public T createComponentTest(ImmutableStringReader var1, C var2); public P lookupPredicateType(ImmutableStringReader var1, Identifier var2) throws CommandSyntaxException; public Stream listPredicateTypes(); public T createPredicateTest(ImmutableStringReader var1, P var2, Dynamic var3) throws CommandSyntaxException; public T negate(T var1); public T anyOf(List var1); } private static class TagLookupRule extends ResourceLookupRule, T> { private TagLookupRule(NamedRule idParser, Context context) { super(idParser, context); } @Override protected T validateElement(ImmutableStringReader reader, Identifier id) throws Exception { return ((Context)this.context).forTagType(reader, id); } @Override public Stream possibleResources() { return ((Context)this.context).listTagTypes(); } } private static class ComponentLookupRule extends ResourceLookupRule, C> { private ComponentLookupRule(NamedRule idParser, Context context) { super(idParser, context); } @Override protected C validateElement(ImmutableStringReader reader, Identifier id) throws Exception { return ((Context)this.context).lookupComponentType(reader, id); } @Override public Stream possibleResources() { return ((Context)this.context).listComponentTypes(); } } private static class PredicateLookupRule extends ResourceLookupRule, P> { private PredicateLookupRule(NamedRule idParser, Context context) { super(idParser, context); } @Override protected P validateElement(ImmutableStringReader reader, Identifier id) throws Exception { return ((Context)this.context).lookupPredicateType(reader, id); } @Override public Stream possibleResources() { return ((Context)this.context).listPredicateTypes(); } } }