From 44dfbbb419f1736530c04c02a651f7757cf83f3d Mon Sep 17 00:00:00 2001
From: Wyvest <45589059+Wyvest@users.noreply.github.com>
Date: Sun, 5 Jun 2022 15:27:36 +0700
Subject: rewrite command manager, stop using essential relocate, and reformat
code (#34)
* reformat code
* reformat code
rewrite command manager
stop using essential relocate
---
.../cc/polyfrost/oneconfig/utils/GuiUtils.java | 19 +-
.../cc/polyfrost/oneconfig/utils/InputUtils.java | 23 +-
.../cc/polyfrost/oneconfig/utils/JsonUtils.java | 2 +-
.../polyfrost/oneconfig/utils/Multithreading.java | 4 +-
.../cc/polyfrost/oneconfig/utils/NetworkUtils.java | 22 +-
.../cc/polyfrost/oneconfig/utils/OneUIScreen.java | 4 +-
.../cc/polyfrost/oneconfig/utils/TextUtils.java | 14 +-
.../cc/polyfrost/oneconfig/utils/TickDelay.java | 4 +-
.../oneconfig/utils/commands/CommandHelper.java | 19 --
.../oneconfig/utils/commands/CommandManager.java | 289 ++++++++++++---------
.../utils/commands/annotations/Command.java | 6 +-
.../oneconfig/utils/commands/annotations/Main.java | 1 +
.../oneconfig/utils/commands/annotations/Name.java | 1 +
.../utils/commands/annotations/SubCommand.java | 3 +
.../utils/commands/arguments/ArgumentParser.java | 30 ++-
.../utils/commands/arguments/Arguments.java | 2 +-
.../utils/commands/arguments/BooleanParser.java | 20 ++
.../utils/commands/arguments/DoubleParser.java | 6 +-
.../utils/commands/arguments/FloatParser.java | 6 +-
.../utils/commands/arguments/IntegerParser.java | 6 +-
.../oneconfig/utils/hypixel/HypixelUtils.java | 13 +-
21 files changed, 298 insertions(+), 196 deletions(-)
delete mode 100644 src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandHelper.java
(limited to 'src/main/java/cc/polyfrost/oneconfig/utils')
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java
index c6afb00..29c26fb 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java
@@ -3,20 +3,21 @@ package cc.polyfrost.oneconfig.utils;
import cc.polyfrost.oneconfig.events.EventManager;
import cc.polyfrost.oneconfig.events.event.RenderEvent;
import cc.polyfrost.oneconfig.events.event.Stage;
-import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
-import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
-import cc.polyfrost.oneconfig.libs.universal.UScreen;
+import gg.essential.universal.UMinecraft;
+import gg.essential.universal.UScreen;
+import me.kbrewster.eventbus.Subscribe;
import net.minecraft.client.gui.GuiScreen;
/**
* A class containing utility methods for working with GuiScreens.
*/
public final class GuiUtils {
+ private static long time = -1L;
+ private static long deltaTime = 17L;
+
static {
EventManager.INSTANCE.register(new GuiUtils());
}
- private static long time = -1L;
- private static long deltaTime = 17L;
/**
* Displays a screen after a tick, preventing mouse sync issues.
@@ -27,7 +28,9 @@ public final class GuiUtils {
new TickDelay(() -> UScreen.displayScreen(screen), 1);
}
- /** Close the current open GUI screen. */
+ /**
+ * Close the current open GUI screen.
+ */
public static void closeScreen() {
UScreen.displayScreen(null);
}
@@ -35,8 +38,8 @@ public final class GuiUtils {
/**
* Gets the delta time (in milliseconds) between frames.
*
- * Not to be confused with Minecraft deltaTicks / renderPartialTicks, which can be gotten via
- * {@link cc.polyfrost.oneconfig.events.event.TimerUpdateEvent}
+ * Not to be confused with Minecraft deltaTicks / renderPartialTicks, which can be gotten via
+ * {@link cc.polyfrost.oneconfig.events.event.TimerUpdateEvent}
*
*
* @return the delta time.
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java
index 0ba4c00..7ec5ee5 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java
@@ -1,14 +1,14 @@
package cc.polyfrost.oneconfig.utils;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
-import cc.polyfrost.oneconfig.libs.universal.UResolution;
+import gg.essential.universal.UResolution;
import org.lwjgl.input.Mouse;
/**
* Various utility methods for input.
*
* All values returned from this class are not scaled to Minecraft's GUI scale.
- * For scaled values, see {@link cc.polyfrost.oneconfig.libs.universal.UMouse}.
+ * For scaled values, see {@link gg.essential.universal.UMouse}.
*
*/
public final class InputUtils {
@@ -28,10 +28,10 @@ public final class InputUtils {
/**
* Checks whether the mouse is currently over a specific region and clicked.
*
- * @param x the x position of the region
- * @param y the y position of the region
- * @param width the width of the region
- * @param height the height of the region
+ * @param x the x position of the region
+ * @param y the y position of the region
+ * @param width the width of the region
+ * @param height the height of the region
* @param ignoreBlock if true, will ignore {@link InputUtils#blockClicks(boolean)}
* @return true if the mouse is clicked and is over the region, false if not
* @see InputUtils#isAreaHovered(int, int, int, int)
@@ -43,9 +43,9 @@ public final class InputUtils {
/**
* Checks whether the mouse is currently over a specific region and clicked.
*
- * @param x the x position of the region
- * @param y the y position of the region
- * @param width the width of the region
+ * @param x the x position of the region
+ * @param y the y position of the region
+ * @param width the width of the region
* @param height the height of the region
* @return true if the mouse is clicked and is over the region, false if not
* @see InputUtils#isAreaClicked(int, int, int, int, boolean)
@@ -78,7 +78,7 @@ public final class InputUtils {
* Gets the current mouse X position.
*
* All values returned from this class are not scaled to Minecraft's GUI scale.
- * For scaled values, see {@link cc.polyfrost.oneconfig.libs.universal.UMouse}.
+ * For scaled values, see {@link gg.essential.universal.UMouse}.
*
*
* @return the current mouse X position
@@ -92,7 +92,7 @@ public final class InputUtils {
* Gets the current mouse Y position.
*
* All values returned from this class are not scaled to Minecraft's GUI scale.
- * For scaled values, see {@link cc.polyfrost.oneconfig.libs.universal.UMouse}.
+ * For scaled values, see {@link gg.essential.universal.UMouse}.
*
*
* @return the current mouse Y position
@@ -111,6 +111,7 @@ public final class InputUtils {
/**
* Whether clicks are blocked
+ *
* @return true if clicks are blocked, false if not
*/
public static boolean isBlockingClicks() {
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java
index 67881e9..320b630 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java
@@ -16,7 +16,7 @@ public final class JsonUtils {
/**
* Parses a string into a {@link JsonElement}.
*
- * @param string The string to parse.
+ * @param string The string to parse.
* @param catchExceptions Whether to catch exceptions.
* @return The {@link JsonElement}.
* @see JsonParser#parse(String)
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java b/src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java
index 518d699..745ded8 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java
@@ -52,7 +52,7 @@ public class Multithreading {
* Schedules the runnable to run asynchronously after the specified delay.
*
* @param runnable The runnable to run.
- * @param delay The delay before the runnable is run.
+ * @param delay The delay before the runnable is run.
* @param timeUnit The {@link TimeUnit} of the delay.
* @see Multithreading#submitScheduled(Runnable, long, TimeUnit)
*/
@@ -64,7 +64,7 @@ public class Multithreading {
* Submits the Runnable to the executor after a delay, making it run asynchronously.
*
* @param runnable The runnable to run.
- * @param delay The delay before the runnable is run.
+ * @param delay The delay before the runnable is run.
* @param timeUnit The {@link TimeUnit} of the delay.
* @return The future representing the submitted runnable.
* @see ScheduledExecutorService#schedule(Runnable, long, TimeUnit)
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java
index 767f36f..5578e1e 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java
@@ -1,7 +1,7 @@
package cc.polyfrost.oneconfig.utils;
-import cc.polyfrost.oneconfig.libs.universal.UDesktop;
import com.google.gson.JsonElement;
+import gg.essential.universal.UDesktop;
import org.apache.commons.io.IOUtils;
import java.io.*;
@@ -18,9 +18,10 @@ public final class NetworkUtils {
/**
* Gets the contents of a URL as a String.
- * @param url The URL to read.
+ *
+ * @param url The URL to read.
* @param userAgent The user agent to use.
- * @param timeout The timeout in milliseconds.
+ * @param timeout The timeout in milliseconds.
* @param useCaches Whether to use caches.
* @return The contents of the URL.
*/
@@ -47,9 +48,9 @@ public final class NetworkUtils {
/**
* Gets the contents of a URL as a JsonElement.
*
- * @param url The URL to read.
+ * @param url The URL to read.
* @param userAgent The user agent to use.
- * @param timeout The timeout in milliseconds.
+ * @param timeout The timeout in milliseconds.
* @param useCaches Whether to use caches.
* @return The contents of the URL.
* @see NetworkUtils#getString(String, String, int, boolean)
@@ -72,10 +73,11 @@ public final class NetworkUtils {
/**
* Downloads a file from a URL.
- * @param url The URL to download from.
- * @param file The file to download to.
+ *
+ * @param url The URL to download from.
+ * @param file The file to download to.
* @param userAgent The user agent to use.
- * @param timeout The timeout in milliseconds.
+ * @param timeout The timeout in milliseconds.
* @param useCaches Whether to use caches.
* @return Whether the download was successful.
*/
@@ -92,7 +94,8 @@ public final class NetworkUtils {
/**
* Downloads a file from a URL.
- * @param url The URL to download from.
+ *
+ * @param url The URL to download from.
* @param file The file to download to.
* @return Whether the download was successful.
* @see NetworkUtils#downloadFile(String, File, String, int, boolean)
@@ -103,6 +106,7 @@ public final class NetworkUtils {
/**
* Gets the SHA-256 hash of a file.
+ *
* @param file The file to hash.
* @return The SHA-256 hash of the file.
*/
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java b/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java
index 6aaec37..dac995d 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java
@@ -1,8 +1,8 @@
package cc.polyfrost.oneconfig.utils;
-import cc.polyfrost.oneconfig.libs.universal.UMatrixStack;
-import cc.polyfrost.oneconfig.libs.universal.UScreen;
import cc.polyfrost.oneconfig.lwjgl.RenderManager;
+import gg.essential.universal.UMatrixStack;
+import gg.essential.universal.UScreen;
import net.minecraft.client.gui.GuiScreen;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.input.Mouse;
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java
index 4fa5125..df37aae 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java
@@ -13,11 +13,12 @@ public final class TextUtils {
/**
* Wraps a string into an array of lines.
- * @param vg The NanoVG context.
- * @param text The text to wrap.
+ *
+ * @param vg The NanoVG context.
+ * @param text The text to wrap.
* @param maxWidth The maximum width of each line.
* @param fontSize The font size.
- * @param font The font to use.
+ * @param font The font to use.
* @return The array of lines.
*/
public static ArrayList wrapText(long vg, String text, float maxWidth, float fontSize, Fonts font) {
@@ -26,11 +27,12 @@ public final class TextUtils {
/**
* Wraps a string into an array of lines.
- * @param vg The NanoVG context.
- * @param text The text to wrap.
+ *
+ * @param vg The NanoVG context.
+ * @param text The text to wrap.
* @param maxWidth The maximum width of each line.
* @param fontSize The font size.
- * @param font The font to use.
+ * @param font The font to use.
* @return The array of lines.
*/
public static ArrayList wrapText(long vg, String text, float maxWidth, float fontSize, Font font) {
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java b/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java
index 35b7b8b..7b6be2c 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java
@@ -3,14 +3,14 @@ package cc.polyfrost.oneconfig.utils;
import cc.polyfrost.oneconfig.events.EventManager;
import cc.polyfrost.oneconfig.events.event.Stage;
import cc.polyfrost.oneconfig.events.event.TickEvent;
-import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
+import me.kbrewster.eventbus.Subscribe;
/**
* Schedules a Runnable to be called after a certain amount of ticks.
*/
public class TickDelay {
- private int delay;
private final Runnable function;
+ private int delay;
public TickDelay(Runnable functionName, int ticks) {
EventManager.INSTANCE.register(this);
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandHelper.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandHelper.java
deleted file mode 100644
index 3e7b7ea..0000000
--- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandHelper.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package cc.polyfrost.oneconfig.utils.commands;
-
-/**
- * A helper class for commands.
- * Extend this class and run {@link CommandHelper#preload()} (which does nothing,
- * just makes loading look nicer lol)
- *
- * @see cc.polyfrost.oneconfig.utils.commands.annotations.Command
- */
-public abstract class CommandHelper {
-
- public CommandHelper() {
- CommandManager.INSTANCE.registerCommand(this);
- }
-
- public void preload() {
-
- }
-}
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandManager.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandManager.java
index 913ee73..32bbf5d 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandManager.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandManager.java
@@ -1,12 +1,14 @@
package cc.polyfrost.oneconfig.utils.commands;
-import cc.polyfrost.oneconfig.libs.universal.ChatColor;
-import cc.polyfrost.oneconfig.libs.universal.UChat;
-import cc.polyfrost.oneconfig.utils.commands.annotations.*;
+import cc.polyfrost.oneconfig.utils.commands.annotations.Command;
+import cc.polyfrost.oneconfig.utils.commands.annotations.Greedy;
+import cc.polyfrost.oneconfig.utils.commands.annotations.Main;
+import cc.polyfrost.oneconfig.utils.commands.annotations.SubCommand;
import cc.polyfrost.oneconfig.utils.commands.arguments.*;
+import gg.essential.universal.ChatColor;
+import gg.essential.universal.UChat;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
-import net.minecraft.util.BlockPos;
import net.minecraftforge.client.ClientCommandHandler;
import java.lang.reflect.InvocationTargetException;
@@ -14,7 +16,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.*;
-import java.util.stream.Collectors;
/**
* Handles the registration of OneConfig commands.
@@ -23,9 +24,9 @@ import java.util.stream.Collectors;
*/
public class CommandManager {
public static final CommandManager INSTANCE = new CommandManager();
- private final HashMap, ArgumentParser>> parsers = new HashMap<>();
private static final String NOT_FOUND_TEXT = "Command not found! Type /@ROOT_COMMAND@ help for help.";
private static final String METHOD_RUN_ERROR = "Error while running @ROOT_COMMAND@ method! Please report this to the developer.";
+ private final HashMap, ArgumentParser>> parsers = new HashMap<>();
private CommandManager() {
addParser(new StringParser());
@@ -61,22 +62,19 @@ public class CommandManager {
/**
* Registers the provided command.
*
- * @param command The command to register.
+ * @param clazz The command to register as a class.
*/
- public void registerCommand(Object command) {
- Class> clazz = command.getClass();
+ public void registerCommand(Class> clazz) {
if (clazz.isAnnotationPresent(Command.class)) {
final Command annotation = clazz.getAnnotation(Command.class);
- ArrayList mainCommandFuncs = new ArrayList<>();
+ final InternalCommand root = new InternalCommand(annotation.value(), annotation.aliases(), annotation.description().trim().isEmpty() ? "Main command for " + annotation.value() : annotation.description(), null);
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(Main.class) && method.getParameterCount() == 0) {
- mainCommandFuncs.add(new InternalCommand.InternalCommandInvoker(annotation.value(), annotation.aliases(), method));
+ root.invokers.add(new InternalCommand.InternalCommandInvoker(annotation.value(), annotation.aliases(), method, root));
break;
}
}
-
- final InternalCommand root = new InternalCommand(annotation.value(), annotation.aliases(), annotation.description().trim().isEmpty() ? "Main command for " + annotation.value() : annotation.description(), mainCommandFuncs);
addToInvokers(clazz.getDeclaredClasses(), root);
ClientCommandHandler.instance.registerCommand(new CommandBase() {
@Override
@@ -94,26 +92,57 @@ public class CommandManager {
if (args.length == 0) {
if (!root.invokers.isEmpty()) {
try {
- root.invokers.stream().findFirst().get().method.invoke(null);
+ root.invokers.get(0).method.invoke(null);
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException |
ExceptionInInitializerError e) {
+ e.printStackTrace();
UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + METHOD_RUN_ERROR);
}
}
} else {
if (annotation.helpCommand() && args[0].equalsIgnoreCase("help")) {
- UChat.chat(sendHelpCommand(root));
+ //UChat.chat(sendHelpCommand(root));
} else {
+ List commands = new ArrayList<>();
+ int depth = 0;
for (InternalCommand command : root.children) {
- String result = runThroughCommands(command, 0, args);
- if (result == null) {
- return;
- } else if (!result.equals(NOT_FOUND_TEXT)) {
- UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + result.replace("@ROOT_COMMAND@", getCommandName()));
- return;
+ int newDepth = loopThroughCommands(commands, 0, command, args, true);
+ if (newDepth != -1) {
+ depth = newDepth;
+ break;
+ }
+ }
+ System.out.println(depth);
+ System.out.println(commands);
+ if (commands.isEmpty()) {
+ UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + NOT_FOUND_TEXT.replace("@ROOT_COMMAND@", annotation.value()));
+ } else {
+ List errors = new ArrayList<>();
+ for (InternalCommand.InternalCommandInvoker invoker : commands) {
+ try {
+ List