Skip to content

Commit 5062f0c

Browse files
committed
Expose command handler in CommandCategory
1 parent 3082d6d commit 5062f0c

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

common/src/main/java/revxrsal/commands/command/CommandCategory.java

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.jetbrains.annotations.NotNull;
44
import org.jetbrains.annotations.Nullable;
55
import org.jetbrains.annotations.UnmodifiableView;
6+
import revxrsal.commands.CommandHandler;
67
import revxrsal.commands.annotation.Default;
78
import revxrsal.commands.core.CommandPath;
89

@@ -34,6 +35,13 @@ public interface CommandCategory {
3435
*/
3536
@NotNull CommandPath getPath();
3637

38+
/**
39+
* Returns the command handler that instantiated this category
40+
*
41+
* @return The owning command handler
42+
*/
43+
@NotNull CommandHandler getCommandHandler();
44+
3745
/**
3846
* Returns the parent category of this category. This can be null
3947
* in case of root categories.

common/src/main/java/revxrsal/commands/core/BaseCommandCategory.java

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.jetbrains.annotations.NotNull;
44
import org.jetbrains.annotations.Nullable;
55
import org.jetbrains.annotations.UnmodifiableView;
6+
import revxrsal.commands.CommandHandler;
67
import revxrsal.commands.command.CommandCategory;
78
import revxrsal.commands.command.ExecutableCommand;
89

@@ -17,6 +18,7 @@ final class BaseCommandCategory implements CommandCategory {
1718
String name;
1819
@Nullable BaseCommandCategory parent;
1920
@Nullable CommandExecutable defaultAction;
21+
CommandHandler handler;
2022

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

34+
@Override public @NotNull CommandHandler getCommandHandler() {
35+
return handler;
36+
}
37+
3238
@Override public @Nullable CommandCategory getParent() {
3339
return parent;
3440
}

common/src/main/java/revxrsal/commands/core/CommandParser.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void parse(@NotNull BaseCommandHandler handler, @NotNull Class<?>
5353
int id = COMMAND_ID.getAndIncrement();
5454
boolean isDefault = reader.contains(Default.class);
5555
paths.forEach(path -> {
56-
for (BaseCommandCategory category : getCategories(isDefault, path)) {
56+
for (BaseCommandCategory category : getCategories(handler, isDefault, path)) {
5757
categories.putIfAbsent(category.path, category);
5858
}
5959
CommandExecutable executable = new CommandExecutable();
@@ -147,12 +147,13 @@ private static Type getInsideGeneric(Type genericType) {
147147
}
148148
}
149149

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

155155
BaseCommandCategory root = new BaseCommandCategory();
156+
root.handler = handler;
156157
root.path = CommandPath.get(parent);
157158
root.name = parent;
158159
categories.add(root);
@@ -163,6 +164,7 @@ private static Set<BaseCommandCategory> getCategories(boolean respectDefault, @N
163164
for (String subcommand : path.getSubcommandPath()) {
164165
pathList.add(subcommand);
165166
BaseCommandCategory cat = new BaseCommandCategory();
167+
cat.handler = handler;
166168
cat.path = CommandPath.get(pathList);
167169
cat.name = cat.path.getName();
168170
categories.add(cat);

0 commit comments

Comments
 (0)