add initial InteractionRegistry

This commit is contained in:
tueem 2024-10-14 00:53:59 +02:00
parent 7fb27795d9
commit 7901e8c380
Signed by: tueem
GPG Key ID: 65C8667EC17A88FB

@ -0,0 +1,33 @@
package net.tomatentum.marinara.registry;
import java.lang.reflect.Method;
import java.util.List;
import net.tomatentum.marinara.handler.InteractionHandler;
import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
import net.tomatentum.marinara.wrapper.LibraryWrapper;
public class InteractionRegistry {
private List<InteractionMethod> interactionMethods;
private LibraryWrapper wrapper;
public InteractionRegistry(LibraryWrapper wrapper) {
this.wrapper = wrapper;
wrapper.subscribeInteractions(this::handle);
}
public void registerInteractions(InteractionHandler interactionHandler) {
for (Method method : interactionHandler.getClass().getMethods()) {
interactionMethods.add(InteractionMethod.create(method, interactionHandler, wrapper));
}
}
public void handle(Object context) {
interactionMethods.forEach((m) -> {
InteractionType type = wrapper.getInteractionType(context.getClass());
if (m.getType().equals(type))
m.run(context);
});
}
}