aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelfull <admin@helfull.de>2023-10-14 15:58:35 +0200
committerGitHub <noreply@github.com>2023-10-14 15:58:35 +0200
commitd376ff6ccac98716de41b5d8d77248254e002d11 (patch)
tree4cedb3a273528a2a1b3a0f3b7fbc788d93be0b20
parent01f95e0ed4106ee4cd76384e1437fdfdc38b0a0d (diff)
downloadskyhanni-d376ff6ccac98716de41b5d8d77248254e002d11.tar.gz
skyhanni-d376ff6ccac98716de41b5d8d77248254e002d11.tar.bz2
skyhanni-d376ff6ccac98716de41b5d8d77248254e002d11.zip
Internal Changes: add support for compacted numbers parsing (#555)
add support for compacted numbers parsing #555
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt16
-rw-r--r--src/test/java/at/hannibal2/skyhanni/test/ItemUtilsTest.kt34
2 files changed, 44 insertions, 6 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt
index 06e7108d9..c81841268 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt
@@ -8,6 +8,7 @@ import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.cachedData
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRecombobulated
import at.hannibal2.skyhanni.utils.StringUtils.matchRegex
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
+import at.hannibal2.skyhanni.utils.NumberUtil.formatNumber
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import net.minecraft.client.Minecraft
@@ -18,6 +19,7 @@ import net.minecraft.nbt.NBTTagList
import net.minecraft.nbt.NBTTagString
import net.minecraftforge.common.util.Constants
import java.util.LinkedList
+import java.util.regex.Matcher
import kotlin.time.Duration.Companion.seconds
object ItemUtils {
@@ -239,7 +241,7 @@ object ItemUtils {
fun isSkyBlockMenuItem(stack: ItemStack?): Boolean = stack?.getInternalName_old() == "SKYBLOCK_MENU"
- private val patternInFront = "(?: *§8(?<amount>[\\d,]+)x )?(?<name>.*)".toPattern()
+ private val patternInFront = "(?: *§8(\\+§[\\d\\w])?(?<amount>[\\d\\.km,]+)(x )?)?(?<name>.*)".toPattern()
private val patternBehind = "(?<name>(?:['\\w-]+ ?)+)(?:§8x(?<amount>[\\d,]+))?".toPattern()
private val itemAmountCache = mutableMapOf<String, Pair<String, Int>>()
@@ -258,10 +260,7 @@ object ItemUtils {
if (matcher.matches()) {
val itemName = matcher.group("name")
if (!itemName.contains("§8x")) {
- val amount = matcher.group("amount")?.replace(",", "")?.toInt() ?: 1
- val pair = Pair(itemName.trim(), amount)
- itemAmountCache[input] = pair
- return pair
+ return makePair(input, itemName.trim(), matcher)
}
}
@@ -277,7 +276,12 @@ object ItemUtils {
}
val itemName = color + matcher.group("name").trim()
- val amount = matcher.group("amount")?.replace(",", "")?.toInt() ?: 1
+ return makePair(input, itemName, matcher)
+ }
+
+ private fun makePair(input: String, itemName: String, matcher: Matcher): Pair<String, Int> {
+ val matcherAmount = matcher.group("amount")
+ val amount = matcherAmount?.formatNumber()?.toInt() ?: 1;
val pair = Pair(itemName, amount)
itemAmountCache[input] = pair
return pair
diff --git a/src/test/java/at/hannibal2/skyhanni/test/ItemUtilsTest.kt b/src/test/java/at/hannibal2/skyhanni/test/ItemUtilsTest.kt
new file mode 100644
index 000000000..0ac8cc8de
--- /dev/null
+++ b/src/test/java/at/hannibal2/skyhanni/test/ItemUtilsTest.kt
@@ -0,0 +1,34 @@
+package at.hannibal2.skyhanni.test
+
+import at.hannibal2.skyhanni.utils.ItemUtils
+import org.junit.jupiter.api.Test
+
+class ItemUtilsTest {
+
+ val items: MutableMap<String, Pair<String, Int>> = mutableMapOf(
+ "§5Hoe of Greatest Tilling" to Pair("§5Hoe of Greatest Tilling", 1),
+ "§fSilver medal §8x2" to Pair("§fSilver medal", 2),
+ "§aJacob's Ticket §8x32" to Pair("§aJacob's Ticket", 32),
+ "§9Delicate V" to Pair("§9Delicate V", 1),
+ " §81x §9Enchanted Sugar Cane" to Pair("§9Enchanted Sugar Cane", 1),
+ "§6Gold medal" to Pair("§6Gold medal", 1),
+ " §8+§319k §7Farming XP" to Pair("§7Farming XP", 19_000),
+ " §8+§215 §7Garden Experience" to Pair("§7Garden Experience", 15),
+ " §8+§c21 Copper" to Pair("Copper", 21),
+ " §8+§b10 Bits" to Pair("Bits", 10),
+ " §8+§37.2k §7Farming XP" to Pair("§7Farming XP", 7_200),
+ )
+
+ @Test
+ fun testReadItemAmount() {
+ for ((itemString, expected) in items) {
+ val results = ItemUtils.readItemAmount(itemString)
+ assert(results != null) {
+ "Could not read item '$itemString'"
+ }
+ assert(results?.equals(expected) == true) {
+ "'${results.toString()}' does not match '$expected'"
+ }
+ }
+ }
+} \ No newline at end of file