Search in sources :

Example 1 with CommandRegistrationException

use of dev.triumphteam.cmd.core.exceptions.CommandRegistrationException in project triumph-cmds by TriumphTeam.

the class SlashCommand method asCommandData.

@NotNull
public SlashCommandData asCommandData() {
    final SlashCommandData commandData = Commands.slash(name, description);
    commandData.setDefaultEnabled(enabledRoles.isEmpty());
    if (isDefault) {
        final SlashSubCommand<S> subCommand = getDefaultSubCommand();
        // Should never be null.
        if (subCommand == null)
            throw new CommandRegistrationException("Could not find default subcommand");
        commandData.addOptions(subCommand.getJdaOptions());
        return commandData;
    }
    final List<SubcommandData> subData = subCommands.entrySet().stream().map(entry -> new SubcommandData(entry.getKey(), entry.getValue().getDescription()).addOptions(entry.getValue().getJdaOptions())).collect(Collectors.toList());
    commandData.addSubcommands(subData);
    return commandData;
}
Also used : Command(dev.triumphteam.cmd.core.Command) NamedArgumentRegistry(dev.triumphteam.cmd.core.argument.named.NamedArgumentRegistry) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) ExecutionProvider(dev.triumphteam.cmd.core.execution.ExecutionProvider) ArgumentRegistry(dev.triumphteam.cmd.core.argument.ArgumentRegistry) MessageRegistry(dev.triumphteam.cmd.core.message.MessageRegistry) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BaseCommand(dev.triumphteam.cmd.core.BaseCommand) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Default(dev.triumphteam.cmd.core.annotation.Default) ChoiceRegistry(dev.triumphteam.cmd.slash.choices.ChoiceRegistry) Registry(dev.triumphteam.cmd.core.registry.Registry) RequirementRegistry(dev.triumphteam.cmd.core.requirement.RequirementRegistry) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull) SenderValidator(dev.triumphteam.cmd.core.sender.SenderValidator) Method(java.lang.reflect.Method) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CommandRegistrationException

use of dev.triumphteam.cmd.core.exceptions.CommandRegistrationException in project triumph-cmds by TriumphTeam.

the class AbstractCommandProcessor method extractCommandNames.

/**
 * Helper method for getting the command names from the command annotation.
 */
private void extractCommandNames() {
    final Command commandAnnotation = annotatedClass.getAnnotation(Command.class);
    if (commandAnnotation == null) {
        final String commandName = baseCommand.getCommand();
        if (commandName == null) {
            throw new CommandRegistrationException("Command name or \"@" + Command.class.getSimpleName() + "\" annotation missing", baseCommand.getClass());
        }
        name = commandName;
        alias.addAll(baseCommand.getAlias());
    } else {
        name = commandAnnotation.value();
        Collections.addAll(alias, commandAnnotation.alias());
    }
    alias.addAll(baseCommand.getAlias());
    if (name.isEmpty()) {
        throw new CommandRegistrationException("Command name must not be empty", baseCommand.getClass());
    }
}
Also used : CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) Command(dev.triumphteam.cmd.core.annotation.Command) BaseCommand(dev.triumphteam.cmd.core.BaseCommand)

Example 3 with CommandRegistrationException

use of dev.triumphteam.cmd.core.exceptions.CommandRegistrationException in project triumph-cmds by TriumphTeam.

the class BukkitCommandManager method getCommandMap.

/**
 * Gets the Command Map to register the commands
 *
 * @return The Command Map
 */
@NotNull
private CommandMap getCommandMap() {
    try {
        final Server server = Bukkit.getServer();
        final Method getCommandMap = server.getClass().getDeclaredMethod("getCommandMap");
        getCommandMap.setAccessible(true);
        return (CommandMap) getCommandMap.invoke(server);
    } catch (final Exception ignored) {
        throw new CommandRegistrationException("Unable get Command Map. Commands will not be registered!");
    }
}
Also used : Server(org.bukkit.Server) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) Method(java.lang.reflect.Method) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) CommandMap(org.bukkit.command.CommandMap) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with CommandRegistrationException

