Skip to content

Commit

Permalink
Fixes & improvements
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ixnoahlive committed Jun 17, 2024
1 parent 0ff5959 commit 587892f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
16 changes: 4 additions & 12 deletions src/main/kotlin/live/ixnoah/tapactions/TapActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/live/ixnoah/tapactions/misc/TitleHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>()

components.map { paramData ->
val keyValue = paramData.replace('=', '').split('')
actionParams[keyValue[0]] = keyValue[1]
val keyValue : List<String?> = paramData.replaceFirst('=', '').split('')
actionParams[keyValue.getOrNull(0) ?: "void"] = keyValue.getOrNull(1) ?: "void"
}

ActionManager.runAction(actionName, actionParams, true)
Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/live/ixnoah/tapactions/misc/Version.kt
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 587892f

Please sign in to comment.