ResourceOrIdArgument.java

net.minecraft.commands.arguments.ResourceOrIdArgument

信息

  • 全限定名:net.minecraft.commands.arguments.ResourceOrIdArgument
  • 类型:public class
  • 包:net.minecraft.commands.arguments
  • 源码路径:src/main/java/net/minecraft/commands/arguments/ResourceOrIdArgument.java
  • 起始行号:L44
  • 实现:ArgumentType<Holder>
  • 职责:

    TODO

字段/常量

  • EXAMPLES

    • 类型: Collection<String>
    • 修饰符: private static final
    • 源码定位: L45
    • 说明:

      TODO

  • ERROR_FAILED_TO_PARSE

    • 类型: DynamicCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L46
    • 说明:

      TODO

  • ERROR_NO_SUCH_ELEMENT

    • 类型: Dynamic2CommandExceptionType
    • 修饰符: public static final
    • 源码定位: L49
    • 说明:

      TODO

  • OPS

    • 类型: DynamicOps<Tag>
    • 修饰符: public static final
    • 源码定位: L52
    • 说明:

      TODO

  • registryLookup

    • 类型: HolderLookup.Provider
    • 修饰符: private final
    • 源码定位: L53
    • 说明:

      TODO

  • elementLookup

    • 类型: Optional<?extends HolderLookup.RegistryLookup<T>>
    • 修饰符: private final
    • 源码定位: L54
    • 说明:

      TODO

  • codec

    • 类型: Codec<T>
    • 修饰符: private final
    • 源码定位: L55
    • 说明:

      TODO

  • grammar

    • 类型: Grammar<ResourceOrIdArgument.Result<T,Tag>>
    • 修饰符: private final
    • 源码定位: L56
    • 说明:

      TODO

  • registryKey

    • 类型: ResourceKey<?extends Registry<T>>
    • 修饰符: private final
    • 源码定位: L57
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.commands.arguments.ResourceOrIdArgument.DialogArgument

    • 类型: class
    • 修饰符: public static
    • 源码定位: L146
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrIdArgument.InlineResult

    • 类型: record
    • 修饰符: public
    • 源码定位: L152
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrIdArgument.LootModifierArgument

    • 类型: class
    • 修饰符: public static
    • 源码定位: L164
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrIdArgument.LootPredicateArgument

    • 类型: class
    • 修饰符: public static
    • 源码定位: L170
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrIdArgument.LootTableArgument

    • 类型: class
    • 修饰符: public static
    • 源码定位: L176
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrIdArgument.ReferenceResult

    • 类型: record
    • 修饰符: public
    • 源码定位: L182
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrIdArgument.Result

    • 类型: interface
    • 修饰符: public sealed
    • 源码定位: L192
    • 说明:

      TODO

构造器

protected ResourceOrIdArgument(CommandBuildContext context, ResourceKey<?extends Registry<T>> registryKey, Codec<T> codec) @ L59

  • 构造器名:ResourceOrIdArgument
  • 源码定位:L59
  • 修饰符:protected

参数:

  • context: CommandBuildContext
  • registryKey: ResourceKey<?extends Registry>
  • codec: Codec

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static <T,O> Grammar<ResourceOrIdArgument.Result<T,O>> createGrammar(ResourceKey<?extends Registry<T>> registryKey, DynamicOps<O> ops) @ L67

  • 方法名:createGrammar
  • 源码定位:L67
  • 返回类型:<T,O> Grammar<ResourceOrIdArgument.Result<T,O>>
  • 修饰符:public static

参数:

  • registryKey: ResourceKey<?extends Registry>
  • ops: DynamicOps

说明:

TODO

public static ResourceOrIdArgument.LootTableArgument lootTable(CommandBuildContext context) @ L89

  • 方法名:lootTable
  • 源码定位:L89
  • 返回类型:ResourceOrIdArgument.LootTableArgument
  • 修饰符:public static

