Skip to content

Commit 9075b33

Browse files
committed
Do not provide mod versions which do not match the client's game version
1 parent ec7a48b commit 9075b33

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
plugins {
4-
kotlin("jvm") version "1.6.0"
4+
kotlin("jvm") version "1.8.21"
5+
kotlin("plugin.serialization") version "1.8.21"
56
}
67

78
group = "com.chattriggers"
@@ -13,7 +14,8 @@ repositories {
1314
}
1415

1516
dependencies {
16-
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0")
17+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.21")
18+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
1719
implementation("club.minnced:discord-webhooks:0.8.0")
1820
implementation("io.javalin:javalin:4.5.0")
1921
implementation("com.zaxxer:HikariCP:5.0.1")

src/main/kotlin/com/chattriggers/website/api/versions.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.chattriggers.website.api
33
import com.fasterxml.jackson.core.Version
44
import io.javalin.apibuilder.ApiBuilder.get
55
import io.javalin.http.Context
6+
import kotlinx.serialization.json.Json
67
import java.io.File
78

89
fun versionRoutes() {
@@ -17,11 +18,21 @@ var lastVersionsCheckTime = System.currentTimeMillis()
1718
fun getVersions(ctx: Context) {
1819
if (System.currentTimeMillis() - lastVersionsCheckTime > VERSIONS_CHECK_TIMEOUT) {
1920
allowedVersions = loadVersions()
20-
2121
lastVersionsCheckTime = System.currentTimeMillis()
2222
}
2323

24-
val versions = allowedVersions.groupBy({
24+
// Only consider mod versions which are available for the requested version. The
25+
// frontend uses the value "all" to signify it wants every version
26+
val requestModVersion = ctx.queryParam("modVersion") ?: "1.8.9"
27+
val versionsToConsider = if (requestModVersion == "all") {
28+
allowedVersions.keys
29+
} else {
30+
allowedVersions.filter {(_, values) ->
31+
values.any { it == requestModVersion }
32+
}.keys
33+
}
34+
35+
val versions = versionsToConsider.groupBy({
2536
"${it.majorVersion}.${it.minorVersion}"
2637
}, { version ->
2738
val snapshot = if (version.isSnapshot) {
@@ -33,8 +44,7 @@ fun getVersions(ctx: Context) {
3344
ctx.status(200).json(versions)
3445
}
3546

36-
private fun loadVersions() = File(VERSIONS_FILE)
37-
.readText()
38-
.split("\n")
39-
.filter { it.isNotBlank() }
40-
.map { it.trim().toVersion() }
47+
private fun loadVersions(): Map<Version, List<String>> {
48+
return Json.decodeFromString<Map<String, List<String>>>(File(VERSIONS_FILE).readText())
49+
.mapKeys { it.key.toVersion() }
50+
}

versions.txt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
2.0.0
2-
1.3.0
3-
1.2.2
4-
1.2.1
5-
1.2.0
6-
1.1.2
7-
1.1.1
8-
1.1.0
9-
1.0.0
10-
0.18.4
1+
{
2+
"2.0.0": ["1.8.9"],
3+
"1.3.0": ["1.8.9"],
4+
"1.2.2": ["1.8.9"],
5+
"1.2.1": ["1.8.9"],
6+
"1.2.0": ["1.8.9"],
7+
"1.1.2": ["1.8.9"],
8+
"1.1.1": ["1.8.9"],
9+
"1.1.0": ["1.8.9"],
10+
"1.0.0": ["1.8.9"],
11+
"0.18.4": ["1.8.9"],
12+
"3.0.0-beta": ["1.19.4"]
13+
}

0 commit comments

Comments
 (0)