From 688869d1a5d60b8bdd89c78e0deab6b53b04a694 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 28 May 2022 22:41:38 +0700 Subject: command utils --- .../utils/commands/annotations/Command.java | 115 +++++++++++++++++++++ .../utils/commands/annotations/Greedy.java | 20 ++++ .../oneconfig/utils/commands/annotations/Main.java | 20 ++++ .../oneconfig/utils/commands/annotations/Name.java | 22 ++++ .../utils/commands/annotations/Optional.java | 12 +++ .../utils/commands/annotations/SubCommand.java | 34 ++++++ 6 files changed, 223 insertions(+) create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Greedy.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Main.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Name.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Optional.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/SubCommand.java (limited to 'src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations') 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. + *
+ * To start, create a class which is annotated with this annotation, and then a method which is annotated with {@link Main}. + *
{@code + * @literal @Command(name = "test", description = "A test command", aliases = {"t"}) + * public class TestCommand { + * @literal @Main + * public static void mainCommandMethod() { + * // do stuff + * } + * } + * }+ *
+ * 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 must be static. + * If the methods are not static, the {@link CommandManager} will throw an exception. + * + *
+ * 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: + *
{@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 + * } + * }+ * + * + *
+ * Of course, {@link SubCommand}s can be added and "stacked". For example, the following command class: + *
{@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 + * } + * } + * } + * } + * }+ * + * + * 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)}. + * + *
+ * Note: if you're viewing this in IntelliJ or just see the @literal tag everywhere, please ignore that. + *
+ * + * @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 ""; +} -- cgit