use of dev.triumphteam.cmd.core.exceptions.CommandRegistrationException in project triumph-cmds by TriumphTeam.

the class BukkitCommandManager method getBukkitCommands.

@NotNull
private Map<String, org.bukkit.command.Command> getBukkitCommands(@NotNull final CommandMap commandMap) {
    try {
        final Field bukkitCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
        bukkitCommands.setAccessible(true);
        // noinspection unchecked
        return (Map<String, org.bukkit.command.Command>) bukkitCommands.get(commandMap);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new CommandRegistrationException("Unable get Bukkit commands. Commands might not be registered correctly!");
    }
}
Also used : Field(java.lang.reflect.Field) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) HashMap(java.util.HashMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Map(java.util.Map) CommandMap(org.bukkit.command.CommandMap) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with CommandRegistrationException

use of dev.triumphteam.cmd.core.exceptions.CommandRegistrationException in project triumph-cmds by TriumphTeam.

the class SlashCommand method addSubCommands.

/**
 * {@inheritDoc}
 */
@Override
public void addSubCommands(@NotNull final BaseCommand baseCommand) {
    for (final Method method : baseCommand.getClass().getDeclaredMethods()) {
        if (Modifier.isPrivate(method.getModifiers()))
            continue;
        final SlashSubCommandProcessor<S> processor = new SlashSubCommandProcessor<>(baseCommand, name, method, registries, choiceRegistry, senderValidator);
        final String subCommandName = processor.getName();
        if (subCommandName == null)
            continue;
        // TODO: 11/27/2021 Remove repeating code for throwing the exception.
        if (isDefault) {
            throw new CommandRegistrationException("Default commands cannot be registered with subcommands", baseCommand.getClass());
        }
        if (processor.isDefault()) {
            if (subCommands.size() > 0) {
                throw new CommandRegistrationException("Default commands cannot be registered with subcommands", baseCommand.getClass());
            }
            isDefault = true;
        }
        final ExecutionProvider executionProvider = processor.isAsync() ? asyncExecutionProvider : syncExecutionProvider;
        subCommands.putIfAbsent(subCommandName, new SlashSubCommand<>(processor, name, executionProvider));
    }
}
Also used : ExecutionProvider(dev.triumphteam.cmd.core.execution.ExecutionProvider) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) Method(java.lang.reflect.Method)

Aggregations

CommandRegistrationException (dev.triumphteam.cmd.core.exceptions.CommandRegistrationException)5 Method (java.lang.reflect.Method)3 NotNull (org.jetbrains.annotations.NotNull)3 BaseCommand (dev.triumphteam.cmd.core.BaseCommand)2 ExecutionProvider (dev.triumphteam.cmd.core.execution.ExecutionProvider)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CommandMap (org.bukkit.command.CommandMap)2 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)2 Command (dev.triumphteam.cmd.core.Command)1 Command (dev.triumphteam.cmd.core.annotation.Command)1 Default (dev.triumphteam.cmd.core.annotation.Default)1 ArgumentRegistry (dev.triumphteam.cmd.core.argument.ArgumentRegistry)1 NamedArgumentRegistry (dev.triumphteam.cmd.core.argument.named.NamedArgumentRegistry)1 MessageRegistry (dev.triumphteam.cmd.core.message.MessageRegistry)1 Registry (dev.triumphteam.cmd.core.registry.Registry)1 RequirementRegistry (dev.triumphteam.cmd.core.requirement.RequirementRegistry)1 SenderValidator (dev.triumphteam.cmd.core.sender.SenderValidator)1 ChoiceRegistry (dev.triumphteam.cmd.slash.choices.ChoiceRegistry)1 Field (java.lang.reflect.Field)1