ResourceOrTagArgument.java

net.minecraft.commands.arguments.ResourceOrTagArgument

信息

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

    TODO

字段/常量

  • EXAMPLES

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

      TODO

  • ERROR_UNKNOWN_TAG

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

      TODO

  • ERROR_INVALID_TAG_TYPE

    • 类型: Dynamic3CommandExceptionType
    • 修饰符: private static final
    • 源码定位: L38
    • 说明:

      TODO

  • registryLookup

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

      TODO

  • registryKey

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

      TODO

内部类/嵌套类型

  • net.minecraft.commands.arguments.ResourceOrTagArgument.Info

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

      TODO

  • net.minecraft.commands.arguments.ResourceOrTagArgument.Info.Template

    • 类型: class
    • 修饰符: public final
    • 源码定位: L120
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrTagArgument.ResourceResult

    • 类型: record
    • 修饰符: private
    • 源码定位: L140
    • 说明:

      TODO

  • net.minecraft.commands.arguments.ResourceOrTagArgument.Result

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

      TODO

  • net.minecraft.commands.arguments.ResourceOrTagArgument.TagResult

    • 类型: record
    • 修饰符: private
    • 源码定位: L169
    • 说明:

      TODO

构造器

public ResourceOrTagArgument(CommandBuildContext context, ResourceKey<?extends Registry<T>> registryKey) @ L44

  • 构造器名:ResourceOrTagArgument
  • 源码定位:L44
  • 修饰符:public

参数:

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

说明:

TODO

方法

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

public static <T> ResourceOrTagArgument<T> resourceOrTag(CommandBuildContext context, ResourceKey<?extends Registry<T>> key) @ L49

  • 方法名:resourceOrTag
  • 源码定位:L49
  • 返回类型: ResourceOrTagArgument
  • 修饰符:public static

参数:

  • context: CommandBuildContext
  • key: ResourceKey<?extends Registry>

说明:

TODO

public static <T> ResourceOrTagArgument.Result<T> getResourceOrTag(CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey) @ L53

  • 方法名:getResourceOrTag
  • 源码定位:L53
  • 返回类型: ResourceOrTagArgument.Result
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String
  • registryKey: ResourceKey<Registry>

说明:

TODO

public ResourceOrTagArgument.Result<T> parse(StringReader reader) @ L67

  • 方法名:parse
  • 源码定位:L67
  • 返回类型:ResourceOrTagArgument.Result
  • 修饰符:public

参数:

  • reader: StringReader

说明:

TODO

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

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

参数:

  • context: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

public Collection<String> getExamples() @ L98

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

参数:

说明:

TODO

代码

public class ResourceOrTagArgument<T> implements ArgumentType<ResourceOrTagArgument.Result<T>> {
    private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012", "#skeletons", "#minecraft:skeletons");
    private static final Dynamic2CommandExceptionType ERROR_UNKNOWN_TAG = new Dynamic2CommandExceptionType(
        (id, registry) -> Component.translatableEscape("argument.resource_tag.not_found", id, registry)
    );
    private static final Dynamic3CommandExceptionType ERROR_INVALID_TAG_TYPE = new Dynamic3CommandExceptionType(
        (id, actualRegistry, expectedRegistry) -> Component.translatableEscape("argument.resource_tag.invalid_type", id, actualRegistry, expectedRegistry)
    );
    private final HolderLookup<T> registryLookup;
    private final ResourceKey<? extends Registry<T>> registryKey;
 
    public ResourceOrTagArgument(CommandBuildContext context, ResourceKey<? extends Registry<T>> registryKey) {
        this.registryKey = registryKey;
        this.registryLookup = context.lookupOrThrow(registryKey);
    }
 
    public static <T> ResourceOrTagArgument<T> resourceOrTag(CommandBuildContext context, ResourceKey<? extends Registry<T>> key) {
        return new ResourceOrTagArgument<>(context, key);
    }
 
    public static <T> ResourceOrTagArgument.Result<T> getResourceOrTag(
        CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey
    ) throws CommandSyntaxException {
        ResourceOrTagArgument.Result<?> argument = context.getArgument(name, ResourceOrTagArgument.Result.class);
        Optional<ResourceOrTagArgument.Result<T>> value = argument.cast(registryKey);
        return value.orElseThrow(() -> argument.unwrap().map(element -> {
            ResourceKey<?> elementKey = element.key();
            return ResourceArgument.ERROR_INVALID_RESOURCE_TYPE.create(elementKey.identifier(), elementKey.registry(), registryKey.identifier());
        }, tag -> {
            TagKey<?> tagKey = tag.key();
            return ERROR_INVALID_TAG_TYPE.create(tagKey.location(), tagKey.registry(), registryKey.identifier());
        }));
    }
 
