From 659a44c010dbedb4c2993f4bc6653370aae0ee19 Mon Sep 17 00:00:00 2001 From: Dominik Date: Tue, 29 Aug 2023 17:46:37 +0200 Subject: [PATCH] Added validate float value in BossBarProgressArgument. Added argument name to BossBarProgressArgument. Configuration improvements. --- .../command/argument/BossBarProgressArgument.java | 9 ++++++++- .../configuration/NotificationConfiguration.java | 6 ++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/imdmk/automessage/command/argument/BossBarProgressArgument.java b/src/main/java/com/github/imdmk/automessage/command/argument/BossBarProgressArgument.java index 7fad324..4a88f97 100644 --- a/src/main/java/com/github/imdmk/automessage/command/argument/BossBarProgressArgument.java +++ b/src/main/java/com/github/imdmk/automessage/command/argument/BossBarProgressArgument.java @@ -1,6 +1,7 @@ package com.github.imdmk.automessage.command.argument; import com.github.imdmk.automessage.notification.configuration.NotificationConfiguration; +import dev.rollczi.litecommands.argument.ArgumentName; import dev.rollczi.litecommands.argument.simple.OneArgument; import dev.rollczi.litecommands.command.LiteInvocation; import dev.rollczi.litecommands.suggestion.Suggestion; @@ -8,6 +9,7 @@ import java.util.List; +@ArgumentName("progress") public class BossBarProgressArgument implements OneArgument { private final NotificationConfiguration notificationConfiguration; @@ -19,11 +21,16 @@ public BossBarProgressArgument(NotificationConfiguration notificationConfigurati @Override public Result parse(LiteInvocation invocation, String argument) { return Result.supplyThrowing(NumberFormatException.class, () -> Float.parseFloat(argument)) - .mapErr(exception -> this.notificationConfiguration.invalidNumberNotification); + .filter(this::isValid, e -> new NumberFormatException("The float value must be greater than 0 and less than 1.0")) + .mapErr(exception -> this.notificationConfiguration.invalidFloatNotification); } @Override public List suggest(LiteInvocation invocation) { return Suggestion.of("0.1", "0.5", "1.0"); } + + private boolean isValid(float value) { + return value > 0.0F && value <= 1.0F; + } } diff --git a/src/main/java/com/github/imdmk/automessage/notification/configuration/NotificationConfiguration.java b/src/main/java/com/github/imdmk/automessage/notification/configuration/NotificationConfiguration.java index d7d7b7d..34d67fc 100644 --- a/src/main/java/com/github/imdmk/automessage/notification/configuration/NotificationConfiguration.java +++ b/src/main/java/com/github/imdmk/automessage/notification/configuration/NotificationConfiguration.java @@ -50,8 +50,6 @@ public class NotificationConfiguration extends OkaeriConfig { public Notification autoMessagesEnabledNotification = new ChatNotification("Enabled automatic messages."); public Notification autoMessagesDisabledNotification = new ChatNotification("Disabled automatic messages."); - public Notification autoMessagesChangedDelayNotification = new ChatNotification("Changed automatic messages delay."); - @Comment("# {POSITION} - Position of automatic message") public Notification autoMessageAddedNotification = new ChatNotification("Added new automatic message with type {TYPE} at position {POSITION}."); public Notification autoMessageRemovedNotification = new ChatNotification("Removed automatic message."); @@ -68,7 +66,6 @@ public class NotificationConfiguration extends OkaeriConfig { "# {NOTIFICATION} - Notification information" }) public Notification autoMessagesListNotification = new ChatNotification("{POSITION}. {NOTIFICATION}"); - public Notification autoMessagesEmptyNotification = new ChatNotification("Automatic messages is empty."); public Notification autoMessageNotFoundNotification = new ChatNotification("Automatic message not found."); @@ -81,7 +78,8 @@ public class NotificationConfiguration extends OkaeriConfig { public Notification invalidUsageFirstNotification = new ChatNotification("Invalid usage:"); public Notification invalidUsageListNotification = new ChatNotification("- {USAGE}"); - public Notification invalidNumberNotification = new ChatNotification("Invalid number."); + public Notification invalidNumberNotification = new ChatNotification("Invalid number value."); + public Notification invalidFloatNotification = new ChatNotification("Invalid float value."); public Notification invalidTypeNotification = new ChatNotification("Invalid notification type."); public Notification missingPermissionNotification = new ChatNotification("Missing permissions: {PERMISSIONS}");