From 432db43bf598017f54642573ea2d96010efc9060 Mon Sep 17 00:00:00 2001 From: Tueem Date: Sun, 15 Dec 2024 15:09:09 +0100 Subject: [PATCH] add remaining parts of AutocompleteInteraction --- .../AutoCompleteInteractionMethod.java | 51 +++++++++++++++++++ .../methods/InteractionMethod.java | 5 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 lib/src/main/java/net/tomatentum/marinara/interaction/methods/AutoCompleteInteractionMethod.java diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/AutoCompleteInteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/AutoCompleteInteractionMethod.java new file mode 100644 index 0000000..ffed507 --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/AutoCompleteInteractionMethod.java @@ -0,0 +1,51 @@ +package net.tomatentum.marinara.interaction.methods; + +import java.lang.reflect.Method; + +import net.tomatentum.marinara.Marinara; +import net.tomatentum.marinara.interaction.InteractionHandler; +import net.tomatentum.marinara.interaction.InteractionType; +import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; +import net.tomatentum.marinara.parser.AnnotationParser; +import net.tomatentum.marinara.parser.SlashCommandParser; + +public class AutoCompleteInteractionMethod extends InteractionMethod { + + private ExecutableSlashCommandDefinition commandDefinition; + + public AutoCompleteInteractionMethod(Method method, + InteractionHandler handler, + Marinara marinara + ) { + super(method, handler, marinara); + } + + @Override + public AnnotationParser[] getParsers() { + return new AnnotationParser[] { + new SlashCommandParser(method, (x) -> { this.commandDefinition = x; } ) + }; + } + + @Override + public Object getParameter(Object context, int index) { + Class type = getMethod().getParameterTypes()[index+1]; + Object autocompleteOptionValue = marinara.getWrapper().getContextObjectProvider().getAutocompleteFocusedOption(context); + if (autocompleteOptionValue != null) + return autocompleteOptionValue; + + return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type); + } + + @Override + public boolean canRun(Object context) { + ExecutableSlashCommandDefinition other = marinara.getWrapper().getCommandDefinition(context); + return commandDefinition.equals(other); + } + + @Override + public InteractionType getType() { + return InteractionType.AUTOCOMPLETE; + } + +} diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java index f9c5a1c..20fec5f 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java @@ -11,6 +11,7 @@ import net.tomatentum.marinara.Marinara; import net.tomatentum.marinara.checks.AppliedCheck; import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionType; +import net.tomatentum.marinara.interaction.annotation.AutoComplete; import net.tomatentum.marinara.interaction.annotation.Button; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; @@ -20,6 +21,8 @@ import net.tomatentum.marinara.parser.InteractionCheckParser; public abstract class InteractionMethod { public static InteractionMethod create(Method method, InteractionHandler handler, Marinara marinara) { + if (method.isAnnotationPresent(AutoComplete.class)) + return new AutoCompleteInteractionMethod(method, handler, marinara); if (method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class)) return new SlashCommandInteractionMethod(method, handler, marinara); if (method.isAnnotationPresent(Button.class)) @@ -53,7 +56,7 @@ public abstract class InteractionMethod { public abstract AnnotationParser[] getParsers(); - public abstract Object getParameter(Object parameter, int index); + public abstract Object getParameter(Object context, int index); public abstract boolean canRun(Object context);