    public ResourceOrTagArgument.Result<T> parse(StringReader reader) throws CommandSyntaxException {
        if (reader.canRead() && reader.peek() == '#') {
            int cursor = reader.getCursor();
 
            try {
                reader.skip();
                Identifier tagId = Identifier.read(reader);
                TagKey<T> tagKey = TagKey.create(this.registryKey, tagId);
                HolderSet.Named<T> holderSet = this.registryLookup
                    .get(tagKey)
                    .orElseThrow(() -> ERROR_UNKNOWN_TAG.createWithContext(reader, tagId, this.registryKey.identifier()));
                return new ResourceOrTagArgument.TagResult<>(holderSet);
            } catch (CommandSyntaxException var6) {
                reader.setCursor(cursor);
                throw var6;
            }
        } else {
            Identifier resourceId = Identifier.read(reader);
            ResourceKey<T> resourceKey = ResourceKey.create(this.registryKey, resourceId);
            Holder.Reference<T> holder = this.registryLookup
                .get(resourceKey)
                .orElseThrow(() -> ResourceArgument.ERROR_UNKNOWN_RESOURCE.createWithContext(reader, resourceId, this.registryKey.identifier()));
            return new ResourceOrTagArgument.ResourceResult<>(holder);
        }
    }
 
    @Override
    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
        return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ALL);
    }
 
    @Override
    public Collection<String> getExamples() {
        return EXAMPLES;
    }
 
    public static class Info<T> implements ArgumentTypeInfo<ResourceOrTagArgument<T>, ResourceOrTagArgument.Info<T>.Template> {
        public void serializeToNetwork(ResourceOrTagArgument.Info<T>.Template template, FriendlyByteBuf out) {
            out.writeResourceKey(template.registryKey);
        }
 
        public ResourceOrTagArgument.Info<T>.Template deserializeFromNetwork(FriendlyByteBuf in) {
            return new ResourceOrTagArgument.Info.Template(in.readRegistryKey());
        }
 
        public void serializeToJson(ResourceOrTagArgument.Info<T>.Template template, JsonObject out) {
            out.addProperty("registry", template.registryKey.identifier().toString());
        }
 
        public ResourceOrTagArgument.Info<T>.Template unpack(ResourceOrTagArgument<T> argument) {
            return new ResourceOrTagArgument.Info.Template(argument.registryKey);
        }
 
        public final class Template implements ArgumentTypeInfo.Template<ResourceOrTagArgument<T>> {
            private final ResourceKey<? extends Registry<T>> registryKey;
 
            private Template(ResourceKey<? extends Registry<T>> registryKey) {
                Objects.requireNonNull(Info.this);
                super();
                this.registryKey = registryKey;
            }
 
            public ResourceOrTagArgument<T> instantiate(CommandBuildContext context) {
                return new ResourceOrTagArgument<>(context, this.registryKey);
            }
 
            @Override
            public ArgumentTypeInfo<ResourceOrTagArgument<T>, ?> type() {
                return Info.this;
            }
        }
    }
 
    private record ResourceResult<T>(Holder.Reference<T> value) implements ResourceOrTagArgument.Result<T> {
        @Override
        public Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap() {
            return Either.left(this.value);
        }
 
        @Override
        public <E> Optional<ResourceOrTagArgument.Result<E>> cast(ResourceKey<? extends Registry<E>> registryKey) {
            return this.value.key().isFor(registryKey) ? Optional.of((ResourceOrTagArgument.Result<E>)this) : Optional.empty();
        }
 
        public boolean test(Holder<T> holder) {
            return holder.equals(this.value);
        }
 
        @Override
        public String asPrintable() {
            return this.value.key().identifier().toString();
        }
    }
 
    public interface Result<T> extends Predicate<Holder<T>> {
        Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap();
 
        <E> Optional<ResourceOrTagArgument.Result<E>> cast(final ResourceKey<? extends Registry<E>> registryKey);
 
        String asPrintable();
    }
 
    private record TagResult<T>(HolderSet.Named<T> tag) implements ResourceOrTagArgument.Result<T> {
        @Override
        public Either<Holder.Reference<T>, HolderSet.Named<T>> unwrap() {
            return Either.right(this.tag);
        }
 
        @Override
        public <E> Optional<ResourceOrTagArgument.Result<E>> cast(ResourceKey<? extends Registry<E>> registryKey) {
            return this.tag.key().isFor(registryKey) ? Optional.of((ResourceOrTagArgument.Result<E>)this) : Optional.empty();
        }
 
        public boolean test(Holder<T> holder) {
            return this.tag.contains(holder);
        }
 
        @Override
        public String asPrintable() {
            return "#" + this.tag.key().location();
        }
    }
}

引用的其他类