ResourceArgument.java

net.minecraft.commands.arguments.ResourceArgument

信息

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

    TODO

字段/常量

  • EXAMPLES

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

      TODO

  • ERROR_NOT_SUMMONABLE_ENTITY

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

      TODO

  • ERROR_UNKNOWN_RESOURCE

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

      TODO

  • ERROR_INVALID_RESOURCE_TYPE

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

      TODO

  • registryKey

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

      TODO

  • registryLookup

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

      TODO

内部类/嵌套类型

  • net.minecraft.commands.arguments.ResourceArgument.Info

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

      TODO

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

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

      TODO

构造器

public ResourceArgument(CommandBuildContext context, ResourceKey<?extends Registry<T>> registryKey) @ L52

  • 构造器名:ResourceArgument
  • 源码定位:L52
  • 修饰符:public

参数:

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

说明:

TODO

方法

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

public static <T> ResourceArgument<T> resource(CommandBuildContext context, ResourceKey<?extends Registry<T>> key) @ L57

  • 方法名:resource
  • 源码定位:L57
  • 返回类型: ResourceArgument
  • 修饰符:public static

参数:

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

说明:

TODO

public static <T> Holder.Reference<T> getResource(CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey) @ L61

  • 方法名:getResource
  • 源码定位:L61
  • 返回类型: Holder.Reference
  • 修饰符:public static

参数:

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

说明:

TODO

public static Holder.Reference<Attribute> getAttribute(CommandContext<CommandSourceStack> context, String name) @ L71

  • 方法名:getAttribute
  • 源码定位:L71
  • 返回类型:Holder.Reference
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<ConfiguredFeature<?,?>> getConfiguredFeature(CommandContext<CommandSourceStack> context, String name) @ L75

  • 方法名:getConfiguredFeature
  • 源码定位:L75
  • 返回类型:Holder.Reference<ConfiguredFeature>
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<Structure> getStructure(CommandContext<CommandSourceStack> context, String name) @ L79

  • 方法名:getStructure
  • 源码定位:L79
  • 返回类型:Holder.Reference
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<EntityType<?>> getEntityType(CommandContext<CommandSourceStack> context, String name) @ L83

  • 方法名:getEntityType
  • 源码定位:L83
  • 返回类型:Holder.Reference<EntityType<?>>
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<EntityType<?>> getSummonableEntityType(CommandContext<CommandSourceStack> context, String name) @ L87

  • 方法名:getSummonableEntityType
  • 源码定位:L87
  • 返回类型:Holder.Reference<EntityType<?>>
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<MobEffect> getMobEffect(CommandContext<CommandSourceStack> context, String name) @ L96

  • 方法名:getMobEffect
  • 源码定位:L96
  • 返回类型:Holder.Reference
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<Enchantment> getEnchantment(CommandContext<CommandSourceStack> context, String name) @ L100

  • 方法名:getEnchantment
  • 源码定位:L100
  • 返回类型:Holder.Reference
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<WorldClock> getClock(CommandContext<CommandSourceStack> context, String name) @ L104

  • 方法名:getClock
  • 源码定位:L104
  • 返回类型:Holder.Reference
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<Timeline> getTimeline(CommandContext<CommandSourceStack> context, String name) @ L108

  • 方法名:getTimeline
  • 源码定位:L108
  • 返回类型:Holder.Reference
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public Holder.Reference<T> parse(StringReader reader) @ L112

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

参数:

  • reader: StringReader

说明:

TODO

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

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

参数:

  • context: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

public Collection<String> getExamples() @ L125

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

参数:

说明:

TODO

代码

