package net.minecraft.commands.arguments;

import java.util.Arrays;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.StringReader;
import net.minecraft.core.Registry;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.level.storage.loot.PredicateManager;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.Recipe;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.advancements.Advancement;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import java.util.Collection;
import net.minecraft.resources.ResourceLocation;
import com.mojang.brigadier.arguments.ArgumentType;

public class ResourceLocationArgument implements ArgumentType<ResourceLocation> {
    private static final Collection<String> EXAMPLES;
    private static final DynamicCommandExceptionType ERROR_UNKNOWN_ADVANCEMENT;
    private static final DynamicCommandExceptionType ERROR_UNKNOWN_RECIPE;
    private static final DynamicCommandExceptionType ERROR_UNKNOWN_PREDICATE;
    private static final DynamicCommandExceptionType ERROR_UNKNOWN_ATTRIBUTE;
    
    public static ResourceLocationArgument id() {
        return new ResourceLocationArgument();
    }
    
    public static Advancement getAdvancement(final CommandContext<CommandSourceStack> commandContext, final String string) throws CommandSyntaxException {
        final ResourceLocation uh3 = (ResourceLocation)commandContext.getArgument(string, (Class)ResourceLocation.class);
        final Advancement w4 = ((CommandSourceStack)commandContext.getSource()).getServer().getAdvancements().getAdvancement(uh3);
        if (w4 == null) {
            throw ResourceLocationArgument.ERROR_UNKNOWN_ADVANCEMENT.create(uh3);
        }
        return w4;
    }
    
    public static Recipe<?> getRecipe(final CommandContext<CommandSourceStack> commandContext, final String string) throws CommandSyntaxException {
        final RecipeManager bmv3 = ((CommandSourceStack)commandContext.getSource()).getServer().getRecipeManager();
        final ResourceLocation uh4 = (ResourceLocation)commandContext.getArgument(string, (Class)ResourceLocation.class);
        return bmv3.byKey(uh4).<Throwable>orElseThrow(() -> ResourceLocationArgument.ERROR_UNKNOWN_RECIPE.create(uh4));
    }
    
    public static LootItemCondition getPredicate(final CommandContext<CommandSourceStack> commandContext, final String string) throws CommandSyntaxException {
        final ResourceLocation uh3 = (ResourceLocation)commandContext.getArgument(string, (Class)ResourceLocation.class);
        final PredicateManager day4 = ((CommandSourceStack)commandContext.getSource()).getServer().getPredicateManager();
        final LootItemCondition ddm5 = day4.get(uh3);
        if (ddm5 == null) {
            throw ResourceLocationArgument.ERROR_UNKNOWN_PREDICATE.create(uh3);
        }
        return ddm5;
    }
    
    public static Attribute getAttribute(final CommandContext<CommandSourceStack> commandContext, final String string) throws CommandSyntaxException {
        final ResourceLocation uh3 = (ResourceLocation)commandContext.getArgument(string, (Class)ResourceLocation.class);
        return Registry.ATTRIBUTE.getOptional(uh3).<Throwable>orElseThrow(() -> ResourceLocationArgument.ERROR_UNKNOWN_ATTRIBUTE.create(uh3));
    }
    
    public static ResourceLocation getId(final CommandContext<CommandSourceStack> commandContext, final String string) {
        return (ResourceLocation)commandContext.getArgument(string, (Class)ResourceLocation.class);
    }
    
    public ResourceLocation parse(final StringReader stringReader) throws CommandSyntaxException {
        return ResourceLocation.read(stringReader);
    }
    
    public Collection<String> getExamples() {
        return ResourceLocationArgument.EXAMPLES;
    }
    
    static {
        EXAMPLES = Arrays.<String>asList("foo", "foo:bar", "012");
        final TranslatableComponent translatableComponent;
        ERROR_UNKNOWN_ADVANCEMENT = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("advancement.advancementNotFound", new Object[] { object });
            return translatableComponent;
        });
        final TranslatableComponent translatableComponent2;
        ERROR_UNKNOWN_RECIPE = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("recipe.notFound", new Object[] { object });
            return translatableComponent2;
        });
        final TranslatableComponent translatableComponent3;
        ERROR_UNKNOWN_PREDICATE = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("predicate.unknown", new Object[] { object });
            return translatableComponent3;
        });
        final TranslatableComponent translatableComponent4;
        ERROR_UNKNOWN_ATTRIBUTE = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("attribute.unknown", new Object[] { object });
            return translatableComponent4;
        });
    }
}
