ResourceKeyArgument.java

net.minecraft.commands.arguments.ResourceKeyArgument

信息

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

    TODO

字段/常量

  • EXAMPLES

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

      TODO

  • ERROR_INVALID_FEATURE

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

      TODO

  • ERROR_INVALID_STRUCTURE

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

      TODO

  • ERROR_INVALID_TEMPLATE_POOL

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

      TODO

  • ERROR_INVALID_RECIPE

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

      TODO

  • ERROR_INVALID_ADVANCEMENT

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

      TODO

  • registryKey

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

      TODO

内部类/嵌套类型

  • net.minecraft.commands.arguments.ResourceKeyArgument.Info

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

      TODO

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

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

      TODO

构造器

public ResourceKeyArgument(ResourceKey<?extends Registry<T>> registryKey) @ L55

  • 构造器名:ResourceKeyArgument
  • 源码定位:L55
  • 修饰符:public

参数:

  • registryKey: ResourceKey<?extends Registry>

说明:

TODO

方法

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

public static <T> ResourceKeyArgument<T> key(ResourceKey<?extends Registry<T>> key) @ L59

  • 方法名:key
  • 源码定位:L59
  • 返回类型: ResourceKeyArgument
  • 修饰符:public static

参数:

  • key: ResourceKey<?extends Registry>

说明:

TODO

public static <T> ResourceKey<T> getRegistryKey(CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey, DynamicCommandExceptionType exceptionType) @ L63

  • 方法名:getRegistryKey
  • 源码定位:L63
  • 返回类型: ResourceKey
  • 修饰符:public static

参数:

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

说明:

TODO

private static <T> Registry<T> getRegistry(CommandContext<CommandSourceStack> context, ResourceKey<?extends Registry<T>> registryKey) @ L71

  • 方法名:getRegistry
  • 源码定位:L71
  • 返回类型: Registry
  • 修饰符:private static

参数:

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

说明:

TODO

private static <T> Holder.Reference<T> resolveKey(CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey, DynamicCommandExceptionType exception) @ L75

  • 方法名:resolveKey
  • 源码定位:L75
  • 返回类型: Holder.Reference
  • 修饰符:private static

参数:

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

说明:

TODO

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

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

参数:

  • context: CommandContext
  • name: String

说明:

TODO

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

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

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static Holder.Reference<StructureTemplatePool> getStructureTemplatePool(CommandContext<CommandSourceStack> context, String name) @ L90

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

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static RecipeHolder<?> getRecipe(CommandContext<CommandSourceStack> context, String name) @ L94

  • 方法名:getRecipe
  • 源码定位:L94
  • 返回类型:RecipeHolder<?>
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public static AdvancementHolder getAdvancement(CommandContext<CommandSourceStack> context, String name) @ L100

  • 方法名:getAdvancement
  • 源码定位:L100
  • 返回类型:AdvancementHolder
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public ResourceKey<T> parse(StringReader reader) @ L110

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

参数:

  • reader: StringReader

说明:

TODO

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

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

参数:

  • context: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

public Collection<String> getExamples() @ L120

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

参数:

说明:

TODO

代码

