Skip to content

Commit

Permalink
Added validate float value in BossBarProgressArgument.
Browse files Browse the repository at this point in the history
Added argument name to BossBarProgressArgument.
Configuration improvements.
  • Loading branch information
imDMK committed Aug 29, 2023
1 parent cf34da3 commit 659a44c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import panda.std.Result;

import java.util.List;

@ArgumentName("progress")
public class BossBarProgressArgument implements OneArgument<Float> {

private final NotificationConfiguration notificationConfiguration;
Expand All @@ -19,11 +21,16 @@ public BossBarProgressArgument(NotificationConfiguration notificationConfigurati
@Override
public Result<Float, ?> 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<Suggestion> suggest(LiteInvocation invocation) {
return Suggestion.of("0.1", "0.5", "1.0");
}

private boolean isValid(float value) {
return value > 0.0F && value <= 1.0F;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public class NotificationConfiguration extends OkaeriConfig {
public Notification autoMessagesEnabledNotification = new ChatNotification("<green>Enabled automatic messages<dark_gray>.");
public Notification autoMessagesDisabledNotification = new ChatNotification("<red>Disabled automatic messages<dark_gray>.");

public Notification autoMessagesChangedDelayNotification = new ChatNotification("<green>Changed automatic messages delay<dark_gray>.");

@Comment("# {POSITION} - Position of automatic message")
public Notification autoMessageAddedNotification = new ChatNotification("<green>Added new automatic message with type {TYPE} at position {POSITION}<dark_gray>.");
public Notification autoMessageRemovedNotification = new ChatNotification("<red>Removed automatic message<dark_gray>.");
Expand All @@ -68,7 +66,6 @@ public class NotificationConfiguration extends OkaeriConfig {
"# {NOTIFICATION} - Notification information"
})
public Notification autoMessagesListNotification = new ChatNotification("<gray>{POSITION}<dark_gray>. <red>{NOTIFICATION}");

public Notification autoMessagesEmptyNotification = new ChatNotification("<red>Automatic messages is empty<dark_gray>.");

public Notification autoMessageNotFoundNotification = new ChatNotification("<red>Automatic message not found<dark_gray>.");
Expand All @@ -81,7 +78,8 @@ public class NotificationConfiguration extends OkaeriConfig {
public Notification invalidUsageFirstNotification = new ChatNotification("<red>Invalid usage<dark_gray>:");
public Notification invalidUsageListNotification = new ChatNotification("<dark_gray>- <red>{USAGE}");

public Notification invalidNumberNotification = new ChatNotification("<red>Invalid number<dark_gray>.");
public Notification invalidNumberNotification = new ChatNotification("<red>Invalid number value<dark_gray>.");
public Notification invalidFloatNotification = new ChatNotification("<red>Invalid float value<dark_gray>.");
public Notification invalidTypeNotification = new ChatNotification("<red>Invalid notification type<dark_gray>.");
public Notification missingPermissionNotification = new ChatNotification("<red>Missing permissions<dark_gray>: <red>{PERMISSIONS}");

Expand Down

0 comments on commit 659a44c

Please sign in to comment.