Compare commits
7 Commits
dev
...
450f1fdaa1
Author | SHA1 | Date | |
---|---|---|---|
450f1fdaa1
|
|||
92540576df
|
|||
8a3cde52fd
|
|||
8495659364
|
|||
0973016a74
|
|||
0590789359
|
|||
2647a1f0b4
|
@@ -2,6 +2,8 @@ package net.tomatentum.marinara;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactory;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactoryImpl;
|
||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
@@ -15,26 +17,32 @@ public class Marinara {
|
||||
return new Marinara(wrapper);
|
||||
}
|
||||
|
||||
private LibraryWrapper wrapper;
|
||||
private ReflectedMethodFactory reflectedMethodFactory;
|
||||
private InteractionRegistry registry;
|
||||
private InteractionCheckRegistry checkRegistry;
|
||||
private LibraryWrapper wrapper;
|
||||
|
||||
private Marinara(LibraryWrapper wrapper) {
|
||||
this.wrapper = wrapper;
|
||||
this.reflectedMethodFactory = new ReflectedMethodFactoryImpl(this);
|
||||
this.registry = new InteractionRegistry(this);
|
||||
this.checkRegistry = new InteractionCheckRegistry();
|
||||
logger.info("Marinara loaded successfully!");
|
||||
}
|
||||
|
||||
public LibraryWrapper getWrapper() {
|
||||
return this.wrapper;
|
||||
}
|
||||
|
||||
public InteractionRegistry getRegistry() {
|
||||
return registry;
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
public InteractionCheckRegistry getCheckRegistry() {
|
||||
return checkRegistry;
|
||||
return this.checkRegistry;
|
||||
}
|
||||
|
||||
public LibraryWrapper getWrapper() {
|
||||
return wrapper;
|
||||
public ReflectedMethodFactory getReflectedMethodFactory() {
|
||||
return this.reflectedMethodFactory;
|
||||
}
|
||||
}
|
||||
|
@@ -8,5 +8,5 @@ import java.lang.annotation.Target;
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AutoComplete {
|
||||
|
||||
public String value();
|
||||
}
|
||||
|
@@ -0,0 +1,63 @@
|
||||
package net.tomatentum.marinara.interaction.components.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.ButtonParser;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class ButtonInteractionMethod extends InteractionMethod {
|
||||
|
||||
private String customId;
|
||||
|
||||
private ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
Class<?> type = method().getParameterTypes()[index+1];
|
||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier identifier() {
|
||||
return InteractionIdentifier.builder()
|
||||
.name(customId)
|
||||
.description("Button")
|
||||
.type(InteractionType.BUTTON)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!method.isAnnotationPresent(Button.class) ||
|
||||
!(containingObject instanceof InteractionHandler)
|
||||
)
|
||||
return null;
|
||||
|
||||
return new ButtonInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
|
||||
ButtonInteractionMethod imethod = (ButtonInteractionMethod) method;
|
||||
parser.add(
|
||||
new ButtonParser(method.method(), x -> imethod.customId = x)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -73,14 +73,14 @@ public class InteractionIdentifier {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof InteractionIdentifier))
|
||||
if (obj == null || !(obj instanceof InteractionIdentifier))
|
||||
return false;
|
||||
InteractionIdentifier ident = (InteractionIdentifier) obj;
|
||||
if (!type().equals(ident.type()))
|
||||
return false;
|
||||
if (!name().equals(ident.name()))
|
||||
return false;
|
||||
return Objects.equals(ident, obj);
|
||||
return Objects.equals(parent(), ident.parent());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -13,8 +13,9 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
String description,
|
||||
InteractionType type,
|
||||
SlashCommandOption[] options,
|
||||
long[] serverIds) {
|
||||
super(parent, name, description, type, options);
|
||||
long[] serverIds,
|
||||
String[] autocompleteRef) {
|
||||
super(parent, name, description, type, options, autocompleteRef);
|
||||
this.serverIds = serverIds;
|
||||
}
|
||||
|
||||
@@ -28,7 +29,7 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
private String description;
|
||||
private SlashCommandOption[] options;
|
||||
private long[] serverIds;
|
||||
|
||||
private String[] autocompleteRef;
|
||||
|
||||
public InteractionIdentifier parent() {
|
||||
return parent;
|
||||
@@ -75,14 +76,24 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
return this;
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier build(boolean autocomplete) {
|
||||
public String[] autocompleteRef() {
|
||||
return this.autocompleteRef;
|
||||
}
|
||||
|
||||
public Builder autocompleteRef(String[] autocompleteRef) {
|
||||
this.autocompleteRef = autocompleteRef;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier build() {
|
||||
return new RootCommandIdentifier(
|
||||
parent,
|
||||
name,
|
||||
description,
|
||||
autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
|
||||
InteractionType.COMMAND,
|
||||
options,
|
||||
serverIds);
|
||||
serverIds,
|
||||
autocompleteRef);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -6,27 +6,39 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptio
|
||||
public class SlashCommandIdentifier extends InteractionIdentifier {
|
||||
|
||||
private SlashCommandOption[] options;
|
||||
private String[] autocompleteRef;
|
||||
|
||||
protected SlashCommandIdentifier(
|
||||
InteractionIdentifier parent,
|
||||
String name,
|
||||
String description,
|
||||
InteractionType type,
|
||||
SlashCommandOption[] options
|
||||
SlashCommandOption[] options,
|
||||
String[] autocompleteRef
|
||||
) {
|
||||
super(parent, name, description, type);
|
||||
this.options = options;
|
||||
this.autocompleteRef = autocompleteRef;
|
||||
}
|
||||
|
||||
public SlashCommandOption[] options() {
|
||||
return this.options;
|
||||
}
|
||||
|
||||
public String[] autocompleteRef() {
|
||||
return this.autocompleteRef;
|
||||
}
|
||||
public SlashCommandIdentifier autocompleteRef(String[] autocompleteRef) {
|
||||
this.autocompleteRef = autocompleteRef;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private InteractionIdentifier parent;
|
||||
private String name;
|
||||
private String description;
|
||||
private SlashCommandOption[] options;
|
||||
private String[] autocompleteRef;
|
||||
|
||||
public InteractionIdentifier parent() {
|
||||
return parent;
|
||||
@@ -64,13 +76,23 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
|
||||
return this;
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier build(boolean autocomplete) {
|
||||
public String[] autocompleteRef() {
|
||||
return this.autocompleteRef;
|
||||
}
|
||||
|
||||
public Builder autocompleteRef(String[] autocompleteRef) {
|
||||
this.autocompleteRef = autocompleteRef;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier build() {
|
||||
return new SlashCommandIdentifier(
|
||||
parent,
|
||||
name,
|
||||
description,
|
||||
autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
|
||||
options);
|
||||
InteractionType.COMMAND,
|
||||
options,
|
||||
autocompleteRef);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,31 +1,30 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||
import net.tomatentum.marinara.parser.AutocompleteParser;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
|
||||
private InteractionIdentifier interactionIdentifier;
|
||||
private String autocompleteRef;
|
||||
|
||||
public AutoCompleteInteractionMethod(Method method,
|
||||
private AutoCompleteInteractionMethod(Method method,
|
||||
InteractionHandler handler,
|
||||
Marinara marinara
|
||||
) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
return new AnnotationParser[] {
|
||||
new SlashCommandParser(method, true, (x) -> { this.interactionIdentifier = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
Class<?> type = method().getParameterTypes()[index+1];
|
||||
@@ -38,7 +37,36 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier identifier() {
|
||||
return interactionIdentifier;
|
||||
return InteractionIdentifier.builder()
|
||||
.type(InteractionType.AUTOCOMPLETE)
|
||||
.name(autocompleteRef)
|
||||
.description("AUTOCOMPLETE")
|
||||
.build();
|
||||
}
|
||||
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!(containingObject instanceof InteractionHandler) ||
|
||||
!method.isAnnotationPresent(AutoComplete.class) ||
|
||||
(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
method.isAnnotationPresent(SubCommand.class)))
|
||||
return null;
|
||||
|
||||
return new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
|
||||
AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method;
|
||||
parser.add(
|
||||
new AutocompleteParser(method.method(), x -> imethod.autocompleteRef = x[0])
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,42 +0,0 @@
|
||||
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.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.ButtonParser;
|
||||
|
||||
public class ButtonInteractionMethod extends InteractionMethod {
|
||||
|
||||
private String customId;
|
||||
|
||||
ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
return new AnnotationParser[] {
|
||||
new ButtonParser(method, (x) -> { this.customId = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
Class<?> type = method().getParameterTypes()[index+1];
|
||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier identifier() {
|
||||
return InteractionIdentifier.builder()
|
||||
.name(customId)
|
||||
.description("Button")
|
||||
.type(InteractionType.BUTTON)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
@@ -1,105 +1,66 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
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;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactory;
|
||||
|
||||
public abstract class InteractionMethod {
|
||||
public abstract class InteractionMethod extends ReflectedMethod {
|
||||
|
||||
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))
|
||||
return new ButtonInteractionMethod(method, handler, marinara);
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Method method;
|
||||
protected InteractionHandler handler;
|
||||
protected Marinara marinara;
|
||||
protected List<AnnotationParser> parsers;
|
||||
protected List<AppliedCheck> appliedChecks;
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
protected InteractionMethod(
|
||||
Method method,
|
||||
InteractionHandler handler,
|
||||
Marinara marinara
|
||||
) {
|
||||
if (!Arrays.asList(handler.getClass().getMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
|
||||
this.method = method;
|
||||
this.handler = handler;
|
||||
super(method, handler);
|
||||
this.marinara = marinara;
|
||||
this.parsers = new ArrayList<>(Arrays.asList(parsers()));
|
||||
this.appliedChecks = new ArrayList<>();
|
||||
|
||||
parsers.add(new InteractionCheckParser(method, appliedChecks::add, marinara.getCheckRegistry()));
|
||||
|
||||
parsers.stream().forEach(AnnotationParser::parse);
|
||||
}
|
||||
|
||||
public void run(Object context) {
|
||||
@Override
|
||||
public Object run(Object context) {
|
||||
Object result = null;
|
||||
if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0)
|
||||
return;
|
||||
return null;
|
||||
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
method.invoke(handler, getParameters(context));
|
||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
logger.error("InteractionMethod failed to run", ex);
|
||||
}
|
||||
result = super.run(context);
|
||||
|
||||
this.appliedChecks.forEach(x -> x.post(context));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public abstract AnnotationParser[] parsers();
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public abstract InteractionIdentifier identifier();
|
||||
|
||||
public Method method() {
|
||||
return method;
|
||||
public Marinara marinara() {
|
||||
return this.marinara;
|
||||
}
|
||||
|
||||
private Object[] getParameters(Object context) {
|
||||
int parameterCount = method.getParameterCount();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
Object parameter;
|
||||
if (i == 0) {
|
||||
parameter = context;
|
||||
}else
|
||||
parameter = getParameter(context, i-1);
|
||||
|
||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
||||
parameters.add(parameter);
|
||||
public List<AppliedCheck> appliedChecks() {
|
||||
return this.appliedChecks;
|
||||
}
|
||||
return parameters.toArray();
|
||||
|
||||
public static abstract class Factory implements ReflectedMethodFactory.Factory {
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
InteractionMethod imethod = (InteractionMethod) method;
|
||||
parser.add(
|
||||
new InteractionCheckParser(method.method(), imethod.appliedChecks::add, imethod.marinara().getCheckRegistry())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,29 +1,27 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.AutocompleteParser;
|
||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
|
||||
private SlashCommandIdentifier interactionIdentifier;
|
||||
|
||||
SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
private SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
return new AnnotationParser[] {
|
||||
new SlashCommandParser(method, false, (x) -> { this.interactionIdentifier = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
return marinara.getWrapper().getContextObjectProvider().convertCommandOption(context, interactionIdentifier.options()[index].name());
|
||||
@@ -34,4 +32,31 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
return interactionIdentifier;
|
||||
}
|
||||
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!(containingObject instanceof InteractionHandler) ||
|
||||
!(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
method.isAnnotationPresent(SubCommand.class)))
|
||||
return null;
|
||||
|
||||
return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
|
||||
SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method;
|
||||
parser.add(
|
||||
new SlashCommandParser(method.method(), x -> imethod.interactionIdentifier = x)
|
||||
);
|
||||
parser.add(
|
||||
new AutocompleteParser(method.method(), x -> imethod.interactionIdentifier.autocompleteRef(x))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,33 @@
|
||||
package net.tomatentum.marinara.parser;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||
|
||||
public class AutocompleteParser implements AnnotationParser {
|
||||
|
||||
private Method method;
|
||||
private Consumer<String[]> consumer;
|
||||
|
||||
public AutocompleteParser(Method method, Consumer<String[]> consumer) {
|
||||
this.method = method;
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class))
|
||||
.map(AutoComplete::value)
|
||||
.toArray(String[]::new);
|
||||
this.consumer.accept(autocompletes);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Method getMethod() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
}
|
@@ -17,14 +17,12 @@ import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
public class SlashCommandParser implements AnnotationParser {
|
||||
|
||||
private Method method;
|
||||
private boolean isAutoComplete;
|
||||
private Consumer<SlashCommandIdentifier> consumer;
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
public SlashCommandParser(Method method, boolean isAutoComplete, Consumer<SlashCommandIdentifier> consumer) {
|
||||
public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) {
|
||||
this.method = method;
|
||||
this.isAutoComplete = isAutoComplete;
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
@@ -38,14 +36,14 @@ public class SlashCommandParser implements AnnotationParser {
|
||||
.description(cmd.description())
|
||||
.options(cmd.options())
|
||||
.serverIds(cmd.serverIds())
|
||||
.build(isAutoComplete);
|
||||
.build();
|
||||
|
||||
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
||||
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
||||
lastIdentifier = InteractionIdentifier.builder()
|
||||
.name(cmdGroup.name())
|
||||
.description(cmdGroup.description())
|
||||
.type(isAutoComplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND)
|
||||
.type(InteractionType.COMMAND)
|
||||
.parent(lastIdentifier)
|
||||
.build();
|
||||
}
|
||||
@@ -56,7 +54,7 @@ public class SlashCommandParser implements AnnotationParser {
|
||||
.name(subCmd.name())
|
||||
.description(subCmd.description())
|
||||
.options(subCmd.options())
|
||||
.build(isAutoComplete);
|
||||
.build();
|
||||
}
|
||||
|
||||
logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());
|
||||
|
@@ -0,0 +1,66 @@
|
||||
package net.tomatentum.marinara.reflection;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
|
||||
public abstract class ReflectedMethod {
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
private Method method;
|
||||
private Object containingObject;
|
||||
|
||||
public ReflectedMethod(Method method, Object containingObject) {
|
||||
if (!Arrays.asList(containingObject.getClass().getMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
this.method = method;
|
||||
this.containingObject = containingObject;
|
||||
}
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public Object run(Object context) {
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
return method.invoke(containingObject, getParameters(context));
|
||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
logger.error("ReflectedMethod failed to run", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Method method() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
public Object containingObject() {
|
||||
return this.containingObject;
|
||||
}
|
||||
|
||||
private Object[] getParameters(Object context) {
|
||||
int parameterCount = method.getParameterCount();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
Object parameter;
|
||||
if (i == 0) {
|
||||
parameter = context;
|
||||
}else
|
||||
parameter = getParameter(context, i-1);
|
||||
|
||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
||||
parameters.add(parameter);
|
||||
}
|
||||
return parameters.toArray();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package net.tomatentum.marinara.reflection;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
|
||||
public interface ReflectedMethodFactory {
|
||||
ReflectedMethod produce(Method method, Object containingClass);
|
||||
ReflectedMethodFactory addFactory(Factory factory);
|
||||
|
||||
public interface Factory {
|
||||
|
||||
ReflectedMethod produce(Marinara marinara, Method method, Object containingObject);
|
||||
void addParser(ReflectedMethod method, List<AnnotationParser> parser);
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
package net.tomatentum.marinara.reflection;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
|
||||
public class ReflectedMethodFactoryImpl implements ReflectedMethodFactory {
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
private Marinara marinara;
|
||||
private List<Factory> factories;
|
||||
|
||||
public ReflectedMethodFactoryImpl(Marinara marinara) {
|
||||
this(marinara, new ArrayList<>());
|
||||
}
|
||||
|
||||
public ReflectedMethodFactoryImpl(Marinara marinara, List<Factory> factories) {
|
||||
this.marinara = marinara;
|
||||
this.factories = factories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Method method, Object containingClass) {
|
||||
Optional<ReflectedMethod> imethod = this.factories.stream()
|
||||
.map(f -> factoryProduce(f, method, containingClass))
|
||||
.filter(Objects::nonNull)
|
||||
.findFirst();
|
||||
|
||||
if (imethod.isEmpty()) {
|
||||
logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method));
|
||||
return null;
|
||||
}
|
||||
|
||||
return imethod.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReflectedMethodFactory addFactory(Factory factory) {
|
||||
this.factories.add(factory);
|
||||
return this;
|
||||
}
|
||||
|
||||
private ReflectedMethod factoryProduce(Factory factory, Method method, Object containingClass) {
|
||||
List<AnnotationParser> parser = new ArrayList<>();
|
||||
ReflectedMethod m = factory.produce(this.marinara, method, containingClass);
|
||||
if (m != null) {
|
||||
factory.addParser(m, parser);
|
||||
parser.forEach(AnnotationParser::parse);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package net.tomatentum.marinara.registry;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -11,6 +12,17 @@ import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
|
||||
public class InteractionEntry {
|
||||
|
||||
public static InteractionEntry findEntry(Set<InteractionEntry> entries, InteractionIdentifier identifier) {
|
||||
Optional<InteractionEntry> oentry = entries.stream()
|
||||
.filter(i -> i.identifier().equals(identifier))
|
||||
.findFirst();
|
||||
|
||||
InteractionEntry entry = oentry.orElse(new InteractionEntry(identifier));
|
||||
if (oentry.isEmpty()) entries.add(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
private InteractionIdentifier identifier;
|
||||
private Set<InteractionMethod> methods;
|
||||
|
||||
@@ -22,11 +34,14 @@ public class InteractionEntry {
|
||||
}
|
||||
|
||||
public InteractionEntry addMethod(InteractionMethod method) {
|
||||
if (!method.identifier().equals(this.identifier))
|
||||
throw new IllegalArgumentException("Method's identifier did not match the entry's identifier");
|
||||
InteractionIdentifier identifier = method.identifier();
|
||||
|
||||
if (!this.identifier().equals(identifier))
|
||||
throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier");
|
||||
|
||||
this.methods.add(method);
|
||||
InteractionIdentifier.tryAddDescriptions(identifier, method.identifier());
|
||||
InteractionIdentifier.tryAddDescriptions(identifier, identifier);
|
||||
logger.debug("Added method {} to entry {}", method.method().getName(), this.identifier);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@@ -13,12 +13,16 @@ import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||
import net.tomatentum.marinara.interaction.components.methods.ButtonInteractionMethod;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ObjectAggregator;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.interaction.methods.AutoCompleteInteractionMethod;
|
||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class InteractionRegistry {
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
@@ -31,21 +35,19 @@ public class InteractionRegistry {
|
||||
this.marinara = marinara;
|
||||
this.identifierProvider = marinara.getWrapper().createIdentifierProvider();
|
||||
marinara.getWrapper().subscribeInteractions(this::handle);
|
||||
marinara.getReflectedMethodFactory()
|
||||
.addFactory(new AutoCompleteInteractionMethod.Factory())
|
||||
.addFactory(new SlashCommandInteractionMethod.Factory())
|
||||
.addFactory(new ButtonInteractionMethod.Factory());
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: Maybe relocate InteractionEntry checking to another class with description merging.
|
||||
*/
|
||||
public void addInteractions(InteractionHandler interactionHandler) {
|
||||
for (Method method : interactionHandler.getClass().getMethods()) {
|
||||
InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara);
|
||||
if (iMethod != null) {
|
||||
Optional<InteractionEntry> oentry = this.interactions.stream()
|
||||
.filter(i -> i.identifier().equals(iMethod.identifier()))
|
||||
.findFirst();
|
||||
|
||||
InteractionEntry entry = oentry.orElse(new InteractionEntry(iMethod.identifier())).addMethod(iMethod);
|
||||
if (oentry.isEmpty()) this.interactions.add(entry);
|
||||
for (Method method : interactionHandler.getClass().getDeclaredMethods()) {
|
||||
ReflectedMethod rMethod = this.marinara.getReflectedMethodFactory().produce(method, interactionHandler);
|
||||
if (rMethod != null && rMethod instanceof InteractionMethod) {
|
||||
InteractionMethod iMethod = (InteractionMethod) rMethod;
|
||||
InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod);
|
||||
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
@@ -71,10 +73,16 @@ public class InteractionRegistry {
|
||||
public void handle(Object context) {
|
||||
logger.debug("Received {} interaction ", context);
|
||||
interactions.forEach((e) -> {
|
||||
if (this.identifierProvider.provide(context).equals(e.identifier())) {
|
||||
if (e.identifier().equals(this.identifierProvider.provide(context, this))) {
|
||||
logger.info("Running {} interaction using {}\ncontext: {}", e.type(), e.toString(), context.toString());
|
||||
e.runAll(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<InteractionEntry> findFor(InteractionIdentifier identifier) {
|
||||
return this.interactions.stream()
|
||||
.filter(x -> x.identifier().equals(identifier))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ import org.slf4j.Logger;
|
||||
|
||||
import io.leangen.geantyref.GenericTypeReflector;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
|
||||
@@ -36,7 +37,7 @@ public class IdentifierProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public InteractionIdentifier provide(Object context) {
|
||||
public InteractionIdentifier provide(Object context, InteractionRegistry registry) {
|
||||
Type type = ReflectionUtil.getMostSpecificClass(
|
||||
converter.keySet().stream().filter(x -> x.isAssignableFrom(context.getClass())).toArray(Class<?>[]::new),
|
||||
context.getClass());
|
||||
@@ -47,12 +48,12 @@ public class IdentifierProvider {
|
||||
@SuppressWarnings("unchecked")
|
||||
Converter<Object> conv = (Converter<Object>) converter.get(type);
|
||||
|
||||
return conv.convert(context);
|
||||
return conv.convert(context, registry);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Converter<T extends Object> {
|
||||
InteractionIdentifier convert(T context);
|
||||
InteractionIdentifier convert(T context, InteractionRegistry registry);
|
||||
}
|
||||
|
||||
public static class LambdaWrapper<T extends Object> implements Converter<T> {
|
||||
@@ -64,8 +65,8 @@ public class IdentifierProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(T context) {
|
||||
return this.converter.convert(context);
|
||||
public InteractionIdentifier convert(T context, InteractionRegistry registry) {
|
||||
return this.converter.convert(context, registry);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,38 +1,53 @@
|
||||
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionEntry;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||
|
||||
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<ChatInputAutoCompleteEvent> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) {
|
||||
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context, InteractionRegistry registry) {
|
||||
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||
String commandName = context.getCommandName();
|
||||
InteractionIdentifier ident;
|
||||
|
||||
if (!options.isEmpty()) {
|
||||
List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
|
||||
if (!sub_options.isEmpty())
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
commandName,
|
||||
options.getFirst().getName(),
|
||||
sub_options.getFirst().getName());
|
||||
else
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
commandName,
|
||||
options.getFirst().getName());
|
||||
}else
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
commandName);
|
||||
|
||||
Optional<InteractionEntry> entry = registry.findFor(ident);
|
||||
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier) {
|
||||
SlashCommandIdentifier sIdent = (SlashCommandIdentifier) entry.get().identifier();
|
||||
return InteractionIdentifier.builder()
|
||||
.type(InteractionType.AUTOCOMPLETE)
|
||||
.name(sIdent.autocompleteRef()[0])
|
||||
.build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,12 +3,13 @@ package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||
import discord4j.core.event.domain.interaction.ButtonInteractionEvent;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
|
||||
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteractionEvent> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ButtonInteractionEvent context) {
|
||||
public InteractionIdentifier convert(ButtonInteractionEvent context, InteractionRegistry registry) {
|
||||
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||
}
|
||||
|
||||
|
@@ -6,13 +6,14 @@ import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||
|
||||
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<ChatInputInteractionEvent> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ChatInputInteractionEvent context) {
|
||||
public InteractionIdentifier convert(ChatInputInteractionEvent context, InteractionRegistry registry) {
|
||||
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||
String commandName = context.getCommandName();
|
||||
|
||||
|
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.Collections;
|
||||
|
||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
@@ -12,7 +13,12 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
public class TestAutocomplete implements InteractionHandler {
|
||||
|
||||
@SlashCommand(name = "test")
|
||||
@AutoComplete
|
||||
@AutoComplete("testAuto")
|
||||
public void exec(ChatInputInteractionEvent context) {
|
||||
|
||||
}
|
||||
|
||||
@AutoComplete("testAuto")
|
||||
public void autocomplete(ChatInputAutoCompleteEvent context, String value) {
|
||||
System.out.println("Success!");
|
||||
assertEquals(value, "test");
|
||||
|
@@ -1,37 +1,53 @@
|
||||
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionEntry;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
|
||||
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<AutocompleteInteraction> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(AutocompleteInteraction context) {
|
||||
public InteractionIdentifier convert(AutocompleteInteraction context, InteractionRegistry registry) {
|
||||
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||
String commandName = context.getCommandName();
|
||||
InteractionIdentifier ident;
|
||||
|
||||
if (!options.isEmpty()) {
|
||||
List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions();
|
||||
if (!sub_options.isEmpty())
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
commandName,
|
||||
options.getFirst().getName(),
|
||||
sub_options.getFirst().getName());
|
||||
else
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
commandName,
|
||||
options.getFirst().getName());
|
||||
}else
|
||||
return InteractionIdentifier.createHierarchy(
|
||||
InteractionType.AUTOCOMPLETE,
|
||||
ident = InteractionIdentifier.createHierarchy(
|
||||
InteractionType.COMMAND,
|
||||
commandName);
|
||||
|
||||
Optional<InteractionEntry> entry = registry.findFor(ident);
|
||||
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier) {
|
||||
SlashCommandIdentifier sIdent = (SlashCommandIdentifier) entry.get().identifier();
|
||||
return InteractionIdentifier.builder()
|
||||
.type(InteractionType.AUTOCOMPLETE)
|
||||
.name(sIdent.autocompleteRef()[0])
|
||||
.build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,12 +4,13 @@ import org.javacord.api.interaction.ButtonInteraction;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
|
||||
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteraction> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(ButtonInteraction context) {
|
||||
public InteractionIdentifier convert(ButtonInteraction context, InteractionRegistry registry) {
|
||||
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||
}
|
||||
|
||||
|
@@ -7,12 +7,13 @@ import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
|
||||
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<SlashCommandInteraction> {
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(SlashCommandInteraction context) {
|
||||
public InteractionIdentifier convert(SlashCommandInteraction context, InteractionRegistry registry) {
|
||||
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||
String commandName = context.getCommandName();
|
||||
if (!options.isEmpty()) {
|
||||
|
@@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
|
||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
@@ -13,7 +14,12 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
public class TestAutocomplete implements InteractionHandler {
|
||||
|
||||
@SlashCommand(name = "test")
|
||||
@AutoComplete
|
||||
@AutoComplete("testAuto")
|
||||
public void exec(SlashCommandCreateEvent context) {
|
||||
|
||||
}
|
||||
|
||||
@AutoComplete("testAuto")
|
||||
public void autocomplete(AutocompleteInteraction context, String value) {
|
||||
System.out.println("Success!");
|
||||
assertEquals(value, "test");
|
||||
|
Reference in New Issue
Block a user