This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
SorceryCraft/src/main/java/com/thebrokenrail/sorcerycraft/command/SpellArgumentType.java

58 lines
2.4 KiB
Java

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.server.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;
public class SpellArgumentType implements ArgumentType<Identifier> {
private static final Collection<String> 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<ServerCommandSource> 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 <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> 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<String> getExamples() {
return EXAMPLES;
}
}