diff options
author | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-06-05 10:39:49 +0100 |
---|---|---|
committer | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-06-05 10:39:49 +0100 |
commit | 17826161c67f6adf5976d7993ac0a229193e2eb6 (patch) | |
tree | f4172013d2db0986f0bb123040be1dc5b5823b3d /src/main/java/cc/polyfrost/oneconfig/utils | |
parent | 087f5404658a1543834f16a89e3436f8399297f6 (diff) | |
parent | 72c024213e7c61411ce12f6032f8ef0659408c3a (diff) | |
download | OneConfig-17826161c67f6adf5976d7993ac0a229193e2eb6.tar.gz OneConfig-17826161c67f6adf5976d7993ac0a229193e2eb6.tar.bz2 OneConfig-17826161c67f6adf5976d7993ac0a229193e2eb6.zip |
Merge remote-tracking branch 'origin/master'
# Conflicts:
# src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java
# src/main/java/cc/polyfrost/oneconfig/gui/animations/EaseInOutQuad.java
# src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java
# src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java
# src/main/java/cc/polyfrost/oneconfig/gui/elements/ModCard.java
# src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java
# src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java
# src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java
# src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java
# src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils')
15 files changed, 250 insertions, 167 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java index fa2a6ea..29c26fb 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/GuiUtils.java @@ -3,22 +3,22 @@ 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. * diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java index 0f50a33..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 { @@ -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 diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java index aea7812..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.*; 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/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 4a008d3..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,9 +1,12 @@ 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.minecraftforge.client.ClientCommandHandler; @@ -13,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. @@ -22,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()); @@ -60,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 @@ -93,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())); } } } @@ -121,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 depth, InternalCommand command, String[] args, boolean checkParams) { + int nextDepth = depth + 1; + if (command.isEqual(args[depth])) { + for (InternalCommand child : command.children) { + if (args.length > nextDepth && child.isEqual(args[nextDepth])) { + int result = loopThroughCommands(commands, nextDepth, child, args, checkParams); + if (result != -1) { + return result; + } } - ArgumentParser<?> parser = parsers.get(param.getType()); - if (parser == null) { - return "No parser for " + invoker.method.getParameterTypes()[currentParam].getSimpleName() + "! Please report this to the mod author."; + } + boolean added = false; + for (InternalCommand.InternalCommandInvoker invoker : command.invokers) { + if (!checkParams || args.length - nextDepth == invoker.method.getParameterCount()) { + commands.add(invoker); + added = true; } - try { - Arguments arguments = new Arguments(Arrays.copyOfRange(args, processed, args.length), param.isAnnotationPresent(Greedy.class)); - try { - Object a = parser.parse(arguments); - if (a != null) { - params.add(a); - processed += arguments.getPosition(); - currentParam++; - } else { - return "Failed to parse " + param.getType().getSimpleName() + "! Please report this to the mod author."; - } - } catch (Exception e) { - return "A " + e.getClass().getSimpleName() + " has occured while try to parse " + param.getType().getSimpleName() + "! Please report this to the mod author."; - } - } catch (Exception e) { - return "A " + e.getClass().getSimpleName() + " has occured while try to parse " + param.getType().getSimpleName() + "! Please report this to the mod author."; + } + if (added) { + return nextDepth; + } + } else { + for (InternalCommand child : command.children) { + int childDepth = loopThroughCommands(commands, nextDepth, child, args, checkParams); + if (childDepth != -1) { + return childDepth; } } - invoker.method.invoke(null, params.toArray()); - return null; - } catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException | - ExceptionInInitializerError e) { - return ChatColor.RED.toString() + ChatColor.BOLD + METHOD_RUN_ERROR; } + return -1; } private void addToInvokers(Class<?>[] classes, InternalCommand parent) { for (Class<?> clazz : classes) { if (clazz.isAnnotationPresent(SubCommand.class)) { SubCommand annotation = clazz.getAnnotation(SubCommand.class); - ArrayList<InternalCommand.InternalCommandInvoker> mainMethods = new ArrayList<>(); + InternalCommand command = new InternalCommand(annotation.value(), annotation.aliases(), annotation.description(), parent); for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(Main.class)) { - mainMethods.add(new InternalCommand.InternalCommandInvoker(annotation.value(), annotation.aliases(), method)); + command.invokers.add(new InternalCommand.InternalCommandInvoker(annotation.value(), annotation.aliases(), method, command)); } } - InternalCommand command = new InternalCommand(annotation.value(), annotation.aliases(), annotation.description(), mainMethods); parent.children.add(command); addToInvokers(clazz.getDeclaredClasses(), command); } } } + private static class CustomError { + public String message; + + public CustomError(String message) { + this.message = message; + } + } + private static class InternalCommand { public final String name; public final String[] aliases; public final String description; - public final ArrayList<InternalCommandInvoker> invokers; + public final ArrayList<InternalCommandInvoker> invokers = new ArrayList<>(); + public final InternalCommand parent; public final ArrayList<InternalCommand> children = new ArrayList<>(); - public InternalCommand(String name, String[] aliases, String description, ArrayList<InternalCommandInvoker> invokers) { + public InternalCommand(String name, String[] aliases, String description, InternalCommand parent) { this.name = name; this.aliases = aliases; - this.invokers = invokers; this.description = description; + this.parent = parent; } public boolean isEqual(String name) { @@ -276,13 +302,24 @@ public class CommandManager { return false; } + @Override + public String toString() { + return "InternalCommand{" + + "name='" + name + '\'' + + ", aliases=" + Arrays.toString(aliases) + + ", description='" + description + '\'' + + ", invokers=" + invokers + + '}'; + } + public static class InternalCommandInvoker { public final String name; public final String[] aliases; public final Method method; public final Parameter[] parameterTypes; + public final InternalCommand parent; - public InternalCommandInvoker(String name, String[] aliases, Method method) { + public InternalCommandInvoker(String name, String[] aliases, Method method, InternalCommand parent) { if (!Modifier.isStatic(method.getModifiers())) { throw new IllegalArgumentException("All command methods must be static!"); } @@ -290,10 +327,21 @@ public class CommandManager { this.aliases = aliases; this.method = method; this.parameterTypes = method.getParameters().clone(); + this.parent = parent; if (Modifier.isPrivate(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) { method.setAccessible(true); } } + + @Override + public String toString() { + return "InternalCommandInvoker{" + + "name='" + name + '\'' + + ", aliases=" + Arrays.toString(aliases) + + ", method=" + method + + ", parameterTypes=" + Arrays.toString(parameterTypes) + + '}'; + } } } } 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 index 89b5227..deec7f1 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/annotations/Command.java @@ -1,6 +1,5 @@ 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; @@ -73,8 +72,7 @@ import java.lang.annotation.Target; * }</pre> * </p> * <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)}. + * To register commands, use {@link CommandManager#registerCommand(Class)}. * * <p> * Note: if you're viewing this in IntelliJ or just see the @literal tag everywhere, please ignore that. 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 index 61d1e07..8bf811b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/ArgumentParser.java @@ -3,12 +3,36 @@ package cc.polyfrost.oneconfig.utils.commands.arguments; import com.google.common.reflect.TypeToken; import org.jetbrains.annotations.Nullable; -@SuppressWarnings("unstable") +import java.lang.reflect.Parameter; +import java.util.Collections; +import java.util.List; + +@SuppressWarnings("UnstableApiUsage") public abstract class ArgumentParser<T> { private final TypeToken<T> type = new TypeToken<T>(getClass()) { }; public final Class<?> typeClass = type.getRawType(); + /** + * Parses the given string into an object of the type specified by this parser. + * Should return null if the string cannot be parsed. + * + * @param arguments The string to parse. + * @return The parsed object, or null if the string cannot be parsed. + */ @Nullable public abstract T parse(Arguments arguments); + + /** + * Returns possible completions for the given arguments. + * Should return an empty list or null if no completions are possible. + * + * @param arguments The arguments to complete. + * @param parameter The parameter to complete. + * @return A list of possible completions, or an empty list or null if no completions are possible. + */ + @Nullable + public List<String> complete(Arguments arguments, Parameter parameter) { + return Collections.emptyList(); + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java index 74a0840..f35cf46 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/Arguments.java @@ -1,9 +1,9 @@ package cc.polyfrost.oneconfig.utils.commands.arguments; public class Arguments { - private int position = 0; public final String[] args; public final boolean greedy; + private int position = 0; public Arguments(String[] args, boolean greedy) { this.args = args; diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java index dfdca2d..7411cbe 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/BooleanParser.java @@ -1,11 +1,31 @@ package cc.polyfrost.oneconfig.utils.commands.arguments; +import com.google.common.collect.Lists; import org.jetbrains.annotations.Nullable; +import java.lang.reflect.Parameter; +import java.util.List; +import java.util.Locale; + public class BooleanParser extends ArgumentParser<Boolean> { + private static final List<String> VALUES = Lists.newArrayList("true", "false"); + @Override public @Nullable Boolean parse(Arguments arguments) { return Boolean.parseBoolean(arguments.poll()); } + + @Override + public @Nullable List<String> complete(Arguments arguments, Parameter parameter) { + String value = arguments.poll(); + if (value != null && !value.trim().isEmpty()) { + for (String v : VALUES) { + if (v.startsWith(value.toLowerCase(Locale.ENGLISH))) { + return Lists.newArrayList(v); + } + } + } + return VALUES; + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java index 8c85849..4379e3a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/DoubleParser.java @@ -5,6 +5,10 @@ import org.jetbrains.annotations.Nullable; public class DoubleParser extends ArgumentParser<Double> { @Override public @Nullable Double parse(Arguments arguments) { - return Double.parseDouble(arguments.poll()); + try { + return Double.parseDouble(arguments.poll()); + } catch (NumberFormatException e) { + return null; + } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java index 7053fcb..08e0a45 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/FloatParser.java @@ -6,6 +6,10 @@ public class FloatParser extends ArgumentParser<Float> { @Override public @Nullable Float parse(Arguments arguments) { - return Float.parseFloat(arguments.poll()); + try { + return Float.parseFloat(arguments.poll()); + } catch (NumberFormatException e) { + return null; + } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java index 6910d4b..89f4eca 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/commands/arguments/IntegerParser.java @@ -3,6 +3,10 @@ package cc.polyfrost.oneconfig.utils.commands.arguments; public class IntegerParser extends ArgumentParser<Integer> { @Override public Integer parse(Arguments arguments) { - return Integer.parseInt(arguments.poll()); + try { + return Integer.parseInt(arguments.poll()); + } catch (NumberFormatException e) { + return null; + } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java index c890939..a952053 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java @@ -2,17 +2,17 @@ package cc.polyfrost.oneconfig.utils.hypixel; import cc.polyfrost.oneconfig.events.EventManager; import cc.polyfrost.oneconfig.events.event.*; -import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; -import cc.polyfrost.oneconfig.libs.universal.UChat; -import cc.polyfrost.oneconfig.libs.universal.UMinecraft; -import cc.polyfrost.oneconfig.libs.universal.wrappers.UPlayer; -import cc.polyfrost.oneconfig.libs.universal.wrappers.message.UTextComponent; import cc.polyfrost.oneconfig.utils.JsonUtils; import cc.polyfrost.oneconfig.utils.Multithreading; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import gg.essential.universal.UChat; +import gg.essential.universal.UMinecraft; +import gg.essential.universal.wrappers.UPlayer; +import gg.essential.universal.wrappers.message.UTextComponent; +import me.kbrewster.eventbus.Subscribe; import java.util.Locale; import java.util.concurrent.TimeUnit; |