From 587892f3f1be5b617132092484246fd92fbbeb93 Mon Sep 17 00:00:00 2001 From: NoahTheNerd Date: Mon, 17 Jun 2024 19:31:55 +0200 Subject: [PATCH] Fixes & improvements * You can now simply specify something like "no-clear" in `tap:bossbar` without having to add a value to it. * Improved version parsing greatly * Fixed a crash with invalid parameters --- .../live/ixnoah/tapactions/TapActions.kt | 16 +++------ .../ixnoah/tapactions/misc/TitleHandler.kt | 6 ++-- .../live/ixnoah/tapactions/misc/Version.kt | 35 +++++++++++++++++++ 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/live/ixnoah/tapactions/misc/Version.kt diff --git a/src/main/kotlin/live/ixnoah/tapactions/TapActions.kt b/src/main/kotlin/live/ixnoah/tapactions/TapActions.kt index 4478ca0..72156b7 100644 --- a/src/main/kotlin/live/ixnoah/tapactions/TapActions.kt +++ b/src/main/kotlin/live/ixnoah/tapactions/TapActions.kt @@ -9,6 +9,7 @@ import live.ixnoah.tapactions.actions.WorldActions import live.ixnoah.tapactions.commands.CreateActionCommand import live.ixnoah.tapactions.events.ClientTick import live.ixnoah.tapactions.events.WorldLoad +import live.ixnoah.tapactions.misc.Version import net.minecraftforge.client.ClientCommandHandler import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.common.Mod @@ -21,16 +22,12 @@ import java.net.URL class TapActions { companion object ModInfo { const val MOD_ID = "tapactions" - const val MOD_VER = "0.2.0" + val MOD_VER = Version("0.2.0") var outdated = false var newerVersion = MOD_VER } - private fun versionParser(semver: String): Int { - return (semver.replace(Regex("\\D+"), "").toIntOrNull() ?: 0) / 10 // div by 10 to exclude patches - } - @Mod.EventHandler fun init(event: FMLInitializationEvent) { // Commands @@ -51,14 +48,9 @@ class TapActions { val apiResponse = Gson().fromJson(InputStreamReader(url.openStream()), JsonArray::class.java) val latestVersion = apiResponse.get(0) as JsonObject - val versionNumber = latestVersion.get("version_number").asString - - if (versionParser(versionNumber) > versionParser(MOD_VER)) { - newerVersion = versionNumber - outdated = true - } - + val LATEST_VER = Version(latestVersion.get("version_number").asString) + if (MOD_VER.lessThan(LATEST_VER)) outdated = true } catch (e: Exception) { throw RuntimeException(e) } diff --git a/src/main/kotlin/live/ixnoah/tapactions/misc/TitleHandler.kt b/src/main/kotlin/live/ixnoah/tapactions/misc/TitleHandler.kt index c64be68..b1dd14d 100644 --- a/src/main/kotlin/live/ixnoah/tapactions/misc/TitleHandler.kt +++ b/src/main/kotlin/live/ixnoah/tapactions/misc/TitleHandler.kt @@ -16,14 +16,14 @@ object TitleHandler { var components = messageContent.split('␟').filter { data -> data.isNotEmpty() } - val actionName = components[0] ?: "void" + val actionName = components.getOrNull(0) ?: "void" components = components.drop(1) val actionParams = mutableMapOf() components.map { paramData -> - val keyValue = paramData.replace('=', '␃').split('␃') - actionParams[keyValue[0]] = keyValue[1] + val keyValue : List = paramData.replaceFirst('=', '␃').split('␃') + actionParams[keyValue.getOrNull(0) ?: "void"] = keyValue.getOrNull(1) ?: "void" } ActionManager.runAction(actionName, actionParams, true) diff --git a/src/main/kotlin/live/ixnoah/tapactions/misc/Version.kt b/src/main/kotlin/live/ixnoah/tapactions/misc/Version.kt new file mode 100644 index 0000000..e55662d --- /dev/null +++ b/src/main/kotlin/live/ixnoah/tapactions/misc/Version.kt @@ -0,0 +1,35 @@ +package live.ixnoah.tapactions.misc + +data class Version(val version: String) { + val domains = version.split('.') + .map { d -> return@map d.toIntOrNull() } + .filterNotNull() + + fun compareTo(other: Version, depth: Int = 3): Int { + if (domains.size < other.domains.size) { + domains.forEachIndexed { i, d -> + if (i > depth) return 0 + if (other.domains[i] == d) return@forEachIndexed + if (other.domains[i] > d) return 1 + if (other.domains[i] < d) return -1 + } + } else { + other.domains.forEachIndexed { i, d -> + if (i > depth) return 0 + if (domains[i] == d) return@forEachIndexed + if (domains[i] > d) return 1 + if (domains[i] < d) return -1 + } + } + + return 0 + } + + fun greaterThan(other: Version, depth: Int = 3): Boolean { + return this.compareTo(other, depth) > 0 + } + + fun lessThan(other: Version, depth: Int = 3): Boolean { + return this.compareTo(other, depth) > 0 + } +} \ No newline at end of file