- add prototype Interactioncheck impementation.
- refactor dependency injection to have all widely used dependencies in the Marinara class.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package net.tomatentum.marinara.registry;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||
|
||||
public class InteractionCheckRegistry {
|
||||
|
||||
private List<InteractionCheck<?>> checks;
|
||||
|
||||
public InteractionCheckRegistry() {
|
||||
this.checks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addCheck(InteractionCheck<?> check) {
|
||||
checks.add(check);
|
||||
}
|
||||
|
||||
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Class<? extends Annotation> annotation) {
|
||||
for (InteractionCheck<?> interactionCheck : checks) {
|
||||
TypeVariable<?> type = interactionCheck.getClass().getTypeParameters()[0];
|
||||
if (type.getClass().equals(annotation.getClass()))
|
||||
return Optional.of(interactionCheck);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
@@ -5,27 +5,27 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
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.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||
|
||||
public class InteractionRegistry {
|
||||
private List<InteractionMethod> interactionMethods;
|
||||
private LibraryWrapper wrapper;
|
||||
private Marinara marinara;
|
||||
|
||||
public InteractionRegistry(LibraryWrapper wrapper) {
|
||||
public InteractionRegistry(Marinara marinara) {
|
||||
this.interactionMethods = new ArrayList<>();
|
||||
this.wrapper = wrapper;
|
||||
wrapper.subscribeInteractions(this::handle);
|
||||
this.marinara = marinara;
|
||||
marinara.getWrapper().subscribeInteractions(this::handle);
|
||||
}
|
||||
|
||||
public void addInteractions(InteractionHandler interactionHandler) {
|
||||
for (Method method : interactionHandler.getClass().getMethods()) {
|
||||
InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, wrapper);
|
||||
InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara);
|
||||
if (iMethod != null)
|
||||
this.interactionMethods.add(iMethod);
|
||||
}
|
||||
@@ -48,12 +48,12 @@ public class InteractionRegistry {
|
||||
defs.add(new SlashCommandDefinition(def.applicationCommand()).addExecutableCommand(def));
|
||||
});
|
||||
|
||||
wrapper.registerSlashCommands(defs.toArray(new SlashCommandDefinition[0]));
|
||||
marinara.getWrapper().registerSlashCommands(defs.toArray(new SlashCommandDefinition[0]));
|
||||
}
|
||||
|
||||
public void handle(Object context) {
|
||||
interactionMethods.forEach((m) -> {
|
||||
InteractionType type = wrapper.getInteractionType(context.getClass());
|
||||
InteractionType type = marinara.getWrapper().getInteractionType(context.getClass());
|
||||
if (m.getType().equals(type))
|
||||
m.run(context);
|
||||
});
|
||||
|
Reference in New Issue
Block a user