Skip to content

Commit 8106db8

Browse files
committed
Convert Update checker to coroutine
1 parent 64e56eb commit 8106db8

File tree

3 files changed

+69
-72
lines changed

3 files changed

+69
-72
lines changed
Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package io.github.rothes.esu.bukkit
22

33
import io.github.rothes.esu.bukkit.user.ConsoleUser
4-
import io.github.rothes.esu.bukkit.util.scheduler.ScheduledTask
5-
import io.github.rothes.esu.bukkit.util.scheduler.Scheduler
6-
import io.github.rothes.esu.core.config.EsuConfig
4+
import io.github.rothes.esu.core.EsuCore
75
import io.github.rothes.esu.core.user.User
86
import io.github.rothes.esu.core.util.UpdateChecker
97
import io.github.rothes.esu.core.util.UpdateChecker.VersionAction
@@ -12,44 +10,29 @@ import java.util.*
1210

1311
object UpdateCheckerMan {
1412

15-
private val checker by lazy { UpdateChecker(
16-
BuildConfig.VERSION_ID.toInt(),
17-
BuildConfig.VERSION_CHANNEL,
18-
BuildConfig.PLUGIN_PLATFORM,
19-
ConsoleUser,
20-
EnumMap<VersionAction, () -> Unit>(VersionAction::class.java).apply {
21-
put(VersionAction.PROHIBIT) { Bukkit.getPluginManager().disablePlugin(bootstrap) }
22-
},
23-
{ Bukkit.getOnlinePlayers().map { it.user } },
24-
"esu")
25-
}
26-
private var task: ScheduledTask? = null
27-
28-
init {
29-
reload()
30-
}
13+
private val checker =
14+
UpdateChecker(
15+
BuildConfig.VERSION_ID.toInt(),
16+
BuildConfig.VERSION_CHANNEL,
17+
BuildConfig.PLUGIN_PLATFORM,
18+
ConsoleUser,
19+
EnumMap<VersionAction, () -> Unit>(VersionAction::class.java).apply {
20+
put(VersionAction.PROHIBIT) { Bukkit.getPluginManager().disablePlugin(bootstrap) }
21+
},
22+
{ Bukkit.getOnlinePlayers().map { it.user } },
23+
EsuCore.instance.basePermissionNode
24+
)
3125

3226
fun reload() {
33-
if (EsuConfig.get().updateChecker) {
34-
if (task == null) {
35-
task = Scheduler.asyncTicks(0, 60 * 60 * 20) {
36-
checker.run()
37-
}
38-
}
39-
} else {
40-
shutdown()
41-
}
27+
checker.onReload()
4228
}
4329

4430
fun shutdown() {
45-
task?.cancel()
46-
task = null
31+
checker.shutdown()
4732
}
4833

4934
fun onJoin(user: User) {
50-
if (EsuConfig.get().updateChecker) {
51-
checker.onJoin(user)
52-
}
35+
checker.onJoin(user)
5336
}
5437

5538
}

core/src/main/kotlin/io/github/rothes/esu/core/util/UpdateChecker.kt

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ package io.github.rothes.esu.core.util
33
import com.google.gson.Gson
44
import com.google.gson.annotations.SerializedName
55
import io.github.rothes.esu.core.EsuCore
6+
import io.github.rothes.esu.core.config.EsuConfig
67
import io.github.rothes.esu.core.config.EsuLang
78
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
9+
import io.github.rothes.esu.core.coroutine.AsyncScope
810
import io.github.rothes.esu.core.user.LogUser
911
import io.github.rothes.esu.core.user.User
1012
import io.github.rothes.esu.core.util.ComponentUtils.unparsed
1113
import io.github.rothes.esu.lib.adventure.text.minimessage.tag.resolver.TagResolver
14+
import kotlinx.coroutines.Job
15+
import kotlinx.coroutines.delay
16+
import kotlinx.coroutines.isActive
17+
import kotlinx.coroutines.launch
1218
import java.net.URI
1319
import java.util.*
20+
import kotlin.time.Duration.Companion.hours
1421

1522
private const val GITHUB_REPO = "Rothes/ESU"
1623

