add IdentifierProvider and wrapper implementations
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package net.tomatentum.marinara.interaction.ident;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||
|
||||
public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
@@ -10,10 +11,10 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
InteractionIdentifier parent,
|
||||
String name,
|
||||
String description,
|
||||
InteractionType type,
|
||||
SlashCommandOption[] options,
|
||||
boolean isAutocomplete,
|
||||
long[] serverIds) {
|
||||
super(parent, name, description, options, isAutocomplete);
|
||||
super(parent, name, description, type, options);
|
||||
this.serverIds = serverIds;
|
||||
}
|
||||
|
||||
@@ -26,7 +27,6 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
private String name;
|
||||
private String description;
|
||||
private SlashCommandOption[] options;
|
||||
private boolean isAutocomplete = false;
|
||||
private long[] serverIds;
|
||||
|
||||
|
||||
@@ -66,15 +66,6 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean autocomplete() {
|
||||
return this.isAutocomplete;
|
||||
}
|
||||
|
||||
public Builder autocomplete(boolean isAutocomplete) {
|
||||
this.isAutocomplete = isAutocomplete;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long[] serverIds() {
|
||||
return this.serverIds;
|
||||
}
|
||||
@@ -84,8 +75,14 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||
return this;
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier build() {
|
||||
return new RootCommandIdentifier(parent, name, description, options, isAutocomplete, serverIds);
|
||||
public SlashCommandIdentifier build(boolean autocomplete) {
|
||||
return new RootCommandIdentifier(
|
||||
parent,
|
||||
name,
|
||||
description,
|
||||
autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
|
||||
options,
|
||||
serverIds);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -11,10 +11,10 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
|
||||
InteractionIdentifier parent,
|
||||
String name,
|
||||
String description,
|
||||
SlashCommandOption[] options,
|
||||
boolean isAutocomplete
|
||||
InteractionType type,
|
||||
SlashCommandOption[] options
|
||||
) {
|
||||
super(parent, name, description, isAutocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND);
|
||||
super(parent, name, description, type);
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
|
||||
private String name;
|
||||
private String description;
|
||||
private SlashCommandOption[] options;
|
||||
private boolean isAutocomplete = false;
|
||||
|
||||
public InteractionIdentifier parent() {
|
||||
return parent;
|
||||
@@ -65,17 +64,13 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean autocomplete() {
|
||||
return this.isAutocomplete;
|
||||
}
|
||||
|
||||
public Builder autocomplete(boolean isAutocomplete) {
|
||||
this.isAutocomplete = isAutocomplete;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier build() {
|
||||
return new SlashCommandIdentifier(parent, name, description, options, isAutocomplete);
|
||||
public SlashCommandIdentifier build(boolean autocomplete) {
|
||||
return new SlashCommandIdentifier(
|
||||
parent,
|
||||
name,
|
||||
description,
|
||||
autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
|
||||
options);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -37,9 +37,8 @@ public class SlashCommandParser implements AnnotationParser {
|
||||
.name(cmd.name())
|
||||
.description(cmd.description())
|
||||
.options(cmd.options())
|
||||
.autocomplete(isAutoComplete)
|
||||
.serverIds(cmd.serverIds())
|
||||
.build();
|
||||
.build(isAutoComplete);
|
||||
|
||||
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
||||
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
||||
@@ -57,8 +56,7 @@ public class SlashCommandParser implements AnnotationParser {
|
||||
.name(subCmd.name())
|
||||
.description(subCmd.description())
|
||||
.options(subCmd.options())
|
||||
.autocomplete(isAutoComplete)
|
||||
.build();
|
||||
.build(isAutoComplete);
|
||||
}
|
||||
|
||||
logger.trace("Parsed using SlashCommandParser for method {} with the result:\n{}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());
|
||||
|
@@ -16,17 +16,19 @@ import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
|
||||
public class InteractionRegistry {
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
private Set<InteractionEntry> interactions;
|
||||
private Marinara marinara;
|
||||
private IdentifierProvider identifierProvider;
|
||||
|
||||
public InteractionRegistry(Marinara marinara) {
|
||||
this.interactions = new HashSet<>();
|
||||
this.marinara = marinara;
|
||||
this.identifierProvider = marinara.getWrapper().createIdentifierProvider();
|
||||
marinara.getWrapper().subscribeInteractions(this::handle);
|
||||
}
|
||||
|
||||
@@ -74,9 +76,8 @@ public class InteractionRegistry {
|
||||
|
||||
public void handle(Object context) {
|
||||
logger.debug("Received {} interaction ", context);
|
||||
LibraryWrapper wrapper = marinara.getWrapper();
|
||||
interactions.forEach((e) -> {
|
||||
if (wrapper.getInteractionIdentifier(context).equals(e.identifier())) {
|
||||
if (this.identifierProvider.provide(context).equals(e.identifier())) {
|
||||
logger.info("Running {} interaction using {}\ncontext: {}", e.type(), e.toString(), context.toString());
|
||||
e.runAll(context);
|
||||
}
|
||||
|
@@ -0,0 +1,72 @@
|
||||
package net.tomatentum.marinara.wrapper;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import io.leangen.geantyref.GenericTypeReflector;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
|
||||
public class IdentifierProvider {
|
||||
|
||||
public static IdentifierProvider of(Converter<?>... converter) {
|
||||
return new IdentifierProvider(Arrays.asList(converter));
|
||||
}
|
||||
|
||||
private Map<Class<?>, Converter<?>> converter;
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
private IdentifierProvider(List<Converter<?>> converter) {
|
||||
this.converter = new HashMap<>();
|
||||
for (Converter<?> conv : converter) {
|
||||
if (conv.getClass().getName().contains("$$Lambda"))
|
||||
throw new IllegalArgumentException("Lambdas cannot be used for IdentifierConverter because of Type erasure.");
|
||||
Type type = GenericTypeReflector.getExactSuperType(conv.getClass(), Converter.class);
|
||||
Type parameterType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||
if (!(parameterType instanceof Class))
|
||||
throw new IllegalArgumentException("Only full Class types are supported by IdentiferConverters");
|
||||
this.converter.put((Class<?>) parameterType, conv);
|
||||
}
|
||||
}
|
||||
|
||||
public InteractionIdentifier provide(Object context) {
|
||||
Type type = ReflectionUtil.getMostSpecificClass(
|
||||
converter.keySet().stream().filter(x -> x.isAssignableFrom(context.getClass())).toArray(Class<?>[]::new),
|
||||
context.getClass());
|
||||
|
||||
if (type == null)
|
||||
logger.debug("No Identifier converter found for context {}", context.getClass().toString());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Converter<Object> conv = (Converter<Object>) converter.get(type);
|
||||
|
||||
return conv.convert(context);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Converter<T extends Object> {
|
||||
InteractionIdentifier convert(T context);
|
||||
}
|
||||
|
||||
public static class LambdaWrapper<T extends Object> implements Converter<T> {
|
||||
|
||||
private Converter<T> converter;
|
||||
|
||||
LambdaWrapper(Converter<T> converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier convert(T context) {
|
||||
return this.converter.convert(context);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -5,14 +5,13 @@ import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
|
||||
public abstract class LibraryWrapper {
|
||||
|
||||
private List<Consumer<Object>> interactionSubscriber;
|
||||
|
||||
protected LibraryWrapper() {
|
||||
interactionSubscriber = new ArrayList<>();
|
||||
this.interactionSubscriber = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void handleInteraction(Object context) {
|
||||
@@ -27,8 +26,8 @@ public abstract class LibraryWrapper {
|
||||
}
|
||||
|
||||
public abstract void registerSlashCommands(SlashCommandDefinition[] defs);
|
||||
public abstract InteractionIdentifier getInteractionIdentifier(Object context);
|
||||
|
||||
|
||||
public abstract IdentifierProvider createIdentifierProvider();
|
||||
public abstract ContextObjectProvider getContextObjectProvider();
|
||||
|
||||
}
|
Reference in New Issue
Block a user