aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal00212@users.noreply.github.com>2024-02-25 10:11:13 +0100
committerhannibal2 <24389977+hannibal00212@users.noreply.github.com>2024-02-25 10:11:13 +0100
commit39510354d45f46e0f3d65c3a5d8f08be5ad680f3 (patch)
tree43697bf707d09a727928afd35cbc6c5634c952e1 /src/main/java
parent368e75a26955703121bdb116503cf0544ad2e96a (diff)
downloadskyhanni-39510354d45f46e0f3d65c3a5d8f08be5ad680f3.tar.gz
skyhanni-39510354d45f46e0f3d65c3a5d8f08be5ad680f3.tar.bz2
skyhanni-39510354d45f46e0f3d65c3a5d8f08be5ad680f3.zip
Made String.formatDouble() and formatLong()return nullable.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt6
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt11
2 files changed, 11 insertions, 6 deletions
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