Compare commits
5 Commits
dev
...
8a3cde52fd
Author | SHA1 | Date | |
---|---|---|---|
8a3cde52fd
|
|||
8495659364
|
|||
0973016a74
|
|||
0590789359
|
|||
2647a1f0b4
|
@@ -2,6 +2,8 @@ package net.tomatentum.marinara;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactory;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactoryImpl;
|
||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
@@ -15,26 +17,32 @@ public class Marinara {
|
||||
return new Marinara(wrapper);
|
||||
}
|
||||
|
||||
private LibraryWrapper wrapper;
|
||||
private ReflectedMethodFactory reflectedMethodFactory;
|
||||
private InteractionRegistry registry;
|
||||
private InteractionCheckRegistry checkRegistry;
|
||||
private LibraryWrapper wrapper;
|
||||
|
||||
private Marinara(LibraryWrapper wrapper) {
|
||||
this.wrapper = wrapper;
|
||||
this.reflectedMethodFactory = new ReflectedMethodFactoryImpl(this);
|
||||
this.registry = new InteractionRegistry(this);
|
||||
this.checkRegistry = new InteractionCheckRegistry();
|
||||
logger.info("Marinara loaded successfully!");
|
||||
}
|
||||
|
||||
public LibraryWrapper getWrapper() {
|
||||
return this.wrapper;
|
||||
}
|
||||
|
||||
public InteractionRegistry getRegistry() {
|
||||
return registry;
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
public InteractionCheckRegistry getCheckRegistry() {
|
||||
return checkRegistry;
|
||||
return this.checkRegistry;
|
||||
}
|
||||
|
||||
public LibraryWrapper getWrapper() {
|
||||
return wrapper;
|
||||
public ReflectedMethodFactory getReflectedMethodFactory() {
|
||||
return this.reflectedMethodFactory;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,63 @@
|
||||
package net.tomatentum.marinara.interaction.components.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.ButtonParser;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class ButtonInteractionMethod extends InteractionMethod {
|
||||
|
||||
private String customId;
|
||||
|
||||
private ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
Class<?> type = method().getParameterTypes()[index+1];
|
||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier identifier() {
|
||||
return InteractionIdentifier.builder()
|
||||
.name(customId)
|
||||
.description("Button")
|
||||
.type(InteractionType.BUTTON)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!method.isAnnotationPresent(Button.class) ||
|
||||
!(containingObject instanceof InteractionHandler)
|
||||
)
|
||||
return null;
|
||||
|
||||
return new ButtonInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
|
||||
ButtonInteractionMethod imethod = (ButtonInteractionMethod) method;
|
||||
parser.add(
|
||||
new ButtonParser(method.method(), x -> imethod.customId = x)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -80,7 +80,7 @@ public class InteractionIdentifier {
|
||||
return false;
|
||||
if (!name().equals(ident.name()))
|
||||
return false;
|
||||
return Objects.equals(ident, obj);
|
||||
return Objects.equals(parent(), ident.parent());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,31 +1,27 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
|
||||
private InteractionIdentifier interactionIdentifier;
|
||||
|
||||
public AutoCompleteInteractionMethod(Method method,
|
||||
private AutoCompleteInteractionMethod(Method method,
|
||||
InteractionHandler handler,
|
||||
Marinara marinara
|
||||
) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
return new AnnotationParser[] {
|
||||
new SlashCommandParser(method, true, (x) -> { this.interactionIdentifier = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
Class<?> type = method().getParameterTypes()[index+1];
|
||||
@@ -41,4 +37,28 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
return interactionIdentifier;
|
||||
}
|
||||
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!method.isAnnotationPresent(AutoComplete.class) ||
|
||||
!(containingObject instanceof InteractionHandler)
|
||||
)
|
||||
return null;
|
||||
|
||||
return new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
|
||||
AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method;
|
||||
parser.add(
|
||||
new SlashCommandParser(method.method(), true, x -> imethod.interactionIdentifier = x)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,42 +0,0 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.ButtonParser;
|
||||
|
||||
public class ButtonInteractionMethod extends InteractionMethod {
|
||||
|
||||
private String customId;
|
||||
|
||||
ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
return new AnnotationParser[] {
|
||||
new ButtonParser(method, (x) -> { this.customId = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
Class<?> type = method().getParameterTypes()[index+1];
|
||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier identifier() {
|
||||
return InteractionIdentifier.builder()
|
||||
.name(customId)
|
||||
.description("Button")
|
||||
.type(InteractionType.BUTTON)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
@@ -1,105 +1,66 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactory;
|
||||
|
||||
public abstract class InteractionMethod {
|
||||
public abstract class InteractionMethod extends ReflectedMethod {
|
||||
|
||||
public static InteractionMethod create(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
if (method.isAnnotationPresent(AutoComplete.class))
|
||||
return new AutoCompleteInteractionMethod(method, handler, marinara);
|
||||
if (method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class))
|
||||
return new SlashCommandInteractionMethod(method, handler, marinara);
|
||||
if (method.isAnnotationPresent(Button.class))
|
||||
return new ButtonInteractionMethod(method, handler, marinara);
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Method method;
|
||||
protected InteractionHandler handler;
|
||||
protected Marinara marinara;
|
||||
protected List<AnnotationParser> parsers;
|
||||
protected List<AppliedCheck> appliedChecks;
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
protected InteractionMethod(
|
||||
Method method,
|
||||
InteractionHandler handler,
|
||||
Marinara marinara
|
||||
) {
|
||||
if (!Arrays.asList(handler.getClass().getMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
|
||||
this.method = method;
|
||||
this.handler = handler;
|
||||
super(method, handler);
|
||||
this.marinara = marinara;
|
||||
this.parsers = new ArrayList<>(Arrays.asList(parsers()));
|
||||
this.appliedChecks = new ArrayList<>();
|
||||
|
||||
parsers.add(new InteractionCheckParser(method, appliedChecks::add, marinara.getCheckRegistry()));
|
||||
|
||||
parsers.stream().forEach(AnnotationParser::parse);
|
||||
}
|
||||
|
||||
public void run(Object context) {
|
||||
@Override
|
||||
public Object run(Object context) {
|
||||
Object result = null;
|
||||
if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0)
|
||||
return;
|
||||
return null;
|
||||
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
method.invoke(handler, getParameters(context));
|
||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
logger.error("InteractionMethod failed to run", ex);
|
||||
}
|
||||
result = super.run(context);
|
||||
|
||||
this.appliedChecks.forEach(x -> x.post(context));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public abstract AnnotationParser[] parsers();
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public abstract InteractionIdentifier identifier();
|
||||
|
||||
public Method method() {
|
||||
return method;
|
||||
public Marinara marinara() {
|
||||
return this.marinara;
|
||||
}
|
||||
|
||||
private Object[] getParameters(Object context) {
|
||||
int parameterCount = method.getParameterCount();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
Object parameter;
|
||||
if (i == 0) {
|
||||
parameter = context;
|
||||
}else
|
||||
parameter = getParameter(context, i-1);
|
||||
|
||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
||||
parameters.add(parameter);
|
||||
public List<AppliedCheck> appliedChecks() {
|
||||
return this.appliedChecks;
|
||||
}
|
||||
return parameters.toArray();
|
||||
|
||||
public static abstract class Factory implements ReflectedMethodFactory.Factory {
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
InteractionMethod imethod = (InteractionMethod) method;
|
||||
parser.add(
|
||||
new InteractionCheckParser(method.method(), imethod.appliedChecks::add, imethod.marinara().getCheckRegistry())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,29 +1,26 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
|
||||
private SlashCommandIdentifier interactionIdentifier;
|
||||
|
||||
SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
private SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
return new AnnotationParser[] {
|
||||
new SlashCommandParser(method, false, (x) -> { this.interactionIdentifier = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
return marinara.getWrapper().getContextObjectProvider().convertCommandOption(context, interactionIdentifier.options()[index].name());
|
||||
@@ -34,4 +31,29 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
return interactionIdentifier;
|
||||
}
|
||||
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
method.isAnnotationPresent(SubCommand.class)) ||
|
||||
!(containingObject instanceof InteractionHandler)
|
||||
)
|
||||
return null;
|
||||
|
||||
return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
|
||||
SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method;
|
||||
parser.add(
|
||||
new SlashCommandParser(method.method(), false, x -> imethod.interactionIdentifier = x)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,66 @@
|
||||
package net.tomatentum.marinara.reflection;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
|
||||
public abstract class ReflectedMethod {
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
private Method method;
|
||||
private Object containingObject;
|
||||
|
||||
public ReflectedMethod(Method method, Object containingObject) {
|
||||
if (!Arrays.asList(containingObject.getClass().getMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
this.method = method;
|
||||
this.containingObject = containingObject;
|
||||
}
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public Object run(Object context) {
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
return method.invoke(containingObject, getParameters(context));
|
||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
logger.error("ReflectedMethod failed to run", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Method method() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
public Object containingObject() {
|
||||
return this.containingObject;
|
||||
}
|
||||
|
||||
private Object[] getParameters(Object context) {
|
||||
int parameterCount = method.getParameterCount();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
Object parameter;
|
||||
if (i == 0) {
|
||||
parameter = context;
|
||||
}else
|
||||
parameter = getParameter(context, i-1);
|
||||
|
||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
||||
parameters.add(parameter);
|
||||
}
|
||||
return parameters.toArray();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package net.tomatentum.marinara.reflection;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
|
||||
public interface ReflectedMethodFactory {
|
||||
ReflectedMethod produce(Method method, Object containingClass);
|
||||
ReflectedMethodFactory addFactory(Factory factory);
|
||||
|
||||
public interface Factory {
|
||||
|
||||
ReflectedMethod produce(Marinara marinara, Method method, Object containingObject);
|
||||
void addParser(ReflectedMethod method, List<AnnotationParser> parser);
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
package net.tomatentum.marinara.reflection;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
|
||||
public class ReflectedMethodFactoryImpl implements ReflectedMethodFactory {
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
private Marinara marinara;
|
||||
private List<Factory> factories;
|
||||
|
||||
public ReflectedMethodFactoryImpl(Marinara marinara) {
|
||||
this(marinara, new ArrayList<>());
|
||||
}
|
||||
|
||||
public ReflectedMethodFactoryImpl(Marinara marinara, List<Factory> factories) {
|
||||
this.marinara = marinara;
|
||||
this.factories = factories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Method method, Object containingClass) {
|
||||
Optional<ReflectedMethod> imethod = this.factories.stream()
|
||||
.map(f -> factoryProduce(f, method, containingClass))
|
||||
.filter(Objects::nonNull)
|
||||
.findFirst();
|
||||
|
||||
if (imethod.isEmpty()) {
|
||||
logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method));
|
||||
return null;
|
||||
}
|
||||
|
||||
return imethod.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReflectedMethodFactory addFactory(Factory factory) {
|
||||
this.factories.add(factory);
|
||||
return this;
|
||||
}
|
||||
|
||||
private ReflectedMethod factoryProduce(Factory factory, Method method, Object containingClass) {
|
||||
List<AnnotationParser> parser = new ArrayList<>();
|
||||
ReflectedMethod m = factory.produce(this.marinara, method, containingClass);
|
||||
if (m != null) {
|
||||
factory.addParser(m, parser);
|
||||
parser.forEach(AnnotationParser::parse);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package net.tomatentum.marinara.registry;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -11,6 +12,17 @@ import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
|
||||
public class InteractionEntry {
|
||||
|
||||
public static InteractionEntry findEntry(Set<InteractionEntry> entries, InteractionIdentifier identifier) {
|
||||
Optional<InteractionEntry> oentry = entries.stream()
|
||||
.filter(i -> i.identifier().equals(identifier))
|
||||
.findFirst();
|
||||
|
||||
InteractionEntry entry = oentry.orElse(new InteractionEntry(identifier));
|
||||
if (oentry.isEmpty()) entries.add(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
private InteractionIdentifier identifier;
|
||||
private Set<InteractionMethod> methods;
|
||||
|
||||
@@ -22,11 +34,14 @@ public class InteractionEntry {
|
||||
}
|
||||
|
||||
public InteractionEntry addMethod(InteractionMethod method) {
|
||||
if (!method.identifier().equals(this.identifier))
|
||||
throw new IllegalArgumentException("Method's identifier did not match the entry's identifier");
|
||||
InteractionIdentifier identifier = method.identifier();
|
||||
|
||||
if (!this.identifier().equals(identifier))
|
||||
throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier");
|
||||
|
||||
this.methods.add(method);
|
||||
InteractionIdentifier.tryAddDescriptions(identifier, method.identifier());
|
||||
InteractionIdentifier.tryAddDescriptions(identifier, identifier);
|
||||
logger.debug("Added method {} to entry {}", method.method().getName(), this.identifier);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@@ -4,7 +4,6 @@ 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;
|
||||
@@ -13,12 +12,16 @@ import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||
import net.tomatentum.marinara.interaction.components.methods.ButtonInteractionMethod;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ObjectAggregator;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.interaction.methods.AutoCompleteInteractionMethod;
|
||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod;
|
||||
import net.tomatentum.marinara.reflection.ReflectedMethod;
|
||||
|
||||
public class InteractionRegistry {
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
@@ -31,21 +34,19 @@ public class InteractionRegistry {
|
||||
this.marinara = marinara;
|
||||
this.identifierProvider = marinara.getWrapper().createIdentifierProvider();
|
||||
marinara.getWrapper().subscribeInteractions(this::handle);
|
||||
marinara.getReflectedMethodFactory()
|
||||
.addFactory(new AutoCompleteInteractionMethod.Factory())
|
||||
.addFactory(new SlashCommandInteractionMethod.Factory())
|
||||
.addFactory(new ButtonInteractionMethod.Factory());
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: Maybe relocate InteractionEntry checking to another class with description merging.
|
||||
*/
|
||||
public void addInteractions(InteractionHandler interactionHandler) {
|
||||
for (Method method : interactionHandler.getClass().getMethods()) {
|
||||
InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara);
|
||||
if (iMethod != null) {
|
||||
Optional<InteractionEntry> oentry = this.interactions.stream()
|
||||
.filter(i -> i.identifier().equals(iMethod.identifier()))
|
||||
.findFirst();
|
||||
|
||||
InteractionEntry entry = oentry.orElse(new InteractionEntry(iMethod.identifier())).addMethod(iMethod);
|
||||
if (oentry.isEmpty()) this.interactions.add(entry);
|
||||
for (Method method : interactionHandler.getClass().getDeclaredMethods()) {
|
||||
ReflectedMethod rMethod = this.marinara.getReflectedMethodFactory().produce(method, interactionHandler);
|
||||
if (rMethod != null && rMethod instanceof InteractionMethod) {
|
||||
InteractionMethod iMethod = (InteractionMethod) rMethod;
|
||||
InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod);
|
||||
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user