参数:

  • context: CommandBuildContext

说明:

TODO

public static Holder<LootTable> getLootTable(CommandContext<CommandSourceStack> context, String name) @ L93

  • 方法名:getLootTable
  • 源码定位:L93
  • 返回类型:Holder
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static ResourceOrIdArgument.LootModifierArgument lootModifier(CommandBuildContext context) @ L97

  • 方法名:lootModifier
  • 源码定位:L97
  • 返回类型:ResourceOrIdArgument.LootModifierArgument
  • 修饰符:public static

参数:

  • context: CommandBuildContext

说明:

TODO

public static Holder<LootItemFunction> getLootModifier(CommandContext<CommandSourceStack> context, String name) @ L101

  • 方法名:getLootModifier
  • 源码定位:L101
  • 返回类型:Holder
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static ResourceOrIdArgument.LootPredicateArgument lootPredicate(CommandBuildContext context) @ L105

  • 方法名:lootPredicate
  • 源码定位:L105
  • 返回类型:ResourceOrIdArgument.LootPredicateArgument
  • 修饰符:public static

参数:

  • context: CommandBuildContext

说明:

TODO

public static Holder<LootItemCondition> getLootPredicate(CommandContext<CommandSourceStack> context, String name) @ L109

  • 方法名:getLootPredicate
  • 源码定位:L109
  • 返回类型:Holder
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static ResourceOrIdArgument.DialogArgument dialog(CommandBuildContext context) @ L113

  • 方法名:dialog
  • 源码定位:L113
  • 返回类型:ResourceOrIdArgument.DialogArgument
  • 修饰符:public static

参数:

  • context: CommandBuildContext

说明:

TODO

public static Holder<Dialog> getDialog(CommandContext<CommandSourceStack> context, String name) @ L117

  • 方法名:getDialog
  • 源码定位:L117
  • 返回类型:Holder
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

private static <T> Holder<T> getResource(CommandContext<CommandSourceStack> context, String name) @ L121

  • 方法名:getResource
  • 源码定位:L121
  • 返回类型: Holder
  • 修饰符:private static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public Holder<T> parse(StringReader reader) @ L125

  • 方法名:parse
  • 源码定位:L125
  • 返回类型:Holder
  • 修饰符:public

参数:

  • reader: StringReader

说明:

TODO

private <O> Holder<T> parse(StringReader reader, Grammar<ResourceOrIdArgument.Result<T,O>> grammar, DynamicOps<O> ops) @ L129

  • 方法名:parse
  • 源码定位:L129
  • 返回类型: Holder
  • 修饰符:private

参数:

  • reader: StringReader
  • grammar: Grammar<ResourceOrIdArgument.Result<T,O>>
  • ops: DynamicOps

说明:

TODO

public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) @ L136

  • 方法名:listSuggestions
  • 源码定位:L136
  • 返回类型: CompletableFuture
  • 修饰符:public

参数:

  • context: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

public Collection<String> getExamples() @ L141

  • 方法名:getExamples
  • 源码定位:L141
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

代码

public class ResourceOrIdArgument<T> implements ArgumentType<Holder<T>> {
    private static final Collection<String> EXAMPLES = List.of("foo", "foo:bar", "012", "{}", "true");
    public static final DynamicCommandExceptionType ERROR_FAILED_TO_PARSE = new DynamicCommandExceptionType(
        error -> Component.translatableEscape("argument.resource_or_id.failed_to_parse", error)
    );
    public static final Dynamic2CommandExceptionType ERROR_NO_SUCH_ELEMENT = new Dynamic2CommandExceptionType(
        (id, registry) -> Component.translatableEscape("argument.resource_or_id.no_such_element", id, registry)
    );
    public static final DynamicOps<Tag> OPS = NbtOps.INSTANCE;
    private final HolderLookup.Provider registryLookup;
    private final Optional<? extends HolderLookup.RegistryLookup<T>> elementLookup;
    private final Codec<T> codec;
    private final Grammar<ResourceOrIdArgument.Result<T, Tag>> grammar;
    private final ResourceKey<? extends Registry<T>> registryKey;
 
