From c363ab97442914c4f62061f57a700f459c863435 Mon Sep 17 00:00:00 2001 From: Tueem Date: Sun, 1 Dec 2024 13:06:42 +0100 Subject: [PATCH] add ability to return false to cancel execution in pre Checks and remove return type on post checks --- .../java/net/tomatentum/marinara/checks/AppliedCheck.java | 5 ++--- .../net/tomatentum/marinara/checks/InteractionCheck.java | 2 +- .../marinara/interaction/methods/InteractionMethod.java | 3 ++- .../net/tomatentum/marinara/test/TestInteractionCheck.java | 3 +-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java b/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java index 4f969f2..44e4c8a 100644 --- a/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java +++ b/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java @@ -24,7 +24,7 @@ public record AppliedCheck(InteractionCheck check, Annotation annotation) { } } - public boolean post(Object context) { + public void post(Object context) { Method[] methods = Arrays.stream(check.getClass().getMethods()) .filter(x -> x.getName().equals("postExec")) .filter(x -> !x.isBridge()) @@ -32,10 +32,9 @@ public record AppliedCheck(InteractionCheck check, Annotation annotation) { Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass()); method.setAccessible(true); try { - return (boolean) method.invoke(check, context, annotation); + method.invoke(check, context, annotation); } catch (IllegalAccessException | InvocationTargetException | SecurityException e) { e.printStackTrace(); - return false; } } diff --git a/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java b/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java index a7c3217..4b38aa4 100644 --- a/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java +++ b/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java @@ -5,6 +5,6 @@ import java.lang.annotation.Annotation; public interface InteractionCheck { public boolean preExec(Object context, A annotation); - public boolean postExec(Object context, A annotation); + public void postExec(Object context, A annotation); } diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java index aa06f0e..f9c5a1c 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java @@ -60,7 +60,8 @@ public abstract class InteractionMethod { public abstract InteractionType getType(); public void run(Object context) { - this.appliedChecks.forEach(x -> x.pre(context)); + if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0) + return; method.setAccessible(true); try { diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java index b52efea..ed1339b 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java @@ -28,11 +28,10 @@ public class TestInteractionCheck implements InteractionCheck