TheBrokenRail
4ebae792ac
All checks were successful
SorceryCraft/pipeline/head This commit looks good
Update API (More Consistent Package Names) Fix Bug When Casting Spell
58 lines
2.4 KiB
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;
|
|
}
|
|
}
|