From 97550ed29fd20a920f0c7b1bf2dc5f7c24c1de84 Mon Sep 17 00:00:00 2001 From: Dalton <48583030+Burchard36@users.noreply.github.com> Date: Sat, 23 Apr 2022 22:17:47 -0700 Subject: [PATCH] Add support for multiple Annotation for commands --- src/main/java/com/burchard36/BurchAPI.java | 47 +++++++++++++++---- .../command/annotation/CommandAliases.java | 12 +++++ .../annotation/CommandDescription.java | 12 +++++ .../command/annotation/CommandName.java | 13 +++++ .../command/annotation/CommandUsage.java | 12 +++++ 5 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/burchard36/command/annotation/CommandAliases.java create mode 100644 src/main/java/com/burchard36/command/annotation/CommandDescription.java create mode 100644 src/main/java/com/burchard36/command/annotation/CommandName.java create mode 100644 src/main/java/com/burchard36/command/annotation/CommandUsage.java diff --git a/src/main/java/com/burchard36/BurchAPI.java b/src/main/java/com/burchard36/BurchAPI.java index e048605..ae24281 100644 --- a/src/main/java/com/burchard36/BurchAPI.java +++ b/src/main/java/com/burchard36/BurchAPI.java @@ -1,7 +1,7 @@ package com.burchard36; import com.burchard36.command.ApiCommand; -import com.burchard36.command.annotation.RegisterCommand; +import com.burchard36.command.annotation.*; import com.burchard36.command.exceptions.CommandConstructorNotFoundException; import com.burchard36.command.exceptions.CommandInterfaceNotFoundException; import com.burchard36.command.exceptions.InvalidCommandAnnotationException; @@ -17,6 +17,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Set; @@ -26,7 +27,8 @@ public final class BurchAPI implements Api { public static JavaPlugin INSTANCE; private HologramManager hologramManager; - public BurchAPI() {} + public BurchAPI() { + } /** * Initializes the API to a plugin @@ -75,15 +77,42 @@ public BurchAPI initializeApi(final JavaPlugin plugin) { } final RegisterCommand commandAnnotation = command.getClass().getAnnotation(RegisterCommand.class); - if (commandAnnotation == null) throw new CommandInterfaceNotFoundException("The command class: " + clazz.getName() + " does not have @RegisterCommand as a Annotation!"); + final CommandName commandName = command.getClass().getAnnotation(CommandName.class); + final CommandDescription commandDescription = command.getClass().getAnnotation(CommandDescription.class); + final CommandUsage commandUsage = command.getClass().getAnnotation(CommandUsage.class); + final CommandAliases commandAliases = command.getClass().getAnnotation(CommandAliases.class); + + if (commandAnnotation != null) { + + if (!commandAnnotation.name().equalsIgnoreCase("")) + command.setCommandName(commandAnnotation.name()); + + if (commandAnnotation.name().equalsIgnoreCase("") && commandName != null + && !commandName.name().equalsIgnoreCase("")) { + command.setCommandName(commandName.name()); + } else throw new InvalidCommandAnnotationException("The class: " + command.getClass().getName() + " did not have a valid command name set!"); + + if (!commandAnnotation.description().equalsIgnoreCase("")) + command.setDescription(commandAnnotation.description()); - if (commandAnnotation.name().equalsIgnoreCase("")) - throw new InvalidCommandAnnotationException("The command class: " + clazz.getName() + " @RegisterCommand interface does not have a name parameter!"); + if (commandAnnotation.description().equalsIgnoreCase("") && commandDescription != null + && !commandDescription.description().equalsIgnoreCase("")) + command.setDescription(commandDescription.description()); - command.setCommandName(commandAnnotation.name()) - .setCommandDescription(commandAnnotation.description()) - .setCommandAliases(commandAnnotation.aliases()) - .setCommandUsage(commandAnnotation.usageMessage()); + if (!commandAnnotation.usageMessage().equalsIgnoreCase("")) + command.setUsage(commandAnnotation.usageMessage()); + + if (commandAnnotation.usageMessage().equalsIgnoreCase("") && commandUsage != null + && !commandUsage.usageMessage().equalsIgnoreCase("")) + command.setUsage(commandUsage.usageMessage()); + + if (commandAnnotation.aliases().length != 0) + command.setAliases(Arrays.asList(commandAnnotation.aliases())); + + if (commandAnnotation.aliases().length <= 0 && commandAliases != null + && commandAliases.aliases().length > 0) + command.setAliases(Arrays.asList(commandAliases.aliases())); + } registerCommand(command); } return this; diff --git a/src/main/java/com/burchard36/command/annotation/CommandAliases.java b/src/main/java/com/burchard36/command/annotation/CommandAliases.java new file mode 100644 index 0000000..b80d0dd --- /dev/null +++ b/src/main/java/com/burchard36/command/annotation/CommandAliases.java @@ -0,0 +1,12 @@ +package com.burchard36.command.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface CommandAliases { + String[] aliases() default {}; +} diff --git a/src/main/java/com/burchard36/command/annotation/CommandDescription.java b/src/main/java/com/burchard36/command/annotation/CommandDescription.java new file mode 100644 index 0000000..860e610 --- /dev/null +++ b/src/main/java/com/burchard36/command/annotation/CommandDescription.java @@ -0,0 +1,12 @@ +package com.burchard36.command.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface CommandDescription { + String description(); +} diff --git a/src/main/java/com/burchard36/command/annotation/CommandName.java b/src/main/java/com/burchard36/command/annotation/CommandName.java new file mode 100644 index 0000000..0c0a9c0 --- /dev/null +++ b/src/main/java/com/burchard36/command/annotation/CommandName.java @@ -0,0 +1,13 @@ +package com.burchard36.command.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface CommandName { + + String name() default ""; +} diff --git a/src/main/java/com/burchard36/command/annotation/CommandUsage.java b/src/main/java/com/burchard36/command/annotation/CommandUsage.java new file mode 100644 index 0000000..930619e --- /dev/null +++ b/src/main/java/com/burchard36/command/annotation/CommandUsage.java @@ -0,0 +1,12 @@ +package com.burchard36.command.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface CommandUsage { + String usageMessage() default ""; +}