From e57a133190a0ea950dcb45f7802960c1e4ab67f5 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Thu, 23 Jul 2020 14:13:14 -0700 Subject: [PATCH 1/3] added Regex annotation --- .gitignore | 1 + .../co/aikar/commands/CommandParameter.java | 12 +++++- .../co/aikar/commands/RegisteredCommand.java | 10 ++++- .../co/aikar/commands/annotation/Regex.java | 38 +++++++++++++++++++ .../acfexample/SomeCommand_ExtraSubs.java | 10 ++++- 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 core/src/main/java/co/aikar/commands/annotation/Regex.java diff --git a/.gitignore b/.gitignore index fe9fc757a..65026171e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ .idea/misc.xml .idea/libraries .idea/uiDesigner.xml +.idea/jarRepositories.xml .idea/kotlinc.xml .idea/modules.xml .idea/usage.statistics.xml diff --git a/core/src/main/java/co/aikar/commands/CommandParameter.java b/core/src/main/java/co/aikar/commands/CommandParameter.java index ba4f7ec59..b505081aa 100644 --- a/core/src/main/java/co/aikar/commands/CommandParameter.java +++ b/core/src/main/java/co/aikar/commands/CommandParameter.java @@ -29,6 +29,7 @@ import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Flags; import co.aikar.commands.annotation.Optional; +import co.aikar.commands.annotation.Regex; import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Values; @@ -59,6 +60,7 @@ public class CommandParameter command, Parameter param, int par this.defaultValue = annotations.getAnnotationValue(param, Default.class, Annotations.REPLACEMENTS | (type != String.class ? Annotations.NO_EMPTY : 0)); this.description = annotations.getAnnotationValue(param, Description.class, Annotations.REPLACEMENTS | Annotations.DEFAULT_EMPTY); this.conditions = annotations.getAnnotationValue(param, Conditions.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY); - + this.regexPattern = annotations.getAnnotationValue(param, Regex.class, Annotations.REPLACEMENTS | Annotations.DEFAULT_EMPTY); //noinspection unchecked this.resolver = manager.getCommandContexts().getResolver(type); if (this.resolver == null) { @@ -272,6 +274,14 @@ public void setConditions(String conditions) { this.conditions = conditions; } + public String getRegexPattern() { + return regexPattern; + } + + public void setRegexPattern(String regexPattern) { + this.regexPattern = regexPattern; + } + public Set getRequiredPermissions() { return permissions; } diff --git a/core/src/main/java/co/aikar/commands/RegisteredCommand.java b/core/src/main/java/co/aikar/commands/RegisteredCommand.java index c0069f1b8..439c1fd66 100644 --- a/core/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/core/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -237,6 +237,12 @@ Map resolveContexts(CommandIssuer sender, List args, int final ContextResolver resolver = parameter.getResolver(); //noinspection unchecked CEC context = (CEC) this.manager.createCommandContext(this, parameter, sender, args, i, passedArgs); + if (!parameter.getRegexPattern().isEmpty() && !context.getFirstArg().matches(parameter.getRegexPattern())) { + sender.sendMessage(MessageType.ERROR, MessageKeys.INVALID_SYNTAX, + "{command}", manager.getCommandPrefix(sender) + command, + "{syntax}", syntaxText); + throw new InvalidCommandArgument(false); + } boolean requiresInput = parameter.requiresInput(); if (requiresInput && remainingRequired > 0) { remainingRequired--; @@ -249,9 +255,9 @@ Map resolveContexts(CommandIssuer sender, List args, int } else if (allowOptional && parameter.isOptional()) { Object value; if (!parameter.isOptionalResolver() || !this.manager.hasPermission(sender, parameterPermissions)) { - value = null; + value = null; } else { - value = resolver.getContext(context); + value = resolver.getContext(context); } if (value == null && parameter.getClass().isPrimitive()) { diff --git a/core/src/main/java/co/aikar/commands/annotation/Regex.java b/core/src/main/java/co/aikar/commands/annotation/Regex.java new file mode 100644 index 000000000..987e23367 --- /dev/null +++ b/core/src/main/java/co/aikar/commands/annotation/Regex.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016-2020 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Regex to apply to an argument + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.PARAMETER}) +public @interface Regex { + String value(); +} diff --git a/example/src/main/java/co/aikar/acfexample/SomeCommand_ExtraSubs.java b/example/src/main/java/co/aikar/acfexample/SomeCommand_ExtraSubs.java index de197d9f3..c05084458 100644 --- a/example/src/main/java/co/aikar/acfexample/SomeCommand_ExtraSubs.java +++ b/example/src/main/java/co/aikar/acfexample/SomeCommand_ExtraSubs.java @@ -29,6 +29,7 @@ import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.HelpCommand; import co.aikar.commands.annotation.Private; +import co.aikar.commands.annotation.Regex; import co.aikar.commands.annotation.Subcommand; import org.bukkit.command.CommandSender; @@ -41,14 +42,19 @@ public void onTestSub2(CommandSender sender, String hi) { sender.sendMessage(hi); } + @Subcommand("testsub test3") + public void onTestSub3(CommandSender sender, @Regex("\\d{2}") int value) { + sender.sendMessage(String.valueOf(value)); + } + @Private @Subcommand("testsub private") - public void privateSub(CommandSender sender){ + public void privateSub(CommandSender sender) { sender.sendMessage("Am a sneaky ninja!"); } @HelpCommand - public void help(CommandSender sender, CommandHelp help){ + public void help(CommandSender sender, CommandHelp help) { help.showHelp(); } } From 7e1523780eb221799b3708dc5e624419689e51a9 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Thu, 23 Jul 2020 14:32:29 -0700 Subject: [PATCH 2/3] changed default to null --- core/src/main/java/co/aikar/commands/CommandParameter.java | 2 +- core/src/main/java/co/aikar/commands/RegisteredCommand.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/co/aikar/commands/CommandParameter.java b/core/src/main/java/co/aikar/commands/CommandParameter.java index b505081aa..7dfd67aaa 100644 --- a/core/src/main/java/co/aikar/commands/CommandParameter.java +++ b/core/src/main/java/co/aikar/commands/CommandParameter.java @@ -80,7 +80,7 @@ public CommandParameter(RegisteredCommand command, Parameter param, int par this.defaultValue = annotations.getAnnotationValue(param, Default.class, Annotations.REPLACEMENTS | (type != String.class ? Annotations.NO_EMPTY : 0)); this.description = annotations.getAnnotationValue(param, Description.class, Annotations.REPLACEMENTS | Annotations.DEFAULT_EMPTY); this.conditions = annotations.getAnnotationValue(param, Conditions.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY); - this.regexPattern = annotations.getAnnotationValue(param, Regex.class, Annotations.REPLACEMENTS | Annotations.DEFAULT_EMPTY); + this.regexPattern = annotations.getAnnotationValue(param, Regex.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY); //noinspection unchecked this.resolver = manager.getCommandContexts().getResolver(type); if (this.resolver == null) { diff --git a/core/src/main/java/co/aikar/commands/RegisteredCommand.java b/core/src/main/java/co/aikar/commands/RegisteredCommand.java index 439c1fd66..a5648a081 100644 --- a/core/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/core/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -237,7 +237,7 @@ Map resolveContexts(CommandIssuer sender, List args, int final ContextResolver resolver = parameter.getResolver(); //noinspection unchecked CEC context = (CEC) this.manager.createCommandContext(this, parameter, sender, args, i, passedArgs); - if (!parameter.getRegexPattern().isEmpty() && !context.getFirstArg().matches(parameter.getRegexPattern())) { + if (parameter.getRegexPattern() != null && !context.getFirstArg().matches(parameter.getRegexPattern())) { sender.sendMessage(MessageType.ERROR, MessageKeys.INVALID_SYNTAX, "{command}", manager.getCommandPrefix(sender) + command, "{syntax}", syntaxText); From d70222b481d900f8eb5d55d092d49198ead1f369 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Thu, 23 Jul 2020 15:00:55 -0700 Subject: [PATCH 3/3] moved regex check to after perm check --- .../java/co/aikar/commands/RegisteredCommand.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/co/aikar/commands/RegisteredCommand.java b/core/src/main/java/co/aikar/commands/RegisteredCommand.java index a5648a081..7545d2998 100644 --- a/core/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/core/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -237,12 +237,6 @@ Map resolveContexts(CommandIssuer sender, List args, int final ContextResolver resolver = parameter.getResolver(); //noinspection unchecked CEC context = (CEC) this.manager.createCommandContext(this, parameter, sender, args, i, passedArgs); - if (parameter.getRegexPattern() != null && !context.getFirstArg().matches(parameter.getRegexPattern())) { - sender.sendMessage(MessageType.ERROR, MessageKeys.INVALID_SYNTAX, - "{command}", manager.getCommandPrefix(sender) + command, - "{syntax}", syntaxText); - throw new InvalidCommandArgument(false); - } boolean requiresInput = parameter.requiresInput(); if (requiresInput && remainingRequired > 0) { remainingRequired--; @@ -278,6 +272,13 @@ Map resolveContexts(CommandIssuer sender, List args, int } } + if (parameter.getRegexPattern() != null && !context.getFirstArg().matches(parameter.getRegexPattern())) { + sender.sendMessage(MessageType.ERROR, MessageKeys.INVALID_SYNTAX, + "{command}", manager.getCommandPrefix(sender) + command, + "{syntax}", syntaxText); + throw new InvalidCommandArgument(false); + } + if (parameter.getValues() != null) { String arg = !args.isEmpty() ? args.get(0) : "";