
com.freya02.botcommands.api.commands.CommandPath Maven / Gradle / Ivy
package com.freya02.botcommands.api.commands;
import com.freya02.botcommands.internal.commands.CommandPathImpl;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.internal.utils.Checks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Represents a path of a command, each path component is delimited with a space, it is the same representation as JDA commands paths given in {@link SlashCommandInteractionEvent#getFullCommandName()}.
*
The different components are name, group and subcommand.
*
This is mainly a utility class to avoid manipulating strings
*/
public interface CommandPath extends Comparable {
@NotNull
static CommandPath of(@NotNull String name, @Nullable String group, @Nullable String subname) {
return new CommandPathImpl(name, group, subname);
}
@NotNull
static CommandPath of(@NotNull String name, @Nullable String subname) {
return new CommandPathImpl(name, null, subname);
}
@NotNull
static CommandPath ofName(@NotNull String name) {
return new CommandPathImpl(name, null, null);
}
@NotNull
static CommandPath of(@NotNull String path) {
final String[] components = path.split(" ");
for (String component : components) {
Checks.matches(component, Checks.ALPHANUMERIC_WITH_DASH, "Path component");
}
return of(components);
}
@NotNull
static CommandPath of(@NotNull String @NotNull ... components) {
if (components.length == 1) {
return new CommandPathImpl(components[0], null, null);
} else if (components.length == 2) {
return new CommandPathImpl(components[0], null, components[1]);
} else if (components.length == 3) {
return new CommandPathImpl(components[0], components[1], components[2]);
} else {
throw new IllegalArgumentException("Invalid path: '" + String.join(" ", components) + "'");
}
}
/**
* Returns the top level name of this command path
*
For a slash command such as "/show me something
", this would be "show
"
*
* @return Top level name of this command path
*/
@NotNull
String getName();
/**
* Returns the subcommand group name of this command path
*
For a slash command such as "/show me something
", this would be "me
"
*
* @return Subcommand group name of this command path
*/
@Nullable
String getGroup();
/**
* Returns the subcommand name of this command path
*
For a slash command such as "/show me something
", this would be "something
"
*
For a slash command such as "/tag info
", this would be "info
"
*
* @return Subcommand name of this command path
*/
@Nullable
String getSubname();
/**
* Returns the number of path components of this command path
*
For a slash command such as "/show me something
", this would be 3
*
For a slash command such as "/tag info
", this would be 2
*
For a slash command such as "/say
", this would be 1
*
* @return The number of path components of this command path
*/
int getNameCount();
/**
* Returns the parent path of this command path
*
For a slash command such as "/show me something
", this would be "/show me
"
*
For a slash command such as "/tag info
", this would be "/tag
"
*
For a slash command such as "/say
", this would be null
*
* @return The parent path of this command path
*/
@Nullable
CommandPath getParent();
/**
* Returns the full encoded path of this command path
*
Each path component is joined with a space delimiter
*
For a slash command such as "/show me something
", this would be "show me something
"
*
* @return The full encoded path of this command path
*/
@NotNull
String getFullPath();
/**
* Returns the full path with the specified separator.
*
For a slash command such as "/show me something
", with a -
separator,
* this would be "show-me-something
"
*
* @return The full path with the specified separator
*/
@NotNull
String getFullPath(char separator);
/**
* Returns the right-most name of this command path
*
For a slash command such as "/show me something
", this would be "something
"
*
* @return The right-most name of this command path
*/
@NotNull
String getLastName();
/**
* Return the name at the i
index
*
* @param i The index of the name to get
* @return The name at the specified index
*/
@Nullable
String getNameAt(int i);
/**
* Returns the JDA path representation of this CommandPath
*
* @return The command path with / in between each component
*/
@NotNull
String toString();
/**
* Returns whether this command path starts with the supplied command path
*
For example "/show me something
" starts with "/show me
"
*
* @param o The other path to test against
* @return true
if this path starts with the other, false
otherwise
*/
boolean startsWith(CommandPath o);
/**
* Indicates if this command path is equal to another object
*
* @param o Another object
* @return true
if they are equal, false
if not
*/
boolean equals(Object o);
@Override
default int compareTo(@NotNull CommandPath o) {
if (this.getNameCount() == o.getNameCount()) {
if (this.equals(o)) {
return 0;
}
}
return this.getFullPath().compareTo(o.getFullPath());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy