From 4e46e05a3b0c627a34631848874fc56d14ba56e4 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 17 Jun 2022 09:51:28 +0200 Subject: [PATCH] Update AbstractSWCommand.ErrorMessage --- src/de/steamwar/command/AbstractSWCommand.java | 5 +++++ src/de/steamwar/command/SWCommandUtils.java | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 012d96a..75feed9 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -381,5 +381,10 @@ public abstract class AbstractSWCommand { * Error message to be displayed when the parameter is invalid. */ String value(); + + /** + * This is the short form for 'allowEmptyArrays'. + */ + boolean allowEAs() default true; } } diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index cea1dd6..86716d2 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -23,6 +23,7 @@ import lombok.Getter; import lombok.experimental.UtilityClass; import java.lang.annotation.Annotation; +import java.lang.reflect.Array; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; @@ -187,8 +188,12 @@ public class SWCommandUtils { AbstractSWCommand.ErrorMessage errorMessage = parameter.getAnnotation(AbstractSWCommand.ErrorMessage.class); if (errorMessage != null) { - return (AbstractValidator) (sender, value, messageSender) -> { - if (value == null) messageSender.send(errorMessage.value(), new Object[0]); + return (AbstractValidator) (sender, value, messageSender) -> { + if (value == null) messageSender.send(errorMessage.value()); + if (!errorMessage.allowEAs() && value != null && value.getClass().isArray() && Array.getLength(value) == 0) { + messageSender.send(errorMessage.value()); + return false; + } return value != null; }; }