diff options
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt | 45 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt | 31 |
2 files changed, 59 insertions, 17 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt index 630d4fb83..459bfc25a 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/CollectionTracker.kt @@ -11,9 +11,12 @@ import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName -import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NEUItems.getItemStack import at.hannibal2.skyhanni.utils.NEUItems.getItemStackOrNull +import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.NumberUtil.formatLong +import at.hannibal2.skyhanni.utils.NumberUtil.isFormatNumber +import at.hannibal2.skyhanni.utils.NumberUtil.percentWithColorCode import at.hannibal2.skyhanni.utils.RenderUtils.renderStringsAndItems import at.hannibal2.skyhanni.utils.StringUtils.removeColor import net.minecraft.client.Minecraft @@ -31,6 +34,7 @@ class CollectionTracker { private var itemName = "" private var internalName: NEUInternalName? = null private var itemAmount = -1L + private var goalAmount = -1L private var lastAmountInInventory = -1 @@ -40,7 +44,7 @@ class CollectionTracker { fun command(args: Array<String>) { if (args.isEmpty()) { if (internalName == null) { - ChatUtils.userError("/shtrackcollection <item name>") + ChatUtils.userError("/shtrackcollection <item name> [goal amount]") return } ChatUtils.chat("Stopped collection tracker.") @@ -48,7 +52,22 @@ class CollectionTracker { return } - val rawName = fixTypo(args.joinToString(" ").lowercase().replace("_", " ")) + val lastArg = args.last() + + val nameArgs = if (lastArg.isFormatNumber()) { + val goal = lastArg.formatLong() + if (goal <= 0) { + ChatUtils.chat("Invalid Amount for Goal.") + return + } + goalAmount = goal + args.dropLast(1).toTypedArray() + } else { + goalAmount = -1L + args + } + + val rawName = fixTypo(nameArgs.joinToString(" ").lowercase().replace("_", " ")) if (rawName == "gemstone") { ChatUtils.userError("Gemstone collection is not supported!") return @@ -57,7 +76,7 @@ class CollectionTracker { return } - val foundInternalName = NEUItems.getInternalNameOrNull(rawName) + val foundInternalName = NEUInternalName.fromItemNameOrNull(rawName) if (foundInternalName == null) { ChatUtils.error("Item '$rawName' does not exist!") return @@ -111,7 +130,8 @@ class CollectionTracker { } private fun resetData() { - itemAmount = -1 + itemAmount = -1L + goalAmount = -1L internalName = null lastAmountInInventory = -1 @@ -121,18 +141,27 @@ class CollectionTracker { } private fun updateDisplay() { - val format = LorenzUtils.formatInteger(itemAmount) + val format = itemAmount.addSeparators() var gainText = "" if (recentGain != 0) { - gainText = "§a+" + LorenzUtils.formatInteger(recentGain) + gainText = "§a+" + recentGain.addSeparators() + } + + if (goalAmount != -1L && itemAmount >= goalAmount) { + ChatUtils.chat("Collection goal of §a${goalAmount.addSeparators()} reached!") + goalAmount = -1L } + val goal = if (goalAmount == -1L) "" else " §f/ §b${goalAmount.addSeparators()} §f(§a${ + itemAmount.percentWithColorCode(goalAmount, 1) + }§f)" + display = Collections.singletonList(buildList { internalName?.let { add(it.getItemStack()) } - add("$itemName collection: §e$format $gainText") + add("$itemName collection: §e$format$goal $gainText") }) } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt index c4dea1046..92cbe4d57 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt @@ -1,5 +1,7 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.utils.LorenzUtils.round +import at.hannibal2.skyhanni.utils.StringUtils.matches import java.text.NumberFormat import java.util.Locale import java.util.TreeMap @@ -171,20 +173,31 @@ object NumberUtil { } val pattern = "^[0-9]*$".toPattern() + val formatPattern = "^[0-9,.]*[kmb]?$".toPattern() fun String.isInt(): Boolean { return isNotEmpty() && pattern.matcher(this).matches() } - fun percentageColor(have: Long, max: Long): LorenzColor { - val percentage = have.fractionOf(max) - return when { - percentage > 0.9 -> LorenzColor.DARK_GREEN - percentage > 0.75 -> LorenzColor.GREEN - percentage > 0.5 -> LorenzColor.YELLOW - percentage > 0.25 -> LorenzColor.GOLD - else -> LorenzColor.RED - } + fun String.isFormatNumber(): Boolean { + return isNotEmpty() && formatPattern.matches(this) + } + + fun percentageColor(percentage: Double) = when { + percentage > 0.9 -> LorenzColor.DARK_GREEN + percentage > 0.75 -> LorenzColor.GREEN + percentage > 0.5 -> LorenzColor.YELLOW + percentage > 0.25 -> LorenzColor.GOLD + else -> LorenzColor.RED + } + + fun percentageColor(have: Long, max: Long): LorenzColor = percentageColor(have.fractionOf(max)) + + fun Number.percentWithColorCode(max: Number, round: Int = 1): String { + val fraction = this.fractionOf(max) + val color = percentageColor(fraction) + val amount = (fraction * 100.0).round(round) + return "${color.getChatColor()}$amount%" } // TODO create new function formatLong, and eventually deprecate this function. |