Skip to content

Commit e57a133

Browse files
committed
added Regex annotation
1 parent be0af2d commit e57a133

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
.idea/misc.xml
99
.idea/libraries
1010
.idea/uiDesigner.xml
11+
.idea/jarRepositories.xml
1112
.idea/kotlinc.xml
1213
.idea/modules.xml
1314
.idea/usage.statistics.xml

core/src/main/java/co/aikar/commands/CommandParameter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import co.aikar.commands.annotation.Description;
3030
import co.aikar.commands.annotation.Flags;
3131
import co.aikar.commands.annotation.Optional;
32+
import co.aikar.commands.annotation.Regex;
3233
import co.aikar.commands.annotation.Single;
3334
import co.aikar.commands.annotation.Syntax;
3435
import co.aikar.commands.annotation.Values;
@@ -59,6 +60,7 @@ public class CommandParameter<CEC extends CommandExecutionContext<CEC, ? extends
5960
private String defaultValue;
6061
private String syntax;
6162
private String conditions;
63+
private String regexPattern;
6264
private boolean requiresInput;
6365
private boolean commandIssuer;
6466
private String[] values;
@@ -78,7 +80,7 @@ public CommandParameter(RegisteredCommand<CEC> command, Parameter param, int par
7880
this.defaultValue = annotations.getAnnotationValue(param, Default.class, Annotations.REPLACEMENTS | (type != String.class ? Annotations.NO_EMPTY : 0));
7981
this.description = annotations.getAnnotationValue(param, Description.class, Annotations.REPLACEMENTS | Annotations.DEFAULT_EMPTY);
8082
this.conditions = annotations.getAnnotationValue(param, Conditions.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
81-
83+
this.regexPattern = annotations.getAnnotationValue(param, Regex.class, Annotations.REPLACEMENTS | Annotations.DEFAULT_EMPTY);
8284
//noinspection unchecked
8385
this.resolver = manager.getCommandContexts().getResolver(type);
8486
if (this.resolver == null) {
@@ -272,6 +274,14 @@ public void setConditions(String conditions) {
272274
this.conditions = conditions;
273275
}
274276

277+
public String getRegexPattern() {
278+
return regexPattern;
279+
}
280+
281+
public void setRegexPattern(String regexPattern) {
282+
this.regexPattern = regexPattern;
283+
}
284+
275285
public Set<String> getRequiredPermissions() {
276286
return permissions;
277287
}

core/src/main/java/co/aikar/commands/RegisteredCommand.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ Map<String, Object> resolveContexts(CommandIssuer sender, List<String> args, int
237237
final ContextResolver<?, CEC> resolver = parameter.getResolver();
238238
//noinspection unchecked
239239
CEC context = (CEC) this.manager.createCommandContext(this, parameter, sender, args, i, passedArgs);
240+
if (!parameter.getRegexPattern().isEmpty() && !context.getFirstArg().matches(parameter.getRegexPattern())) {
241+
sender.sendMessage(MessageType.ERROR, MessageKeys.INVALID_SYNTAX,
242+
"{command}", manager.getCommandPrefix(sender) + command,
243+
"{syntax}", syntaxText);
244+
throw new InvalidCommandArgument(false);
245+
}
240246
boolean requiresInput = parameter.requiresInput();
241247
if (requiresInput && remainingRequired > 0) {
242248
remainingRequired--;
@@ -249,9 +255,9 @@ Map<String, Object> resolveContexts(CommandIssuer sender, List<String> args, int
249255
} else if (allowOptional && parameter.isOptional()) {
250256
Object value;
251257
if (!parameter.isOptionalResolver() || !this.manager.hasPermission(sender, parameterPermissions)) {
252-
value = null;
258+
value = null;
253259
} else {
254-
value = resolver.getContext(context);
260+
value = resolver.getContext(context);
255261
}
256262

257263
if (value == null && parameter.getClass().isPrimitive()) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2016-2020 Daniel Ennis (Aikar) - MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package co.aikar.commands.annotation;
25+
26+
import java.lang.annotation.ElementType;
27+
import java.lang.annotation.Retention;
28+
import java.lang.annotation.RetentionPolicy;
29+
import java.lang.annotation.Target;
30+
31+
/**
32+
* Regex to apply to an argument
33+
*/
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Target({ElementType.PARAMETER})
36+
public @interface Regex {
37+
String value();
38+
}

example/src/main/java/co/aikar/acfexample/SomeCommand_ExtraSubs.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import co.aikar.commands.annotation.CommandCompletion;
3030
import co.aikar.commands.annotation.HelpCommand;
3131
import co.aikar.commands.annotation.Private;
32+
import co.aikar.commands.annotation.Regex;
3233
import co.aikar.commands.annotation.Subcommand;
3334
import org.bukkit.command.CommandSender;
3435

@@ -41,14 +42,19 @@ public void onTestSub2(CommandSender sender, String hi) {
4142
sender.sendMessage(hi);
4243
}
4344

45+
@Subcommand("testsub test3")
46+
public void onTestSub3(CommandSender sender, @Regex("\\d{2}") int value) {
47+
sender.sendMessage(String.valueOf(value));
48+
}
49+
4450
@Private
4551
@Subcommand("testsub private")
46-
public void privateSub(CommandSender sender){
52+
public void privateSub(CommandSender sender) {
4753
sender.sendMessage("Am a sneaky ninja!");
4854
}
4955

5056
@HelpCommand
51-
public void help(CommandSender sender, CommandHelp help){
57+
public void help(CommandSender sender, CommandHelp help) {
5258
help.showHelp();
5359
}
5460
}

0 commit comments

Comments
 (0)