Skip to content

Commit 90b4256

Browse files
committed
changed update checker system
1 parent 959dafb commit 90b4256

File tree

5 files changed

+103
-20
lines changed

5 files changed

+103
-20
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (!File("$rootDir/.git").exists()) {
2525
}
2626

2727
group = "net.onelitefeather"
28-
version = "1.4.0"
28+
version = "1.0.0"
2929

3030
val minecraftVersion = "1.20.6"
3131
val supportedMinecraftVersions = listOf(
@@ -46,7 +46,7 @@ repositories {
4646
dependencies {
4747
compileOnly("io.papermc.paper:paper-api:$minecraftVersion-R0.1-SNAPSHOT")
4848
implementation("net.kyori:adventure-text-minimessage:4.17.0")
49-
49+
implementation("com.github.zafarkhaja:java-semver:0.10.2")
5050

5151
// testing
5252
testImplementation(kotlin("test"))

src/main/kotlin/dev/themeinerlp/attollo/Attollo.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package dev.themeinerlp.attollo
22

33
import dev.themeinerlp.attollo.listener.AttolloListener
44
import dev.themeinerlp.attollo.listener.UpdateCheckerListener
5+
import dev.themeinerlp.attollo.service.UpdateService
56
import org.bukkit.Material
67
import org.bukkit.plugin.java.JavaPlugin
78

89
open class Attollo : JavaPlugin() {
910

1011
lateinit var elevatorBlock: Material
12+
lateinit var updateService: UpdateService
1113

1214
override fun onLoad() {
1315
saveDefaultConfig()
@@ -25,6 +27,17 @@ open class Attollo : JavaPlugin() {
2527
logger.info("Using elevatorBlock: $elevatorBlock")
2628
server.pluginManager.registerEvents(AttolloListener(this), this)
2729
server.pluginManager.registerEvents(UpdateCheckerListener(this), this)
30+
updateChecker()
31+
}
32+
33+
override fun onDisable() {
34+
updateService.shutdown()
35+
}
36+
37+
private fun updateChecker() {
38+
updateService = UpdateService(this)
39+
updateService.run()
40+
updateService.notifyConsole(componentLogger)
2841
}
2942

3043
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
package dev.themeinerlp.attollo
22

3+
import java.net.URI
4+
import java.net.http.HttpRequest
5+
6+
7+
38
const val USE_PERMISSION = "attollo.use"
4-
const val BYTEBIN_BASE_URL = "https://paste.grim.ac/data"
9+
const val NOTIFY_UPDATE_PERMISSION = "attollo.update"
10+
const val BYTEBIN_BASE_URL = "https://paste.grim.ac/data"
11+
12+
val LATEST_RELEASE_VERSION_URI = URI.create("https://hangar.papermc.io/api/v1/projects/Attollo/latestrelease")
13+
val LATEST_RELEASE_VERSION_REQUEST = HttpRequest.newBuilder().GET().uri(LATEST_RELEASE_VERSION_URI).build()
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
package dev.themeinerlp.attollo.listener
22

33
import dev.themeinerlp.attollo.Attollo
4+
import dev.themeinerlp.attollo.NOTIFY_UPDATE_PERMISSION
45
import org.bukkit.event.EventHandler
56
import org.bukkit.event.Listener
67
import org.bukkit.event.player.PlayerJoinEvent
7-
import java.net.URI
88

99
class UpdateCheckerListener(private val attollo: Attollo) : Listener {
1010

1111
@EventHandler
1212
fun onJoin(event: PlayerJoinEvent) {
1313
val player = event.player
14-
if (player.hasPermission("attollo.update")) {
15-
attollo.server.scheduler.runTaskAsynchronously(attollo, Runnable {
16-
val latestVersion = getLatestVersion()
17-
if (attollo.description.version != latestVersion) {
18-
player.sendMessage(
19-
"§aAn update for Attollo is available! Version: $latestVersion",
20-
"§aYou can download it at: https://hangar.papermc.io/OneLiteFeather/Attollo"
21-
)
22-
}
23-
})
14+
if (player.isOp || player.hasPermission(NOTIFY_UPDATE_PERMISSION)) {
15+
attollo.updateService.notifyPlayer(player)
2416
}
2517
}
2618

27-
private fun getLatestVersion(): String {
28-
val url = URI.create("https://hangar.papermc.io/api/v1/projects/Attollo/latestrelease").toURL()
29-
val reader = url.openStream().bufferedReader()
30-
val content = reader.use { it.readText() }
31-
return content
32-
}
3319
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package dev.themeinerlp.attollo.service
2+
3+
import com.github.zafarkhaja.semver.Version
4+
import dev.themeinerlp.attollo.Attollo
5+
import dev.themeinerlp.attollo.LATEST_RELEASE_VERSION_REQUEST
6+
import dev.themeinerlp.attollo.NOTIFY_UPDATE_PERMISSION
7+
import net.kyori.adventure.text.logger.slf4j.ComponentLogger
8+
import net.kyori.adventure.text.minimessage.MiniMessage
9+
import org.bukkit.Bukkit
10+
import org.bukkit.entity.Player
11+
import org.slf4j.LoggerFactory
12+
import java.io.IOException
13+
import java.net.http.HttpClient
14+
import java.net.http.HttpResponse
15+
16+
class UpdateService(plugin: Attollo) : Runnable {
17+
private val hangarClient = HttpClient.newBuilder().build()
18+
private val LOGGER = LoggerFactory.getLogger(UpdateService::class.java)
19+
private val localVersion = Version.parse(plugin.pluginMeta.version)
20+
private var remoteVersion: Version? = null
21+
private val scheduler = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this, 0, 20 * 60 * 60 * 3)
22+
private val DOWNLOAD_URL = "https://hangar.papermc.io/OneLiteFeather/Attollo/versions/%s"
23+
24+
25+
override fun run() {
26+
val remoteVersion: Version? = getNewerVersion()
27+
if (remoteVersion != null) {
28+
this.remoteVersion = remoteVersion
29+
for (onlinePlayer in Bukkit.getOnlinePlayers()) {
30+
if (onlinePlayer.isOp || onlinePlayer.hasPermission(NOTIFY_UPDATE_PERMISSION)) {
31+
notifyPlayer(localVersion, remoteVersion, onlinePlayer)
32+
}
33+
}
34+
}
35+
}
36+
37+
fun notifyConsole(logger: ComponentLogger) {
38+
if (this.remoteVersion != null && remoteVersion?.isHigherThan(this.localVersion) == true) {
39+
logger.warn(MiniMessage.miniMessage().deserialize("<yellow>Your version (${localVersion}) is older than our latest published version (${remoteVersion.toString()}). Please update as soon as possible to get continued support. Or use this link ${DOWNLOAD_URL.format(remoteVersion.toString())}"))
40+
}
41+
}
42+
43+
fun notifyPlayer(player: Player) {
44+
if (this.remoteVersion != null && remoteVersion?.isHigherThan(this.localVersion) == true) {
45+
notifyPlayer(this.localVersion, this.remoteVersion, player)
46+
}
47+
}
48+
49+
private fun notifyPlayer(localVersion: Version, remoteVersion: Version?, player: Player) {
50+
player.sendMessage(MiniMessage.miniMessage().deserialize("<yellow><click:open_url:'https://hangar.papermc.io/OneLiteFeather/Attollo'>Your version (${localVersion}) is older than our latest published version (${remoteVersion.toString()}). Please update as soon as possible to get continued support. Or click me to get on the download page!</click>"))
51+
}
52+
53+
private fun getNewerVersion(): Version? {
54+
try {
55+
val httpResponse = hangarClient.send(
56+
LATEST_RELEASE_VERSION_REQUEST,
57+
HttpResponse.BodyHandlers.ofString()
58+
)
59+
val remoteVersion = Version.parse(httpResponse.body())
60+
if (remoteVersion.isHigherThan(this.localVersion)) {
61+
return remoteVersion
62+
}
63+
} catch (e: IOException) {
64+
LOGGER.error("Something went wrong to check updates", e)
65+
} catch (e: InterruptedException) {
66+
LOGGER.error("Something went wrong to check updates", e)
67+
}
68+
return null
69+
}
70+
71+
fun shutdown() {
72+
hangarClient.shutdownNow()
73+
scheduler.cancel()
74+
}
75+
}

0 commit comments

Comments
 (0)