From 39510354d45f46e0f3d65c3a5d8f08be5ad680f3 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sun, 25 Feb 2024 10:11:13 +0100 Subject: Made String.formatDouble() and formatLong()return nullable. --- src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt | 6 +++++- src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | 11 ++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt b/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt index fe13c5c78..a383b377e 100644 --- a/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt @@ -394,6 +394,10 @@ object SkillAPI { when (first) { "levelwithxp" -> { val xp = second.formatLong() + if (xp == null) { + ChatUtils.userError("Not a valid number: '$second'") + return + } if (xp <= XP_NEEDED_FOR_60) { val level = getLevel(xp) ChatUtils.chat("With §b${xp.addSeparators()} §eXP you would be level §b$level") @@ -410,7 +414,7 @@ object SkillAPI { "xpforlevel" -> { val level = second.toIntOrNull() if (level == null) { - ChatUtils.userError("Not a number: '$second'") + ChatUtils.userError("Not a valid number: '$second'") return } if (level <= 60) { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt index 6deee85bc..390516e0a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt @@ -189,11 +189,11 @@ object NumberUtil { // TODO create new function formatLong, and eventually deprecate this function. @Deprecated("renamed", ReplaceWith("this.formatLong()")) - fun String.formatNumber(): Long = formatLong() + fun String.formatNumber(): Long = formatLong() ?: error("formatNumber has a NumberFormatException with '$this'") - fun String.formatLong(): Long = formatDouble().toLong() + fun String.formatLong(): Long? = formatDouble()?.toLong() - fun String.formatDouble(): Double { + fun String.formatDouble(): Double? { var text = lowercase().replace(",", "") val multiplier = if (text.endsWith("k")) { @@ -206,8 +206,9 @@ object NumberUtil { text = text.substring(0, text.length - 1) 1.bilion } else 1.0 - val d = text.toDouble() - return d * multiplier + return text.toDoubleOrNull()?.let { + it * multiplier + } } val Int.milion get() = this * 1_000_000.0 -- cgit