Skip to content

Commit b0bb351

Browse files
committed
fix: mojang api on other thread
1 parent b902d92 commit b0bb351

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cc.modlabs.kpaper.game
2+
3+
import org.bukkit.entity.Player
4+
5+
interface GamePlayers {
6+
7+
var livingPlayers: MutableList<Player>
8+
9+
var spectators: MutableList<Player>
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cc.modlabs.kpaper.game.countdown
2+
3+
import cc.modlabs.kpaper.game.GamePlayers
4+
import org.bukkit.scheduler.BukkitTask
5+
6+
abstract class Countdown(val game: GamePlayers, val defaultDuration: Int) {
7+
8+
var duration: Int = defaultDuration
9+
10+
11+
lateinit var countdown: BukkitTask
12+
abstract fun start()
13+
14+
abstract fun stop()
15+
}

src/main/kotlin/cc/modlabs/kpaper/utils/MojangAPI.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package cc.modlabs.kpaper.utils
22

33
import com.google.gson.Gson
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
46
import java.net.URI
57

68
class MojangAPI {
79
private val gson = Gson()
810

911
fun getUser(user: String): MclSuccessResponse? {
10-
val url = "https://mcl.flawcra.cc/$user"
11-
val response = URI.create(url).toURL().readText()
12+
try {
13+
CoroutineScope(Dispatchers.Default).run {
14+
val url = "https://mcl.flawcra.cc/$user"
15+
val response = URI.create(url).toURL().readText()
1216

13-
val errorResponse = gson.fromJson(response, MclErrorResponse::class.java)
14-
if (errorResponse.error != null) {
17+
val errorResponse = gson.fromJson(response, MclErrorResponse::class.java)
18+
if (errorResponse.error != null) {
19+
return null
20+
}
21+
22+
return gson.fromJson(response, MclSuccessResponse::class.java)
23+
}
24+
} catch (e: Exception) {
1525
return null
1626
}
17-
18-
return gson.fromJson(response, MclSuccessResponse::class.java)
1927
}
2028
}
2129

0 commit comments

Comments
 (0)