diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 6b817330..c61c5f2b 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,17 +7,16 @@ - - - - - - - - + + + + + + + diff --git a/core/src/main/java/co/aikar/commands/CommandExecutionContext.java b/core/src/main/java/co/aikar/commands/CommandExecutionContext.java index f23a553e..620a20b2 100644 --- a/core/src/main/java/co/aikar/commands/CommandExecutionContext.java +++ b/core/src/main/java/co/aikar/commands/CommandExecutionContext.java @@ -27,9 +27,10 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Parameter; import java.util.List; import java.util.Map; +import java.util.Set; @SuppressWarnings({"WeakerAccess"}) -public class CommandExecutionContext { +public class CommandExecutionContext { private final RegisteredCommand cmd; private final CommandParameter param; protected final I issuer; @@ -69,7 +70,7 @@ public class CommandExecutionContext getPermissions() { + return param.getPermissions(); + } + public boolean isOptional() { return param.isOptional(); } @@ -170,6 +175,7 @@ public class CommandExecutionContext > { +public class CommandParameter> { private final Parameter parameter; private final Class type; private final String name; @@ -49,6 +53,8 @@ public class CommandParameter resolver; private boolean optional; + private Set permissions = new HashSet<>(); + private String permission; private String description; private String defaultValue; private String syntax; @@ -82,6 +88,7 @@ public class CommandParameter resolver) { return resolver instanceof IssuerAwareContextResolver - || resolver instanceof IssuerOnlyContextResolver - || resolver instanceof OptionalContextResolver; + || resolver instanceof IssuerOnlyContextResolver + || resolver instanceof OptionalContextResolver; } @@ -256,4 +271,8 @@ public class CommandParameter getPermissions() { + 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 9c519f51..7fc2f56c 100644 --- a/core/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/core/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -226,11 +226,13 @@ public class RegisteredCommand 0) { remainingRequired--; } + if (args.isEmpty() && !(isLast && type == String[].class)) { if (allowOptional && parameter.getDefaultValue() != null) { args.add(parameter.getDefaultValue()); } else if (allowOptional && parameter.isOptional()) { Object value = parameter.isOptionalResolver() ? resolver.getContext(context) : null; + if (value == null && parameter.getClass().isPrimitive()) { throw new IllegalStateException("Parameter " + parameter.getName() + " is primitive and does not support Optional."); } @@ -244,6 +246,7 @@ public class RegisteredCommand parameterPermissions = parameter.getPermissions(); + if (!parameter.isOptionalResolver() && parameterPermissions != null && !parameterPermissions.isEmpty()) { + if (allowOptional && parameter.isOptional()) { + for (String perm : parameterPermissions) { + if (!perm.isEmpty() && !sender.hasPermission(perm)) { + sender.sendMessage(MessageType.ERROR, MessageKeys.PERMISSION_DENIED); + return null; + } + } + } else { + throw new IllegalStateException("Using CommandPermission annotation on parameter that is not optional is useless and you should not do it."); + } + } + //noinspection unchecked this.manager.conditions.validateConditions(context, paramValue); passedArgs.put(parameterName, paramValue); diff --git a/core/src/main/java/co/aikar/commands/annotation/CommandPermission.java b/core/src/main/java/co/aikar/commands/annotation/CommandPermission.java index 96c3111a..b41bafb5 100644 --- a/core/src/main/java/co/aikar/commands/annotation/CommandPermission.java +++ b/core/src/main/java/co/aikar/commands/annotation/CommandPermission.java @@ -30,11 +30,11 @@ import java.lang.annotation.Target; /** * Sets the permission required to perform this command. - * + *

* Permission format will vary based on implementation platform */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.TYPE}) +@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER}) public @interface CommandPermission { String value(); }