replace ExecutableCommandDefinition with InteractionIdentifiers
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package net.tomatentum.marinara.registry;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.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 {
|
||||
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) {
|
||||
if (!method.identifier().equals(this.identifier))
|
||||
throw new IllegalArgumentException("Method's identifier did not match the entry's identifier");
|
||||
|
||||
this.methods.add(method);
|
||||
InteractionIdentifier.tryAddDescriptions(identifier, method.identifier());
|
||||
return this;
|
||||
}
|
||||
|
||||
public void runAll(Object context) {
|
||||
this.methods.stream().forEach(x -> {
|
||||
logger.debug("Running Method {} from {} with context {}", x.toString(), this.toString(), context.toString());
|
||||
x.run(context);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof InteractionEntry))
|
||||
return false;
|
||||
InteractionEntry other = (InteractionEntry) obj;
|
||||
return other.identifier().equals(identifier());
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
@@ -2,8 +2,10 @@ package net.tomatentum.marinara.registry;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -11,28 +13,36 @@ 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.commands.ExecutableSlashCommandDefinition;
|
||||
import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod;
|
||||
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.interaction.methods.InteractionMethod;
|
||||
|
||||
public class InteractionRegistry {
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
private List<InteractionMethod> interactionMethods;
|
||||
private Set<InteractionEntry> interactions;
|
||||
private Marinara marinara;
|
||||
|
||||
public InteractionRegistry(Marinara marinara) {
|
||||
this.interactionMethods = new ArrayList<>();
|
||||
this.interactions = new HashSet<>();
|
||||
this.marinara = marinara;
|
||||
marinara.getWrapper().subscribeInteractions(this::handle);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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) {
|
||||
this.interactionMethods.add(iMethod);
|
||||
logger.debug("Added {} method from {}", iMethod.getMethod().getName(), interactionHandler.getClass().getSimpleName());
|
||||
Optional<InteractionEntry> entry = this.interactions.stream().filter(iMethod::equals).findFirst();
|
||||
if (entry.isEmpty()) {
|
||||
interactions.add(new InteractionEntry(iMethod.identifier()).addMethod(iMethod));
|
||||
}else
|
||||
entry.get().addMethod(iMethod);
|
||||
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
logger.info("Added all Interactions from {}", interactionHandler.getClass().getSimpleName());
|
||||
@@ -40,25 +50,22 @@ public class InteractionRegistry {
|
||||
|
||||
public void registerCommands() {
|
||||
List<SlashCommandDefinition> defs = new ArrayList<>();
|
||||
List<ExecutableSlashCommandDefinition> execDefs = interactionMethods.stream()
|
||||
.filter((x) -> x.getClass().isAssignableFrom(SlashCommandInteractionMethod.class))
|
||||
.map((x) -> ((SlashCommandInteractionMethod)x).getCommandDefinition())
|
||||
List<SlashCommandIdentifier> slashIdentifiers = interactions.stream()
|
||||
.filter((x) -> x.type().equals(InteractionType.COMMAND))
|
||||
.map((x) -> (SlashCommandIdentifier)x.identifier())
|
||||
.toList();
|
||||
|
||||
execDefs.forEach((def) -> {
|
||||
slashIdentifiers.forEach((ident) -> {
|
||||
Optional<SlashCommandDefinition> appDef = defs.stream()
|
||||
.filter((x) -> x.getSlashCommand().equals(def.applicationCommand()))
|
||||
.filter((x) -> x.rootIdentifier().equals(ident.rootNode()))
|
||||
.findFirst();
|
||||
if (appDef.isPresent())
|
||||
appDef.get().addExecutableCommand(def);
|
||||
else
|
||||
defs.add(new SlashCommandDefinition(def.applicationCommand()).addExecutableCommand(def));
|
||||
|
||||
logger.debug("Added Executable Command {}{}{} for registration",
|
||||
def.applicationCommand().name(),
|
||||
def.subCommandGroup() == null ? "" : "." + def.subCommandGroup().name(),
|
||||
def.subCommand() == null ? "" : "." + def.subCommand().name()
|
||||
);
|
||||
if (appDef.isPresent())
|
||||
appDef.get().addIdentifier(ident);
|
||||
else
|
||||
defs.add(
|
||||
new SlashCommandDefinition((RootCommandIdentifier) ident.rootNode())
|
||||
.addIdentifier(ident));
|
||||
});
|
||||
|
||||
marinara.getWrapper().registerSlashCommands(defs.toArray(SlashCommandDefinition[]::new));
|
||||
@@ -66,12 +73,12 @@ public class InteractionRegistry {
|
||||
}
|
||||
|
||||
public void handle(Object context) {
|
||||
InteractionType type = marinara.getWrapper().getInteractionType(context);
|
||||
logger.debug("Received {} interaction ", context);
|
||||
interactionMethods.forEach((m) -> {
|
||||
if (m.getType().equals(type) && m.canRun(context)) {
|
||||
logger.info("Running {} interaction using {}\ncontext: {}", type, m.getMethod().toString(), context.toString());
|
||||
m.run(context);
|
||||
LibraryWrapper wrapper = marinara.getWrapper();
|
||||
interactions.forEach((e) -> {
|
||||
if (wrapper.getInteractionIdentifier(context).equals(e.identifier())) {
|
||||
logger.info("Running {} interaction using {}\ncontext: {}", e.type(), e.toString(), context.toString());
|
||||
e.runAll(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user