From 8c6d98a6664661da795a73e4370044d809ea70b5 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 26 Apr 2022 11:17:24 +0100 Subject: [PATCH] Added string progress bars --- .../java/com/willfp/eco/util/StringUtils.java | 54 +++++++++++++++++++ eco-api/src/test/java/StringUtilsTest.java | 43 +++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java index e4ac0646..2f4f0355 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java @@ -13,6 +13,7 @@ import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.md_5.bungee.api.ChatColor; +import org.apache.commons.lang.Validate; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -633,6 +634,59 @@ public final class StringUtils { return SPACE_AROUND_CHARACTER.get(separator).split(input); } + /** + * Create progress bar. + * + * @param character The bar character. + * @param bars The number of bars. + * @param progress The bar progress, between 0 and 1. + * @param completeFormat The color of a complete bar section. + * @param inProgressFormat The color of an in-progress bar section. + * @param incompleteFormat The color of an incomplete bar section. + * @return The progress bar. + */ + @NotNull + public static String createProgressBar(final char character, + final int bars, + final double progress, + @NotNull final String completeFormat, + @NotNull final String inProgressFormat, + @NotNull final String incompleteFormat) { + Validate.isTrue(progress >= 0 && progress <= 1, "Progress must be between 0 and 1!"); + Validate.isTrue(bars > 1, "Must have at least 2 bars!"); + + String completeColor = format(completeFormat); + String inProgressColor = format(inProgressFormat); + String incompleteColor = format(incompleteFormat); + + StringBuilder builder = new StringBuilder(); + + // Full bar special case. + if (progress == 1) { + builder.append(completeColor); + builder.append(String.valueOf(character).repeat(bars)); + return builder.toString(); + } + + int completeBars = (int) Math.floor(progress * bars); + int incompleteBars = bars - completeBars - 1; + + if (completeBars > 0) { + builder.append(completeColor) + .append(String.valueOf(character).repeat(completeBars)); + } + + builder.append(inProgressColor) + .append(character); + + if (incompleteBars > 0) { + builder.append(incompleteColor) + .append(String.valueOf(character).repeat(incompleteBars)); + } + + return builder.toString(); + } + /** * Options for formatting. */ diff --git a/eco-api/src/test/java/StringUtilsTest.java b/eco-api/src/test/java/StringUtilsTest.java index 53259358..9c6e85ea 100644 --- a/eco-api/src/test/java/StringUtilsTest.java +++ b/eco-api/src/test/java/StringUtilsTest.java @@ -26,4 +26,47 @@ public class StringUtilsTest { StringUtils.splitAround("one|| two", "||") ); } + + @Test + public void testProgressBar() { + Assertions.assertEquals( + "&a--------", + StringUtils.createProgressBar( + '-', + 8, + 1, + "&a", "&e", "&c" + ) + ); + + Assertions.assertEquals( + "&a-------&e-", + StringUtils.createProgressBar( + '-', + 8, + 0.99, + "&a", "&e", "&c" + ) + ); + + Assertions.assertEquals( + "&e-&c-------", + StringUtils.createProgressBar( + '-', + 8, + 0.0001, + "&a", "&e", "&c" + ) + ); + + Assertions.assertEquals( + "&a----&e-&c---", + StringUtils.createProgressBar( + '-', + 8, + 0.5, + "&a", "&e", "&c" + ) + ); + } }