From caa2ee70892d3978bbbe7ec760b82b3a5532f2cd Mon Sep 17 00:00:00 2001 From: tueem Date: Tue, 18 Mar 2025 09:33:53 +0100 Subject: [PATCH] refactor(command): move both choices vars to different annotation --- .../interaction/commands/SlashCommandDefinition.java | 10 ++++++---- .../commands/annotation/CommandChoices.java | 8 ++++++++ .../commands/annotation/SlashCommandOption.java | 3 +-- .../marinara/test/discord4j/TestCommand.java | 3 ++- .../tomatentum/marinara/test/javacord/TestCommand.java | 3 ++- 5 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/CommandChoices.java 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 65aa47c..0fc0f67 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 @@ -6,6 +6,7 @@ import java.util.Set; import org.slf4j.Logger; +import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption.PlaceHolderEnum; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice; @@ -18,10 +19,11 @@ import net.tomatentum.marinara.util.LoggerUtil; public class SlashCommandDefinition { public static SlashCommandOptionChoice[] getActualChoices(SlashCommandOption option) { - SlashCommandOptionChoice[] choices = option.choices(); - if (choices.length <= 0 && !option.choiceEnum().equals(PlaceHolderEnum.class)) - choices = EnumChoices.of(option.choiceEnum()).choices(); - return choices; + CommandChoices choices = option.choices(); + SlashCommandOptionChoice[] actualChoices = choices.value(); + if (choices.value().length <= 0 && !choices.cenum().equals(PlaceHolderEnum.class)) + actualChoices = EnumChoices.of(choices.cenum()).choices(); + return actualChoices; } private Set entries; diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/CommandChoices.java b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/CommandChoices.java new file mode 100644 index 0000000..aea8b8e --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/CommandChoices.java @@ -0,0 +1,8 @@ +package net.tomatentum.marinara.interaction.commands.annotation; + +import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption.PlaceHolderEnum; + +public @interface CommandChoices { + public SlashCommandOptionChoice[] value() default {}; + public Class> cenum() default PlaceHolderEnum.class; +} diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/SlashCommandOption.java b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/SlashCommandOption.java index d7b845f..fe1c995 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/SlashCommandOption.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/commands/annotation/SlashCommandOption.java @@ -15,8 +15,7 @@ public @interface SlashCommandOption { public SlashCommandOptionType type() default SlashCommandOptionType.STRING; public boolean required() default false; public boolean autocomplete() default false; - public SlashCommandOptionChoice[] choices() default {}; - public Class> choiceEnum() default PlaceHolderEnum.class; + public CommandChoices choices() default @CommandChoices; public static enum PlaceHolderEnum { diff --git a/wrapper/discord4j/src/test/java/net/tomatentum/marinara/test/discord4j/TestCommand.java b/wrapper/discord4j/src/test/java/net/tomatentum/marinara/test/discord4j/TestCommand.java index 8a98424..9482748 100644 --- a/wrapper/discord4j/src/test/java/net/tomatentum/marinara/test/discord4j/TestCommand.java +++ b/wrapper/discord4j/src/test/java/net/tomatentum/marinara/test/discord4j/TestCommand.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; import net.tomatentum.marinara.interaction.InteractionHandler; +import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; @@ -20,7 +21,7 @@ public class TestCommand implements InteractionHandler { name = "foo", description = "foo bar is very fooby", type = SlashCommandOptionType.STRING, - choiceEnum = TestChoiceEnum.class + choices = @CommandChoices(cenum = TestChoiceEnum.class) ) } ) diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/javacord/TestCommand.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/javacord/TestCommand.java index e7aca90..d6f371c 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/javacord/TestCommand.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/javacord/TestCommand.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.javacord.api.interaction.SlashCommandInteraction; import net.tomatentum.marinara.interaction.InteractionHandler; +import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; @@ -21,7 +22,7 @@ public class TestCommand implements InteractionHandler { name = "foo", description = "foo bar is very fooby", type = SlashCommandOptionType.STRING, - choiceEnum = TestChoiceEnum.class + choices = @CommandChoices(cenum = TestChoiceEnum.class) ) } )