From ae7e66d06dec042470b78f82bc5ce5bbdfe146c2 Mon Sep 17 00:00:00 2001 From: tueem Date: Sat, 26 Oct 2024 19:43:31 +0200 Subject: [PATCH] Add Helper methods for easier command registration --- .../commands/SlashCommandDefinition.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/commands/SlashCommandDefinition.java b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/SlashCommandDefinition.java index 9932f21..fbdbc0d 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/commands/SlashCommandDefinition.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/SlashCommandDefinition.java @@ -1,10 +1,13 @@ package net.tomatentum.marinara.interaction.commands; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; +import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; +import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; public class SlashCommandDefinition { private List executableDefinitons; @@ -50,4 +53,30 @@ public class SlashCommandDefinition { executableDefinitons.forEach(set::add); return set.toArray(new ExecutableSlashCommandDefinition[0]); } + + public SubCommandGroup[] getSubCommandGroups() { + HashSet subCommandGroups = new HashSet<>(); + + for (ExecutableSlashCommandDefinition execDef : getUniqueExecutableDefinitions()) { + String currName = execDef.subCommandGroup().name(); + if (execDef.subCommandGroup() == null) + continue; + SubCommandGroup[] matching = (SubCommandGroup[]) subCommandGroups.stream().filter((x) -> x.name().equals(currName)).toArray(); + if (matching.length < 1) + subCommandGroups.add(execDef.subCommandGroup()); + else if (matching[0].description().isBlank() && execDef.subCommandGroup().description().isBlank()) { + subCommandGroups.removeIf((x) -> x.name().equals(currName)); + subCommandGroups.add(execDef.subCommandGroup()); + } + } + + return subCommandGroups.toArray(new SubCommandGroup[0]); + } + + public SubCommand[] getSubCommands(SubCommandGroup group) { + if (group == null) + return (SubCommand[]) Arrays.asList(getUniqueExecutableDefinitions()).stream().filter((x) -> x.subCommandGroup() == null && x.subCommand() != null).toArray(); + else + return (SubCommand[]) Arrays.asList(getUniqueExecutableDefinitions()).stream().filter((x) -> x.subCommandGroup().name().equals(group.name()) && x.subCommand() != null).toArray(); + } }