public class ResourceKeyArgument<T> implements ArgumentType<ResourceKey<T>> {
    private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012");
    private static final DynamicCommandExceptionType ERROR_INVALID_FEATURE = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("commands.place.feature.invalid", value)
    );
    private static final DynamicCommandExceptionType ERROR_INVALID_STRUCTURE = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("commands.place.structure.invalid", value)
    );
    private static final DynamicCommandExceptionType ERROR_INVALID_TEMPLATE_POOL = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("commands.place.jigsaw.invalid", value)
    );
    private static final DynamicCommandExceptionType ERROR_INVALID_RECIPE = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("recipe.notFound", value)
    );
    private static final DynamicCommandExceptionType ERROR_INVALID_ADVANCEMENT = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("advancement.advancementNotFound", value)
    );
    private final ResourceKey<? extends Registry<T>> registryKey;
 
    public ResourceKeyArgument(ResourceKey<? extends Registry<T>> registryKey) {
        this.registryKey = registryKey;
    }
 
    public static <T> ResourceKeyArgument<T> key(ResourceKey<? extends Registry<T>> key) {
        return new ResourceKeyArgument<>(key);
    }
 
    public static <T> ResourceKey<T> getRegistryKey(
        CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey, DynamicCommandExceptionType exceptionType
    ) throws CommandSyntaxException {
        ResourceKey<?> argument = context.getArgument(name, ResourceKey.class);
        Optional<ResourceKey<T>> value = argument.cast(registryKey);
        return value.orElseThrow(() -> exceptionType.create(argument.identifier()));
    }
 
    private static <T> Registry<T> getRegistry(CommandContext<CommandSourceStack> context, ResourceKey<? extends Registry<T>> registryKey) {
        return context.getSource().getServer().registryAccess().lookupOrThrow(registryKey);
    }
 
    private static <T> Holder.Reference<T> resolveKey(
        CommandContext<CommandSourceStack> context, String name, ResourceKey<Registry<T>> registryKey, DynamicCommandExceptionType exception
    ) throws CommandSyntaxException {
        ResourceKey<T> key = getRegistryKey(context, name, registryKey, exception);
        return getRegistry(context, registryKey).get(key).orElseThrow(() -> exception.create(key.identifier()));
    }
 
    public static Holder.Reference<ConfiguredFeature<?, ?>> getConfiguredFeature(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return resolveKey(context, name, Registries.CONFIGURED_FEATURE, ERROR_INVALID_FEATURE);
    }
 
    public static Holder.Reference<Structure> getStructure(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return resolveKey(context, name, Registries.STRUCTURE, ERROR_INVALID_STRUCTURE);
    }
 
    public static Holder.Reference<StructureTemplatePool> getStructureTemplatePool(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return resolveKey(context, name, Registries.TEMPLATE_POOL, ERROR_INVALID_TEMPLATE_POOL);
    }
 
    public static RecipeHolder<?> getRecipe(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        RecipeManager recipeManager = context.getSource().getServer().getRecipeManager();
        ResourceKey<Recipe<?>> key = getRegistryKey(context, name, Registries.RECIPE, ERROR_INVALID_RECIPE);
        return recipeManager.byKey(key).orElseThrow(() -> ERROR_INVALID_RECIPE.create(key.identifier()));
    }
 
    public static AdvancementHolder getAdvancement(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        ResourceKey<Advancement> key = getRegistryKey(context, name, Registries.ADVANCEMENT, ERROR_INVALID_ADVANCEMENT);
        AdvancementHolder advancement = context.getSource().getServer().getAdvancements().get(key.identifier());
        if (advancement == null) {
            throw ERROR_INVALID_ADVANCEMENT.create(key.identifier());
        } else {
            return advancement;
        }
    }
 
    public ResourceKey<T> parse(StringReader reader) throws CommandSyntaxException {
        Identifier resourceId = Identifier.read(reader);
        return ResourceKey.create(this.registryKey, resourceId);
    }
 
    @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<ResourceKeyArgument<T>, ResourceKeyArgument.Info<T>.Template> {
        public void serializeToNetwork(ResourceKeyArgument.Info<T>.Template template, FriendlyByteBuf out) {
            out.writeResourceKey(template.registryKey);
        }
 
        public ResourceKeyArgument.Info<T>.Template deserializeFromNetwork(FriendlyByteBuf in) {
            return new ResourceKeyArgument.Info.Template(in.readRegistryKey());
        }
 
        public void serializeToJson(ResourceKeyArgument.Info<T>.Template template, JsonObject out) {
            out.addProperty("registry", template.registryKey.identifier().toString());
        }
 
        public ResourceKeyArgument.Info<T>.Template unpack(ResourceKeyArgument<T> argument) {
            return new ResourceKeyArgument.Info.Template(argument.registryKey);
        }
 
        public final class Template implements ArgumentTypeInfo.Template<ResourceKeyArgument<T>> {
            private final ResourceKey<? extends Registry<T>> registryKey;
 
            private Template(ResourceKey<? extends Registry<T>> registryKey) {
                Objects.requireNonNull(Info.this);
                super();
                this.registryKey = registryKey;
            }
 
            public ResourceKeyArgument<T> instantiate(CommandBuildContext context) {
                return new ResourceKeyArgument<>(this.registryKey);
            }
 
            @Override
            public ArgumentTypeInfo<ResourceKeyArgument<T>, ?> type() {
                return Info.this;
            }
        }
    }
}

引用的其他类