Compare commits
No commits in common. "070319853aff34a1d72286a6b842a7390755c95b" and "0b7b607a23e7b93320c40875dacf20a204a0259f" have entirely different histories.
070319853a
...
0b7b607a23
@ -5,14 +5,13 @@ import org.slf4j.Logger;
|
|||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
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.ReflectedMethodFactory;
|
||||||
import net.tomatentum.marinara.reflection.ReflectedMethodFactoryImpl;
|
import net.tomatentum.marinara.reflection.ReflectedMethodFactoryImpl;
|
||||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
||||||
|
import net.tomatentum.marinara.registry.InteractionExecutor;
|
||||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||||
|
import net.tomatentum.marinara.registry.ProcessorInteractionExecutor;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
public class Marinara {
|
public class Marinara {
|
||||||
@ -27,17 +26,16 @@ public class Marinara {
|
|||||||
private ReflectedMethodFactory reflectedMethodFactory;
|
private ReflectedMethodFactory reflectedMethodFactory;
|
||||||
private InteractionRegistry registry;
|
private InteractionRegistry registry;
|
||||||
private InteractionCheckRegistry checkRegistry;
|
private InteractionCheckRegistry checkRegistry;
|
||||||
private MethodExecutor interactionExecutor;
|
private InteractionExecutor interactionExecutor;
|
||||||
|
|
||||||
private Marinara(LibraryWrapper wrapper) {
|
private Marinara(LibraryWrapper wrapper) {
|
||||||
this.wrapper = wrapper;
|
this.wrapper = wrapper;
|
||||||
this.reflectedMethodFactory = new ReflectedMethodFactoryImpl(this);
|
this.reflectedMethodFactory = new ReflectedMethodFactoryImpl(this);
|
||||||
this.registry = new InteractionRegistry(this);
|
this.registry = new InteractionRegistry(this);
|
||||||
this.checkRegistry = new InteractionCheckRegistry();
|
this.checkRegistry = new InteractionCheckRegistry();
|
||||||
IdentifierProvider provider = wrapper.createIdentifierProvider();
|
this.interactionExecutor = new ProcessorInteractionExecutor(wrapper.createIdentifierProvider(), this)
|
||||||
this.interactionExecutor = (MethodExecutor) new ProcessorMethodExecutor()
|
.addProcessor(new DirectInteractionProcessor(InteractionType.COMMAND, InteractionType.BUTTON))
|
||||||
.addProcessor(new DirectInteractionProcessor(getRegistry(), provider, InteractionType.COMMAND, InteractionType.BUTTON))
|
.addProcessor(new AutocompleteInteractionProcessor());
|
||||||
.addProcessor(new AutocompleteInteractionProcessor(this, provider));
|
|
||||||
wrapper.subscribeInteractions(this.interactionExecutor::handle);
|
wrapper.subscribeInteractions(this.interactionExecutor::handle);
|
||||||
logger.info("Marinara loaded successfully!");
|
logger.info("Marinara loaded successfully!");
|
||||||
}
|
}
|
||||||
@ -58,7 +56,7 @@ public class Marinara {
|
|||||||
return this.reflectedMethodFactory;
|
return this.reflectedMethodFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodExecutor getInteractionExecutor() {
|
public InteractionExecutor getInteractionExecutor() {
|
||||||
return interactionExecutor;
|
return interactionExecutor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,9 +43,9 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
|||||||
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
||||||
ReflectedMethod rMethod = null;
|
ReflectedMethod rMethod = null;
|
||||||
if (method.isAnnotationPresent(Button.class) &&
|
if (method.isAnnotationPresent(Button.class) &&
|
||||||
(containingObject instanceof InteractionHandler iHandler)
|
(containingObject instanceof InteractionHandler)
|
||||||
)
|
)
|
||||||
rMethod = new ButtonInteractionMethod(method, iHandler, marinara);
|
rMethod = new ButtonInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||||
|
|
||||||
return Optional.ofNullable(rMethod);
|
return Optional.ofNullable(rMethod);
|
||||||
}
|
}
|
||||||
@ -54,8 +54,9 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
|||||||
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
public void addParser(ReflectedMethod method, List<AnnotationParser> parser) {
|
||||||
super.addParser(method, parser);
|
super.addParser(method, parser);
|
||||||
|
|
||||||
|
ButtonInteractionMethod imethod = (ButtonInteractionMethod) method;
|
||||||
parser.add(
|
parser.add(
|
||||||
new ButtonParser(method.method(), x -> ((ButtonInteractionMethod) method).customId = x)
|
new ButtonParser(method.method(), x -> imethod.customId = x)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,31 +4,24 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.marinara.Marinara;
|
||||||
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.registry.InteractionEntry;
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
|
||||||
|
|
||||||
public class AutocompleteInteractionProcessor extends InteractionMethodProcessor {
|
public class AutocompleteInteractionProcessor implements InteractionProcessor {
|
||||||
|
|
||||||
private Marinara marinara;
|
|
||||||
|
|
||||||
public AutocompleteInteractionProcessor(Marinara marinara, IdentifierProvider provider) {
|
|
||||||
super(provider, Set.of(InteractionType.AUTOCOMPLETE));
|
|
||||||
this.marinara = marinara;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processInteraction(Object context, InteractionIdentifier identifier) {
|
public void process(Object context, InteractionIdentifier identifier, Marinara marinara) {
|
||||||
Optional<InteractionEntry> entry = this.marinara.getRegistry().findFor(convertToCommandIdentifier(identifier));
|
if (!identifier.type().equals(InteractionType.AUTOCOMPLETE))
|
||||||
|
return;
|
||||||
|
Optional<InteractionEntry> entry = marinara.getRegistry().findFor(convertToCommandIdentifier(identifier));
|
||||||
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier sIdent) {
|
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier sIdent) {
|
||||||
List<String> autocompleteRefs = Arrays.asList(this.marinara.getWrapper().getContextObjectProvider()
|
List<String> autocompleteRefs = Arrays.asList(marinara.getWrapper().getContextObjectProvider()
|
||||||
.getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options()));
|
.getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options()));
|
||||||
List<Object> results = this.marinara.getRegistry().interactions().stream()
|
List<Object> results = marinara.getRegistry().interactions().stream()
|
||||||
.filter(e -> e.type().equals(InteractionType.AUTOCOMPLETE))
|
.filter(e -> e.type().equals(InteractionType.AUTOCOMPLETE))
|
||||||
.filter(e -> autocompleteRefs.contains(e.identifier().name()))
|
.filter(e -> autocompleteRefs.contains(e.identifier().name()))
|
||||||
.map(e -> e.runAll(context))
|
.map(e -> e.runAll(context))
|
||||||
|
@ -2,23 +2,28 @@ package net.tomatentum.marinara.interaction.processor;
|
|||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.Marinara;
|
||||||
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.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
|
||||||
|
|
||||||
public class DirectInteractionProcessor extends InteractionMethodProcessor {
|
public class DirectInteractionProcessor implements InteractionProcessor {
|
||||||
|
|
||||||
private InteractionRegistry registry;
|
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||||
|
private Set<InteractionType> types;
|
||||||
|
|
||||||
public DirectInteractionProcessor(InteractionRegistry registry, IdentifierProvider provider, InteractionType... types) {
|
public DirectInteractionProcessor(InteractionType... types) {
|
||||||
super(provider, Set.of(types));
|
this.types = Set.of(types);
|
||||||
this.registry = registry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void processInteraction(Object context, InteractionIdentifier identifier) {
|
public void process(Object context, InteractionIdentifier identifier, Marinara marinara) {
|
||||||
this.registry.interactions().stream()
|
if (!types.contains(identifier.type()))
|
||||||
|
return;
|
||||||
|
logger.debug("Processing {} : {} with context {}", identifier, identifier.type(), context);
|
||||||
|
marinara.getRegistry().interactions().stream()
|
||||||
.filter(e -> e.identifier().equals(identifier))
|
.filter(e -> e.identifier().equals(identifier))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.ifPresent(e -> e.runAll(context));
|
.ifPresent(e -> e.runAll(context));
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
package net.tomatentum.marinara.interaction.processor;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
|
||||||
import net.tomatentum.marinara.reflection.MethodProcessor;
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
|
||||||
|
|
||||||
public abstract class InteractionMethodProcessor implements MethodProcessor {
|
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
private IdentifierProvider provider;
|
|
||||||
private Set<InteractionType> types;
|
|
||||||
|
|
||||||
protected InteractionMethodProcessor(IdentifierProvider provider, Set<InteractionType> types) {
|
|
||||||
this.provider = provider;
|
|
||||||
this.types = types;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void process(Object context) {
|
|
||||||
InteractionIdentifier identifier = this.provider.provide(context);
|
|
||||||
if (!this.types.contains(identifier.type())) return;
|
|
||||||
logger.debug("Processing {} : {} with context {}", identifier, identifier.type(), context);
|
|
||||||
this.processInteraction(context, identifier);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract void processInteraction(Object context, InteractionIdentifier identifier);
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,10 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.processor;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.Marinara;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
|
||||||
|
public interface InteractionProcessor {
|
||||||
|
|
||||||
|
void process(Object context, InteractionIdentifier identifier, Marinara marinara);
|
||||||
|
|
||||||
|
}
|
@ -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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,7 @@
|
|||||||
|
package net.tomatentum.marinara.registry;
|
||||||
|
|
||||||
|
public interface InteractionExecutor {
|
||||||
|
|
||||||
|
void handle(Object context);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package net.tomatentum.marinara.registry;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.Marinara;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.processor.InteractionProcessor;
|
||||||
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class ProcessorInteractionExecutor implements InteractionExecutor {
|
||||||
|
|
||||||
|
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||||
|
|
||||||
|
private Set<InteractionProcessor> processors;
|
||||||
|
private IdentifierProvider identifierProvider;
|
||||||
|
private Marinara marinara;
|
||||||
|
|
||||||
|
public ProcessorInteractionExecutor(IdentifierProvider identifierProvider, Marinara marinara) {
|
||||||
|
this.processors = new HashSet<>();
|
||||||
|
this.identifierProvider = identifierProvider;
|
||||||
|
this.marinara = marinara;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProcessorInteractionExecutor addProcessor(InteractionProcessor processor) {
|
||||||
|
this.processors.add(processor);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(Object context) {
|
||||||
|
logger.debug("Received {} interaction ", context);
|
||||||
|
InteractionIdentifier identifier = this.identifierProvider.provide(context);
|
||||||
|
logger.debug("Processing {} : {} interaction ", identifier, identifier.type());
|
||||||
|
processors.forEach(x -> x.process(context, identifier, this.marinara));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user