From 7901e8c380720ff9e6a8a8e43a92bd77b25dfeec Mon Sep 17 00:00:00 2001 From: tueem Date: Mon, 14 Oct 2024 00:53:59 +0200 Subject: [PATCH] add initial InteractionRegistry --- .../registry/InteractionRegistry.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java diff --git a/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java new file mode 100644 index 0000000..59ed25c --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java @@ -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 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); + }); + } +}