From d2754b99c98e7d4640efe19437567f868b6ad994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Jimenez?= Date: Sat, 9 Mar 2019 17:03:04 +0100 Subject: [PATCH] =?UTF-8?q?Add=20support=20for=20CommandPermission=20annot?= =?UTF-8?q?ation=20as=20parameter=20for=20permiss=E2=80=A6=20(#197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …ions by Optional annotation In the past we could not handle a permission in relation to the presence of an optional argument, it forced us to manage it in the body of the command, and could be repetitive and boring, note that it also works with the annotation Default --- .idea/compiler.xml | 15 ++++++----- .../commands/CommandExecutionContext.java | 11 ++++++-- .../co/aikar/commands/CommandParameter.java | 25 ++++++++++++++++--- .../co/aikar/commands/RegisteredCommand.java | 19 +++++++++++++- .../annotation/CommandPermission.java | 4 +-- 5 files changed, 58 insertions(+), 16 deletions(-) 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(); }