@@ -30,14 +37,40 @@ class UpdateChecker(
3037
private val messages = mutableListOf<RemoteMessage>()
3138
private var errorCount = 0
3239

40+
private var task: Job? = null
41+
42+
init {
43+
onReload()
44+
}
45+
46+
fun onReload() {
47+
if (EsuConfig.get().updateChecker) {
48+
if (task == null) {
49+
task = AsyncScope.launch {
50+
while (isActive) {
51+
run()
52+
delay(1.hours)
53+
}
54+
}
55+
}
56+
} else {
57+
shutdown()
58+
}
59+
}
60+
3361
fun onJoin(user: User) {
34-
if (user.hasPermission(perm)) {
62+
if (EsuConfig.get().updateChecker && user.hasPermission(perm)) {
3563
for (msg in messages) {
3664
user.message(user.localed(msg.langMap) { it.message }, *msg.args)
3765
}
3866
}
3967
}
4068

69+
fun shutdown() {
70+
task?.cancel()
71+
task = null
72+
}
73+
4174
fun run() {
4275
val info = fetch()
4376
for (message in info.errorMessage) {
@@ -78,7 +111,7 @@ class UpdateChecker(
78111
}
79112
}
80113

81-
fun fetch(): CheckedInfo {
114+
private fun fetch(): CheckedInfo {
82115
val fetch = getResponse("ghfast.top/https://raw.githubusercontent.com")
83116
val errors = mutableListOf<LocaleMessage>()
84117

@@ -157,13 +190,13 @@ class UpdateChecker(
157190
)
158191
}
159192

160-
data class CheckedInfo(
193+
private data class CheckedInfo(
161194
val notifications: List<Notification>?,
162195
val latestVersionName: String,
163196
val errorMessage: List<LocaleMessage> = listOf(),
164197
)
165198

166-
data class Notification(
199+
private data class Notification(
167200
val actions: List<VersionAction>,
168201
@SerializedName("message_times")
169202
val messageTimes: Int,
Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,36 @@
11
package io.github.rothes.esu.velocity
22

3-
import com.velocitypowered.api.scheduler.ScheduledTask
4-
import io.github.rothes.esu.core.config.EsuConfig
53
import io.github.rothes.esu.core.user.User
64
import io.github.rothes.esu.core.util.UpdateChecker
75
import io.github.rothes.esu.core.util.UpdateChecker.VersionAction
86
import io.github.rothes.esu.velocity.user.ConsoleUser
9-
import java.time.Duration
10-
import java.util.EnumMap
7+
import java.util.*
118

129
object UpdateCheckerMan {
1310

14-
private val checker by lazy { UpdateChecker(
15-
BuildConfig.VERSION_ID.toInt(),
16-
BuildConfig.VERSION_CHANNEL,
17-
BuildConfig.PLUGIN_PLATFORM,
18-
ConsoleUser,
19-
EnumMap<VersionAction, () -> Unit>(VersionAction::class.java).apply {
20-
put(VersionAction.PROHIBIT) { /* TODO */ }
21-
},
22-
{ plugin.server.allPlayers.map { it.user } },
23-
"vesu")
24-
}
25-
private var task: ScheduledTask? = null
26-
27-
init {
28-
reload()
29-
}
11+
private val checker =
12+
UpdateChecker(
13+
BuildConfig.VERSION_ID.toInt(),
14+
BuildConfig.VERSION_CHANNEL,
15+
BuildConfig.PLUGIN_PLATFORM,
16+
ConsoleUser,
17+
EnumMap<VersionAction, () -> Unit>(VersionAction::class.java).apply {
18+
put(VersionAction.PROHIBIT) { /* TODO */ }
19+
},
20+
{ plugin.server.allPlayers.map { it.user } },
21+
"vesu"
22+
)
3023

3124
fun reload() {
32-
if (EsuConfig.get().updateChecker) {
33-
if (task == null) {
34-
task = plugin.server.scheduler.buildTask(plugin.bootstrap, Runnable { checker.run() })
35-
.clearDelay()
36-
.repeat(Duration.ofHours(1))
37-
.schedule()
38-
}
39-
} else {
40-
shutdown()
41-
}
25+
checker.onReload()
4226
}
4327

4428
fun shutdown() {
45-
task?.cancel()
46-
task = null
29+
checker.shutdown()
4730
}
4831

4932
fun onJoin(user: User) {
50-
if (EsuConfig.get().updateChecker) {
51-
checker.onJoin(user)
52-
}
33+
checker.onJoin(user)
5334
}
5435

5536
}

0 commit comments

Comments
 (0)