From 5f7f3d7747485714915a1f18066b511bf0e6c37a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 8 Jan 2022 18:11:00 +0100 Subject: [PATCH] Add custom command builder and help text test --- .../java/buttondevteam/lib/chat/Command2.java | 31 +++++++++------- .../lib/chat/CoreCommandBuilder.java | 37 +++++++++++++++++++ .../lib/chat/CoreCommandNode.java | 7 +++- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java 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 8419050..e1693a5 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/chat/Command2.java @@ -10,6 +10,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.tree.LiteralCommandNode; import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import lombok.val; @@ -30,11 +31,11 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Collectors; -import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; +import static buttondevteam.lib.chat.CoreCommandBuilder.literal; /** * The method name is the subcommand, use underlines (_) to add further subcommands. @@ -288,19 +289,19 @@ public abstract class Command2, TP extends Command2Send public abstract void registerCommand(TC command); - protected List> registerCommand(TC command, char commandChar) { - return registerCommand(command, dispatcher.register(getCommandNode(command)), commandChar); + protected LiteralCommandNode registerCommand(TC command, char commandChar) { + return dispatcher.register(getCommandNode(command)); } private LiteralArgumentBuilder getCommandNode(TC command) { var path = command.getCommandPath().split(" "); if (path.length == 0) throw new IllegalArgumentException("Attempted to register a command with no command path!"); - LiteralArgumentBuilder inner = literal(path[0]); + CoreCommandBuilder inner = literal(path[0]); var outer = inner; for (int i = path.length - 1; i >= 0; i--) { - LiteralArgumentBuilder literal = literal(path[i]); - outer = literal.executes(this::executeHelpText).then(outer); + CoreCommandBuilder literal = literal(path[i]); + outer = (CoreCommandBuilder) literal.executes(this::executeHelpText).then(outer); } var subcommandMethods = command.getClass().getMethods(); for (var subcommandMethod : subcommandMethods) { @@ -312,23 +313,25 @@ public abstract class Command2, TP extends Command2Send } private LiteralArgumentBuilder getSubcommandNode(Method method, String[] helpText) { - LiteralArgumentBuilder ret = literal(method.getName()); - return ret.executes(this::executeCommand); // TODO: CoreCommandNode helpText + CoreCommandBuilder ret = literal(method.getName()); + return ret.helps(helpText).executes(this::executeCommand); } private CoreArgumentBuilder getCommandParameters(Parameter[] parameters) { - + return null; // TODO } private int executeHelpText(CommandContext context) { - + System.out.println("Nodes:\n" + context.getNodes().stream().map(node -> node.getNode().getName() + "@" + node.getRange()).collect(Collectors.joining("\n"))); + return 0; } private int executeCommand(CommandContext context) { - + System.out.println("Execute command"); + return 0; } - protected List> registerCommand(TC command, @SuppressWarnings("SameParameterValue") char commandChar) { + /*protected List> registerCommand(TC command, @SuppressWarnings("SameParameterValue") char commandChar) { this.commandChar = commandChar; Method mainMethod = null; boolean nosubs = true; @@ -381,7 +384,7 @@ public abstract class Command2, TP extends Command2Send scmd.helpText = scmdHelp; } return addedSubcommands; - } + }*/ private String[] getParameterHelp(Method method, String[] ht, String subcommand, String[] parameters) { val str = method.getDeclaringClass().getResourceAsStream("/commands.yml"); diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java b/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java new file mode 100644 index 0000000..cade3e1 --- /dev/null +++ b/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandBuilder.java @@ -0,0 +1,37 @@ +package buttondevteam.lib.chat; + +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.tree.CommandNode; + +public class CoreCommandBuilder extends LiteralArgumentBuilder { + private String[] helpText; + + protected CoreCommandBuilder(String literal) { + super(literal); + } + + @Override + protected CoreCommandBuilder getThis() { + return this; + } + + public static CoreCommandBuilder literal(String name) { + return new CoreCommandBuilder<>(name); + } + + public CoreCommandBuilder helps(String[] helpText) { + this.helpText = helpText; + return this; + } + + @Override + public CoreCommandNode build() { + var result = new CoreCommandNode<>(this.getLiteral(), this.getCommand(), this.getRequirement(), this.getRedirect(), this.getRedirectModifier(), this.isFork(), helpText); + + for (CommandNode node : this.getArguments()) { + result.addChild(node); + } + + return result; + } +} diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandNode.java b/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandNode.java index ab8b8d2..8cffd67 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandNode.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/chat/CoreCommandNode.java @@ -4,11 +4,16 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.RedirectModifier; import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; +import lombok.Getter; import java.util.function.Predicate; public class CoreCommandNode extends LiteralCommandNode { - public CoreCommandNode(String literal, Command command, Predicate requirement, CommandNode redirect, RedirectModifier modifier, boolean forks, String helpText) { + @Getter + private final String[] helpText; + + public CoreCommandNode(String literal, Command command, Predicate requirement, CommandNode redirect, RedirectModifier modifier, boolean forks, String[] helpText) { super(literal, command, requirement, redirect, modifier, forks); + this.helpText = helpText; } }