From 410b299ad65d900aa0b9b6659114a01c41b048b5 Mon Sep 17 00:00:00 2001 From: tueem Date: Tue, 15 Oct 2024 17:30:39 +0200 Subject: [PATCH] add first ApplicationCommandDefinition implementation --- .../ApplicationCommandDefinition.java | 48 ++++++++++++++++--- .../registry/InteractionRegistry.java | 2 +- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/commands/ApplicationCommandDefinition.java b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/ApplicationCommandDefinition.java index 8df5248..ac87f27 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/commands/ApplicationCommandDefinition.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/ApplicationCommandDefinition.java @@ -3,18 +3,54 @@ package net.tomatentum.marinara.interaction.commands; import java.util.ArrayList; import java.util.List; -public record ApplicationCommandDefinition( - List executableDefinitons, - String applicationCommand -) { +import net.tomatentum.marinara.interaction.commands.annotation.ApplicationCommand; - public ApplicationCommandDefinition(String applicationCommand) { - this(new ArrayList<>(), applicationCommand); +public class ApplicationCommandDefinition { + private List 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 getExecutableDefinitons() { + return executableDefinitons; + } + + public int getSubCommandGroupCount() { + return subCommandGroupCount; + } + } diff --git a/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java index d0f774f..462cd94 100644 --- a/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java +++ b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java @@ -37,7 +37,7 @@ public class InteractionRegistry { execDefs.forEach((def) -> { Optional 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);