aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt8
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropTimeCommand.kt10
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt7
3 files changed, 10 insertions, 15 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt b/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt
index b21c10d41..b4965c3ed 100644
--- a/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt
+++ b/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt
@@ -21,7 +21,7 @@ import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ItemUtils.cleanName
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
-import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
+import at.hannibal2.skyhanni.utils.NumberUtil.formatLongOrUserError
import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary
import at.hannibal2.skyhanni.utils.SimpleTimeMark
@@ -394,11 +394,7 @@ object SkillAPI {
val second = it[1]
when (first) {
"levelwithxp" -> {
- val xp = second.formatLong()
- if (xp == null) {
- ChatUtils.userError("Not a valid number: '$second'")
- return
- }
+ val xp = second.formatLongOrUserError() ?: return
if (xp <= XP_NEEDED_FOR_60) {
val level = getLevel(xp)
ChatUtils.chat("With §b${xp.addSeparators()} §eXP you would be level §b$level")
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropTimeCommand.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropTimeCommand.kt
index 217b01259..74269c52d 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropTimeCommand.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropTimeCommand.kt
@@ -7,7 +7,7 @@ import at.hannibal2.skyhanni.utils.CollectionUtils.sorted
import at.hannibal2.skyhanni.utils.ItemUtils.itemName
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
-import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber
+import at.hannibal2.skyhanni.utils.NumberUtil.formatLongOrUserError
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TimeUtils
@@ -26,13 +26,7 @@ object GardenCropTimeCommand {
return
}
- val rawAmount = args[0]
- val amount = try {
- rawAmount.formatNumber()
- } catch (e: NumberFormatException) {
- ChatUtils.userError("Not a valid number: '$rawAmount'")
- return
- }
+ val amount = args[0].formatLongOrUserError() ?: return
val multipliers = CropMoneyDisplay.multipliers
if (multipliers.isEmpty()) {
ChatUtils.userError("Data not loaded yet. Join the garden and display the money per hour display.")
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt
index 390516e0a..b481eff35 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt
@@ -189,10 +189,15 @@ object NumberUtil {
// TODO create new function formatLong, and eventually deprecate this function.
@Deprecated("renamed", ReplaceWith("this.formatLong()"))
- fun String.formatNumber(): Long = formatLong() ?: error("formatNumber has a NumberFormatException with '$this'")
+ fun String.formatNumber(): Long = formatLong() ?: throw NumberFormatException("formatNumber has a NumberFormatException with '$this'")
fun String.formatLong(): Long? = formatDouble()?.toLong()
+ fun String.formatLongOrUserError(): Long? = formatDouble()?.toLong() ?: run {
+ ChatUtils.userError("Not a valid number: '$this'")
+ return@run null
+ }
+
fun String.formatDouble(): Double? {
var text = lowercase().replace(",", "")