Compare commits
	
		
			4 Commits
		
	
	
		
			master
			...
			f89ae5e425
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f89ae5e425 | |||
| 582e0f0bae | |||
| 0ea330d48b | |||
| c241f6b1fe | 
							
								
								
									
										40
									
								
								.gitea/workflows/gradle-publish-dev.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								.gitea/workflows/gradle-publish-dev.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | |||||||
|  | name: Publish | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   push: | ||||||
|  |     branches: [dev] | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   Gradle-Publish: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |  | ||||||
|  |     steps: | ||||||
|  |       - name: Checkout code | ||||||
|  |         uses: actions/checkout@v4 | ||||||
|  |  | ||||||
|  |       - name: Set up JDK | ||||||
|  |         uses: actions/setup-java@v4 | ||||||
|  |         with: | ||||||
|  |           java-version: '23' | ||||||
|  |           check-latest: true | ||||||
|  |           distribution: 'zulu' | ||||||
|  |  | ||||||
|  |       - name: Setup Gradle | ||||||
|  |         uses: gradle/actions/setup-gradle@v4 | ||||||
|  |         with: | ||||||
|  |           add-job-summary: always | ||||||
|  |           cache-cleanup: on-success | ||||||
|  |  | ||||||
|  |       - name: Cache Gradle dependencies | ||||||
|  |         uses: actions/cache@v4 | ||||||
|  |         with: | ||||||
|  |           path: |  | ||||||
|  |             ~/.gradle/caches | ||||||
|  |           key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | ||||||
|  |           restore-keys: | | ||||||
|  |             ${{ runner.os }}-gradle- | ||||||
|  |  | ||||||
|  |       - name: Publish Dev | ||||||
|  |         env: | ||||||
|  |           GITEA_TOKEN: ${{ secrets.PUBLISH_PACKAGE_TOKEN }} | ||||||
|  |         run: chmod +x gradlew; ./gradlew publishAllPublicationsToGiteaRepository  | ||||||
| @@ -1,11 +1,11 @@ | |||||||
| name: Test | name: Publish | ||||||
|  |  | ||||||
| on: | on: | ||||||
|   push: |   push: | ||||||
|     branches: [master] |     branches: [master] | ||||||
|  |  | ||||||
| jobs: | jobs: | ||||||
|   Gradle-Test: |   Gradle-Publish: | ||||||
|     runs-on: ubuntu-latest |     runs-on: ubuntu-latest | ||||||
|  |  | ||||||
|     steps: |     steps: | ||||||
| @@ -34,7 +34,7 @@ jobs: | |||||||
|           restore-keys: | |           restore-keys: | | ||||||
|             ${{ runner.os }}-gradle- |             ${{ runner.os }}-gradle- | ||||||
|  |  | ||||||
|       - name: Publish |       - name: Publish Release | ||||||
|         env: |         env: | ||||||
|           GITEA_TOKEN: ${{ secrets.PUBLISH_PACKAGE_TOKEN }} |           GITEA_TOKEN: ${{ secrets.PUBLISH_PACKAGE_TOKEN }} | ||||||
|         run: chmod +x gradlew; ./gradlew publishAllPublicationsToGiteaRepository |         run: chmod +x gradlew; ./gradlew publishAllPublicationsToGiteaRepository -Prelease | ||||||
| @@ -8,6 +8,7 @@ allprojects { | |||||||
|     group = "net.tomatentum.Marinara" |     group = "net.tomatentum.Marinara" | ||||||
|     version = "1.0.0-RC1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "") |     version = "1.0.0-RC1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "") | ||||||
|     description = "A simple but powerful, library-agnostic Discord Interaction Wrapper." |     description = "A simple but powerful, library-agnostic Discord Interaction Wrapper." | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| subprojects { | subprojects { | ||||||
| @@ -18,6 +19,8 @@ subprojects { | |||||||
|         publishing { |         publishing { | ||||||
|             publications { |             publications { | ||||||
|                 create<MavenPublication>("maven") { |                 create<MavenPublication>("maven") { | ||||||
|  |                     if (!project.hasProperty("release")) | ||||||
|  |                         artifactId = project.getName() + "-dev" | ||||||
|                     from(components["java"]) |                     from(components["java"]) | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|   | |||||||
| @@ -1,22 +1,34 @@ | |||||||
| package net.tomatentum.marinara; | package net.tomatentum.marinara; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.registry.InteractionCheckRegistry; | ||||||
| import net.tomatentum.marinara.registry.InteractionRegistry; | import net.tomatentum.marinara.registry.InteractionRegistry; | ||||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; | import net.tomatentum.marinara.wrapper.LibraryWrapper; | ||||||
|  |  | ||||||
| public class Marinara { | public class Marinara { | ||||||
|      |      | ||||||
|     public static <T extends LibraryWrapper> Marinara load(LibraryWrapper wrapper) { |     public static <T extends LibraryWrapper> Marinara load(LibraryWrapper wrapper) { | ||||||
|         InteractionRegistry registry = new InteractionRegistry(wrapper); |         return new Marinara(wrapper); | ||||||
|         return new Marinara(registry); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private InteractionRegistry registry; |     private InteractionRegistry registry; | ||||||
|  |     private InteractionCheckRegistry checkRegistry; | ||||||
|  |     private LibraryWrapper wrapper; | ||||||
|  |  | ||||||
|     private Marinara(InteractionRegistry registry) { |     private Marinara(LibraryWrapper wrapper) { | ||||||
|         this.registry = registry; |         this.wrapper = wrapper; | ||||||
|  |         this.registry = new InteractionRegistry(this); | ||||||
|  |         this.checkRegistry = new InteractionCheckRegistry(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public InteractionRegistry getRegistry() { |     public InteractionRegistry getRegistry() { | ||||||
|         return registry; |         return registry; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public InteractionCheckRegistry getCheckRegistry() { | ||||||
|  |         return checkRegistry; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public LibraryWrapper getWrapper() { | ||||||
|  |         return wrapper; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -0,0 +1,26 @@ | |||||||
|  | package net.tomatentum.marinara.checks; | ||||||
|  |  | ||||||
|  | import java.lang.annotation.Annotation; | ||||||
|  | import java.lang.reflect.InvocationTargetException; | ||||||
|  |  | ||||||
|  | public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) { | ||||||
|  |      | ||||||
|  |     public boolean pre() { | ||||||
|  |         try { | ||||||
|  |             return (boolean) check.getClass().getMethod("preExec", annotation.getClass()).invoke(check, annotation); | ||||||
|  |         } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public boolean post() { | ||||||
|  |         try { | ||||||
|  |             return (boolean) check.getClass().getMethod("postExec", annotation.getClass()).invoke(check, annotation); | ||||||
|  |         } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,10 @@ | |||||||
|  | package net.tomatentum.marinara.checks; | ||||||
|  |  | ||||||
|  | import java.lang.annotation.Annotation; | ||||||
|  |  | ||||||
|  | public interface InteractionCheck<A extends Annotation> { | ||||||
|  |  | ||||||
|  |     public boolean preExec(A annotation); | ||||||
|  |     public boolean postExec(A annotation); | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -2,39 +2,40 @@ package net.tomatentum.marinara.interaction.methods; | |||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.Marinara; | ||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
| import net.tomatentum.marinara.interaction.InteractionType; | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
| import net.tomatentum.marinara.interaction.annotation.Button; | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; | import net.tomatentum.marinara.parser.ButtonParser; | ||||||
|  |  | ||||||
| public class ButtonInteractionMethod extends InteractionMethod { | public class ButtonInteractionMethod extends InteractionMethod { | ||||||
|  |  | ||||||
|     private String customId; |     private String customId; | ||||||
|  |  | ||||||
|     ButtonInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { |     ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) { | ||||||
|         super(method, handler, wrapper); |         super(method, handler, marinara); | ||||||
|         parseMethod(); |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public AnnotationParser[] getParsers() { | ||||||
|  |         return new AnnotationParser[] { | ||||||
|  |             new ButtonParser(method, (x) -> { this.customId = x; } ) | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public Object getParameter(Object parameter, int index) { |     public Object getParameter(Object parameter, int index) { | ||||||
|         Class<?> type = getMethod().getParameterTypes()[index+1]; |         Class<?> type = getMethod().getParameterTypes()[index+1]; | ||||||
|         return wrapper.getComponentContextObject(parameter, type); |         return marinara.getWrapper().getComponentContextObject(parameter, type); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public boolean canRun(Object context) { |     public boolean canRun(Object context) { | ||||||
|         return wrapper.getButtonId(context).equals(customId); |         return marinara.getWrapper().getButtonId(context).equals(customId); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public InteractionType getType() { |     public InteractionType getType() { | ||||||
|         return InteractionType.BUTTON; |         return InteractionType.BUTTON; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void parseMethod() { |  | ||||||
|         Button button = getMethod().getAnnotation(Button.class); |  | ||||||
|         this.customId = button.value(); |  | ||||||
|     } |  | ||||||
|      |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,35 +7,52 @@ import java.util.ArrayList; | |||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.Marinara; | ||||||
|  | import net.tomatentum.marinara.checks.AppliedCheck; | ||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
| import net.tomatentum.marinara.interaction.InteractionType; | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
| import net.tomatentum.marinara.interaction.annotation.Button; | import net.tomatentum.marinara.interaction.annotation.Button; | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | ||||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
|  | import net.tomatentum.marinara.parser.InteractionCheckParser; | ||||||
|  |  | ||||||
| public abstract class InteractionMethod { | public abstract class InteractionMethod { | ||||||
|  |  | ||||||
|     public static InteractionMethod create(Method method, InteractionHandler handler, LibraryWrapper wrapper) { |     public static InteractionMethod create(Method method, InteractionHandler handler, Marinara marinara) { | ||||||
|         if (method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class)) |         if (method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class)) | ||||||
|             return new SlashCommandInteractionMethod(method, handler, wrapper); |             return new SlashCommandInteractionMethod(method, handler, marinara); | ||||||
|         if (method.isAnnotationPresent(Button.class)) |         if (method.isAnnotationPresent(Button.class)) | ||||||
|             return new ButtonInteractionMethod(method, handler, wrapper); |             return new ButtonInteractionMethod(method, handler, marinara); | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     protected Method method; |     protected Method method; | ||||||
|     protected InteractionHandler handler; |     protected InteractionHandler handler; | ||||||
|     protected LibraryWrapper wrapper; |     protected Marinara marinara; | ||||||
|  |     protected List<AnnotationParser> parsers; | ||||||
|  |     protected List<AppliedCheck> appliedChecks; | ||||||
|  |  | ||||||
|     protected InteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { |     protected InteractionMethod(Method method,  | ||||||
|  |         InteractionHandler handler,  | ||||||
|  |         Marinara marinara | ||||||
|  |         ) { | ||||||
|         if (!Arrays.asList(handler.getClass().getMethods()).contains(method)) |         if (!Arrays.asList(handler.getClass().getMethods()).contains(method)) | ||||||
|             throw new InvalidParameterException("Method does not apply to specified handler"); |             throw new InvalidParameterException("Method does not apply to specified handler"); | ||||||
|  |  | ||||||
|         this.method = method; |         this.method = method; | ||||||
|         this.handler = handler; |         this.handler = handler; | ||||||
|         this.wrapper = wrapper; |         this.marinara = marinara; | ||||||
|  |         this.parsers = new ArrayList<>(Arrays.asList(getParsers())); | ||||||
|  |         this.appliedChecks = new ArrayList<>(); | ||||||
|  |  | ||||||
|  |         parsers.add(new InteractionCheckParser(method, appliedChecks::add, marinara.getCheckRegistry())); | ||||||
|  |  | ||||||
|  |         parsers.stream().forEach(AnnotationParser::parse); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public abstract AnnotationParser[] getParsers(); | ||||||
|  |  | ||||||
|     public abstract Object getParameter(Object parameter, int index); |     public abstract Object getParameter(Object parameter, int index); | ||||||
|  |  | ||||||
|     public abstract boolean canRun(Object context); |     public abstract boolean canRun(Object context); | ||||||
| @@ -43,6 +60,23 @@ public abstract class InteractionMethod { | |||||||
|     public abstract InteractionType getType(); |     public abstract InteractionType getType(); | ||||||
|  |  | ||||||
|     public void run(Object context) { |     public void run(Object context) { | ||||||
|  |         this.appliedChecks.forEach(AppliedCheck::pre); | ||||||
|  |  | ||||||
|  |         method.setAccessible(true); | ||||||
|  |         try { | ||||||
|  |             method.invoke(handler, getParameters(context)); | ||||||
|  |         }catch (IllegalAccessException | InvocationTargetException ex) { | ||||||
|  |             throw new RuntimeException(ex); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         this.appliedChecks.forEach(AppliedCheck::post); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Method getMethod() { | ||||||
|  |         return method; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private Object[] getParameters(Object context) { | ||||||
|         int parameterCount = method.getParameterCount(); |         int parameterCount = method.getParameterCount(); | ||||||
|         List<Object> parameters = new ArrayList<>(); |         List<Object> parameters = new ArrayList<>(); | ||||||
|          |          | ||||||
| @@ -53,16 +87,7 @@ public abstract class InteractionMethod { | |||||||
|             } |             } | ||||||
|             parameters.add(getParameter(context, i-1)); |             parameters.add(getParameter(context, i-1)); | ||||||
|         } |         } | ||||||
|         method.setAccessible(true); |         return parameters.toArray(); | ||||||
|         try { |  | ||||||
|             method.invoke(handler, parameters.toArray()); |  | ||||||
|         }catch (IllegalAccessException | InvocationTargetException ex) { |  | ||||||
|             throw new RuntimeException(ex); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public Method getMethod() { |  | ||||||
|         return method; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,32 +2,36 @@ package net.tomatentum.marinara.interaction.methods; | |||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.Marinara; | ||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
| import net.tomatentum.marinara.interaction.InteractionType; | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
| import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; | import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | import net.tomatentum.marinara.parser.SlashCommandParser; | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; |  | ||||||
| import net.tomatentum.marinara.util.ReflectionUtil; |  | ||||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; |  | ||||||
|  |  | ||||||
| public class SlashCommandInteractionMethod extends InteractionMethod { | public class SlashCommandInteractionMethod extends InteractionMethod { | ||||||
|  |  | ||||||
|     private ExecutableSlashCommandDefinition commandDefinition; |     private ExecutableSlashCommandDefinition commandDefinition; | ||||||
|  |  | ||||||
|     SlashCommandInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { |     SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) { | ||||||
|         super(method, handler, wrapper); |         super(method, handler, marinara); | ||||||
|         parseMethod(); |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public AnnotationParser[] getParsers() { | ||||||
|  |         return new AnnotationParser[] {  | ||||||
|  |             new SlashCommandParser(method, (x) -> { this.commandDefinition = x; } )  | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public Object getParameter(Object context, int index) { |     public Object getParameter(Object context, int index) { | ||||||
|         return wrapper.convertCommandOption(context, commandDefinition.options()[index].type(), commandDefinition.options()[index].name()); |         return marinara.getWrapper().convertCommandOption(context, commandDefinition.options()[index].type(), commandDefinition.options()[index].name()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public boolean canRun(Object context) { |     public boolean canRun(Object context) { | ||||||
|         ExecutableSlashCommandDefinition other = wrapper.getCommandDefinition(context); |         ExecutableSlashCommandDefinition other = marinara.getWrapper().getCommandDefinition(context); | ||||||
|         return commandDefinition.equals(other); |         return commandDefinition.equals(other); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -40,24 +44,8 @@ public class SlashCommandInteractionMethod extends InteractionMethod { | |||||||
|         return commandDefinition; |         return commandDefinition; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void parseMethod() { |     public void setCommandDefinition(ExecutableSlashCommandDefinition commandDefinition) { | ||||||
|         ReflectionUtil.checkValidCommandMethod(method); |         this.commandDefinition = commandDefinition; | ||||||
|  |  | ||||||
|         SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class); |  | ||||||
|         ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder(); |  | ||||||
|         builder.setApplicationCommand(cmd); |  | ||||||
|  |  | ||||||
|         if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) { |  | ||||||
|             SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class); |  | ||||||
|             builder.setSubCommandGroup(cmdGroup); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) { |  | ||||||
|             SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class); |  | ||||||
|             builder.setSubCommand(subCmd); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         this.commandDefinition = builder.build(); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -0,0 +1,8 @@ | |||||||
|  | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  |  | ||||||
|  | public interface AnnotationParser { | ||||||
|  |     void parse(); | ||||||
|  |     Method getMethod(); | ||||||
|  | } | ||||||
| @@ -0,0 +1,29 @@ | |||||||
|  | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.function.Consumer; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.interaction.annotation.Button; | ||||||
|  |  | ||||||
|  | public class ButtonParser implements AnnotationParser { | ||||||
|  |      | ||||||
|  |     private Method method; | ||||||
|  |     private Consumer<String> consumer; | ||||||
|  |  | ||||||
|  |     public ButtonParser(Method method, Consumer<String> consumer) { | ||||||
|  |         this.method = method; | ||||||
|  |         this.consumer = consumer; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void parse() { | ||||||
|  |         Button button = getMethod().getAnnotation(Button.class); | ||||||
|  |         this.consumer.accept(button.value()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Method getMethod() { | ||||||
|  |         return this.method; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,42 @@ | |||||||
|  | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
|  | import java.lang.annotation.Annotation; | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.Optional; | ||||||
|  | import java.util.function.Consumer; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.checks.AppliedCheck; | ||||||
|  | import net.tomatentum.marinara.checks.InteractionCheck; | ||||||
|  | import net.tomatentum.marinara.registry.InteractionCheckRegistry; | ||||||
|  |  | ||||||
|  | public class InteractionCheckParser implements AnnotationParser { | ||||||
|  |  | ||||||
|  |     private InteractionCheckRegistry checkRegistry; | ||||||
|  |     private Method method; | ||||||
|  |     private Consumer<AppliedCheck> consumer; | ||||||
|  |  | ||||||
|  |     public InteractionCheckParser(Method method, Consumer<AppliedCheck> consumer, InteractionCheckRegistry checkRegistry) { | ||||||
|  |         this.checkRegistry = checkRegistry; | ||||||
|  |         this.method = method; | ||||||
|  |         this.consumer = consumer; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void parse() { | ||||||
|  |         Annotation[] annotations = method.getAnnotations(); | ||||||
|  |         Arrays.stream(annotations).forEach(this::convertAnnotation); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private void convertAnnotation(Annotation annotation) { | ||||||
|  |             Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.getClass()); | ||||||
|  |             if (check.isPresent()) | ||||||
|  |                 consumer.accept(new AppliedCheck(check.get(), annotation)); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Method getMethod() { | ||||||
|  |         return this.method; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,63 @@ | |||||||
|  | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.function.Consumer; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; | ||||||
|  | import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | ||||||
|  | import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | ||||||
|  | import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; | ||||||
|  | import net.tomatentum.marinara.util.ReflectionUtil; | ||||||
|  |  | ||||||
|  | public class SlashCommandParser implements AnnotationParser { | ||||||
|  |  | ||||||
|  |     private Method method; | ||||||
|  |     private Consumer<ExecutableSlashCommandDefinition> consumer; | ||||||
|  |  | ||||||
|  |     public SlashCommandParser(Method method, Consumer<ExecutableSlashCommandDefinition> consumer) { | ||||||
|  |         this.method = method; | ||||||
|  |         this.consumer = consumer; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void parse() { | ||||||
|  |         this.checkValidCommandMethod(method); | ||||||
|  |  | ||||||
|  |         SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class); | ||||||
|  |         ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder(); | ||||||
|  |         builder.setApplicationCommand(cmd); | ||||||
|  |  | ||||||
|  |         if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) { | ||||||
|  |             SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class); | ||||||
|  |             builder.setSubCommandGroup(cmdGroup); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) { | ||||||
|  |             SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class); | ||||||
|  |             builder.setSubCommand(subCmd); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         consumer.accept(builder.build()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Method getMethod() { | ||||||
|  |        return this.method; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private void checkValidCommandMethod(Method method) { | ||||||
|  |         if (method.isAnnotationPresent(SlashCommand.class) &&  | ||||||
|  |             method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) { | ||||||
|  |             throw new RuntimeException(method.getName() + ": Can't have ApplicationCommand Annotation on Class and Method"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (!ReflectionUtil.isAnnotationPresent(method, SlashCommand.class)) | ||||||
|  |             throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method"); | ||||||
|  |  | ||||||
|  |         if ((method.isAnnotationPresent(SubCommand.class) &&  | ||||||
|  |             !ReflectionUtil.isAnnotationPresent(method, SlashCommand.class))) { | ||||||
|  |             throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -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.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.Marinara; | ||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
| import net.tomatentum.marinara.interaction.InteractionType; | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
| import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; | import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; | ||||||
| import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; | import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; | ||||||
| import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod; | import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod; | ||||||
| import net.tomatentum.marinara.interaction.methods.InteractionMethod; | import net.tomatentum.marinara.interaction.methods.InteractionMethod; | ||||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; |  | ||||||
|  |  | ||||||
| public class InteractionRegistry { | public class InteractionRegistry { | ||||||
|     private List<InteractionMethod> interactionMethods; |     private List<InteractionMethod> interactionMethods; | ||||||
|     private LibraryWrapper wrapper; |     private Marinara marinara; | ||||||
|  |  | ||||||
|     public InteractionRegistry(LibraryWrapper wrapper) { |     public InteractionRegistry(Marinara marinara) { | ||||||
|         this.interactionMethods = new ArrayList<>(); |         this.interactionMethods = new ArrayList<>(); | ||||||
|         this.wrapper = wrapper; |         this.marinara = marinara; | ||||||
|         wrapper.subscribeInteractions(this::handle); |         marinara.getWrapper().subscribeInteractions(this::handle); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void addInteractions(InteractionHandler interactionHandler) { |     public void addInteractions(InteractionHandler interactionHandler) { | ||||||
|         for (Method method : interactionHandler.getClass().getMethods()) { |         for (Method method : interactionHandler.getClass().getMethods()) { | ||||||
|             InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, wrapper); |             InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara); | ||||||
|             if (iMethod != null) |             if (iMethod != null) | ||||||
|                 this.interactionMethods.add(iMethod); |                 this.interactionMethods.add(iMethod); | ||||||
|         } |         } | ||||||
| @@ -48,12 +48,12 @@ public class InteractionRegistry { | |||||||
|                 defs.add(new SlashCommandDefinition(def.applicationCommand()).addExecutableCommand(def)); |                 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) { |     public void handle(Object context) { | ||||||
|         interactionMethods.forEach((m) -> { |         interactionMethods.forEach((m) -> { | ||||||
|             InteractionType type = wrapper.getInteractionType(context.getClass()); |             InteractionType type = marinara.getWrapper().getInteractionType(context.getClass()); | ||||||
|             if (m.getType().equals(type)) |             if (m.getType().equals(type)) | ||||||
|                 m.run(context); |                 m.run(context); | ||||||
|         }); |         }); | ||||||
|   | |||||||
| @@ -3,9 +3,6 @@ package net.tomatentum.marinara.util; | |||||||
| import java.lang.annotation.Annotation; | import java.lang.annotation.Annotation; | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
|  |  | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; |  | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; |  | ||||||
|  |  | ||||||
| public final class ReflectionUtil { | public final class ReflectionUtil { | ||||||
|  |  | ||||||
|     public static <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) { |     public static <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) { | ||||||
| @@ -20,20 +17,5 @@ public final class ReflectionUtil { | |||||||
|             method.getAnnotation(annotationClass) :  |             method.getAnnotation(annotationClass) :  | ||||||
|             method.getDeclaringClass().getAnnotation(annotationClass); |             method.getDeclaringClass().getAnnotation(annotationClass); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void checkValidCommandMethod(Method method) { |  | ||||||
|         if (method.isAnnotationPresent(SlashCommand.class) &&  |  | ||||||
|             method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) { |  | ||||||
|             throw new RuntimeException(method.getName() + ": Can't have ApplicationCommand Annotation on Class and Method"); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         if (!isAnnotationPresent(method, SlashCommand.class)) |  | ||||||
|             throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method"); |  | ||||||
|  |  | ||||||
|         if ((method.isAnnotationPresent(SubCommand.class) &&  |  | ||||||
|             !isAnnotationPresent(method, SlashCommand.class))) { |  | ||||||
|             throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class"); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user