Compare commits
No commits in common. "b0742e65adbb7ce6b4b7f94975d51e6c6f55b7bc" and "b809411faf8da8004434500ab0e05bd9176af6b4" have entirely different histories.
b0742e65ad
...
b809411faf
@ -3,54 +3,18 @@ package net.tomatentum.marinara.interaction.commands;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.ApplicationCommand;
|
public record ApplicationCommandDefinition(
|
||||||
|
List<ExecutableCommandDefinition> executableDefinitons,
|
||||||
|
String applicationCommand
|
||||||
|
) {
|
||||||
|
|
||||||
public class ApplicationCommandDefinition {
|
public ApplicationCommandDefinition(String applicationCommand) {
|
||||||
private List<ExecutableCommandDefinition> executableDefinitons;
|
this(new ArrayList<>(), applicationCommand);
|
||||||
private ApplicationCommand applicationCommand;
|
|
||||||
private int subCommandGroupCount = -1;
|
|
||||||
private boolean isRootCommand = false;
|
|
||||||
|
|
||||||
public ApplicationCommandDefinition(ApplicationCommand applicationCommand) {
|
|
||||||
this.executableDefinitons = new ArrayList<>();
|
|
||||||
this.applicationCommand = applicationCommand;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApplicationCommandDefinition addExecutableCommand(ExecutableCommandDefinition def) {
|
public ApplicationCommandDefinition addExecutableCommand(ExecutableCommandDefinition def) {
|
||||||
if (this.subCommandGroupCount == -1)
|
|
||||||
this.subCommandGroupCount = def.subCommandGroups().length;
|
|
||||||
if (def.subCommandGroups().length != subCommandGroupCount)
|
|
||||||
throw new IllegalArgumentException(def + ": has a non matching amount of subcommand groups. All subcommands must have the same amount of subcommand groups!");
|
|
||||||
if (def.applicationCommand() != null) {
|
|
||||||
if (applicationCommand == null)
|
|
||||||
this.applicationCommand = def.applicationCommand();
|
|
||||||
if (!this.applicationCommand.equals(def.applicationCommand()))
|
|
||||||
throw new IllegalArgumentException(def + ": has a non matching Application Command description. Please edit it to equal all other descriptions or remove it to use other definitions descriptions");
|
|
||||||
}
|
|
||||||
if (isRootCommand) {
|
|
||||||
if (!def.isRootCommand())
|
|
||||||
throw new IllegalArgumentException(def + ": cannot have subcommands and rootcommand definitions together");
|
|
||||||
long subCommandAmount = executableDefinitons.stream()
|
|
||||||
.filter((x) -> !x.isRootCommand())
|
|
||||||
.count();
|
|
||||||
if (subCommandAmount > 0)
|
|
||||||
throw new IllegalArgumentException(def + ": cannot have subcommands and rootcommand definitions together");
|
|
||||||
}
|
|
||||||
|
|
||||||
executableDefinitons.add(def);
|
executableDefinitons.add(def);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApplicationCommand getApplicationCommand() {
|
|
||||||
return applicationCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ExecutableCommandDefinition> getExecutableDefinitons() {
|
|
||||||
return executableDefinitons;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSubCommandGroupCount() {
|
|
||||||
return subCommandGroupCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package net.tomatentum.marinara.interaction.commands;
|
package net.tomatentum.marinara.interaction.commands;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.ApplicationCommand;
|
import org.apache.logging.log4j.core.tools.picocli.CommandLine.Command;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.CommandOption;
|
import net.tomatentum.marinara.interaction.commands.annotation.CommandOption;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
|
||||||
|
|
||||||
public record ExecutableCommandDefinition(
|
public record ExecutableCommandDefinition(
|
||||||
ApplicationCommand applicationCommand,
|
String applicationCommand,
|
||||||
SubCommand subCommand,
|
String applicationCommandDescription,
|
||||||
String[] subCommandGroups,
|
String[] subCommandGroups,
|
||||||
|
String subCommand,
|
||||||
|
String subCommandDescription,
|
||||||
CommandOption[] options) {
|
CommandOption[] options) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -20,54 +22,71 @@ public record ExecutableCommandDefinition(
|
|||||||
other.subCommand.equals(this.subCommand);
|
other.subCommand.equals(this.subCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String toString() {
|
|
||||||
return applicationCommand.name() + subCommand.name() != null ? "::" + subCommand.name() : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isRootCommand() {
|
|
||||||
return subCommand == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private ApplicationCommand applicationCommand;
|
private String applicationCommandName;
|
||||||
private SubCommand subCommand;
|
private String applicationCommandDescription;
|
||||||
private String[] subCommandGroupNames;
|
private String[] subCommandGroupNames;
|
||||||
|
private String subCommandName;
|
||||||
|
private String subCommandDescription;
|
||||||
|
public CommandOption[] options;
|
||||||
|
|
||||||
public Builder() {
|
public Builder() {
|
||||||
this.subCommandGroupNames = new String[0];
|
this.subCommandGroupNames = new String[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecutableCommandDefinition build() {
|
public ExecutableCommandDefinition build() {
|
||||||
if (applicationCommand == null)
|
if (applicationCommandName == null)
|
||||||
throw new IllegalArgumentException("applicationCommandName cant be null");
|
throw new IllegalArgumentException("applicationCommandName cant be null");
|
||||||
|
|
||||||
return new ExecutableCommandDefinition(applicationCommand, subCommand, subCommandGroupNames, subCommand != null ? subCommand.options() : applicationCommand.options());
|
return new ExecutableCommandDefinition(applicationCommandName, applicationCommandDescription, subCommandGroupNames, subCommandName, subCommandDescription, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApplicationCommand(ApplicationCommand applicationCommand) {
|
public void setApplicationCommandName(String applicationCommandName) {
|
||||||
this.applicationCommand = applicationCommand;
|
this.applicationCommandName = applicationCommandName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSubCommand(SubCommand subCommand) {
|
public void setApplicationCommandDescription(String applicationCommandDescription) {
|
||||||
this.subCommand = subCommand;
|
this.applicationCommandDescription = applicationCommandDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSubCommandGroupNames(String[] subCommandGroupNames) {
|
public void setSubCommandGroupNames(String[] subCommandGroupNames) {
|
||||||
this.subCommandGroupNames = subCommandGroupNames;
|
this.subCommandGroupNames = subCommandGroupNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApplicationCommand getApplicationCommand() {
|
public void setSubCommandName(String subCommandName) {
|
||||||
return applicationCommand;
|
this.subCommandName = subCommandName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SubCommand getSubCommand() {
|
public void setSubCommandDescription(String subCommandDescription) {
|
||||||
return subCommand;
|
this.subCommandDescription = subCommandDescription;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOptions(CommandOption[] options) {
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApplicationCommandName() {
|
||||||
|
return applicationCommandName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApplicationCommandDescription() {
|
||||||
|
return applicationCommandDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getSubCommandGroupNames() {
|
public String[] getSubCommandGroupNames() {
|
||||||
return subCommandGroupNames;
|
return subCommandGroupNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSubCommandName() {
|
||||||
|
return subCommandName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSubCommandDescription() {
|
||||||
|
return subCommandDescription;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandOption[] getOptions() {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,9 @@ public class CommandInteractionMethod extends InteractionMethod {
|
|||||||
|
|
||||||
ApplicationCommand cmd = ReflectionUtil.getAnnotation(method, ApplicationCommand.class);
|
ApplicationCommand cmd = ReflectionUtil.getAnnotation(method, ApplicationCommand.class);
|
||||||
ExecutableCommandDefinition.Builder builder = new ExecutableCommandDefinition.Builder();
|
ExecutableCommandDefinition.Builder builder = new ExecutableCommandDefinition.Builder();
|
||||||
builder.setApplicationCommand(cmd);
|
builder.setApplicationCommandName(cmd.name());
|
||||||
|
builder.setApplicationCommandDescription(cmd.description());
|
||||||
|
builder.setOptions(parseOptions());
|
||||||
|
|
||||||
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
||||||
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
||||||
@ -55,10 +57,21 @@ public class CommandInteractionMethod extends InteractionMethod {
|
|||||||
|
|
||||||
if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) {
|
if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) {
|
||||||
SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class);
|
SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class);
|
||||||
builder.setSubCommand(subCmd);
|
builder.setSubCommandName(subCmd.name());
|
||||||
|
builder.setSubCommandDescription(subCmd.description());
|
||||||
}
|
}
|
||||||
|
|
||||||
this.commandDefinition = builder.build();
|
this.commandDefinition = builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CommandOption[] parseOptions() {
|
||||||
|
if (method.isAnnotationPresent(SubCommand.class)) {
|
||||||
|
SubCommand subCmd = method.getAnnotation(SubCommand.class);
|
||||||
|
return subCmd.options();
|
||||||
|
}else {
|
||||||
|
ApplicationCommand subCmd = method.getAnnotation(ApplicationCommand.class);
|
||||||
|
return subCmd.options();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ public class InteractionRegistry {
|
|||||||
|
|
||||||
execDefs.forEach((def) -> {
|
execDefs.forEach((def) -> {
|
||||||
Optional<ApplicationCommandDefinition> appDef = defs.stream()
|
Optional<ApplicationCommandDefinition> appDef = defs.stream()
|
||||||
.filter((x) -> x.getApplicationCommand().equals(def.applicationCommand()))
|
.filter((x) -> x.applicationCommand().equals(def.applicationCommand()))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
if (appDef.isPresent())
|
if (appDef.isPresent())
|
||||||
appDef.get().addExecutableCommand(def);
|
appDef.get().addExecutableCommand(def);
|
||||||
|
@ -4,7 +4,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ApplicationCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableCommandDefinition;
|
import net.tomatentum.marinara.interaction.commands.ExecutableCommandDefinition;
|
||||||
import net.tomatentum.marinara.interaction.commands.option.OptionType;
|
import net.tomatentum.marinara.interaction.commands.option.OptionType;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
@ -17,8 +16,8 @@ public abstract class LibraryWrapper {
|
|||||||
interactionSubscriber = new ArrayList<>();
|
interactionSubscriber = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void registerGlobalCommand(ApplicationCommandDefinition def);
|
public abstract void registerGlobalCommand();
|
||||||
public abstract void registerServerCommand(ApplicationCommandDefinition def);
|
public abstract void registerServerCommand();
|
||||||
|
|
||||||
public void handleInteraction(Object context) {
|
public void handleInteraction(Object context) {
|
||||||
interactionSubscriber.forEach((o) -> o.accept(context));
|
interactionSubscriber.forEach((o) -> o.accept(context));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user