/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.gson.JsonObject * com.mojang.brigadier.StringReader * com.mojang.brigadier.arguments.ArgumentType * com.mojang.brigadier.context.CommandContext * com.mojang.brigadier.exceptions.CommandSyntaxException * com.mojang.brigadier.exceptions.DynamicCommandExceptionType * com.mojang.brigadier.suggestion.Suggestions * com.mojang.brigadier.suggestion.SuggestionsBuilder */ 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.advancements.AdvancementHolder; 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.Identifier; import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.item.crafting.RecipeManager; 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(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> registryKey; public ResourceKeyArgument(ResourceKey> registryKey) { this.registryKey = registryKey; } public static ResourceKeyArgument key(ResourceKey> key) { return new ResourceKeyArgument(key); } public static ResourceKey getRegistryKey(CommandContext context, String name, ResourceKey> registryKey, DynamicCommandExceptionType exceptionType) throws CommandSyntaxException { ResourceKey argument = (ResourceKey)context.getArgument(name, ResourceKey.class); Optional> value = argument.cast(registryKey); return value.orElseThrow(() -> exceptionType.create((Object)argument.identifier())); } private static Registry getRegistry(CommandContext context, ResourceKey> registryKey) { return ((CommandSourceStack)context.getSource()).getServer().registryAccess().lookupOrThrow(registryKey); } private static Holder.Reference resolveKey(CommandContext context, String name, ResourceKey> registryKey, DynamicCommandExceptionType exception) throws CommandSyntaxException { ResourceKey key = ResourceKeyArgument.getRegistryKey(context, name, registryKey, exception); return (Holder.Reference)ResourceKeyArgument.getRegistry(context, registryKey).get(key).orElseThrow(() -> exception.create((Object)key.identifier())); } public static Holder.Reference> getConfiguredFeature(CommandContext context, String name) throws CommandSyntaxException { return ResourceKeyArgument.resolveKey(context, name, Registries.CONFIGURED_FEATURE, ERROR_INVALID_FEATURE); } public static Holder.Reference getStructure(CommandContext context, String name) throws CommandSyntaxException { return ResourceKeyArgument.resolveKey(context, name, Registries.STRUCTURE, ERROR_INVALID_STRUCTURE); } public static Holder.Reference getStructureTemplatePool(CommandContext context, String name) throws CommandSyntaxException { return ResourceKeyArgument.resolveKey(context, name, Registries.TEMPLATE_POOL, ERROR_INVALID_TEMPLATE_POOL); } public static RecipeHolder getRecipe(CommandContext context, String name) throws CommandSyntaxException { RecipeManager recipeManager = ((CommandSourceStack)context.getSource()).getServer().getRecipeManager(); ResourceKey> key = ResourceKeyArgument.getRegistryKey(context, name, Registries.RECIPE, ERROR_INVALID_RECIPE); return recipeManager.byKey(key).orElseThrow(() -> ERROR_INVALID_RECIPE.create((Object)key.identifier())); } public static AdvancementHolder getAdvancement(CommandContext context, String name) throws CommandSyntaxException { ResourceKey key = ResourceKeyArgument.getRegistryKey(context, name, Registries.ADVANCEMENT, ERROR_INVALID_ADVANCEMENT); AdvancementHolder advancement = ((CommandSourceStack)context.getSource()).getServer().getAdvancements().get(key.identifier()); if (advancement == null) { throw ERROR_INVALID_ADVANCEMENT.create((Object)key.identifier()); } return advancement; } public ResourceKey parse(StringReader reader) throws CommandSyntaxException { Identifier resourceId = Identifier.read(reader); return ResourceKey.create(this.registryKey, resourceId); } public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { return SharedSuggestionProvider.listSuggestions(context, builder, this.registryKey, SharedSuggestionProvider.ElementSuggestionType.ELEMENTS); } public Collection getExamples() { return EXAMPLES; } public static class Info implements ArgumentTypeInfo, Template> { @Override public void serializeToNetwork(Template template, FriendlyByteBuf out) { out.writeResourceKey(template.registryKey); } @Override public Template deserializeFromNetwork(FriendlyByteBuf in) { return new Template(in.readRegistryKey()); } @Override public void serializeToJson(Template template, JsonObject out) { out.addProperty("registry", template.registryKey.identifier().toString()); } @Override public Template unpack(ResourceKeyArgument argument) { return new Template(argument.registryKey); } public final class Template implements ArgumentTypeInfo.Template> { private final ResourceKey> registryKey; private Template(ResourceKey> registryKey) { this.registryKey = registryKey; } @Override public ResourceKeyArgument instantiate(CommandBuildContext context) { return new ResourceKeyArgument(this.registryKey); } @Override public ArgumentTypeInfo, ?> type() { return Info.this; } } } }