Compare commits

..

No commits in common. "29bb7e667e22969e2dcb3db9b396af18ba2d6fe4" and "4332592dfa921c089da3b51e067f4ab888424242" have entirely different histories.

9 changed files with 7 additions and 62 deletions

@ -14,7 +14,7 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
.filter(x -> x.getName().equals("preExec")) .filter(x -> x.getName().equals("preExec"))
.filter(x -> !x.isBridge()) .filter(x -> !x.isBridge())
.toArray(s -> new Method[s]); .toArray(s -> new Method[s]);
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType()); Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass());
method.setAccessible(true); method.setAccessible(true);
try { try {
return (boolean) method.invoke(check, context, annotation); return (boolean) method.invoke(check, context, annotation);
@ -29,7 +29,7 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
.filter(x -> x.getName().equals("postExec")) .filter(x -> x.getName().equals("postExec"))
.filter(x -> !x.isBridge()) .filter(x -> !x.isBridge())
.toArray(s -> new Method[s]); .toArray(s -> new Method[s]);
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType()); Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass());
method.setAccessible(true); method.setAccessible(true);
try { try {
method.invoke(check, context, annotation); method.invoke(check, context, annotation);

@ -54,7 +54,7 @@ public class InteractionRegistry {
public void handle(Object context) { public void handle(Object context) {
interactionMethods.forEach((m) -> { interactionMethods.forEach((m) -> {
InteractionType type = marinara.getWrapper().getInteractionType(context.getClass()); InteractionType type = marinara.getWrapper().getInteractionType(context.getClass());
if (m.getType().equals(type) && m.canRun(context)) if (m.getType().equals(type))
m.run(context); m.run(context);
}); });
} }

@ -23,10 +23,6 @@ public final class ReflectionUtil {
} }
public static int getCastDepth(Class<?> child, Class<?> parent) { public static int getCastDepth(Class<?> child, Class<?> parent) {
if (parent.equals(Object.class))
return Integer.MAX_VALUE;
if (!parent.isAssignableFrom(child)) { if (!parent.isAssignableFrom(child)) {
throw new IllegalArgumentException("The specified class is not a child class of the specified parent."); throw new IllegalArgumentException("The specified class is not a child class of the specified parent.");
} }
@ -81,7 +77,7 @@ public final class ReflectionUtil {
Class<?> currMostSpecific = null; Class<?> currMostSpecific = null;
for (Class<?> currClass : classes) { for (Class<?> currClass : classes) {
int currCastDepth = getCastDepth(base, currClass); int currCastDepth = getCastDepth(base, currClass);
if (currCastDepth <= min) { if (currCastDepth < min) {
min = currCastDepth; min = currCastDepth;
currMostSpecific = currClass; currMostSpecific = currClass;
} }

@ -1,9 +1,5 @@
package net.tomatentum.marinara.wrapper.javacord.checks; package net.tomatentum.marinara.wrapper.javacord.checks;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Optional; import java.util.Optional;
import org.javacord.api.entity.permission.PermissionType; import org.javacord.api.entity.permission.PermissionType;
@ -14,8 +10,6 @@ import net.tomatentum.marinara.checks.InteractionCheck;
public class PermissionCheck implements InteractionCheck<PermissionCheck.HasPermission> { public class PermissionCheck implements InteractionCheck<PermissionCheck.HasPermission> {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface HasPermission { public static @interface HasPermission {
public PermissionType[] value(); public PermissionType[] value();
} }

@ -20,7 +20,7 @@ public class ButtonTest {
LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock()); //null okay as we don't use the discord API in this test. LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock()); //null okay as we don't use the discord API in this test.
Marinara marinara = Marinara.load(wrapper); Marinara marinara = Marinara.load(wrapper);
marinara.getRegistry().addInteractions(new TestButton()); marinara.getRegistry().addInteractions(new TestButton());
wrapper.handleInteraction(new ButtonInteractionMock("test")); wrapper.handleInteraction(new ButtonInteractionMock());
assertTrue(TestButton.didRun); assertTrue(TestButton.didRun);
} }

@ -1,9 +1,7 @@
package net.tomatentum.marinara.test; package net.tomatentum.marinara.test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.javacord.api.entity.permission.PermissionType;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.api.TestInstance.Lifecycle;
@ -11,10 +9,8 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
import net.tomatentum.marinara.Marinara; import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.test.mocks.ButtonInteractionMock; import net.tomatentum.marinara.test.mocks.ButtonInteractionMock;
import net.tomatentum.marinara.test.mocks.DiscordApiMock; import net.tomatentum.marinara.test.mocks.DiscordApiMock;
import net.tomatentum.marinara.test.mocks.ServerMock;
import net.tomatentum.marinara.wrapper.LibraryWrapper; import net.tomatentum.marinara.wrapper.LibraryWrapper;
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper; import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
@TestInstance(Lifecycle.PER_CLASS) @TestInstance(Lifecycle.PER_CLASS)
public class InteractionCheckTest { public class InteractionCheckTest {
@ -25,23 +21,9 @@ public class InteractionCheckTest {
Marinara marinara = Marinara.load(wrapper); Marinara marinara = Marinara.load(wrapper);
marinara.getCheckRegistry().addCheck(new TestInteractionCheck()); marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
marinara.getRegistry().addInteractions(new TestButton()); marinara.getRegistry().addInteractions(new TestButton());
wrapper.handleInteraction(new ButtonInteractionMock("test")); wrapper.handleInteraction(new ButtonInteractionMock());
assertTrue(TestInteractionCheck.preExecuted); assertTrue(TestInteractionCheck.preExecuted);
assertTrue(TestInteractionCheck.postExecuted); assertTrue(TestInteractionCheck.postExecuted);
} }
@Test
public void testPermissionCheck() {
LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock());
Marinara marinara = Marinara.load(wrapper);
marinara.getCheckRegistry().addCheck(new PermissionCheck());
marinara.getRegistry().addInteractions(new TestButton());
wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck"));
assertTrue(TestButton.didPermRun);
TestButton.didPermRun = false;
ServerMock.TESTPERMISSION = PermissionType.ATTACH_FILE;
wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck"));
assertFalse(TestButton.didPermRun);
}
} }

@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.javacord.api.entity.channel.TextChannel; import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.Message; import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.permission.PermissionType;
import org.javacord.api.entity.server.Server; import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User; import org.javacord.api.entity.user.User;
import org.javacord.api.interaction.ButtonInteraction; import org.javacord.api.interaction.ButtonInteraction;
@ -12,7 +11,6 @@ import org.javacord.api.interaction.ButtonInteraction;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.annotation.Button; import net.tomatentum.marinara.interaction.annotation.Button;
import net.tomatentum.marinara.test.TestInteractionCheck.TestCheck; import net.tomatentum.marinara.test.TestInteractionCheck.TestCheck;
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck.HasPermission;
public class TestButton implements InteractionHandler { public class TestButton implements InteractionHandler {
@ -30,13 +28,4 @@ public class TestButton implements InteractionHandler {
System.out.println("Success!"); System.out.println("Success!");
} }
public static boolean didPermRun = false;
@Button("permissionCheck")
@HasPermission({PermissionType.ADMINISTRATOR})
public void exec(ButtonInteraction interaction) {
didPermRun = true;
System.out.println("It worked!");
}
} }

@ -23,12 +23,6 @@ import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater;
public class ButtonInteractionMock implements ButtonInteraction { public class ButtonInteractionMock implements ButtonInteraction {
private String customId;
public ButtonInteractionMock(String customId) {
this.customId = customId;
}
@Override @Override
public Message getMessage() { public Message getMessage() {
return new MessageMock(); return new MessageMock();
@ -36,7 +30,7 @@ public class ButtonInteractionMock implements ButtonInteraction {
@Override @Override
public String getCustomId() { public String getCustomId() {
return this.customId; return "test";
} }
@Override @Override

@ -1,7 +1,5 @@
package net.tomatentum.marinara.test.mocks; package net.tomatentum.marinara.test.mocks;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.time.Instant; import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.EnumSet; import java.util.EnumSet;
@ -29,7 +27,6 @@ import org.javacord.api.entity.channel.ServerTextChannel;
import org.javacord.api.entity.channel.ServerThreadChannel; import org.javacord.api.entity.channel.ServerThreadChannel;
import org.javacord.api.entity.channel.ServerVoiceChannel; import org.javacord.api.entity.channel.ServerVoiceChannel;
import org.javacord.api.entity.emoji.KnownCustomEmoji; import org.javacord.api.entity.emoji.KnownCustomEmoji;
import org.javacord.api.entity.permission.PermissionType;
import org.javacord.api.entity.permission.Role; import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.server.ActiveThreads; import org.javacord.api.entity.server.ActiveThreads;
import org.javacord.api.entity.server.Ban; import org.javacord.api.entity.server.Ban;
@ -2262,12 +2259,5 @@ public class ServerMock implements Server {
// TODO Auto-generated method stub // TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getSystemChannelFlags'"); throw new UnsupportedOperationException("Unimplemented method 'getSystemChannelFlags'");
} }
public static PermissionType TESTPERMISSION = PermissionType.ADMINISTRATOR;
@Override
public boolean hasPermissions(User user, PermissionType... type) {
assertNotNull(user);
assertNotNull(type);
return TESTPERMISSION.equals(type[0]);
}
} }