    protected ResourceOrIdArgument(CommandBuildContext context, ResourceKey<? extends Registry<T>> registryKey, Codec<T> codec) {
        this.registryLookup = context;
        this.elementLookup = context.lookup(registryKey);
        this.registryKey = registryKey;
        this.codec = codec;
        this.grammar = createGrammar(registryKey, OPS);
    }
 
    public static <T, O> Grammar<ResourceOrIdArgument.Result<T, O>> createGrammar(ResourceKey<? extends Registry<T>> registryKey, DynamicOps<O> ops) {
        Grammar<O> inlineValueGrammar = SnbtGrammar.createParser(ops);
        Dictionary<StringReader> rules = new Dictionary<>();
        Atom<ResourceOrIdArgument.Result<T, O>> result = Atom.of("result");
        Atom<Identifier> id = Atom.of("id");
        Atom<O> value = Atom.of("value");
        rules.put(id, IdentifierParseRule.INSTANCE);
        rules.put(value, inlineValueGrammar.top().value());
        NamedRule<StringReader, ResourceOrIdArgument.Result<T, O>> topRule = rules.put(
            result, Term.alternative(rules.named(id), rules.named(value)), scope -> {
                Identifier parsedId = scope.get(id);
                if (parsedId != null) {
                    return new ResourceOrIdArgument.ReferenceResult<>(ResourceKey.create(registryKey, parsedId));
                } else {
                    O parsedInline = scope.getOrThrow(value);
                    return new ResourceOrIdArgument.InlineResult<>(parsedInline);
                }
            }
        );
        return new Grammar<>(rules, topRule);
    }
 
    public static ResourceOrIdArgument.LootTableArgument lootTable(CommandBuildContext context) {
        return new ResourceOrIdArgument.LootTableArgument(context);
    }
 
