add remaining parts of AutocompleteInteraction

This commit is contained in:
Tueem 2024-12-15 15:09:09 +01:00
parent 7a2c15d877
commit 432db43bf5
Signed by: tueem
GPG Key ID: F2CE0513D231AD7A
2 changed files with 55 additions and 1 deletions
lib/src/main/java/net/tomatentum/marinara/interaction/methods

@ -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;
}
}

@ -11,6 +11,7 @@ import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.checks.AppliedCheck; import net.tomatentum.marinara.checks.AppliedCheck;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.InteractionType; 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.annotation.Button;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
@ -20,6 +21,8 @@ import net.tomatentum.marinara.parser.InteractionCheckParser;
public abstract class InteractionMethod { public abstract class InteractionMethod {
public static InteractionMethod create(Method method, InteractionHandler handler, Marinara marinara) { 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)) if (method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class))
return new SlashCommandInteractionMethod(method, handler, marinara); return new SlashCommandInteractionMethod(method, handler, marinara);
if (method.isAnnotationPresent(Button.class)) if (method.isAnnotationPresent(Button.class))
@ -53,7 +56,7 @@ public abstract class InteractionMethod {
public abstract AnnotationParser[] getParsers(); 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); public abstract boolean canRun(Object context);