move CommandConverter to wrapper package
All checks were successful
github-mirror / push-github (push) Successful in 3s
Build / Gradle-Build (push) Successful in 31s
Test / Gradle-Test (push) Successful in 46s

This commit is contained in:
2025-03-04 11:16:44 +01:00
parent f4ee258eb1
commit f940f48566
5 changed files with 6 additions and 5 deletions

View File

@@ -0,0 +1,61 @@
package net.tomatentum.marinara.wrapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
public class CommandConverter<A extends Object, O extends Object, C extends Object> {
public static <A, O, C> CommandConverter<A, O, C> of(Spec<A, O, C> spec) {
return new CommandConverter<>(spec);
}
private Spec<A, O, C> spec;
CommandConverter(Spec<A, O, C> spec) {
this.spec = spec;
}
public A convert(SlashCommandDefinition def) {
List<O> options = new ArrayList<>();
if (!def.isRootCommand()) {
Arrays.stream(def.getSubCommands()).map(this::convertSubCommand).forEach(options::add);
Arrays.stream(def.getSubCommandGroups()).map(x -> this.convertSubCommandGroup(def, x)).forEach(options::add);
}else
Arrays.stream(def.rootIdentifier().options()).map(this::convertOption).forEach(options::add);
return spec.convertCommand(def.rootIdentifier(), options);
}
private O convertSubCommandGroup(SlashCommandDefinition def, InteractionIdentifier identifier) {
SlashCommandIdentifier[] subCommands = def.getSubCommands(identifier.name());
List<O> convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommand).toList();
return spec.convertSubCommandGroup(identifier, convertedSubCommands);
}
private O convertSubCommand(SlashCommandIdentifier identifier) {
List<O> options = Arrays.stream(identifier.options()).map(this::convertOption).toList();
return spec.convertSubCommand(identifier, options);
}
private O convertOption(SlashCommandOption option) {
List<C> choices = Arrays.stream(SlashCommandDefinition.getActualChoices(option)).map(spec::convertChoice).toList();
return spec.convertOption(option, choices);
}
public static interface Spec<A extends Object, O extends Object, C extends Object> {
public A convertCommand(RootCommandIdentifier rootIdentifier, List<O> options);
public O convertSubCommandGroup(InteractionIdentifier identifier, List<O> subCommands);
public O convertSubCommand(InteractionIdentifier identifier, List<O> options);
public O convertOption(SlashCommandOption option, List<C> choices);
public C convertChoice(SlashCommandOptionChoice choice);
}
}