Skip to content

Commit

Permalink
Fix @DefaultFor paths getting incorrectly combined with @command and @…
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Feb 6, 2023
1 parent b6c3111 commit 219edc9
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions common/src/main/java/revxrsal/commands/core/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandPath, BaseCommandCategory> categories = handler.categories;
Map<CommandPath, CommandExecutable> subactions = new HashMap<>();
for (Method method : getAllMethods(container)) {
/* Parse annotations on a method */
AnnotationReader reader = AnnotationReader.create(handler, method);
Expand Down Expand Up @@ -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<CommandPath> 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) {
Expand All @@ -159,7 +158,7 @@ public static void parse(@NotNull BaseCommandHandler handler, @NotNull Class<?>
categories.putIfAbsent(category.path, category);
}

List<CommandPath> defaultPathsAndNormalPath = new ArrayList<>();
Set<CommandPath> defaultPathsAndNormalPath = new HashSet<>();
defaultPathsAndNormalPath.add(path);
defaultPathsAndNormalPath.addAll(defaultPaths);
for (CommandPath p : defaultPathsAndNormalPath) {
Expand All @@ -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;
}
});
}

/**
Expand Down Expand Up @@ -441,6 +432,12 @@ private static List<CommandPath> getCommandPath(@NotNull Class<?> container,
@NotNull AnnotationReader reader) {
List<CommandPath> paths = new ArrayList<>();

DefaultFor defaultFor = reader.get(DefaultFor.class);
if (defaultFor != null) {
return Arrays.stream(defaultFor.value()).map(CommandPath::parse)
.collect(Collectors.toList());
}

List<String> commands = new ArrayList<>();
List<String> 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" +
Expand Down

0 comments on commit 219edc9

Please sign in to comment.