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;
}
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());
}
}
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!");
}
}
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!");
}
}
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));
}
}
Aggregations