package com.thebrokenrail.sorcerycraft.command; 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 java.util.Arrays; import java.util.Collection; import java.util.concurrent.CompletableFuture; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import com.thebrokenrail.sorcerycraft.SorceryCraft; import com.thebrokenrail.sorcerycraft.spell.api.Spell; import com.thebrokenrail.sorcerycraft.spell.api.registry.SpellRegistry; import net.minecraft.command.CommandSource; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; public class SpellArgumentType implements ArgumentType { private static final Collection EXAMPLES = Arrays.asList("sorcerycraft:heal_spell", "sorcerycraft:damage_spell"); public static final DynamicCommandExceptionType NOT_FOUND_EXCEPTION = new DynamicCommandExceptionType((object) -> new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.unknown_spell", object)); public SpellArgumentType() { } public static SpellArgumentType spell() { return new SpellArgumentType(); } public static Identifier getSpell(CommandContext context, String name) throws CommandSyntaxException { return validate(context.getArgument(name, Identifier.class)); } private static Identifier validate(Identifier identifier) throws CommandSyntaxException { Spell testSpell = SpellRegistry.getSpell(identifier, 0); if (testSpell == null) { throw NOT_FOUND_EXCEPTION.create(identifier); } return identifier; } @Override public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { return CommandSource.suggestIdentifiers(Arrays.asList(SpellRegistry.getSpellsID()), builder); } public Identifier parse(StringReader stringReader) throws CommandSyntaxException { return validate(Identifier.fromCommandInput(stringReader)); } public Collection getExamples() { return EXAMPLES; } }