Skip to content

Commit

Permalink
Expose command handler in CommandCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Jan 23, 2022
1 parent 3082d6d commit 5062f0c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import revxrsal.commands.CommandHandler;
import revxrsal.commands.annotation.Default;
import revxrsal.commands.core.CommandPath;

Expand Down Expand Up @@ -34,6 +35,13 @@ public interface CommandCategory {
*/
@NotNull CommandPath getPath();

/**
* Returns the command handler that instantiated this category
*
* @return The owning command handler
*/
@NotNull CommandHandler getCommandHandler();

/**
* Returns the parent category of this category. This can be null
* in case of root categories.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import revxrsal.commands.CommandHandler;
import revxrsal.commands.command.CommandCategory;
import revxrsal.commands.command.ExecutableCommand;

Expand All @@ -17,6 +18,7 @@ final class BaseCommandCategory implements CommandCategory {
String name;
@Nullable BaseCommandCategory parent;
@Nullable CommandExecutable defaultAction;
CommandHandler handler;

final Map<CommandPath, ExecutableCommand> commands = new HashMap<>();
final Map<CommandPath, BaseCommandCategory> categories = new HashMap<>();
Expand All @@ -29,6 +31,10 @@ final class BaseCommandCategory implements CommandCategory {
return path;
}

@Override public @NotNull CommandHandler getCommandHandler() {
return handler;
}

@Override public @Nullable CommandCategory getParent() {
return parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void parse(@NotNull BaseCommandHandler handler, @NotNull Class<?>
int id = COMMAND_ID.getAndIncrement();
boolean isDefault = reader.contains(Default.class);
paths.forEach(path -> {
for (BaseCommandCategory category : getCategories(isDefault, path)) {
for (BaseCommandCategory category : getCategories(handler, isDefault, path)) {
categories.putIfAbsent(category.path, category);
}
CommandExecutable executable = new CommandExecutable();
Expand Down Expand Up @@ -147,12 +147,13 @@ private static Type getInsideGeneric(Type genericType) {
}
}

private static Set<BaseCommandCategory> getCategories(boolean respectDefault, @NotNull CommandPath path) {
private static Set<BaseCommandCategory> getCategories(CommandHandler handler, boolean respectDefault, @NotNull CommandPath path) {
if (path.size() == 1 && !respectDefault) return Collections.emptySet();
String parent = path.getParent();
Set<BaseCommandCategory> categories = new HashSet<>();

BaseCommandCategory root = new BaseCommandCategory();
root.handler = handler;
root.path = CommandPath.get(parent);
root.name = parent;
categories.add(root);
Expand All @@ -163,6 +164,7 @@ private static Set<BaseCommandCategory> getCategories(boolean respectDefault, @N
for (String subcommand : path.getSubcommandPath()) {
pathList.add(subcommand);
BaseCommandCategory cat = new BaseCommandCategory();
cat.handler = handler;
cat.path = CommandPath.get(pathList);
cat.name = cat.path.getName();
categories.add(cat);
Expand Down

0 comments on commit 5062f0c

Please sign in to comment.