/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.gson.JsonObject * com.mojang.brigadier.ImmutableStringReader * com.mojang.brigadier.StringReader * com.mojang.brigadier.arguments.ArgumentType * com.mojang.brigadier.context.CommandContext * com.mojang.brigadier.exceptions.CommandSyntaxException * com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType * com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType * 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.ImmutableStringReader; 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.Dynamic2CommandExceptionType; import com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType; 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.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.HolderLookup; 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.effect.MobEffect; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.level.levelgen.feature.ConfiguredFeature; import net.minecraft.world.level.levelgen.structure.Structure; public class ResourceArgument implements ArgumentType> { private static final Collection 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> registryKey; private final HolderLookup registryLookup; public ResourceArgument(CommandBuildContext context, ResourceKey> registryKey) { this.registryKey = registryKey; this.registryLookup = context.lookupOrThrow(registryKey); } public static ResourceArgument resource(CommandBuildContext context, ResourceKey> key) { return new ResourceArgument(context, key); } public static Holder.Reference getResource(CommandContext context, String name, ResourceKey> registryKey) throws CommandSyntaxException { Holder.Reference argument = (Holder.Reference)context.getArgument(name, Holder.Reference.class); ResourceKey argumentKey = argument.key(); if (argumentKey.isFor(registryKey)) { return argument; } throw ERROR_INVALID_RESOURCE_TYPE.create((Object)argumentKey.identifier(), (Object)argumentKey.registry(), (Object)registryKey.identifier()); } public static Holder.Reference getAttribute(CommandContext context, String name) throws CommandSyntaxException { return ResourceArgument.getResource(context, name, Registries.ATTRIBUTE); } public static Holder.Reference> getConfiguredFeature(CommandContext context, String name) throws CommandSyntaxException { return ResourceArgument.getResource(context, name, Registries.CONFIGURED_FEATURE); } public static Holder.Reference getStructure(CommandContext context, String name) throws CommandSyntaxException { return ResourceArgument.getResource(context, name, Registries.STRUCTURE); } public static Holder.Reference> getEntityType(CommandContext context, String name) throws CommandSyntaxException { return ResourceArgument.getResource(context, name, Registries.ENTITY_TYPE); } public static Holder.Reference> getSummonableEntityType(CommandContext context, String name) throws CommandSyntaxException { Holder.Reference> result = ResourceArgument.getResource(context, name, Registries.ENTITY_TYPE); if (!((EntityType)result.value()).canSummon()) { throw ERROR_NOT_SUMMONABLE_ENTITY.create((Object)result.key().identifier().toString()); } return result; } public static Holder.Reference getMobEffect(CommandContext context, String name) throws CommandSyntaxException { return ResourceArgument.getResource(context, name, Registries.MOB_EFFECT); } public static Holder.Reference getEnchantment(CommandContext context, String name) throws CommandSyntaxException { return ResourceArgument.getResource(context, name, Registries.ENCHANTMENT); } public Holder.Reference parse(StringReader reader) throws CommandSyntaxException { Identifier resourceId = Identifier.read(reader); ResourceKey keyInRegistry = ResourceKey.create(this.registryKey, resourceId); return this.registryLookup.get(keyInRegistry).orElseThrow(() -> ERROR_UNKNOWN_RESOURCE.createWithContext((ImmutableStringReader)reader, (Object)resourceId, (Object)this.registryKey.identifier())); } 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(ResourceArgument 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 ResourceArgument instantiate(CommandBuildContext context) { return new ResourceArgument(context, this.registryKey); } @Override public ArgumentTypeInfo, ?> type() { return Info.this; } } } }