Skip to content

Commit 9b81e86

Browse files
committed
feat: added update checker
1 parent 1c23e74 commit 9b81e86

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ bukkit {
7777
description = "Allows the player to use the plugin"
7878
default = Default.TRUE
7979
}
80+
register("attollo.update") {
81+
description = "Allows the player to see update notifications"
82+
default = Default.OP
83+
}
8084
}
8185
commands {
8286
register("attollo") {

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

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

33
import dev.themeinerlp.attollo.listener.AttolloListener
4+
import dev.themeinerlp.attollo.listener.UpdateCheckerListener
45
import org.bukkit.Material
56
import org.bukkit.plugin.java.JavaPlugin
67

78
open class Attollo : JavaPlugin() {
89

9-
1010
lateinit var elevatorBlock: Material
11+
1112
override fun onLoad() {
1213
saveDefaultConfig()
1314
}
@@ -23,6 +24,7 @@ open class Attollo : JavaPlugin() {
2324
elevatorBlock = material ?: Material.DAYLIGHT_DETECTOR
2425
logger.info("Using elevatorBlock: $elevatorBlock")
2526
server.pluginManager.registerEvents(AttolloListener(this), this)
27+
server.pluginManager.registerEvents(UpdateCheckerListener(this), this)
2628
}
2729

28-
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dev.themeinerlp.attollo.listener
2+
3+
import dev.themeinerlp.attollo.Attollo
4+
import org.bukkit.event.EventHandler
5+
import org.bukkit.event.Listener
6+
import org.bukkit.event.player.PlayerJoinEvent
7+
import java.net.URI
8+
9+
class UpdateCheckerListener(private val attollo: Attollo) : Listener {
10+
11+
@EventHandler
12+
fun onJoin(event: PlayerJoinEvent) {
13+
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+
})
24+
}
25+
}
26+
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+
}
33+
}

0 commit comments

Comments
 (0)