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
---
.../java/cc/polyfrost/oneconfig/OneConfig.java | 3 +
.../cc/polyfrost/oneconfig/test/TestCommand.java | 39 +++
.../oneconfig/utils/commands/CommandHelper.java | 19 ++
.../oneconfig/utils/commands/CommandManager.java | 295 +++++++++++++++++++++
.../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 +++
.../utils/commands/arguments/ArgumentParser.java | 12 +
.../utils/commands/arguments/Arguments.java | 33 +++
.../utils/commands/arguments/BooleanParser.java | 11 +
.../utils/commands/arguments/DoubleParser.java | 10 +
.../utils/commands/arguments/FloatParser.java | 11 +
.../utils/commands/arguments/IntegerParser.java | 8 +
.../utils/commands/arguments/StringParser.java | 18 ++
17 files changed, 682 insertions(+)
create mode 100644 src/main/java/cc/polyfrost/oneconfig/test/TestCommand.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandHelper.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandManager.java
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
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java
create mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/StringParser.java
(limited to 'src/main')
diff --git a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java b/src/main/java/cc/polyfrost/oneconfig/OneConfig.java
index 60bb37e..c1301d3 100644
--- a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java
+++ b/src/main/java/cc/polyfrost/oneconfig/OneConfig.java
@@ -11,7 +11,9 @@ import cc.polyfrost.oneconfig.lwjgl.BlurHandler;
import cc.polyfrost.oneconfig.lwjgl.RenderManager;
import cc.polyfrost.oneconfig.lwjgl.font.Fonts;
import cc.polyfrost.oneconfig.lwjgl.image.Images;
+import cc.polyfrost.oneconfig.test.TestCommand;
import cc.polyfrost.oneconfig.test.TestConfig;
+import cc.polyfrost.oneconfig.utils.commands.CommandManager;
import cc.polyfrost.oneconfig.utils.hypixel.HypixelUtils;
import net.minecraft.launchwrapper.Launch;
import net.minecraftforge.client.ClientCommandHandler;
@@ -50,6 +52,7 @@ public class OneConfig {
public void onFMLInitialization(net.minecraftforge.fml.common.event.FMLInitializationEvent event) {
BlurHandler.INSTANCE.load();
testConfig = new TestConfig();
+ CommandManager.registerCommand(new TestCommand());
ClientCommandHandler.instance.registerCommand(new OneConfigCommand());
EventManager.INSTANCE.register(new HudCore());
EventManager.INSTANCE.register(HypixelUtils.INSTANCE);
diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestCommand.java b/src/main/java/cc/polyfrost/oneconfig/test/TestCommand.java
new file mode 100644
index 0000000..753c353
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/test/TestCommand.java
@@ -0,0 +1,39 @@
+package cc.polyfrost.oneconfig.test;
+
+import cc.polyfrost.oneconfig.libs.universal.UChat;
+import cc.polyfrost.oneconfig.utils.commands.annotations.Command;
+import cc.polyfrost.oneconfig.utils.commands.annotations.Main;
+import cc.polyfrost.oneconfig.utils.commands.annotations.Name;
+import cc.polyfrost.oneconfig.utils.commands.annotations.SubCommand;
+
+@Command(value = "test", aliases = {"t"})
+public class TestCommand {
+
+ @Main
+ private static void main() { // /test
+ UChat.chat("Main command");
+ }
+
+ @SubCommand(value = "subcommand", aliases = {"s"})
+ private static class TestSubCommand {
+
+ @Main(priority = 999)
+ private static void main(int a, float b, @Name("named c") String c) { // /test subcommand
+ * 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 ""; +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java new file mode 100644 index 0000000..d9d51b0 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java @@ -0,0 +1,12 @@ +package cc.polyfrost.oneconfig.utils.commands.arguments; + +import com.google.common.reflect.TypeToken; +import org.jetbrains.annotations.Nullable; + +@SuppressWarnings("unstable") +public abstract class ArgumentParser