From 4bf3d65450678b56b185358153c01af354766bd3 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 22:55:31 +0200 Subject: [PATCH 01/15] Merged ArgCommand.java inside Command --- .../zffu/hardened/api/commands/Command.java | 8 ++++++++ .../api/commands/types/ArgCommand.java | 20 ------------------- 2 files changed, 8 insertions(+), 20 deletions(-) delete mode 100644 api/src/main/java/net/zffu/hardened/api/commands/types/ArgCommand.java diff --git a/api/src/main/java/net/zffu/hardened/api/commands/Command.java b/api/src/main/java/net/zffu/hardened/api/commands/Command.java index dbfee0f..3b14489 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/Command.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/Command.java @@ -1,5 +1,6 @@ package net.zffu.hardened.api.commands; +import net.zffu.hardened.api.args.Argument; import net.zffu.hardened.api.commands.validator.CommandValidator; import net.zffu.hardened.api.context.CommandContext; import net.zffu.hardened.api.invoker.CommandInvoker; @@ -27,6 +28,13 @@ public interface Command extends CommandExecution { */ @NotNull String[] getAliases(); + /** + *

Gets the arguments that the command contains. Used in argument parsing

+ *

Note: No checks for required arguments are done by default, you should use a validator node for that.

+ * @return the command arguments as an {@link Argument[]} + */ + @NotNull Argument[] getArguments(); + /** *

Gets the command validator. The command validator is used to validate that a specific context can run the command.

* @return the {@link CommandValidator} diff --git a/api/src/main/java/net/zffu/hardened/api/commands/types/ArgCommand.java b/api/src/main/java/net/zffu/hardened/api/commands/types/ArgCommand.java deleted file mode 100644 index 67ed624..0000000 --- a/api/src/main/java/net/zffu/hardened/api/commands/types/ArgCommand.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.zffu.hardened.api.commands.types; - -import net.zffu.hardened.api.args.Argument; -import net.zffu.hardened.api.commands.validator.CommandValidator; -import org.jetbrains.annotations.NotNull; - -/** - *

A command that has argument(s) registered into it.

- *

Warn: No checks for this are there by default, those checks should be implemented in the {@link CommandValidator}

- * @since 1.0.0 - */ -public interface ArgCommand { - - /** - *

Gets the arguments that the command contains. Used in argument parsing

- * @return the command arguments as an {@link Argument[]} - */ - @NotNull Argument[] getArguments(); - -} From e4342e925cf942a1d55fa93c9579078065acfc8f Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 22:55:51 +0200 Subject: [PATCH 02/15] Swapped for a List approach --- api/src/main/java/net/zffu/hardened/api/commands/Command.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/Command.java b/api/src/main/java/net/zffu/hardened/api/commands/Command.java index 3b14489..ced70e7 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/Command.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/Command.java @@ -7,6 +7,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; + /** *

The base of every command registered trough the Hardened API. Any command that uses this API should implement this.

*

An interface that stores basic command metadata, the {@link CommandValidator} and the execution function.

@@ -33,7 +35,7 @@ public interface Command extends CommandExecution { *

Note: No checks for required arguments are done by default, you should use a validator node for that.

* @return the command arguments as an {@link Argument[]} */ - @NotNull Argument[] getArguments(); + @NotNull List getArguments(); /** *

Gets the command validator. The command validator is used to validate that a specific context can run the command.

From d990e7b0830cb166734b22f5bf019efe564411e1 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 22:56:49 +0200 Subject: [PATCH 03/15] Added arguments inside BuilderCommand --- .../hardened/api/commands/builder/BuilderCommand.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/builder/BuilderCommand.java b/api/src/main/java/net/zffu/hardened/api/commands/builder/BuilderCommand.java index eb2cdb2..de4739b 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/builder/BuilderCommand.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/builder/BuilderCommand.java @@ -1,5 +1,6 @@ package net.zffu.hardened.api.commands.builder; +import net.zffu.hardened.api.args.Argument; import net.zffu.hardened.api.commands.Command; import net.zffu.hardened.api.commands.CommandExecution; import net.zffu.hardened.api.commands.validator.CommandValidator; @@ -8,12 +9,16 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.List; + public class BuilderCommand implements Command { protected String primaryName; protected String[] aliases; protected NodeCommandValidator validator = new NodeCommandValidator(); protected CommandExecution execution; + protected List arguments = new ArrayList<>(); public BuilderCommand(String primaryName) { this.primaryName = primaryName; @@ -29,6 +34,11 @@ public BuilderCommand(String primaryName) { return this.aliases; } + @Override + public @NotNull List getArguments() { + return this.arguments; + } + @Nullable @Override public CommandValidator getValidator() { From 8641a52c574e8cbcd69994f2770fd66a9e12f774 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 22:57:59 +0200 Subject: [PATCH 04/15] Added arg builder func --- .../hardened/api/commands/builder/CommandBuilder.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java index 29e7c3f..3bc7dfb 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java @@ -1,5 +1,6 @@ package net.zffu.hardened.api.commands.builder; +import net.zffu.hardened.api.args.Argument; import net.zffu.hardened.api.commands.CommandExecution; import net.zffu.hardened.api.commands.validator.nodes.NodeCommandValidator; import net.zffu.hardened.api.commands.validator.nodes.ValidatorNode; @@ -68,6 +69,16 @@ public CommandBuilder disabled(boolean disabled) { return this.node(new DisabledNode(disabled)); } + /** + * Adds an {@link Argument} inside the command. + * @param argument + * @return + */ + public CommandBuilder arg(Argument argument) { + this.command.arguments.add(argument); + return this; + } + /** * Sets the command's execution logic. * @param execution the logic as a {@link CommandExecution} From d17e0ad50165fe9e5783034bcbe49b221b673170 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 22:58:23 +0200 Subject: [PATCH 05/15] Renamed arg to argument --- .../net/zffu/hardened/api/commands/builder/CommandBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java index 3bc7dfb..0addff3 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java @@ -74,7 +74,7 @@ public CommandBuilder disabled(boolean disabled) { * @param argument * @return */ - public CommandBuilder arg(Argument argument) { + public CommandBuilder argument(Argument argument) { this.command.arguments.add(argument); return this; } From ab3d731ad7523ccd090d43fdbbba393673507df4 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 22:59:31 +0200 Subject: [PATCH 06/15] Added argument builder func variant --- .../hardened/api/commands/builder/CommandBuilder.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java index 0addff3..12cac1a 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java @@ -1,6 +1,7 @@ package net.zffu.hardened.api.commands.builder; import net.zffu.hardened.api.args.Argument; +import net.zffu.hardened.api.args.ArgumentTypes; import net.zffu.hardened.api.commands.CommandExecution; import net.zffu.hardened.api.commands.validator.nodes.NodeCommandValidator; import net.zffu.hardened.api.commands.validator.nodes.ValidatorNode; @@ -79,6 +80,16 @@ public CommandBuilder argument(Argument argument) { return this; } + /** + * Adds an {@link Argument} inside the command. + * @param type the arg type + * @return + */ + public CommandBuilder argument(ArgumentTypes type) { + this.command.arguments.add(new Argument(type.get())); + return this; + } + /** * Sets the command's execution logic. * @param execution the logic as a {@link CommandExecution} From f16ad7e636149a90a6938747dff5970a12b4f49c Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:00:25 +0200 Subject: [PATCH 07/15] Added optionalArg builder func --- .../hardened/api/commands/builder/CommandBuilder.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java index 12cac1a..b2f4bf6 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java @@ -90,6 +90,16 @@ public CommandBuilder argument(ArgumentTypes type) { return this; } + /** + * Adds an optional {@link Argument} inside the command. + * @param type the arg type + * @return + */ + public CommandBuilder optionalArg(ArgumentTypes type) { + this.command.arguments.add(new Argument(type.get()).optional(true)); + return this; + } + /** * Sets the command's execution logic. * @param execution the logic as a {@link CommandExecution} From b47b757d0a01f2e1f943b05dee9371ad5c9a324d Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:07:49 +0200 Subject: [PATCH 08/15] Removed ArgumentTypes.java enum --- .../zffu/hardened/api/args/ArgumentTypes.java | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 api/src/main/java/net/zffu/hardened/api/args/ArgumentTypes.java diff --git a/api/src/main/java/net/zffu/hardened/api/args/ArgumentTypes.java b/api/src/main/java/net/zffu/hardened/api/args/ArgumentTypes.java deleted file mode 100644 index 296cb55..0000000 --- a/api/src/main/java/net/zffu/hardened/api/args/ArgumentTypes.java +++ /dev/null @@ -1,29 +0,0 @@ -package net.zffu.hardened.api.args; - -import net.zffu.hardened.api.args.types.IntegerArgument; -import net.zffu.hardened.api.args.types.StringArgument; - -/** - *

An enum containing instances of each argument type that is in the Hardened API.

- * @since 1.0.0 - */ -public enum ArgumentTypes { - - STRING(new StringArgument()), - INT(new IntegerArgument()); - - private ArgumentType type; - - private ArgumentTypes(ArgumentType type) { - this.type = type; - } - - /** - * Gets the corresponding {@link ArgumentType} instance. - * @return - */ - public ArgumentType get() { - return this.type; - } - -} From c6364d3d047b4121ca3a65957082f96d5511deb7 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:10:03 +0200 Subject: [PATCH 09/15] Added simple Arguments class --- .../net/zffu/hardened/api/args/Arguments.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 api/src/main/java/net/zffu/hardened/api/args/Arguments.java diff --git a/api/src/main/java/net/zffu/hardened/api/args/Arguments.java b/api/src/main/java/net/zffu/hardened/api/args/Arguments.java new file mode 100644 index 0000000..bcaa746 --- /dev/null +++ b/api/src/main/java/net/zffu/hardened/api/args/Arguments.java @@ -0,0 +1,32 @@ +package net.zffu.hardened.api.args; + +import net.zffu.hardened.api.args.types.IntegerArgument; +import net.zffu.hardened.api.args.types.StringArgument; + +/** + * Useful class to generate {@link Argument} instances faster. + * @since 1.0.0 + * @see {@link Argument} + */ +public class Arguments { + + // Default Argument Instances for caching. + private static IntegerArgument DEFAULT_INT = new IntegerArgument(); + private static StringArgument DEFAULT_STRING = new StringArgument(); + + /** + * Gets a simple integer argument. + * @return + */ + public static IntegerArgument integer() { + return DEFAULT_INT; + } + + /** + * Gets a simple string argument. + * @return + */ + public static StringArgument string() { + return DEFAULT_STRING; + } +} From 7a7552e058f59241f92e8fe18f07caaaef2d12a5 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:10:48 +0200 Subject: [PATCH 10/15] Removed caching --- .../main/java/net/zffu/hardened/api/args/Arguments.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/net/zffu/hardened/api/args/Arguments.java b/api/src/main/java/net/zffu/hardened/api/args/Arguments.java index bcaa746..dd8fe66 100644 --- a/api/src/main/java/net/zffu/hardened/api/args/Arguments.java +++ b/api/src/main/java/net/zffu/hardened/api/args/Arguments.java @@ -10,16 +10,12 @@ */ public class Arguments { - // Default Argument Instances for caching. - private static IntegerArgument DEFAULT_INT = new IntegerArgument(); - private static StringArgument DEFAULT_STRING = new StringArgument(); - /** * Gets a simple integer argument. * @return */ public static IntegerArgument integer() { - return DEFAULT_INT; + return new IntegerArgument(); } /** @@ -27,6 +23,6 @@ public static IntegerArgument integer() { * @return */ public static StringArgument string() { - return DEFAULT_STRING; + return new StringArgument(); } } From 9cc210dbb7b9efb85a15e6b10df30ba77a037dda Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:11:14 +0200 Subject: [PATCH 11/15] Removed useless builder functions --- .../api/commands/builder/CommandBuilder.java | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java index b2f4bf6..0addff3 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/builder/CommandBuilder.java @@ -1,7 +1,6 @@ package net.zffu.hardened.api.commands.builder; import net.zffu.hardened.api.args.Argument; -import net.zffu.hardened.api.args.ArgumentTypes; import net.zffu.hardened.api.commands.CommandExecution; import net.zffu.hardened.api.commands.validator.nodes.NodeCommandValidator; import net.zffu.hardened.api.commands.validator.nodes.ValidatorNode; @@ -80,26 +79,6 @@ public CommandBuilder argument(Argument argument) { return this; } - /** - * Adds an {@link Argument} inside the command. - * @param type the arg type - * @return - */ - public CommandBuilder argument(ArgumentTypes type) { - this.command.arguments.add(new Argument(type.get())); - return this; - } - - /** - * Adds an optional {@link Argument} inside the command. - * @param type the arg type - * @return - */ - public CommandBuilder optionalArg(ArgumentTypes type) { - this.command.arguments.add(new Argument(type.get()).optional(true)); - return this; - } - /** * Sets the command's execution logic. * @param execution the logic as a {@link CommandExecution} From b5bffcb8aca8a8671b03d0b3f25ea1bf03f8b7f0 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:11:29 +0200 Subject: [PATCH 12/15] Removed broken imports --- .../net/zffu/hardened/examples/api/BuilderCommandExample.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/api-examples/src/main/java/net/zffu/hardened/examples/api/BuilderCommandExample.java b/api-examples/src/main/java/net/zffu/hardened/examples/api/BuilderCommandExample.java index 0adc183..62400e9 100644 --- a/api-examples/src/main/java/net/zffu/hardened/examples/api/BuilderCommandExample.java +++ b/api-examples/src/main/java/net/zffu/hardened/examples/api/BuilderCommandExample.java @@ -1,7 +1,5 @@ package net.zffu.hardened.examples.api; -import net.zffu.hardened.api.args.Argument; -import net.zffu.hardened.api.args.ArgumentTypes; import net.zffu.hardened.api.commands.Command; import net.zffu.hardened.api.commands.builder.CommandBuilder; import net.zffu.hardened.api.invoker.InvokerType; From edbc2c9d85898f0c5aff37152672848569844daf Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:15:31 +0200 Subject: [PATCH 13/15] Fixed some imports and stuff --- .../net/zffu/hardened/api/commands/Command.java | 1 - .../zffu/hardened/api/context/CommandContext.java | 13 +++++++------ .../zffu/hardened/spigot/handler/SpigotCommand.java | 5 ++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/net/zffu/hardened/api/commands/Command.java b/api/src/main/java/net/zffu/hardened/api/commands/Command.java index ced70e7..2ac238a 100644 --- a/api/src/main/java/net/zffu/hardened/api/commands/Command.java +++ b/api/src/main/java/net/zffu/hardened/api/commands/Command.java @@ -3,7 +3,6 @@ import net.zffu.hardened.api.args.Argument; import net.zffu.hardened.api.commands.validator.CommandValidator; import net.zffu.hardened.api.context.CommandContext; -import net.zffu.hardened.api.invoker.CommandInvoker; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/api/src/main/java/net/zffu/hardened/api/context/CommandContext.java b/api/src/main/java/net/zffu/hardened/api/context/CommandContext.java index 5c31474..cfe6d35 100644 --- a/api/src/main/java/net/zffu/hardened/api/context/CommandContext.java +++ b/api/src/main/java/net/zffu/hardened/api/context/CommandContext.java @@ -1,8 +1,8 @@ package net.zffu.hardened.api.context; +import com.sun.org.apache.xpath.internal.Arg; import net.zffu.hardened.api.args.Argument; -import net.zffu.hardened.api.args.ArgumentType; -import net.zffu.hardened.api.commands.types.ArgCommand; +import net.zffu.hardened.api.commands.Command; import net.zffu.hardened.api.invoker.CommandInvoker; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -41,7 +41,7 @@ public CommandContext(@NotNull CommandInvoker invoker, @Nullable Object[] args, /** *

Locates the argument in the preformatted argument values pool and casts it to the according class.

*

Note: This function automatically checks for casting

- * @param index the argument value's index to find it (also is the argument's index in the {@link ArgCommand}) + * @param index the argument value's index to find it (also is the argument's index in the {@link Command}) * @param clazz the {@link Class} of the output data type. * @param the output data type. * @return the argument value casted to the provided type. @@ -65,15 +65,16 @@ public CommandContext(@NotNull CommandInvoker invoker, @Nullable Object[] args, } /** - *

Preformats / parses the given raw {@link String} arguments and outputs the parsed arguments based on the registered arguments in the {@link ArgCommand}

- * @param command the {@link ArgCommand}. Used to get the argument types. + *

Preformats / parses the given raw {@link String} arguments and outputs the parsed arguments based on the registered arguments in the {@link Command}

+ * @param command the {@link Command}. Used to get the argument types. * @param args the raw string argument array. * @return the parsed arguments as a {@link Object[]}; */ - public static Object[] preFormatArguments(@NotNull ArgCommand command, @NotNull String[] args) { + public static Object[] preFormatArguments(@NotNull Command command, @NotNull String[] args) { Object[] arguments = new Object[args.length]; int index = 0; + for(Argument argument : command.getArguments()) { if(argument.isOptional()) continue; //todo: add optional argument handling Object o = argument.getType().fromString(args[index]); diff --git a/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommand.java b/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommand.java index 72d10c9..9748e1c 100644 --- a/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommand.java +++ b/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommand.java @@ -1,6 +1,5 @@ package net.zffu.hardened.spigot.handler; -import net.zffu.hardened.api.commands.types.ArgCommand; import net.zffu.hardened.api.context.CommandContext; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -20,12 +19,12 @@ public SpigotCommand(net.zffu.hardened.api.commands.Command command) { super(command.getPrimaryName()); this.command = command; if(this.command.getAliases() != null && this.command.getAliases().length > 0) this.setAliases(Arrays.asList(this.command.getAliases())); - this.parseArgs = this.command instanceof ArgCommand; + this.parseArgs = !this.command.getArguments().isEmpty(); } @Override public boolean execute(CommandSender sender, String commandLabel, String[] args) { - CommandContext context = new CommandContext(SpigotCommandHandler.fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments((ArgCommand) this.command, args) : null), commandLabel); + CommandContext context = new CommandContext(SpigotCommandHandler.fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments(this.command, args) : null), commandLabel); this.command.run(context); return true; } From bf1b5c56c29184594094805f0139d6b5e6933937 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:16:09 +0200 Subject: [PATCH 14/15] Fixed spigot handler --- .../zffu/hardened/spigot/handler/SpigotCommandHandler.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommandHandler.java b/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommandHandler.java index a17d710..5796e0a 100644 --- a/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommandHandler.java +++ b/spigot/src/main/java/net/zffu/hardened/spigot/handler/SpigotCommandHandler.java @@ -1,6 +1,5 @@ package net.zffu.hardened.spigot.handler; -import net.zffu.hardened.api.commands.types.ArgCommand; import net.zffu.hardened.api.context.CommandContext; import net.zffu.hardened.api.invoker.CommandInvoker; import net.zffu.hardened.spigot.invokers.ConsoleInvoker; @@ -23,11 +22,11 @@ public class SpigotCommandHandler implements CommandExecutor { public SpigotCommandHandler(net.zffu.hardened.api.commands.Command command) { this.command = command; - this.parseArgs = this.command instanceof ArgCommand; + this.parseArgs = !this.command.getArguments().isEmpty(); } public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - CommandContext context = new CommandContext(fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments((ArgCommand) this.command, args) : null), label); + CommandContext context = new CommandContext(fromCommandSender(sender), (this.parseArgs ? CommandContext.preFormatArguments(this.command, args) : null), label); this.command.run(context); return true; } From ee5d3548ef5ce99f3a3f5c21ee75db24cb097a94 Mon Sep 17 00:00:00 2001 From: Zffu Date: Thu, 25 Jul 2024 23:19:15 +0200 Subject: [PATCH 15/15] Added argument guide in readme --- README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74abf62..633df34 100644 --- a/README.md +++ b/README.md @@ -46,12 +46,27 @@ For example, you could make your command require the invoker to be a player and builder.type(InvokerType.PLAYER).permission("test.permission"); ``` +#### Adding Arguments +In order to add arguments to your command you can use the `Arguments` class to generate the arguments. + +For example lets make a string argument. +```java +builder.argument(Arguments.string()); +``` + +To make the argument optional simply do that instead: +```java +builder.argument(Arguments.string().optional(true)); +``` + +**INFO:** Arguments are required by default. + #### Make the command do something In order to make the command do something, you can use the `execute` builder function. -In this example, we make the command send "hello" to the player who executed the command (for Spigot): +In this example, we make the command send "hello (argument 1)" to the player who executed the command (for Spigot): ```java -builder.execute((ctx) -> (((PlayerInvoker)ctx.getInvoker()).getPlayer()).sendMessage("Hello")) +builder.execute((ctx) -> (((PlayerInvoker)ctx.getInvoker()).getPlayer()).sendMessage("Hello " + ctx.get(0, String.class))) ``` #### Building the command