ae7e66d06dec042470b78f82bc5ce5bbdfe146c2

rewrite both methods in a more efficient way
This commit is contained in:
tueem 2024-10-29 20:58:08 +01:00
parent 1aa052a85b
commit 0615741def
No known key found for this signature in database
GPG Key ID: 819A0F7C36B9CF07

@ -2,6 +2,7 @@ package net.tomatentum.marinara.interaction.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -41,29 +42,42 @@ public class SlashCommandDefinition {
} }
public SubCommandGroup[] getSubCommandGroups() { public SubCommandGroup[] getSubCommandGroups() {
HashSet<SubCommandGroup> subCommandGroups = new HashSet<>(); List<SubCommandGroup> subCommandGroups = Arrays.stream(getExecutableDefinitons())
.filter((x) -> x.subCommandGroup() != null)
.map((x) -> x.subCommandGroup())
.toList();
for (ExecutableSlashCommandDefinition execDef : getUniqueExecutableDefinitions()) { HashMap<String, SubCommandGroup> subCommandGroupMap = new HashMap<>();
String currName = execDef.subCommandGroup().name(); subCommandGroups.forEach((x) -> {
if (execDef.subCommandGroup() == null) SubCommandGroup current = subCommandGroupMap.get(x.name());
continue; if (current == null || (current.description().isBlank() && !x.description().isBlank()))
SubCommandGroup[] matching = (SubCommandGroup[]) subCommandGroups.stream().filter((x) -> x.name().equals(currName)).toArray(); subCommandGroupMap.put(x.name(), x);
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]); return (SubCommandGroup[]) subCommandGroupMap.values().toArray();
} }
public SubCommand[] getSubCommands(SubCommandGroup group) { public SubCommand[] getSubCommands(String groupName) {
if (group == null) List<SubCommand> subCommands;
return (SubCommand[]) Arrays.asList(getUniqueExecutableDefinitions()).stream().filter((x) -> x.subCommandGroup() == null && x.subCommand() != null).toArray(); if (groupName == null)
subCommands = Arrays.stream(getExecutableDefinitons())
.filter((x) -> x.subCommandGroup() == null && x.subCommand() != null)
.map((x) -> x.subCommand())
.toList();
else else
return (SubCommand[]) Arrays.asList(getUniqueExecutableDefinitions()).stream().filter((x) -> x.subCommandGroup().name().equals(group.name()) && x.subCommand() != null).toArray(); subCommands = Arrays.stream(getExecutableDefinitons())
.filter((x) -> x.subCommandGroup().name().equals(groupName) && x.subCommand() != null)
.map((x) -> x.subCommand())
.toList();
HashMap<String, SubCommand> subCommandMap = new HashMap<>();
subCommands.forEach((x) -> {
SubCommand current = subCommandMap.get(x.name());
if (current == null || (current.description().isBlank() && !x.description().isBlank()))
subCommandMap.put(x.name(), x);
});
return (SubCommand[]) subCommandMap.values().toArray();
} }
public SlashCommand getSlashCommand() { public SlashCommand getSlashCommand() {
@ -74,12 +88,6 @@ public class SlashCommandDefinition {
return executableDefinitons.toArray(new ExecutableSlashCommandDefinition[0]); return executableDefinitons.toArray(new ExecutableSlashCommandDefinition[0]);
} }
public ExecutableSlashCommandDefinition[] getUniqueExecutableDefinitions() {
HashSet<ExecutableSlashCommandDefinition> set = new HashSet<>();
executableDefinitons.forEach(set::add);
return set.toArray(new ExecutableSlashCommandDefinition[0]);
}
public boolean isRootCommand() { public boolean isRootCommand() {
return isRootCommand; return isRootCommand;
} }