From 219edc9e4e558431cee75b1563c1752c2fc9d40e Mon Sep 17 00:00:00 2001 From: Reflxction Dev Date: Mon, 6 Feb 2023 19:52:47 +0300 Subject: [PATCH] Fix @DefaultFor paths getting incorrectly combined with @Command and @Subcommand --- .../revxrsal/commands/core/CommandParser.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/revxrsal/commands/core/CommandParser.java b/common/src/main/java/revxrsal/commands/core/CommandParser.java index 13bb1a32..51aa003c 100644 --- a/common/src/main/java/revxrsal/commands/core/CommandParser.java +++ b/common/src/main/java/revxrsal/commands/core/CommandParser.java @@ -109,7 +109,6 @@ public static void parse(@NotNull BaseCommandHandler handler, @NotNull Object bo @SneakyThrows public static void parse(@NotNull BaseCommandHandler handler, @NotNull Class container, @NotNull Object boundTarget) { Map categories = handler.categories; - Map subactions = new HashMap<>(); for (Method method : getAllMethods(container)) { /* Parse annotations on a method */ AnnotationReader reader = AnnotationReader.create(handler, method); @@ -139,11 +138,11 @@ public static void parse(@NotNull BaseCommandHandler handler, @NotNull Class int id = COMMAND_ID.getAndIncrement(); /* Check if the command is default, and if so, generate a path for it */ - boolean isDefault = reader.contains(Default.class) || reader.contains(DefaultFor.class); String[] defPaths = reader.get(DefaultFor.class, DefaultFor::value); List defaultPaths = defPaths == null ? emptyList() : Arrays.stream(defPaths) .map(CommandPath::parse) .collect(Collectors.toList()); + boolean isDefault = reader.contains(Default.class) || !defaultPaths.isEmpty(); /* Generate categories for default paths if not created already */ for (CommandPath defaultPath : defaultPaths) { @@ -159,7 +158,7 @@ public static void parse(@NotNull BaseCommandHandler handler, @NotNull Class categories.putIfAbsent(category.path, category); } - List defaultPathsAndNormalPath = new ArrayList<>(); + Set defaultPathsAndNormalPath = new HashSet<>(); defaultPathsAndNormalPath.add(path); defaultPathsAndNormalPath.addAll(defaultPaths); for (CommandPath p : defaultPathsAndNormalPath) { @@ -186,20 +185,12 @@ public static void parse(@NotNull BaseCommandHandler handler, @NotNull Class .filter(c -> c.getCommandIndex() != -1) .collect(toMap(CommandParameter::getCommandIndex, c -> c)); executable.usage = reader.get(Usage.class, Usage::value, () -> generateUsage(executable)); - if (registerAsDefault) - subactions.put(p, executable); - else + if (!registerAsDefault) { putOrError(handler.executables, p, executable, "A command with path '" + p.toRealString() + "' already exists!"); + } } }); } - - subactions.forEach((path, subaction) -> { - BaseCommandCategory cat = categories.get(path); - if (cat != null) { // should never be null but let's just do that - cat.defaultAction = subaction; - } - }); } /** @@ -441,6 +432,12 @@ private static List getCommandPath(@NotNull Class container, @NotNull AnnotationReader reader) { List paths = new ArrayList<>(); + DefaultFor defaultFor = reader.get(DefaultFor.class); + if (defaultFor != null) { + return Arrays.stream(defaultFor.value()).map(CommandPath::parse) + .collect(Collectors.toList()); + } + List commands = new ArrayList<>(); List subcommands = new ArrayList<>(); Command commandAnnotation = reader.get(Command.class, "Method " + method.getName() + " does not have a parent command! You might have forgotten one of the following:\n" +