aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-06-05 15:27:36 +0700
committerGitHub <noreply@github.com>2022-06-05 10:27:36 +0200
commit44dfbbb419f1736530c04c02a651f7757cf83f3d (patch)
tree1e8171573680b9415cecf199d479d49f7ad1f48a /src/main/java/cc/polyfrost/oneconfig/utils
parent494d4f0bd0856e8e8d373003c82729ca722c6ccf (diff)
downloadOneConfig-44dfbbb419f1736530c04c02a651f7757cf83f3d.tar.gz
OneConfig-44dfbbb419f1736530c04c02a651f7757cf83f3d.tar.bz2
OneConfig-44dfbbb419f1736530c04c02a651f7757cf83f3d.zip
rewrite command manager, stop using essential relocate, and reformat code (#34)
* reformat code * reformat code rewrite command manager stop using essential relocate
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java19
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java23
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java2
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java4
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java22
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/OneUIScreen.java4
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java14
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java4
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandHelper.java19
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/CommandManager.java289
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java6
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Main.java1
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Name.java1
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/SubCommand.java3
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java30
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java2
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java20
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java6
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java6
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java6
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java13
21 files changed, 298 insertions, 196 deletions
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.
* <p><b>
- * 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}
* </b></p>
*
* @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.
* <p>
* 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}.
* </p>
*/
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.
* <p>
* 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}.
* </p>
*
* @return the current mouse X position
@@ -92,7 +92,7 @@ public final class InputUtils {
* Gets the current mouse Y position.
* <p>
* 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}.
* </p>
*
* @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<String> 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<String> 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<Class<?>, 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<Class<?>, 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<InternalCommand.InternalCommandInvoker> 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<InternalCommand.InternalCommandInvoker> 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<CustomError> errors = new ArrayList<>();
+ for (InternalCommand.InternalCommandInvoker invoker : commands) {
+ try {
+ List<Object> params = getParametersForInvoker(invoker, depth, args);
+ if (params.size() == 1) {
+ Object first = params.get(0);
+ if (first instanceof CustomError) {
+ errors.add((CustomError) first);
+ continue;
+ }
+ }
+ invoker.method.invoke(null, params.toArray());
+ return;
+ } catch (Exception e) {
+ e.printStackTrace();
+ UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + METHOD_RUN_ERROR);
+ return;
+ }
+ }
+ if (!errors.isEmpty()) {
+ UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + "Multiple errors occurred:");
+ for (CustomError error : errors) {
+ UChat.chat(" " + ChatColor.RED + ChatColor.BOLD + error.message);
+ }
}
}
- UChat.chat(ChatColor.RED.toString() + ChatColor.BOLD + NOT_FOUND_TEXT.replace("@ROOT_COMMAND@", getCommandName()));
}
}
}
@@ -122,146 +151,142 @@ public class CommandManager {
public int getRequiredPermissionLevel() {
return -1;
}
- });
- }
- }
- private String sendHelpCommand(InternalCommand root) {
- StringBuilder builder = new StringBuilder();
- builder.append(ChatColor.GOLD.toString() + "Help for " + ChatColor.BOLD + root.name + ChatColor.RESET + ChatColor.GOLD + ":\n");
- builder.append("\n");
- for (InternalCommand command : root.children) {
- runThroughCommandsHelp(root.name, root, builder);
- }
- builder.append("\n" + ChatColor.GOLD + "Aliases: " + ChatColor.BOLD);
- int index = 0;
- for (String alias : root.aliases) {
- ++index;
- builder.append(alias + (index < root.aliases.length ? ", " : ""));
- }
- builder.append("\n");
- return builder.toString();
- }
+ /*/
+ @Override
+ public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) {
+ List<InternalCommand.InternalCommandInvoker> commands = new ArrayList<>();
+ int depth = 0;
+ for (InternalCommand command : root.children) {
+ int newDepth = loopThroughCommands(commands, 0, command, args, false);
+ if (newDepth != -1) {
+ depth = newDepth;
+ break;
+ }
+ }
+ System.out.println(depth);
+ System.out.println(commands);
+ if (!commands.isEmpty()) {
+ for (InternalCommand.InternalCommandInvoker invoker : commands) {
+ try {
+ List<Object> params = getParametersForInvoker(invoker, depth, args);
+ invoker.method.invoke(null, params.toArray());
+ return;
+ } catch (Exception ignored) {
- private void runThroughCommandsHelp(String append, InternalCommand command, StringBuilder builder) {
- for (InternalCommand.InternalCommandInvoker invoker : command.invokers) {
- builder.append("\n" + ChatColor.GOLD + "/" + append + " " + command.name);
- for (Parameter parameter : invoker.method.getParameters()) {
- String name = parameter.getName();
- if (parameter.isAnnotationPresent(Name.class)) {
- name = parameter.getAnnotation(Name.class).value();
+ }
+ }
+ }
}
- builder.append(" <" + name + ">");
- }
- if (!command.description.trim().isEmpty()) {
- builder.append(": " + ChatColor.BOLD + command.description);
- }
- }
- for (InternalCommand subCommand : command.children) {
- runThroughCommandsHelp(append + " " + command.name, subCommand, builder);
+
+ */
+ });
}
}
- private String runThroughCommands(InternalCommand command, int layer, String[] args) {
- int newLayer = layer + 1;
- if (command.isEqual(args[layer]) && !command.invokers.isEmpty()) {
- Set<InternalCommand.InternalCommandInvoker> invokers = command.invokers.stream().filter(invoker -> newLayer == args.length - invoker.parameterTypes.length).sorted(Comparator.comparingInt((a) -> a.method.getAnnotation(Main.class).priority())).collect(Collectors.toSet());
- if (!invokers.isEmpty()) {
- for (InternalCommand.InternalCommandInvoker invoker : invokers) {
- try {
- String a = tryInvoker(invoker, newLayer, args);
- if (a == null) {
- return null;
- } else if (a.contains(METHOD_RUN_ERROR)) {
- return a;
- }
- } catch (Exception ignored) {
-
- }
- }
- } else {
- for (InternalCommand subCommand : command.children) {
- String result = runThroughCommands(subCommand, newLayer, args);
- if (result == null) {
- return null;
- } else if (!result.equals(NOT_FOUND_TEXT)) {
- return result;
+ private List<Object> getParametersForInvoker(InternalCommand.InternalCommandInvoker invoker, int depth, String[] args) {
+ List<Object> parameters = new ArrayList<>();
+ int processed = depth;
+ int currentParam = 0;
+ while (processed < args.length) {
+ Parameter param = invoker.method.getParameters()[currentParam];
+ if (param.isAnnotationPresent(Greedy.class) && currentParam + 1 != invoker.method.getParameterCount()) {
+ return Collections.singletonList(new CustomError("Parsing failed: Greedy parameter must be the last one."));
+ }
+ ArgumentParser<?> parser = parsers.get(param.getType());
+ if (parser == null) {
+ return Collections.singletonList(new CustomError("No parser for " + invoker.method.getParameterTypes()[currentParam].getSimpleName() + "! Please report this to the mod author."));
+ }
+ try {
+ Arguments arguments = new Arguments(Arrays.copyOfRange(args, processed, args.length), param.isAnnotationPresent(Greedy.class));
+ try {
+ Object a = parser.parse(arguments);
+ if (a != null) {
+ parameters.add(a);
+ processed += arguments.getPosition();
+ currentParam++;
+ } else {
+ return Collections.singletonList(new CustomError("Failed to parse " + param.getType().getSimpleName() + "! Please report this to the mod author."));
}
+ } catch (Exception e) {
+ return Collections.singletonList(new CustomError("A " + e.getClass().getSimpleName() + " has occured while try to parse " + param.getType().getSimpleName() + "! Please report this to the mod author."));
}
+ } catch (Exception e) {
+ return Collections.singletonList(new CustomError("A " + e.getClass().getSimpleName() + " has occured while try to parse " + param.getType().getSimpleName() + "! Please report this to the mod author."));
}
}
- return NOT_FOUND_TEXT;
+ return parameters;
}
- private String tryInvoker(InternalCommand.InternalCommandInvoker invoker, int newLayer, String[] args) {
- try {
- ArrayList<Object> params = new ArrayList<>();
- int processed = newLayer;
- int currentParam = 0;
- while (processed < args.length) {
- Parameter param = invoker.method.getParameters()[currentParam];
- if (param.isAnnotationPresent(Greedy.class) && currentParam + 1 != invoker.method.getParameterCount()) {
- return "Parsing failed: Greedy parameter must be the last one.";
+ private int loopThroughCommands(List<InternalCommand.InternalCommandInvoker> commands, int