package net.minecraft.commands.arguments; import com.google.gson.JsonObject; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import java.util.Arrays; import java.util.Collection; import java.util.Optional; import java.util.concurrent.CompletableFuture; import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.commands.synchronization.ArgumentTypeInfo; import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.core.registries.Registries; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.levelgen.feature.ConfiguredFeature; import net.minecraft.world.level.levelgen.structure.Structure; import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool; public class ResourceKeyArgument implements ArgumentType> { private static final Collection EXAMPLES = Arrays.asList("foo", "foo:bar", "012"); private static final DynamicCommandExceptionType ERROR_INVALID_FEATURE = new DynamicCommandExceptionType( object -> Component.translatableEscape("commands.place.feature.invalid", object) ); private static final DynamicCommandExceptionType ERROR_INVALID_STRUCTURE = new DynamicCommandExceptionType( object -> Component.translatableEscape("commands.place.structure.invalid", object) ); private static final DynamicCommandExceptionType ERROR_INVALID_TEMPLATE_POOL = new DynamicCommandExceptionType( object -> Component.translatableEscape("commands.place.jigsaw.invalid", object) ); final ResourceKey> registryKey; public ResourceKeyArgument(ResourceKey> registryKey) { this.registryKey = registryKey; } public static ResourceKeyArgument key(ResourceKey> registryKey) { return new ResourceKeyArgument<>(registryKey); } private static ResourceKey getRegistryKey( CommandContext context, String argument, ResourceKey> registryKey, DynamicCommandExceptionType exception ) throws CommandSyntaxException { ResourceKey resourceKey = context.getArgument(argument, ResourceKey.class); Optional> optional = resourceKey.cast(registryKey); return (ResourceKey)optional.orElseThrow(() -> exception.create(resourceKey)); } private static Registry getRegistry(CommandContext context, ResourceKey> registryKey) { return context.getSource().getServer().registryAccess().registryOrThrow(registryKey); } private static Holder.Reference resolveKey( CommandContext context, String argument, ResourceKey> registryKey, DynamicCommandExceptionType exception ) throws CommandSyntaxException { ResourceKey resourceKey = getRegistryKey(context, argument, registryKey, exception); return (Holder.Reference)getRegistry(context, registryKey).getHolder(resourceKey).orElseThrow(() -> exception.create(resourceKey.location())); } public static Holder.Reference> getConfiguredFeature(CommandContext context, String argument) throws CommandSyntaxException { return resolveKey(context, argument, Registries.CONFIGURED_FEATURE, ERROR_INVALID_FEATURE); } public static Holder.Reference getStructure(CommandContext context, String argument) throws CommandSyntaxException { return resolveKey(context, argument, Registries.STRUCTURE, ERROR_INVALID_STRUCTURE); } public static Holder.Reference getStructureTemplatePool(CommandContext context, String argument) throws CommandSyntaxException { return resolveKey(context, argument, Registries.TEMPLATE_POOL, ERROR_INVALID_TEMPLATE_POOL); } public ResourceKey parse(StringReader reader) throws CommandSyntaxException { ResourceLocation resourceLocation = ResourceLocation.read(reader); return ResourceKey.create(this.registryKey, resourceLocation); } @Override public CompletableFuture listSuggestions(CommandContext commandContext, SuggestionsBuilder suggestionsBuilder) { return commandContext.getSource() instanceof SharedSuggestionProvider sharedSuggestionProvider ? sharedSuggestionProvider.suggestRegistryElements( this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ELEMENTS, suggestionsBuilder, commandContext ) : suggestionsBuilder.buildFuture(); } @Override public Collection getExamples() { return EXAMPLES; } public static class Info implements ArgumentTypeInfo, ResourceKeyArgument.Info.Template> { public void serializeToNetwork(ResourceKeyArgument.Info.Template template, FriendlyByteBuf buffer) { buffer.writeResourceKey(template.registryKey); } public ResourceKeyArgument.Info.Template deserializeFromNetwork(FriendlyByteBuf buffer) { return new ResourceKeyArgument.Info.Template(buffer.readRegistryKey()); } public void serializeToJson(ResourceKeyArgument.Info.Template template, JsonObject json) { json.addProperty("registry", template.registryKey.location().toString()); } public ResourceKeyArgument.Info.Template unpack(ResourceKeyArgument argument) { return new ResourceKeyArgument.Info.Template(argument.registryKey); } public final class Template implements ArgumentTypeInfo.Template> { final ResourceKey> registryKey; Template(final ResourceKey> registryKey) { this.registryKey = registryKey; } public ResourceKeyArgument instantiate(CommandBuildContext context) { return new ResourceKeyArgument<>(this.registryKey); } @Override public ArgumentTypeInfo, ?> type() { return Info.this; } } } }