diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2.java b/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2.java index d27c571..a60c3af 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2.java @@ -28,11 +28,13 @@ import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; import static buttondevteam.lib.chat.CoreCommandBuilder.literal; +import static buttondevteam.lib.chat.CoreCommandBuilder.literalNoOp; /** * The method name is the subcommand, use underlines (_) to add further subcommands. @@ -242,7 +244,7 @@ public abstract class Command2, TP extends Command2Send * @return The processed command node * @throws Exception Something broke */ - protected LiteralCommandNode processSubcommand(TC command, Method method) throws Exception { + protected LiteralCommandNode processSubcommand(TC command, Method method, Subcommand subcommand) throws Exception { val params = new ArrayList(method.getParameterCount()); Class[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 0) @@ -254,8 +256,8 @@ public abstract class Command2, TP extends Command2Send var pdata = getParameterData(method, i1); arguments.put(pdata.name, new CommandArgument(pdata.name, cl, pdata.description)); } - var sd = new SubcommandData(parameterTypes[0], arguments, command, command.getHelpText(method, method.getAnnotation(Subcommand.class)), null); // TODO: Help text - return getSubcommandNode(method, sd); // TODO: Integrate with getCommandNode and store SubcommandData instead of help text + // TODO: Dynamic help text + return getSubcommandNode(method, parameterTypes[0], command).helps(command.getHelpText(method, subcommand)).build(); } /** @@ -291,27 +293,28 @@ public abstract class Command2, TP extends Command2Send var path = command.getCommandPath().split(" "); if (path.length == 0) throw new IllegalArgumentException("Attempted to register a command with no command path!"); - CoreCommandBuilder inner = literal(path[0]); + CoreCommandBuilder inner = literalNoOp(path[0]); var outer = inner; for (int i = path.length - 1; i >= 0; i--) { - CoreCommandBuilder literal = literal(path[i]); - outer = (CoreCommandBuilder) literal.executes(this::executeHelpText).then(outer); + // TODO: This looks like it will duplicate the first node + CoreCommandBuilder literal = literalNoOp(path[i]); + outer = (CoreCommandBuilder) literal.executes(this::executeHelpText).then(outer); } var subcommandMethods = command.getClass().getMethods(); for (var subcommandMethod : subcommandMethods) { var ann = subcommandMethod.getAnnotation(Subcommand.class); - if (ann == null) continue; + if (ann == null) continue; // TODO: Replace def nodes with executing ones if needed inner.then(getSubcommandNode(subcommandMethod, ann.helpText())); } return outer; } - private LiteralArgumentBuilder getSubcommandNode(Method method, String[] helpText) { - CoreCommandBuilder ret = literal(method.getName()); - return ret.helps(helpText).executes(this::executeCommand); + private CoreCommandBuilder getSubcommandNode(Method method, Class senderType, TC command) { + CoreCommandBuilder ret = literal(method.getName(), senderType, getCommandParameters(method.getParameters()), command); + return (CoreCommandBuilder) ret.executes(this::executeCommand); } - private CoreArgumentBuilder getCommandParameters(Parameter[] parameters) { + private Map getCommandParameters(Parameter[] parameters) { return null; // TODO } diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java b/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java index 5bb97e9..29b50f4 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java @@ -25,6 +25,10 @@ public class CoreCommandBuilder> extends LiteralArgum return new CoreCommandBuilder<>(name, senderType, arguments, command); } + public static > CoreCommandBuilder literalNoOp(String name) { + return literal(name, Command2Sender.class, Map.of(), null); + } + /** * Static help text added through annotations. May be overwritten with the getter. * diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/chat/commands/SubcommandData.java b/Chroma-Core/src/main/java/buttondevteam/lib/chat/commands/SubcommandData.java index 63d3829..4711b04 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/chat/commands/SubcommandData.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/chat/commands/SubcommandData.java @@ -4,6 +4,7 @@ import buttondevteam.lib.chat.ICommand2; import lombok.Builder; import lombok.RequiredArgsConstructor; +import javax.annotation.Nullable; import java.util.Map; import java.util.function.Function; @@ -27,8 +28,9 @@ public final class SubcommandData> { */ public final Map arguments; /** - * The original command class that this data belongs to. + * The original command class that this data belongs to. If null, that meaans only the help text can be used. */ + @Nullable public final TC command; /**