diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-28 17:55:02 +0200 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-28 17:55:02 +0200 |
commit | ad94530c89f8358338b9a4034b92b3a444001df6 (patch) | |
tree | 9ad6f14ad517523eec76f102ff0f1fc8cf8d85c1 /src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations | |
parent | a398d1ccf81bd1586dafd15bd0cdd5dd2cb30bc2 (diff) | |
parent | 4df2a5f6c815b0dd8cc8b68aaf164a40e63fa57d (diff) | |
download | OneConfig-ad94530c89f8358338b9a4034b92b3a444001df6.tar.gz OneConfig-ad94530c89f8358338b9a4034b92b3a444001df6.tar.bz2 OneConfig-ad94530c89f8358338b9a4034b92b3a444001df6.zip |
Merge branch 'master' of github.com:Polyfrost/OneConfig
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations')
6 files changed, 223 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java new file mode 100644 index 0000000..7717d46 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java @@ -0,0 +1,115 @@ +package cc.polyfrost.oneconfig.utils.commands.annotations; + +import cc.polyfrost.oneconfig.utils.commands.CommandHelper; +import cc.polyfrost.oneconfig.utils.commands.CommandManager; +import cc.polyfrost.oneconfig.utils.commands.arguments.ArgumentParser; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks a class as a command. + * <p> + * To start, create a class which is annotated with this annotation, and then a method which is annotated with {@link Main}. + * <pre>{@code + * @literal @Command(name = "test", description = "A test command", aliases = {"t"}) + * public class TestCommand { + * @literal @Main + * public static void mainCommandMethod() { + * // do stuff + * } + * } + * }</pre> + * <p> + * Keep in mind how {@code mainCommandMethod} is a private and static method. + * With OneConfig's command utility, methods for commands can be any kind of visibility, and <b><i>must be static</i></b>. + * If the methods are not static, the {@link CommandManager} will throw an exception. + * + * <p> + * Command methods can also having multiple parameters of virtually any type, as long as it is a + * {@link String}, {@code boolean}, {@code int}, {@code double}, {@code float}, + * or is added as an {@link ArgumentParser} and added to the + * {@link CommandManager} via {@link CommandManager#addParser(ArgumentParser)}. + * Parameters can also be annotated with various annotations, such as {@link Name}, which names the parameter + * for the user, {@link Greedy}, which takes all following arguments along with itself, and more to come. + * For example, the following command method: + * <pre>{@code + * @literal @Main + * private static void mainCommandMethod(String arg1, @Name("nameOfSecondArgument") boolean arg2, int arg3, double arg4, float arg5, @Greedy String greedyArgument) { + * // do things here + * System.out.println(greedyArgument); // Greedily takes all remaining arguments after greedyArgument as well as itself. 1984 + * } + * }</pre> + * </p> + * + * <p> + * Of course, {@link SubCommand}s can be added and "stacked". For example, the following command class: + * <pre>{@code + * @literal @Command(name = "mycommand", aliases = {"alias1"}, description = "My command description") + * public class TestCommand { + * @literal @Main + * private static void mainCommandMethod() { + * // do things here + * } + * + * @literal @SubCommand(name = "subcommand", aliases = {"subalias1"}, description = "My subcommand description") + * private static class SubCommandClass { + * @literal @Main + * private static void subCommandMethod() { + * // do things here + * } + * + * @literal @SubCommand(name = "subsubcommand", aliases = {"subsubalias1"}, description = "My subsubcommand description") + * private static class SubSubCommandClass { + * @literal @Main + * private static void subSubCommandMethod() { + * // do things here + * } + * } + * } + * } + * }</pre> + * </p> + * + * To register commands, either extend {@link CommandHelper} and run {@link CommandHelper#preload()} (which does nothing, + * just makes loading look nicer lol), or use {@link CommandManager#registerCommand(Object)}. + * + * <p> + * Note: if you're viewing this in IntelliJ or just see the @literal tag everywhere, please ignore that. + * </p> + * + * @see cc.polyfrost.oneconfig.test.TestCommand + * @see Main + * @see CommandManager + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface Command { + /** + * The name of the command. + * + * @return The name of the command. + */ + String value(); + + /** + * The aliases of the command. + * + * @return The aliases of the command. + */ + String[] aliases() default {}; + + /** + * The description of the command. + * + * @return The description of the command. + */ + String description() default ""; + + /** + * Whether the command generates a help command. + */ + boolean helpCommand() default true; +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Greedy.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Greedy.java new file mode 100644 index 0000000..31c948d --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Greedy.java @@ -0,0 +1,20 @@ +package cc.polyfrost.oneconfig.utils.commands.annotations; + +import cc.polyfrost.oneconfig.utils.commands.arguments.Arguments; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Indicates that the specific parameter should capture all remaining arguments and itself + * Can only be used on the last parameter, and only works with {@link cc.polyfrost.oneconfig.utils.commands.arguments.StringParser} by default. + * + * @see cc.polyfrost.oneconfig.utils.commands.arguments.StringParser#parse(Arguments) + * @see Command + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.PARAMETER}) +public @interface Greedy { +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Main.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Main.java new file mode 100644 index 0000000..3c105c7 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Main.java @@ -0,0 +1,20 @@ +package cc.polyfrost.oneconfig.utils.commands.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks a method as the main method of a command. + * + * @see Command + * @see SubCommand + * @see cc.polyfrost.oneconfig.utils.commands.CommandManager + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +public @interface Main { + String description() default ""; + int priority() default 1000; +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Name.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Name.java new file mode 100644 index 0000000..ef178a0 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Name.java @@ -0,0 +1,22 @@ +package cc.polyfrost.oneconfig.utils.commands.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks the name of a parameter. + * + * @see Main + * @see Command + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.PARAMETER}) +public @interface Name { + /** + * The name of the parameter. + * @return The name of the parameter. + */ + String value(); +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Optional.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Optional.java new file mode 100644 index 0000000..4dbe8b5 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Optional.java @@ -0,0 +1,12 @@ +package cc.polyfrost.oneconfig.utils.commands.annotations; + +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.PARAMETER}) +public @interface Optional { + +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/SubCommand.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/SubCommand.java new file mode 100644 index 0000000..b1cf035 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/SubCommand.java @@ -0,0 +1,34 @@ +package cc.polyfrost.oneconfig.utils.commands.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks a class as a subcommand. Can be stacked together. + * + * @see Command + * @see cc.polyfrost.oneconfig.utils.commands.CommandManager + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface SubCommand { + /** + * The name of the command. + * @return The name of the command. + */ + String value(); + + /** + * The aliases of the command. + * @return The aliases of the command. + */ + String[] aliases() default {}; + + /** + * The description of the command. + * @return The description of the command. + */ + String description() default ""; +} |