add first ApplicationCommandDefinition implementation

This commit is contained in:
tueem 2024-10-15 17:30:39 +02:00
parent 89d172e2f3
commit 410b299ad6
No known key found for this signature in database
GPG Key ID: 4B2F166FCDCC96C6
2 changed files with 43 additions and 7 deletions

@ -3,18 +3,54 @@ package net.tomatentum.marinara.interaction.commands;
import java.util.ArrayList;
import java.util.List;
public record ApplicationCommandDefinition(
List<ExecutableCommandDefinition> executableDefinitons,
String applicationCommand
) {
import net.tomatentum.marinara.interaction.commands.annotation.ApplicationCommand;
public ApplicationCommandDefinition(String applicationCommand) {
this(new ArrayList<>(), applicationCommand);
public class ApplicationCommandDefinition {
private List<ExecutableCommandDefinition> executableDefinitons;
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) {
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);
return this;
}
public ApplicationCommand getApplicationCommand() {
return applicationCommand;
}
public List<ExecutableCommandDefinition> getExecutableDefinitons() {
return executableDefinitons;
}
public int getSubCommandGroupCount() {
return subCommandGroupCount;
}
}

@ -37,7 +37,7 @@ public class InteractionRegistry {
execDefs.forEach((def) -> {
Optional<ApplicationCommandDefinition> appDef = defs.stream()
.filter((x) -> x.applicationCommand().equals(def.applicationCommand()))
.filter((x) -> x.getApplicationCommand().equals(def.applicationCommand()))
.findFirst();
if (appDef.isPresent())
appDef.get().addExecutableCommand(def);