    public static Holder<LootTable> getLootTable(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name);
    }
 
    public static ResourceOrIdArgument.LootModifierArgument lootModifier(CommandBuildContext context) {
        return new ResourceOrIdArgument.LootModifierArgument(context);
    }
 
    public static Holder<LootItemFunction> getLootModifier(CommandContext<CommandSourceStack> context, String name) {
        return getResource(context, name);
    }
 
    public static ResourceOrIdArgument.LootPredicateArgument lootPredicate(CommandBuildContext context) {
        return new ResourceOrIdArgument.LootPredicateArgument(context);
    }
 
    public static Holder<LootItemCondition> getLootPredicate(CommandContext<CommandSourceStack> context, String name) {
        return getResource(context, name);
    }
 
    public static ResourceOrIdArgument.DialogArgument dialog(CommandBuildContext context) {
        return new ResourceOrIdArgument.DialogArgument(context);
    }
 
    public static Holder<Dialog> getDialog(CommandContext<CommandSourceStack> context, String name) {
        return getResource(context, name);
    }
 
    private static <T> Holder<T> getResource(CommandContext<CommandSourceStack> context, String name) {
        return context.getArgument(name, Holder.class);
    }
 
    public @Nullable Holder<T> parse(StringReader reader) throws CommandSyntaxException {
        return this.parse(reader, this.grammar, OPS);
    }
 
    private <O> @Nullable Holder<T> parse(StringReader reader, Grammar<ResourceOrIdArgument.Result<T, O>> grammar, DynamicOps<O> ops) throws CommandSyntaxException {
        ResourceOrIdArgument.Result<T, O> contents = grammar.parseForCommands(reader);
        return this.elementLookup.isEmpty()
            ? null
            : contents.parse(reader, this.registryLookup, ops, this.codec, (HolderLookup.RegistryLookup<T>)this.elementLookup.get());
    }
 
    @Override
    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
        return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ELEMENTS);
    }
 
    @Override
    public Collection<String> getExamples() {
        return EXAMPLES;
    }
 
    public static class DialogArgument extends ResourceOrIdArgument<Dialog> {
        protected DialogArgument(CommandBuildContext context) {
            super(context, Registries.DIALOG, Dialog.DIRECT_CODEC);
        }
    }
 
    public record InlineResult<T, O>(O value) implements ResourceOrIdArgument.Result<T, O> {
        @Override
        public Holder<T> parse(
            ImmutableStringReader reader, HolderLookup.Provider lookup, DynamicOps<O> ops, Codec<T> codec, HolderLookup.RegistryLookup<T> elementLookup
        ) throws CommandSyntaxException {
            return Holder.direct(
                codec.parse(lookup.createSerializationContext(ops), this.value)
                    .getOrThrow(msg -> ResourceOrIdArgument.ERROR_FAILED_TO_PARSE.createWithContext(reader, msg))
            );
        }
    }
 
    public static class LootModifierArgument extends ResourceOrIdArgument<LootItemFunction> {
        protected LootModifierArgument(CommandBuildContext context) {
            super(context, Registries.ITEM_MODIFIER, LootItemFunctions.ROOT_CODEC);
        }
    }
 
    public static class LootPredicateArgument extends ResourceOrIdArgument<LootItemCondition> {
        protected LootPredicateArgument(CommandBuildContext context) {
            super(context, Registries.PREDICATE, LootItemCondition.DIRECT_CODEC);
        }
    }
 
    public static class LootTableArgument extends ResourceOrIdArgument<LootTable> {
        protected LootTableArgument(CommandBuildContext context) {
            super(context, Registries.LOOT_TABLE, LootTable.DIRECT_CODEC);
        }
    }
 
    public record ReferenceResult<T, O>(ResourceKey<T> key) implements ResourceOrIdArgument.Result<T, O> {
        @Override
        public Holder<T> parse(
            ImmutableStringReader reader, HolderLookup.Provider lookup, DynamicOps<O> ops, Codec<T> codec, HolderLookup.RegistryLookup<T> elementLookup
        ) throws CommandSyntaxException {
            return elementLookup.get(this.key)
                .orElseThrow(() -> ResourceOrIdArgument.ERROR_NO_SUCH_ELEMENT.createWithContext(reader, this.key.identifier(), this.key.registry()));
        }
    }
 
    public sealed interface Result<T, O> permits ResourceOrIdArgument.InlineResult, ResourceOrIdArgument.ReferenceResult {
        Holder<T> parse(
            ImmutableStringReader reader, HolderLookup.Provider lookup, DynamicOps<O> ops, Codec<T> codec, HolderLookup.RegistryLookup<T> elementLookup
        ) throws CommandSyntaxException;
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • SharedSuggestionProvider

    • 引用位置: 方法调用
    • 关联成员: SharedSuggestionProvider.listSuggestions()
  • Holder

    • 引用位置: 实现/方法调用/返回值
    • 关联成员: Holder.direct()
  • HolderLookup

    • 引用位置: 字段
  • Registry

    • 引用位置: 参数/字段
  • SnbtGrammar

    • 引用位置: 方法调用
    • 关联成员: SnbtGrammar.createParser()
  • Tag

    • 引用位置: 字段
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatableEscape()
  • ResourceKey

    • 引用位置: 参数/字段/方法调用
    • 关联成员: ResourceKey.create()
  • Dialog

    • 引用位置: 返回值
  • Atom

    • 引用位置: 方法调用
    • 关联成员: Atom.of()
  • Term

    • 引用位置: 方法调用
    • 关联成员: Term.alternative()
  • Grammar

    • 引用位置: 参数/字段/返回值
  • LootTable

    • 引用位置: 返回值
  • LootItemFunction

    • 引用位置: 返回值
  • LootItemCondition

    • 引用位置: 返回值