refactor(core): restructure MethodProcessors
This commit is contained in:
parent
60ead419e2
commit
070319853a
@ -5,13 +5,14 @@ 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 {
|
||||||
@ -26,16 +27,17 @@ public class Marinara {
|
|||||||
private ReflectedMethodFactory reflectedMethodFactory;
|
private ReflectedMethodFactory reflectedMethodFactory;
|
||||||
private InteractionRegistry registry;
|
private InteractionRegistry registry;
|
||||||
private InteractionCheckRegistry checkRegistry;
|
private InteractionCheckRegistry checkRegistry;
|
||||||
private InteractionExecutor interactionExecutor;
|
private MethodExecutor 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();
|
||||||
this.interactionExecutor = new ProcessorInteractionExecutor(wrapper.createIdentifierProvider(), this)
|
IdentifierProvider provider = wrapper.createIdentifierProvider();
|
||||||
.addProcessor(new DirectInteractionProcessor(InteractionType.COMMAND, InteractionType.BUTTON))
|
this.interactionExecutor = (MethodExecutor) new ProcessorMethodExecutor()
|
||||||
.addProcessor(new AutocompleteInteractionProcessor());
|
.addProcessor(new DirectInteractionProcessor(getRegistry(), provider, InteractionType.COMMAND, InteractionType.BUTTON))
|
||||||
|
.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!");
|
||||||
}
|
}
|
||||||
@ -56,7 +58,7 @@ public class Marinara {
|
|||||||
return this.reflectedMethodFactory;
|
return this.reflectedMethodFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InteractionExecutor getInteractionExecutor() {
|
public MethodExecutor getInteractionExecutor() {
|
||||||
return interactionExecutor;
|
return interactionExecutor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,24 +4,31 @@ 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 implements InteractionProcessor {
|
public class AutocompleteInteractionProcessor extends InteractionMethodProcessor {
|
||||||
|
|
||||||
|
private Marinara marinara;
|
||||||
|
|
||||||
|
public AutocompleteInteractionProcessor(Marinara marinara, IdentifierProvider provider) {
|
||||||
|
super(provider, Set.of(InteractionType.AUTOCOMPLETE));
|
||||||
|
this.marinara = marinara;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(Object context, InteractionIdentifier identifier, Marinara marinara) {
|
public void processInteraction(Object context, InteractionIdentifier identifier) {
|
||||||
if (!identifier.type().equals(InteractionType.AUTOCOMPLETE))
|
Optional<InteractionEntry> entry = this.marinara.getRegistry().findFor(convertToCommandIdentifier(identifier));
|
||||||
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(marinara.getWrapper().getContextObjectProvider()
|
List<String> autocompleteRefs = Arrays.asList(this.marinara.getWrapper().getContextObjectProvider()
|
||||||
.getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options()));
|
.getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options()));
|
||||||
List<Object> results = marinara.getRegistry().interactions().stream()
|
List<Object> results = this.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,28 +2,23 @@ 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.util.LoggerUtil;
|
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
public class DirectInteractionProcessor implements InteractionProcessor {
|
public class DirectInteractionProcessor extends InteractionMethodProcessor {
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
private InteractionRegistry registry;
|
||||||
private Set<InteractionType> types;
|
|
||||||
|
|
||||||
public DirectInteractionProcessor(InteractionType... types) {
|
public DirectInteractionProcessor(InteractionRegistry registry, IdentifierProvider provider, InteractionType... types) {
|
||||||
this.types = Set.of(types);
|
super(provider, Set.of(types));
|
||||||
|
this.registry = registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(Object context, InteractionIdentifier identifier, Marinara marinara) {
|
protected void processInteraction(Object context, InteractionIdentifier identifier) {
|
||||||
if (!types.contains(identifier.type()))
|
this.registry.interactions().stream()
|
||||||
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));
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
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);
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,7 @@
|
|||||||
|
package net.tomatentum.marinara.reflection;
|
||||||
|
|
||||||
|
public interface MethodExecutor {
|
||||||
|
|
||||||
|
void handle(Object context);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package net.tomatentum.marinara.reflection;
|
||||||
|
|
||||||
|
public interface MethodProcessor {
|
||||||
|
|
||||||
|
void process(Object context);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package net.tomatentum.marinara.reflection;
|
||||||
|
|
||||||
|
public interface ProcessorContainer {
|
||||||
|
|
||||||
|
ProcessorContainer addProcessor(MethodProcessor processor);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
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,7 +0,0 @@
|
|||||||
package net.tomatentum.marinara.registry;
|
|
||||||
|
|
||||||
public interface InteractionExecutor {
|
|
||||||
|
|
||||||
void handle(Object context);
|
|
||||||
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
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