public class ResourceArgument<T> implements ArgumentType<Holder.Reference<T>> {
    private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012");
    private static final DynamicCommandExceptionType ERROR_NOT_SUMMONABLE_ENTITY = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("entity.not_summonable", value)
    );
    public static final Dynamic2CommandExceptionType ERROR_UNKNOWN_RESOURCE = new Dynamic2CommandExceptionType(
        (id, registry) -> Component.translatableEscape("argument.resource.not_found", id, registry)
    );
    public static final Dynamic3CommandExceptionType ERROR_INVALID_RESOURCE_TYPE = new Dynamic3CommandExceptionType(
        (id, actualRegistry, expectedRegistry) -> Component.translatableEscape("argument.resource.invalid_type", id, actualRegistry, expectedRegistry)
    );
    private final ResourceKey<? extends Registry<T>> registryKey;
    private final HolderLookup<T> registryLookup;
 
    public ResourceArgument(CommandBuildContext context, ResourceKey<? extends Registry<T>> registryKey) {
        this.registryKey = registryKey;
        this.registryLookup = context.lookupOrThrow(registryKey);
    }
 
    public static <T> ResourceArgument<T> resource(CommandBuildContext context, ResourceKey<? extends Registry<T>> key) {
        return new ResourceArgument<>(context, key);
    }
 
    public static <T> Holder.Reference<T> getResource(CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey) throws CommandSyntaxException {
        Holder.Reference<T> argument = context.getArgument(name, Holder.Reference.class);
        ResourceKey<?> argumentKey = argument.key();
        if (argumentKey.isFor(registryKey)) {
            return argument;
        } else {
            throw ERROR_INVALID_RESOURCE_TYPE.create(argumentKey.identifier(), argumentKey.registry(), registryKey.identifier());
        }
    }
 
    public static Holder.Reference<Attribute> getAttribute(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.ATTRIBUTE);
    }
 
    public static Holder.Reference<ConfiguredFeature<?, ?>> getConfiguredFeature(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.CONFIGURED_FEATURE);
    }
 
    public static Holder.Reference<Structure> getStructure(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.STRUCTURE);
    }
 
    public static Holder.Reference<EntityType<?>> getEntityType(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.ENTITY_TYPE);
    }
 
    public static Holder.Reference<EntityType<?>> getSummonableEntityType(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        Holder.Reference<EntityType<?>> result = getResource(context, name, Registries.ENTITY_TYPE);
        if (!result.value().canSummon()) {
            throw ERROR_NOT_SUMMONABLE_ENTITY.create(result.key().identifier().toString());
        } else {
            return result;
        }
    }
 
    public static Holder.Reference<MobEffect> getMobEffect(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.MOB_EFFECT);
    }
 
    public static Holder.Reference<Enchantment> getEnchantment(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.ENCHANTMENT);
    }
 
    public static Holder.Reference<WorldClock> getClock(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.WORLD_CLOCK);
    }
 
    public static Holder.Reference<Timeline> getTimeline(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return getResource(context, name, Registries.TIMELINE);
    }
 
    public Holder.Reference<T> parse(StringReader reader) throws CommandSyntaxException {
        Identifier resourceId = Identifier.read(reader);
        ResourceKey<T> keyInRegistry = ResourceKey.create(this.registryKey, resourceId);
        return this.registryLookup
            .get(keyInRegistry)
            .orElseThrow(() -> ERROR_UNKNOWN_RESOURCE.createWithContext(reader, resourceId, this.registryKey.identifier()));
    }
 
    @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 Info<T> implements ArgumentTypeInfo<ResourceArgument<T>, ResourceArgument.Info<T>.Template> {
        public void serializeToNetwork(ResourceArgument.Info<T>.Template template, FriendlyByteBuf out) {
            out.writeResourceKey(template.registryKey);
        }
 
        public ResourceArgument.Info<T>.Template deserializeFromNetwork(FriendlyByteBuf in) {
            return new ResourceArgument.Info.Template(in.readRegistryKey());
        }
 
        public void serializeToJson(ResourceArgument.Info<T>.Template template, JsonObject out) {
            out.addProperty("registry", template.registryKey.identifier().toString());
        }
 
        public ResourceArgument.Info<T>.Template unpack(ResourceArgument<T> argument) {
            return new ResourceArgument.Info.Template(argument.registryKey);
        }
 
        public final class Template implements ArgumentTypeInfo.Template<ResourceArgument<T>> {
            private final ResourceKey<? extends Registry<T>> registryKey;
 
            private Template(ResourceKey<? extends Registry<T>> registryKey) {
                Objects.requireNonNull(Info.this);
                super();
                this.registryKey = registryKey;
            }
 
            public ResourceArgument<T> instantiate(CommandBuildContext context) {
                return new ResourceArgument<>(context, this.registryKey);
            }
 
            @Override
            public ArgumentTypeInfo<ResourceArgument<T>, ?> type() {
                return Info.this;
            }
        }
    }
}

引用的其他类