refactor(reflection): migrate to using cutin library
This commit is contained in:
parent
ebf5600e29
commit
ef9384336a
@ -8,7 +8,7 @@ javacord = "3.8.0"
|
|||||||
discord4j = "3.2.7"
|
discord4j = "3.2.7"
|
||||||
geantyref = "2.0.0"
|
geantyref = "2.0.0"
|
||||||
mockito = "5.15.2"
|
mockito = "5.15.2"
|
||||||
cutin = "0.1.0"
|
cutin = "0.1.1-cad019e"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
||||||
@ -17,4 +17,4 @@ javacord = { module = "org.javacord:javacord", version.ref = "javacord"}
|
|||||||
discord4j = { module = "com.discord4j:discord4j-core", version.ref = "discord4j"}
|
discord4j = { module = "com.discord4j:discord4j-core", version.ref = "discord4j"}
|
||||||
geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"}
|
geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"}
|
||||||
mockito = {module = "org.mockito:mockito-core", version.ref = "mockito"}
|
mockito = {module = "org.mockito:mockito-core", version.ref = "mockito"}
|
||||||
cutin = {module = "net.tomatentum.cutin:lib", version.ref = "cutin"}
|
cutin = {module = "net.tomatentum.cutin:lib-dev", version.ref = "cutin"}
|
||||||
|
@ -17,7 +17,7 @@ dependencies {
|
|||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
implementation(libs.slf4j)
|
implementation(libs.slf4j)
|
||||||
implementation(libs.geantyref)
|
implementation(libs.geantyref)
|
||||||
implementation(libs.cutin)
|
api(libs.cutin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply a specific Java toolchain to ease working on different environments.
|
// Apply a specific Java toolchain to ease working on different environments.
|
||||||
|
@ -1,17 +1,25 @@
|
|||||||
package net.tomatentum.marinara;
|
package net.tomatentum.marinara;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodExecutor;
|
||||||
|
import net.tomatentum.cutin.ProcessorMethodExecutor;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.container.InteractionCheckContainer;
|
||||||
|
import net.tomatentum.marinara.container.InteractionMethodContainer;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.processor.AutocompleteInteractionProcessor;
|
import net.tomatentum.marinara.interaction.processor.AutocompleteInteractionProcessor;
|
||||||
import net.tomatentum.marinara.interaction.processor.DirectInteractionProcessor;
|
import net.tomatentum.marinara.interaction.processor.DirectInteractionProcessor;
|
||||||
import net.tomatentum.marinara.reflection.MethodExecutor;
|
|
||||||
import net.tomatentum.marinara.reflection.ProcessorMethodExecutor;
|
|
||||||
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;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
|
import net.tomatentum.marinara.util.ObjectAggregator;
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
@ -24,41 +32,53 @@ public class Marinara {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private LibraryWrapper wrapper;
|
private LibraryWrapper wrapper;
|
||||||
private ReflectedMethodFactory reflectedMethodFactory;
|
private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer;
|
||||||
private InteractionRegistry registry;
|
private MethodContainer<InteractionIdentifier, Object> interactionContainer;
|
||||||
private InteractionCheckRegistry checkRegistry;
|
private MethodExecutor<Object> interactionExecutor;
|
||||||
private MethodExecutor interactionExecutor;
|
|
||||||
|
|
||||||
private Marinara(LibraryWrapper wrapper) {
|
private Marinara(LibraryWrapper wrapper) {
|
||||||
this.wrapper = wrapper;
|
this.wrapper = wrapper;
|
||||||
this.reflectedMethodFactory = new ReflectedMethodFactoryImpl(this);
|
this.checkContainer = new InteractionCheckContainer();
|
||||||
this.registry = new InteractionRegistry(this);
|
this.interactionContainer = new InteractionMethodContainer(getCheckContainer(), getWrapper().getContextObjectProvider());
|
||||||
this.checkRegistry = new InteractionCheckRegistry();
|
|
||||||
IdentifierProvider provider = wrapper.createIdentifierProvider();
|
IdentifierProvider provider = wrapper.createIdentifierProvider();
|
||||||
this.interactionExecutor = (MethodExecutor) new ProcessorMethodExecutor()
|
ProcessorMethodExecutor<InteractionIdentifier, Object> exec = new ProcessorMethodExecutor<>(getInteractionContainer());
|
||||||
.addProcessor(new DirectInteractionProcessor(getRegistry(), provider, InteractionType.COMMAND, InteractionType.BUTTON))
|
exec
|
||||||
.addProcessor(new AutocompleteInteractionProcessor(this, provider));
|
.addProcessor(new DirectInteractionProcessor(provider, InteractionType.COMMAND, InteractionType.BUTTON))
|
||||||
|
.addProcessor(new AutocompleteInteractionProcessor(getWrapper(), provider));
|
||||||
|
this.interactionExecutor = exec;
|
||||||
wrapper.subscribeInteractions(this.interactionExecutor::handle);
|
wrapper.subscribeInteractions(this.interactionExecutor::handle);
|
||||||
logger.info("Marinara loaded successfully!");
|
logger.info("Marinara loaded successfully!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO move to future interactionstructure module
|
||||||
|
public void registerCommands() {
|
||||||
|
List<InteractionIdentifier> slashIdentifiers = getInteractionContainer().identifiers().stream()
|
||||||
|
.filter(i -> i.type().equals(InteractionType.COMMAND))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
SlashCommandDefinition[] defs = new ObjectAggregator<InteractionIdentifier, RootCommandIdentifier, SlashCommandDefinition>(
|
||||||
|
i -> Arrays.asList((RootCommandIdentifier)i.rootNode()),
|
||||||
|
SlashCommandDefinition::addIdentifier,
|
||||||
|
SlashCommandDefinition::new)
|
||||||
|
.aggregate(slashIdentifiers)
|
||||||
|
.toArray(SlashCommandDefinition[]::new);
|
||||||
|
|
||||||
|
wrapper.getRegisterer().register(defs);
|
||||||
|
}
|
||||||
|
|
||||||
public LibraryWrapper getWrapper() {
|
public LibraryWrapper getWrapper() {
|
||||||
return this.wrapper;
|
return this.wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InteractionRegistry getRegistry() {
|
public MethodContainer<InteractionIdentifier, Object> getInteractionContainer() {
|
||||||
return this.registry;
|
return this.interactionContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InteractionCheckRegistry getCheckRegistry() {
|
public MethodContainer<CheckMethodIdentifier, CheckExecutionContext> getCheckContainer() {
|
||||||
return this.checkRegistry;
|
return this.checkContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReflectedMethodFactory getReflectedMethodFactory() {
|
public MethodExecutor<Object> getInteractionExecutor() {
|
||||||
return this.reflectedMethodFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MethodExecutor getInteractionExecutor() {
|
|
||||||
return interactionExecutor;
|
return interactionExecutor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,50 +1,28 @@
|
|||||||
package net.tomatentum.marinara.checks;
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
public record AppliedCheck(
|
||||||
|
Annotation annotation,
|
||||||
|
ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> preExec,
|
||||||
|
ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> postExec
|
||||||
|
) {
|
||||||
|
|
||||||
private static Logger logger = LoggerUtil.getLogger(AppliedCheck.class);
|
private static Logger logger = LoggerUtil.getLogger(AppliedCheck.class);
|
||||||
|
|
||||||
public boolean pre(Object context) {
|
public boolean pre(Object context) {
|
||||||
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
logger.debug("Running InteractionCheck preExec {} with annotation {}", preExec(), annotation());
|
||||||
.filter(x -> x.getName().equals("preExec"))
|
return (boolean) preExec().run(new CheckExecutionContext(annotation, context));
|
||||||
.filter(x -> !x.isBridge())
|
|
||||||
.toArray(s -> new Method[s]);
|
|
||||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
|
||||||
method.setAccessible(true);
|
|
||||||
try {
|
|
||||||
logger.debug("Executing pre check {} with context {}", check.getClass().getName(), context.toString());
|
|
||||||
boolean result = (boolean) method.invoke(check, context, annotation);
|
|
||||||
logger.debug("Pre Check {} {} with context {}", check.getClass().getName(), result ? "succeeded" : "failed", context.toString());
|
|
||||||
return result;
|
|
||||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
|
||||||
logger.error("Failed executing pre-check", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void post(Object context) {
|
public void post(Object context) {
|
||||||
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
logger.debug("Running InteractionCheck postExec {} with annotation {}", postExec(), annotation());
|
||||||
.filter(x -> x.getName().equals("postExec"))
|
postExec().run(new CheckExecutionContext(annotation, context));
|
||||||
.filter(x -> !x.isBridge())
|
|
||||||
.toArray(s -> new Method[s]);
|
|
||||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
|
||||||
method.setAccessible(true);
|
|
||||||
try {
|
|
||||||
logger.debug("Executing post check {} with context {}", check.getClass().getName(), context.toString());
|
|
||||||
method.invoke(check, context, annotation);
|
|
||||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
|
||||||
logger.error("Failed executing post-check", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
|
public record CheckExecutionContext(Annotation annotation, Object originalContext) {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
public record CheckMethodIdentifier(Type annotationType, CheckMethodType type) {
|
||||||
|
|
||||||
|
public enum CheckMethodType {
|
||||||
|
PRE("preExec"),
|
||||||
|
POST("postExec");
|
||||||
|
|
||||||
|
private String methodName;
|
||||||
|
|
||||||
|
private CheckMethodType(String methodName) {
|
||||||
|
this.methodName = methodName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String methodName() {
|
||||||
|
return this.methodName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final String toString() {
|
||||||
|
return "InteractionCheck(%s, %s)".formatted(annotationType, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.BestCandidateMethod;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||||
|
import net.tomatentum.marinara.parser.InteractionCheckClassParser;
|
||||||
|
|
||||||
|
public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext> {
|
||||||
|
|
||||||
|
private CheckMethodIdentifier identifier;
|
||||||
|
|
||||||
|
public InteractionCheckMethod(String methodName, Object containingObject) {
|
||||||
|
super(methodName, containingObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getParameter(CheckExecutionContext context, int index) {
|
||||||
|
switch (index) {
|
||||||
|
case 0:
|
||||||
|
return context.originalContext();
|
||||||
|
case 1:
|
||||||
|
return context.annotation();
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CheckMethodIdentifier identifier() {
|
||||||
|
return this.identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class InteractionCheckMethodFactory extends BestCandidateMethod.Factory<CheckMethodIdentifier, CheckExecutionContext> {
|
||||||
|
|
||||||
|
private CheckMethodType type;
|
||||||
|
|
||||||
|
public InteractionCheckMethodFactory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> methodContainer, CheckMethodType type) {
|
||||||
|
super(methodContainer, type.methodName());
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public void addParser(ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> method, List<MethodParser> parsers) {
|
||||||
|
parsers.add(
|
||||||
|
new InteractionCheckClassParser((Class<InteractionCheck<?>>) method.containingObject().getClass(),
|
||||||
|
a -> ((InteractionCheckMethod) method).identifier = new CheckMethodIdentifier(a, type))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce(String methodName,
|
||||||
|
Object containingObject) {
|
||||||
|
if (!(containingObject instanceof InteractionCheck))
|
||||||
|
return Optional.empty();
|
||||||
|
return Optional.of(new InteractionCheckMethod(methodName, containingObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package net.tomatentum.marinara.container;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactoryImpl;
|
||||||
|
import net.tomatentum.cutin.container.LoneMethodContainer;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||||
|
import net.tomatentum.marinara.checks.InteractionCheckMethod.InteractionCheckMethodFactory;
|
||||||
|
|
||||||
|
public class InteractionCheckContainer extends LoneMethodContainer<CheckMethodIdentifier, CheckExecutionContext> {
|
||||||
|
|
||||||
|
public InteractionCheckContainer() {
|
||||||
|
super(new ReflectedMethodFactoryImpl<>());
|
||||||
|
super.factory()
|
||||||
|
.addFactory(new InteractionCheckMethodFactory(this, CheckMethodType.PRE))
|
||||||
|
.addFactory(new InteractionCheckMethodFactory(this, CheckMethodType.POST));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package net.tomatentum.marinara.container;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactoryImpl;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.container.MultiMethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.components.methods.ButtonInteractionMethod;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
import net.tomatentum.marinara.interaction.methods.AutoCompleteInteractionMethod;
|
||||||
|
import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod;
|
||||||
|
|
||||||
|
public class InteractionMethodContainer extends MultiMethodContainer<InteractionIdentifier, Object> {
|
||||||
|
|
||||||
|
|
||||||
|
private static ReflectedMethodFactory<InteractionIdentifier, Object> createFactory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkRegistry, ContextObjectProvider cop) {
|
||||||
|
return new ReflectedMethodFactoryImpl<InteractionIdentifier, Object>()
|
||||||
|
.addFactory(new AutoCompleteInteractionMethod.Factory(checkRegistry, cop))
|
||||||
|
.addFactory(new SlashCommandInteractionMethod.Factory(checkRegistry, cop))
|
||||||
|
.addFactory(new ButtonInteractionMethod.Factory(checkRegistry, cop));
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionMethodContainer(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkRegistry, ContextObjectProvider cop) {
|
||||||
|
super(createFactory(checkRegistry, cop));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MethodContainer<InteractionIdentifier, Object> addMethod(ReflectedMethod<InteractionIdentifier, Object> method) {
|
||||||
|
super.identifiers().stream()
|
||||||
|
.filter(method.identifier()::equals)
|
||||||
|
.forEach(i -> InteractionIdentifier.tryAddDescriptions(i, method.identifier()));
|
||||||
|
return super.addMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,28 +4,37 @@ import java.lang.reflect.Method;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
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.Button;
|
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
|
||||||
import net.tomatentum.marinara.parser.ButtonParser;
|
import net.tomatentum.marinara.parser.ButtonParser;
|
||||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
public class ButtonInteractionMethod extends InteractionMethod {
|
public class ButtonInteractionMethod extends InteractionMethod {
|
||||||
|
|
||||||
private String customId;
|
private String customId;
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
private ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
private ButtonInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) {
|
||||||
super(method, handler, marinara);
|
super(method, handler);
|
||||||
|
this.cop = cop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getParameter(Object context, int index) {
|
public Object getParameter(Object context, int index) {
|
||||||
Class<?> type = method().getParameterTypes()[index+1];
|
Class<?> type = method().getParameterTypes()[index];
|
||||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
Object superResult = super.getParameter(context, index);
|
||||||
|
if (superResult == null)
|
||||||
|
return this.cop.getComponentContextObject(context, type);
|
||||||
|
else
|
||||||
|
return superResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -39,19 +48,26 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
|||||||
|
|
||||||
public static class Factory extends InteractionMethod.Factory {
|
public static class Factory extends InteractionMethod.Factory {
|
||||||
|
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
|
public Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer, ContextObjectProvider cop) {
|
||||||
|
super(checkContainer);
|
||||||
|
this.cop = cop;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) {
|
||||||
ReflectedMethod rMethod = null;
|
ButtonInteractionMethod rMethod = null;
|
||||||
if (method.isAnnotationPresent(Button.class) &&
|
if (method.isAnnotationPresent(Button.class) &&
|
||||||
(containingObject instanceof InteractionHandler iHandler)
|
(containingObject instanceof InteractionHandler iHandler)
|
||||||
)
|
)
|
||||||
rMethod = new ButtonInteractionMethod(method, iHandler, marinara);
|
rMethod = new ButtonInteractionMethod(method, iHandler, this.cop);
|
||||||
|
|
||||||
return Optional.ofNullable(rMethod);
|
return Optional.ofNullable(rMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||||
super.addParser(method, parser);
|
super.addParser(method, parser);
|
||||||
|
|
||||||
parser.add(
|
parser.add(
|
||||||
|
@ -4,40 +4,45 @@ import java.lang.reflect.Method;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
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.AutoComplete;
|
||||||
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;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
|
||||||
import net.tomatentum.marinara.parser.AutocompleteParser;
|
import net.tomatentum.marinara.parser.AutocompleteParser;
|
||||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
public class AutoCompleteInteractionMethod extends InteractionMethod {
|
public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||||
|
|
||||||
private String autocompleteRef;
|
private String autocompleteRef;
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
private AutoCompleteInteractionMethod(Method method,
|
private AutoCompleteInteractionMethod(Method method,
|
||||||
InteractionHandler handler,
|
InteractionHandler handler,
|
||||||
Marinara marinara
|
ContextObjectProvider cop
|
||||||
) {
|
) {
|
||||||
super(method, handler, marinara);
|
super(method, handler);
|
||||||
|
this.cop = cop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getParameter(Object context, int index) {
|
public Object getParameter(Object context, int index) {
|
||||||
Class<?> type = method().getParameterTypes()[index+1];
|
Class<?> type = method().getParameterTypes()[index];
|
||||||
Object contextObject = marinara.getWrapper().getContextObjectProvider().getInteractionContextObject(context, type);
|
Object contextObject = this.cop.getInteractionContextObject(context, type);
|
||||||
if (contextObject != null)
|
if (contextObject != null)
|
||||||
return contextObject;
|
return contextObject;
|
||||||
|
|
||||||
Object autocompleteOptionValue = marinara.getWrapper().getContextObjectProvider().getAutocompleteFocusedOption(context).input();
|
Object autocompleteOptionValue = this.cop.getAutocompleteFocusedOption(context).input();
|
||||||
if (type.isInstance(autocompleteOptionValue))
|
if (type.isInstance(autocompleteOptionValue))
|
||||||
return autocompleteOptionValue;
|
return autocompleteOptionValue;
|
||||||
|
|
||||||
return null;
|
return super.getParameter(context, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -51,20 +56,27 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
|||||||
|
|
||||||
public static class Factory extends InteractionMethod.Factory {
|
public static class Factory extends InteractionMethod.Factory {
|
||||||
|
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
|
public Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer, ContextObjectProvider cop) {
|
||||||
|
super(checkContainer);
|
||||||
|
this.cop = cop;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) {
|
||||||
ReflectedMethod rMethod = null;
|
AutoCompleteInteractionMethod rMethod = null;
|
||||||
if ((containingObject instanceof InteractionHandler iHandler) &&
|
if ((containingObject instanceof InteractionHandler iHandler) &&
|
||||||
method.isAnnotationPresent(AutoComplete.class) &&
|
method.isAnnotationPresent(AutoComplete.class) &&
|
||||||
!(method.isAnnotationPresent(SlashCommand.class) ||
|
!(method.isAnnotationPresent(SlashCommand.class) ||
|
||||||
method.isAnnotationPresent(SubCommand.class)))
|
method.isAnnotationPresent(SubCommand.class)))
|
||||||
rMethod = new AutoCompleteInteractionMethod(method, iHandler, marinara);
|
rMethod = new AutoCompleteInteractionMethod(method, iHandler, cop);
|
||||||
|
|
||||||
return Optional.ofNullable(rMethod);
|
return Optional.ofNullable(rMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||||
super.addParser(method, parser);
|
super.addParser(method, parser);
|
||||||
|
|
||||||
parser.add(
|
parser.add(
|
||||||
|
@ -4,30 +4,36 @@ import java.lang.reflect.Method;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
|
||||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
|
||||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactory;
|
|
||||||
|
|
||||||
public abstract class InteractionMethod extends ReflectedMethod {
|
public abstract class InteractionMethod extends ReflectedMethod<InteractionIdentifier, Object> {
|
||||||
|
|
||||||
protected Marinara marinara;
|
|
||||||
protected List<AppliedCheck> appliedChecks;
|
protected List<AppliedCheck> appliedChecks;
|
||||||
|
|
||||||
protected InteractionMethod(
|
protected InteractionMethod(
|
||||||
Method method,
|
Method method,
|
||||||
InteractionHandler handler,
|
InteractionHandler handler
|
||||||
Marinara marinara
|
|
||||||
) {
|
) {
|
||||||
super(method, handler);
|
super(method, handler);
|
||||||
this.marinara = marinara;
|
|
||||||
this.appliedChecks = new ArrayList<>();
|
this.appliedChecks = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getParameter(Object context, int index) {
|
||||||
|
if (index == 0)
|
||||||
|
return context;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object run(Object context) {
|
public Object run(Object context) {
|
||||||
Object result = null;
|
Object result = null;
|
||||||
@ -41,23 +47,23 @@ public abstract class InteractionMethod extends ReflectedMethod {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract InteractionIdentifier identifier();
|
|
||||||
|
|
||||||
public Marinara marinara() {
|
|
||||||
return this.marinara;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<AppliedCheck> appliedChecks() {
|
public List<AppliedCheck> appliedChecks() {
|
||||||
return this.appliedChecks;
|
return this.appliedChecks;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract static class Factory implements ReflectedMethodFactory.Factory {
|
public abstract static class Factory implements ReflectedMethodFactory.Factory<InteractionIdentifier, Object> {
|
||||||
|
|
||||||
|
private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer;
|
||||||
|
|
||||||
|
protected Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) {
|
||||||
|
this.checkContainer = checkContainer;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||||
InteractionMethod imethod = (InteractionMethod) method;
|
InteractionMethod imethod = (InteractionMethod) method;
|
||||||
parser.add(
|
parser.add(
|
||||||
new InteractionCheckParser(method.method(), imethod.appliedChecks::add, imethod.marinara().getCheckRegistry())
|
new InteractionCheckParser(method.method(), imethod.appliedChecks::add, this.checkContainer)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,27 +4,36 @@ import java.lang.reflect.Method;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
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;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
|
||||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
public class SlashCommandInteractionMethod extends InteractionMethod {
|
public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||||
|
|
||||||
private SlashCommandIdentifier interactionIdentifier;
|
private SlashCommandIdentifier interactionIdentifier;
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
private SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
private SlashCommandInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) {
|
||||||
super(method, handler, marinara);
|
super(method, handler);
|
||||||
|
this.cop = cop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getParameter(Object context, int index) {
|
public Object getParameter(Object context, int index) {
|
||||||
return marinara.getWrapper().getContextObjectProvider().convertCommandOption(context, interactionIdentifier.options()[index].name());
|
Object superResult = super.getParameter(context, index);
|
||||||
|
if (superResult == null)
|
||||||
|
return this.cop.convertCommandOption(context, interactionIdentifier.options()[index-1].name());
|
||||||
|
else
|
||||||
|
return superResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -34,18 +43,25 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
|||||||
|
|
||||||
public static class Factory extends InteractionMethod.Factory {
|
public static class Factory extends InteractionMethod.Factory {
|
||||||
|
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
|
public Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer, ContextObjectProvider cop) {
|
||||||
|
super(checkContainer);
|
||||||
|
this.cop = cop;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) {
|
||||||
ReflectedMethod rMethod = null;
|
SlashCommandInteractionMethod rMethod = null;
|
||||||
if ((containingObject instanceof InteractionHandler iHandler) &&
|
if ((containingObject instanceof InteractionHandler iHandler) &&
|
||||||
(method.isAnnotationPresent(SlashCommand.class) ||
|
(method.isAnnotationPresent(SlashCommand.class) ||
|
||||||
method.isAnnotationPresent(SubCommand.class)))
|
method.isAnnotationPresent(SubCommand.class)))
|
||||||
rMethod = new SlashCommandInteractionMethod(method, iHandler, marinara);
|
rMethod = new SlashCommandInteractionMethod(method, iHandler, cop);
|
||||||
return Optional.ofNullable(rMethod);
|
return Optional.ofNullable(rMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||||
super.addParser(method, parser);
|
super.addParser(method, parser);
|
||||||
|
|
||||||
parser.add(
|
parser.add(
|
||||||
|
@ -6,37 +6,38 @@ import java.util.Objects;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
import net.tomatentum.marinara.registry.InteractionEntry;
|
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
public class AutocompleteInteractionProcessor extends InteractionMethodProcessor {
|
public class AutocompleteInteractionProcessor extends InteractionMethodProcessor {
|
||||||
|
|
||||||
private Marinara marinara;
|
private LibraryWrapper wrapper;
|
||||||
|
|
||||||
public AutocompleteInteractionProcessor(Marinara marinara, IdentifierProvider provider) {
|
public AutocompleteInteractionProcessor(LibraryWrapper wrapper, IdentifierProvider provider) {
|
||||||
super(provider, Set.of(InteractionType.AUTOCOMPLETE));
|
super(provider, Set.of(InteractionType.AUTOCOMPLETE));
|
||||||
this.marinara = marinara;
|
this.wrapper = wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processInteraction(Object context, InteractionIdentifier identifier) {
|
public void processInteraction(Object context, MethodContainer<InteractionIdentifier, Object> container, InteractionIdentifier identifier) {
|
||||||
Optional<InteractionEntry> entry = this.marinara.getRegistry().findFor(convertToCommandIdentifier(identifier));
|
Optional<InteractionIdentifier> oIdent = container.identifiers().stream()
|
||||||
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier sIdent) {
|
.filter(i -> convertToCommandIdentifier(identifier).equals(i))
|
||||||
List<String> autocompleteRefs = Arrays.asList(this.marinara.getWrapper().getContextObjectProvider()
|
.findFirst();
|
||||||
|
if (oIdent.isPresent() && oIdent.get() instanceof SlashCommandIdentifier sIdent) {
|
||||||
|
List<String> autocompleteRefs = Arrays.asList(this.wrapper.getContextObjectProvider()
|
||||||
.getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options()));
|
.getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options()));
|
||||||
List<Object> results = this.marinara.getRegistry().interactions().stream()
|
List<Object> results = container.methods().stream()
|
||||||
.filter(e -> e.type().equals(InteractionType.AUTOCOMPLETE))
|
.filter(m -> m.identifier().type().equals(InteractionType.AUTOCOMPLETE))
|
||||||
.filter(e -> autocompleteRefs.contains(e.identifier().name()))
|
.filter(m -> autocompleteRefs.contains(m.identifier().name()))
|
||||||
.map(e -> e.runAll(context))
|
.map(m -> m.run(context))
|
||||||
.flatMap(Arrays::stream)
|
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.toList();
|
.toList();
|
||||||
if (!results.isEmpty())
|
if (!results.isEmpty())
|
||||||
marinara.getWrapper().respondAutocomplete(context, results);
|
this.wrapper.respondAutocomplete(context, results);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,26 +2,21 @@ package net.tomatentum.marinara.interaction.processor;
|
|||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
public class DirectInteractionProcessor extends InteractionMethodProcessor {
|
public class DirectInteractionProcessor extends InteractionMethodProcessor {
|
||||||
|
|
||||||
private InteractionRegistry registry;
|
|
||||||
|
|
||||||
public DirectInteractionProcessor(InteractionRegistry registry, IdentifierProvider provider, InteractionType... types) {
|
public DirectInteractionProcessor(IdentifierProvider provider, InteractionType... types) {
|
||||||
super(provider, Set.of(types));
|
super(provider, Set.of(types));
|
||||||
this.registry = registry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void processInteraction(Object context, InteractionIdentifier identifier) {
|
protected void processInteraction(Object context, MethodContainer<InteractionIdentifier, Object> container, InteractionIdentifier identifier) {
|
||||||
this.registry.interactions().stream()
|
container.findFor(identifier).forEach(m -> m.run(context));
|
||||||
.filter(e -> e.identifier().equals(identifier))
|
|
||||||
.findFirst()
|
|
||||||
.ifPresent(e -> e.runAll(context));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,14 @@ import java.util.Set;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodProcessor;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.reflection.MethodProcessor;
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
public abstract class InteractionMethodProcessor implements MethodProcessor {
|
public abstract class InteractionMethodProcessor implements MethodProcessor<InteractionIdentifier, Object> {
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||||
|
|
||||||
@ -23,13 +24,16 @@ public abstract class InteractionMethodProcessor implements MethodProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(Object context) {
|
public void process(Object context, MethodContainer<InteractionIdentifier, Object> container) {
|
||||||
InteractionIdentifier identifier = this.provider.provide(context);
|
InteractionIdentifier identifier = this.provider.provide(context);
|
||||||
if (!this.types.contains(identifier.type())) return;
|
if (!this.types.contains(identifier.type())) return;
|
||||||
logger.debug("Processing {} : {} with context {}", identifier, identifier.type(), context);
|
logger.debug("Processing {} : {} with context {}", identifier, identifier.type(), context);
|
||||||
this.processInteraction(context, identifier);
|
this.processInteraction(context, container, identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void processInteraction(Object context, InteractionIdentifier identifier);
|
protected abstract void processInteraction(
|
||||||
|
Object context,
|
||||||
|
MethodContainer<InteractionIdentifier, Object> container,
|
||||||
|
InteractionIdentifier identifier);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
package net.tomatentum.marinara.parser;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
public interface AnnotationParser {
|
|
||||||
void parse();
|
|
||||||
Method getMethod();
|
|
||||||
}
|
|
@ -4,9 +4,16 @@ import java.lang.reflect.Method;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||||
|
|
||||||
public class AutocompleteParser implements AnnotationParser {
|
public class AutocompleteParser implements MethodParser {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private Method method;
|
private Method method;
|
||||||
private Consumer<String[]> consumer;
|
private Consumer<String[]> consumer;
|
||||||
@ -21,13 +28,8 @@ public class AutocompleteParser implements AnnotationParser {
|
|||||||
String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class))
|
String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class))
|
||||||
.map(AutoComplete::value)
|
.map(AutoComplete::value)
|
||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
|
logger.trace("Parsed AutoComplete annotation {} for method {}", autocompletes, ReflectionUtil.getFullMethodName(method));
|
||||||
this.consumer.accept(autocompletes);
|
this.consumer.accept(autocompletes);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Method getMethod() {
|
|
||||||
return this.method;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,12 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class ButtonParser implements AnnotationParser {
|
public class ButtonParser implements MethodParser {
|
||||||
|
|
||||||
private Method method;
|
private Method method;
|
||||||
private Consumer<String> consumer;
|
private Consumer<String> consumer;
|
||||||
@ -23,14 +24,9 @@ public class ButtonParser implements AnnotationParser {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parse() {
|
public void parse() {
|
||||||
Button button = getMethod().getAnnotation(Button.class);
|
Button button = this.method.getAnnotation(Button.class);
|
||||||
logger.trace("Parsed Button annotation {} for method {}", button.toString(), ReflectionUtil.getFullMethodName(method));
|
logger.trace("Parsed Button annotation {} for method {}", button, ReflectionUtil.getFullMethodName(method));
|
||||||
this.consumer.accept(button.value());
|
this.consumer.accept(button.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Method getMethod() {
|
|
||||||
return this.method;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package net.tomatentum.marinara.parser;
|
||||||
|
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import io.leangen.geantyref.GenericTypeReflector;
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||||
|
|
||||||
|
public class InteractionCheckClassParser implements MethodParser {
|
||||||
|
|
||||||
|
private Class<? extends InteractionCheck<?>> interactionCheckType;
|
||||||
|
private Consumer<Type> annotationTypeConsumer;
|
||||||
|
|
||||||
|
public InteractionCheckClassParser(Class<? extends InteractionCheck<?>> interactionCheckType, Consumer<Type> annotationTypeConsumer) {
|
||||||
|
this.interactionCheckType = interactionCheckType;
|
||||||
|
this.annotationTypeConsumer = annotationTypeConsumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void parse() {
|
||||||
|
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheckType, InteractionCheck.class);
|
||||||
|
Type typeParam = type.getActualTypeArguments()[0];
|
||||||
|
this.annotationTypeConsumer.accept(typeParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,27 +3,29 @@ package net.tomatentum.marinara.parser;
|
|||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class InteractionCheckParser implements AnnotationParser {
|
public class InteractionCheckParser implements MethodParser {
|
||||||
|
|
||||||
private InteractionCheckRegistry checkRegistry;
|
private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer;
|
||||||
private Method method;
|
private Method method;
|
||||||
private Consumer<AppliedCheck> consumer;
|
private Consumer<AppliedCheck> consumer;
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||||
|
|
||||||
public InteractionCheckParser(Method method, Consumer<AppliedCheck> consumer, InteractionCheckRegistry checkRegistry) {
|
public InteractionCheckParser(Method method, Consumer<AppliedCheck> consumer, MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) {
|
||||||
this.checkRegistry = checkRegistry;
|
this.checkContainer = checkContainer;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.consumer = consumer;
|
this.consumer = consumer;
|
||||||
}
|
}
|
||||||
@ -35,17 +37,13 @@ public class InteractionCheckParser implements AnnotationParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void convertAnnotation(Annotation annotation) {
|
private void convertAnnotation(Annotation annotation) {
|
||||||
Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.annotationType());
|
var preExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.PRE));
|
||||||
if (check.isPresent()) {
|
var postExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.POST));
|
||||||
AppliedCheck appliedCheck = new AppliedCheck(check.get(), annotation);
|
if (preExec.isPresent() && postExec.isPresent()) {
|
||||||
logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", check.getClass().getName(), annotation.toString(), ReflectionUtil.getFullMethodName(method));
|
AppliedCheck appliedCheck = new AppliedCheck(annotation, preExec.get(), postExec.get());
|
||||||
|
logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", preExec.get().containingObject(), annotation, ReflectionUtil.getFullMethodName(method));
|
||||||
consumer.accept(appliedCheck);
|
consumer.accept(appliedCheck);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Method getMethod() {
|
|
||||||
return this.method;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
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;
|
||||||
@ -12,9 +14,8 @@ import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
|
|||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class SlashCommandParser implements AnnotationParser {
|
public class SlashCommandParser implements MethodParser {
|
||||||
|
|
||||||
private Method method;
|
private Method method;
|
||||||
private Consumer<SlashCommandIdentifier> consumer;
|
private Consumer<SlashCommandIdentifier> consumer;
|
||||||
@ -57,15 +58,10 @@ public class SlashCommandParser implements AnnotationParser {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());
|
logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier);
|
||||||
consumer.accept((SlashCommandIdentifier) lastIdentifier);
|
consumer.accept((SlashCommandIdentifier) lastIdentifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Method getMethod() {
|
|
||||||
return this.method;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkValidCommandMethod(Method method) {
|
private void checkValidCommandMethod(Method method) {
|
||||||
if (method.isAnnotationPresent(SlashCommand.class) &&
|
if (method.isAnnotationPresent(SlashCommand.class) &&
|
||||||
method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) {
|
method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) {
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package net.tomatentum.marinara.reflection;
|
|
||||||
|
|
||||||
public interface MethodExecutor {
|
|
||||||
|
|
||||||
void handle(Object context);
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package net.tomatentum.marinara.reflection;
|
|
||||||
|
|
||||||
public interface MethodProcessor {
|
|
||||||
|
|
||||||
void process(Object context);
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package net.tomatentum.marinara.reflection;
|
|
||||||
|
|
||||||
public interface ProcessorContainer {
|
|
||||||
|
|
||||||
ProcessorContainer addProcessor(MethodProcessor processor);
|
|
||||||
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package net.tomatentum.marinara.reflection;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
|
|
||||||
public class ProcessorMethodExecutor implements MethodExecutor, ProcessorContainer {
|
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
private Set<MethodProcessor> processors;
|
|
||||||
|
|
||||||
public ProcessorMethodExecutor() {
|
|
||||||
this.processors = new HashSet<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ProcessorContainer addProcessor(MethodProcessor processor) {
|
|
||||||
processors.add(processor);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handle(Object context) {
|
|
||||||
logger.debug("Received {} interaction ", context);
|
|
||||||
processors.forEach(x -> x.process(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,66 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package net.tomatentum.marinara.reflection;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
|
||||||
|
|
||||||
public interface ReflectedMethodFactory {
|
|
||||||
Optional<ReflectedMethod> produce(Method method, Object containingClass);
|
|
||||||
ReflectedMethodFactory addFactory(Factory factory);
|
|
||||||
|
|
||||||
public interface Factory {
|
|
||||||
|
|
||||||
Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject);
|
|
||||||
void addParser(ReflectedMethod method, List<AnnotationParser> parser);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,62 +0,0 @@
|
|||||||
package net.tomatentum.marinara.reflection;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
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 Optional<ReflectedMethod> produce(Method method, Object containingClass) {
|
|
||||||
Optional<ReflectedMethod> imethod = this.factories.stream()
|
|
||||||
.map(f -> factoryProduce(f, method, containingClass))
|
|
||||||
.filter(Optional::isPresent)
|
|
||||||
.map(Optional::get)
|
|
||||||
.findFirst();
|
|
||||||
|
|
||||||
if (imethod.isEmpty()) {
|
|
||||||
logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method));
|
|
||||||
}
|
|
||||||
|
|
||||||
return imethod;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ReflectedMethodFactory addFactory(Factory factory) {
|
|
||||||
this.factories.add(factory);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Optional<ReflectedMethod> factoryProduce(Factory factory, Method method, Object containingClass) {
|
|
||||||
List<AnnotationParser> parser = new ArrayList<>();
|
|
||||||
Optional<ReflectedMethod> m = factory.produce(this.marinara, method, containingClass);
|
|
||||||
m.ifPresent(x -> {
|
|
||||||
factory.addParser(x, parser);
|
|
||||||
parser.forEach(AnnotationParser::parse);
|
|
||||||
});
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
package net.tomatentum.marinara.registry;
|
|
||||||
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import io.leangen.geantyref.GenericTypeReflector;
|
|
||||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
|
|
||||||
public class InteractionCheckRegistry {
|
|
||||||
|
|
||||||
private List<InteractionCheck<?>> checks;
|
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public InteractionCheckRegistry() {
|
|
||||||
this.checks = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addCheck(InteractionCheck<?> check) {
|
|
||||||
checks.add(check);
|
|
||||||
logger.info("Registered Check {}", check.getClass().getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Type annotation) {
|
|
||||||
for (InteractionCheck<?> interactionCheck : checks) {
|
|
||||||
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheck.getClass(), InteractionCheck.class);
|
|
||||||
Type typeParam = type.getActualTypeArguments()[0];
|
|
||||||
if (typeParam.equals(annotation))
|
|
||||||
return Optional.of(interactionCheck);
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,90 +0,0 @@
|
|||||||
package net.tomatentum.marinara.registry;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
|
||||||
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;
|
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public InteractionEntry(InteractionIdentifier identifier) {
|
|
||||||
this.identifier = identifier;
|
|
||||||
this.methods = new HashSet<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public InteractionEntry addMethod(InteractionMethod method) {
|
|
||||||
InteractionIdentifier midentifier = method.identifier();
|
|
||||||
|
|
||||||
if (!this.identifier().equals(midentifier))
|
|
||||||
throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier");
|
|
||||||
|
|
||||||
this.methods.add(method);
|
|
||||||
InteractionIdentifier.tryAddDescriptions(midentifier, midentifier);
|
|
||||||
logger.debug("Added method {} to entry {}", method.method().getName(), this.identifier);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object[] runAll(Object context) {
|
|
||||||
return this.methods.stream()
|
|
||||||
.map(x -> {
|
|
||||||
logger.debug("Running Method {} from {} with context {}", x, this, context);
|
|
||||||
return x.run(context);
|
|
||||||
})
|
|
||||||
.flatMap(o -> o instanceof Object[] oArray ? Arrays.stream(oArray) : Stream.<Object>of(o))
|
|
||||||
.toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (!(obj instanceof InteractionEntry))
|
|
||||||
return false;
|
|
||||||
InteractionEntry other = (InteractionEntry) obj;
|
|
||||||
return other.identifier().equals(identifier());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return this.identifier().hashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "InteractionEntry(%s)".formatted(identifier().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
public InteractionType type() {
|
|
||||||
return identifier().type();
|
|
||||||
}
|
|
||||||
|
|
||||||
public InteractionIdentifier identifier() {
|
|
||||||
return identifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<InteractionMethod> methods() {
|
|
||||||
return methods;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,80 +0,0 @@
|
|||||||
package net.tomatentum.marinara.registry;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
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.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());
|
|
||||||
private Set<InteractionEntry> interactions;
|
|
||||||
private Marinara marinara;
|
|
||||||
|
|
||||||
public InteractionRegistry(Marinara marinara) {
|
|
||||||
this.interactions = new HashSet<>();
|
|
||||||
this.marinara = marinara;
|
|
||||||
marinara.getReflectedMethodFactory()
|
|
||||||
.addFactory(new AutoCompleteInteractionMethod.Factory())
|
|
||||||
.addFactory(new SlashCommandInteractionMethod.Factory())
|
|
||||||
.addFactory(new ButtonInteractionMethod.Factory());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addInteractions(InteractionHandler interactionHandler) {
|
|
||||||
for (Method method : interactionHandler.getClass().getDeclaredMethods()) {
|
|
||||||
Optional<ReflectedMethod> rMethod = this.marinara.getReflectedMethodFactory().produce(method, interactionHandler);
|
|
||||||
rMethod.ifPresent(x -> {
|
|
||||||
if (x instanceof InteractionMethod) {
|
|
||||||
InteractionMethod iMethod = (InteractionMethod) x;
|
|
||||||
InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod);
|
|
||||||
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
logger.info("Added all Interactions from {}", interactionHandler.getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerCommands() {
|
|
||||||
List<InteractionIdentifier> slashIdentifiers = interactions.stream()
|
|
||||||
.filter((x) -> x.type().equals(InteractionType.COMMAND))
|
|
||||||
.map((x) -> x.identifier())
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
SlashCommandDefinition[] defs = new ObjectAggregator<InteractionIdentifier, RootCommandIdentifier, SlashCommandDefinition>(
|
|
||||||
i -> Arrays.asList((RootCommandIdentifier)i.rootNode()),
|
|
||||||
SlashCommandDefinition::addIdentifier,
|
|
||||||
SlashCommandDefinition::new)
|
|
||||||
.aggregate(slashIdentifiers)
|
|
||||||
.toArray(SlashCommandDefinition[]::new);
|
|
||||||
|
|
||||||
marinara.getWrapper().getRegisterer().register(defs);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<InteractionEntry> findFor(InteractionIdentifier identifier) {
|
|
||||||
return this.interactions.stream()
|
|
||||||
.filter(x -> x.identifier().equals(identifier))
|
|
||||||
.findFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<InteractionEntry> interactions() {
|
|
||||||
return this.interactions;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,107 +0,0 @@
|
|||||||
package net.tomatentum.marinara.util;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public final class ReflectionUtil {
|
|
||||||
|
|
||||||
public static <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) {
|
|
||||||
if (method.isAnnotationPresent(annotationClass) || method.getDeclaringClass().isAnnotationPresent(annotationClass))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) {
|
|
||||||
return method.isAnnotationPresent(annotationClass) ?
|
|
||||||
method.getAnnotation(annotationClass) :
|
|
||||||
method.getDeclaringClass().getAnnotation(annotationClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getCastDepth(Class<?> child, Class<?> parent) {
|
|
||||||
|
|
||||||
if (parent.equals(Object.class))
|
|
||||||
return Integer.MAX_VALUE;
|
|
||||||
|
|
||||||
if (!parent.isAssignableFrom(child)) {
|
|
||||||
throw new IllegalArgumentException("The specified class is not a child class of the specified parent.");
|
|
||||||
}
|
|
||||||
|
|
||||||
int depth = 0;
|
|
||||||
Class<?> curr = child;
|
|
||||||
List<Class<?>> parents = new ArrayList<>();
|
|
||||||
|
|
||||||
while (!curr.equals(parent)) {
|
|
||||||
depth++;
|
|
||||||
parents.add(curr.getSuperclass());
|
|
||||||
parents.addAll(Arrays.asList(curr.getInterfaces()));
|
|
||||||
|
|
||||||
for (Class<?> currParent : parents) {
|
|
||||||
if (currParent != null && parent.isAssignableFrom(currParent)) {
|
|
||||||
curr = currParent;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
parents.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return depth;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Method getMostSpecificMethod(Method[] methods, Class<?>... parameters) {
|
|
||||||
List<Method> compatibleMethods = Arrays.stream(methods)
|
|
||||||
.filter(x -> isMethodCallable(x, parameters))
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
if (compatibleMethods.size() == 0)
|
|
||||||
throw new IllegalArgumentException("There are no compatible Methods provided");
|
|
||||||
|
|
||||||
for (int i = 0; i < parameters.length; i++) {
|
|
||||||
final int currI = i;
|
|
||||||
Class<?>[] parameterTypes = compatibleMethods.stream()
|
|
||||||
.map(x -> x.getParameterTypes()[currI])
|
|
||||||
.toArray(x -> new Class[x]);
|
|
||||||
|
|
||||||
Class<?> mostSpecific = getMostSpecificClass(parameterTypes, parameters[i]);
|
|
||||||
|
|
||||||
compatibleMethods = compatibleMethods.stream()
|
|
||||||
.filter(x -> Objects.equals(x.getParameterTypes()[currI], mostSpecific))
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return compatibleMethods.getFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Class<?> getMostSpecificClass(Class<?>[] classes, Class<?> base) {
|
|
||||||
int min = Integer.MAX_VALUE;
|
|
||||||
Class<?> currMostSpecific = null;
|
|
||||||
for (Class<?> currClass : classes) {
|
|
||||||
int currCastDepth = getCastDepth(base, currClass);
|
|
||||||
if (currCastDepth <= min) {
|
|
||||||
min = currCastDepth;
|
|
||||||
currMostSpecific = currClass;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return currMostSpecific;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isMethodCallable(Method method, Class<?>... parameters) {
|
|
||||||
if (!Objects.equals(method.getParameterCount(), parameters.length))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
Class<?>[] methodParams = method.getParameterTypes();
|
|
||||||
for (int i = 0; i < parameters.length; i++) {
|
|
||||||
if (!methodParams[i].isAssignableFrom(parameters[i]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFullMethodName(Method method) {
|
|
||||||
return method.getDeclaringClass().getName() + "." + method.getName();
|
|
||||||
}
|
|
||||||
}
|
|
@ -10,9 +10,9 @@ import java.util.Map;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import io.leangen.geantyref.GenericTypeReflector;
|
import io.leangen.geantyref.GenericTypeReflector;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class IdentifierProvider {
|
public class IdentifierProvider {
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ class AutoCompleteTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestAutocomplete());
|
marinara.getInteractionContainer().addAllMethods(new TestAutocomplete());
|
||||||
wrapper.handleInteraction(autoCompleteEventMock);
|
wrapper.handleInteraction(autoCompleteEventMock);
|
||||||
verify(autoCompleteEventMock).respondWithSuggestions(any());
|
verify(autoCompleteEventMock).respondWithSuggestions(any());
|
||||||
}
|
}
|
||||||
|
@ -13,15 +13,15 @@ import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
|||||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class ButtonTest {
|
class ButtonTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testButtonExecution() {
|
void testButtonExecution() {
|
||||||
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(buttonEventMock);
|
wrapper.handleInteraction(buttonEventMock);
|
||||||
assertTrue(TestButton.didRun);
|
assertTrue(TestButton.didRun);
|
||||||
}
|
}
|
||||||
|
@ -24,16 +24,16 @@ import net.tomatentum.marinara.wrapper.discord4j.checks.PermissionCheck;
|
|||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class InteractionCheckTest {
|
class InteractionCheckTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInteractionCheck() {
|
void testInteractionCheck() {
|
||||||
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
|
marinara.getCheckContainer().addAllMethods(new TestInteractionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(buttonEventMock);
|
wrapper.handleInteraction(buttonEventMock);
|
||||||
|
|
||||||
assertTrue(TestInteractionCheck.preExecuted);
|
assertTrue(TestInteractionCheck.preExecuted);
|
||||||
@ -42,7 +42,7 @@ public class InteractionCheckTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPermissionCheck() {
|
void testPermissionCheck() {
|
||||||
Member memberMock = mock();
|
Member memberMock = mock();
|
||||||
Interaction interactionMock = mock();
|
Interaction interactionMock = mock();
|
||||||
|
|
||||||
@ -54,8 +54,8 @@ public class InteractionCheckTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new PermissionCheck());
|
marinara.getCheckContainer().addAllMethods(new PermissionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
|
|
||||||
wrapper.handleInteraction(buttonEventMock);
|
wrapper.handleInteraction(buttonEventMock);
|
||||||
assertFalse(TestButton.didPermRun);
|
assertFalse(TestButton.didPermRun);
|
||||||
|
@ -22,9 +22,9 @@ import net.tomatentum.marinara.Marinara;
|
|||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class SlashCommandTest {
|
class SlashCommandTest {
|
||||||
|
|
||||||
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
private static String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
||||||
GatewayDiscordClient client;
|
GatewayDiscordClient client;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
@ -41,8 +41,8 @@ public class SlashCommandTest {
|
|||||||
@Test
|
@Test
|
||||||
void testSlashCommand() {
|
void testSlashCommand() {
|
||||||
Marinara marinara = Marinara.load(new Discord4JWrapper(client));
|
Marinara marinara = Marinara.load(new Discord4JWrapper(client));
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
marinara.getRegistry().registerCommands();
|
marinara.registerCommands();
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ public class SlashCommandTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(client);
|
LibraryWrapper wrapper = new Discord4JWrapper(client);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
|
|
||||||
wrapper.handleInteraction(eventMock);
|
wrapper.handleInteraction(eventMock);
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package net.tomatentum.marinara.test.discord4j;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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.ChatInputAutoCompleteEvent;
|
||||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||||
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
||||||
@ -23,9 +21,8 @@ public class TestAutocomplete implements InteractionHandler {
|
|||||||
autocompletes = @AutoComplete("testAuto")
|
autocompletes = @AutoComplete("testAuto")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@AutoComplete("testAuto")
|
|
||||||
public void exec(ChatInputInteractionEvent context) {
|
public void exec(ChatInputInteractionEvent context) {
|
||||||
|
// Not executed just there for autocomplete to work
|
||||||
}
|
}
|
||||||
|
|
||||||
@AutoComplete("testAuto")
|
@AutoComplete("testAuto")
|
||||||
|
@ -26,7 +26,7 @@ public class TestCommand implements InteractionHandler {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
public void exec(ChatInputInteractionEvent event, String test) {
|
public void exec(ChatInputInteractionEvent event, String test) {
|
||||||
assertEquals(test, "test");
|
assertEquals("test", test);
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,10 @@ import net.tomatentum.marinara.Marinara;
|
|||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||||
|
|
||||||
public class AutoCompleteTest {
|
class AutoCompleteTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAutocomplete() {
|
void testAutocomplete() {
|
||||||
|
|
||||||
SlashCommandInteractionOption optionMock = mock();
|
SlashCommandInteractionOption optionMock = mock();
|
||||||
AutocompleteInteraction autocompleteInteractionMock = mock();
|
AutocompleteInteraction autocompleteInteractionMock = mock();
|
||||||
@ -33,7 +33,7 @@ public class AutoCompleteTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestAutocomplete());
|
marinara.getInteractionContainer().addAllMethods(new TestAutocomplete());
|
||||||
wrapper.handleInteraction(autocompleteInteractionMock);
|
wrapper.handleInteraction(autocompleteInteractionMock);
|
||||||
verify(autocompleteInteractionMock).respondWithChoices(any());
|
verify(autocompleteInteractionMock).respondWithChoices(any());
|
||||||
}
|
}
|
||||||
|
@ -12,13 +12,13 @@ import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
|||||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class ButtonTest {
|
class ButtonTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testButtonExecution() {
|
void testButtonExecution() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
||||||
assertTrue(TestButton.didRun);
|
assertTrue(TestButton.didRun);
|
||||||
}
|
}
|
||||||
|
@ -19,25 +19,25 @@ import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
|||||||
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
|
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class InteractionCheckTest {
|
class InteractionCheckTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInteractionCheck() {
|
void testInteractionCheck() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null);
|
LibraryWrapper wrapper = new JavacordWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
|
marinara.getCheckContainer().addAllMethods(new TestInteractionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
||||||
assertTrue(TestInteractionCheck.preExecuted);
|
assertTrue(TestInteractionCheck.preExecuted);
|
||||||
assertTrue(TestInteractionCheck.postExecuted);
|
assertTrue(TestInteractionCheck.postExecuted);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPermissionCheck() {
|
void testPermissionCheck() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null);
|
LibraryWrapper wrapper = new JavacordWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new PermissionCheck());
|
marinara.getCheckContainer().addAllMethods(new PermissionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
|
|
||||||
Server serverMock = mock();
|
Server serverMock = mock();
|
||||||
ButtonInteraction buttonInteractionMock = CommonMocks.getButtonInteractionMock("permissionCheck", serverMock);
|
ButtonInteraction buttonInteractionMock = CommonMocks.getButtonInteractionMock("permissionCheck", serverMock);
|
||||||
|
@ -20,7 +20,7 @@ import net.tomatentum.marinara.Marinara;
|
|||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class SlashCommandTest {
|
class SlashCommandTest {
|
||||||
|
|
||||||
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
||||||
DiscordApi api;
|
DiscordApi api;
|
||||||
@ -41,8 +41,8 @@ public class SlashCommandTest {
|
|||||||
@Test
|
@Test
|
||||||
void testSlashCommand() {
|
void testSlashCommand() {
|
||||||
Marinara marinara = Marinara.load(new JavacordWrapper(api));
|
Marinara marinara = Marinara.load(new JavacordWrapper(api));
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
marinara.getRegistry().registerCommands();
|
marinara.registerCommands();
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ public class SlashCommandTest {
|
|||||||
void testSlashCommandExecution() {
|
void testSlashCommandExecution() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(api);
|
LibraryWrapper wrapper = new JavacordWrapper(api);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
|
|
||||||
SlashCommandInteractionOption optionMock = mock();
|
SlashCommandInteractionOption optionMock = mock();
|
||||||
SlashCommandInteraction interactionMock = mock();
|
SlashCommandInteraction interactionMock = mock();
|
||||||
|
@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
|
|
||||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||||
|
|
||||||
@ -24,15 +23,14 @@ public class TestAutocomplete implements InteractionHandler {
|
|||||||
autocompletes = @AutoComplete("testAuto")
|
autocompletes = @AutoComplete("testAuto")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@AutoComplete("testAuto")
|
|
||||||
public void exec(SlashCommandInteraction context) {
|
public void exec(SlashCommandInteraction context) {
|
||||||
|
//only here for command definition
|
||||||
}
|
}
|
||||||
|
|
||||||
@AutoComplete("testAuto")
|
@AutoComplete("testAuto")
|
||||||
public void autocomplete(AutocompleteInteraction context, String value) {
|
public void autocomplete(AutocompleteInteraction context, String value) {
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
assertEquals(value, "test");
|
assertEquals("test", value);
|
||||||
context.respondWithChoices(Collections.emptyList());
|
context.respondWithChoices(Collections.emptyList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@ import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck.HasPermis
|
|||||||
|
|
||||||
public class TestButton implements InteractionHandler {
|
public class TestButton implements InteractionHandler {
|
||||||
|
|
||||||
|
|
||||||
public static boolean didRun = false;
|
public static boolean didRun = false;
|
||||||
|
|
||||||
@Button("test")
|
@Button("test")
|
||||||
@TestCheck
|
@TestCheck
|
||||||
public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) {
|
public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) {
|
||||||
|
@ -11,6 +11,7 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptio
|
|||||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||||
|
|
||||||
public class TestCommand implements InteractionHandler {
|
public class TestCommand implements InteractionHandler {
|
||||||
|
|
||||||
@SlashCommand(
|
@SlashCommand(
|
||||||
name = "test",
|
name = "test",
|
||||||
description = "testingen",
|
description = "testingen",
|
||||||
@ -27,7 +28,8 @@ public class TestCommand implements InteractionHandler {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
public void exec(SlashCommandInteraction interaction, String test) {
|
public void exec(SlashCommandInteraction interaction, String test) {
|
||||||
assertEquals(test, "test");
|
assertEquals("test", test);
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user