From 318d215f3a04a1441f658ea22040ba11b2851aa3 Mon Sep 17 00:00:00 2001 From: TheLeCrafter <47987588+TheLeCrafter@users.noreply.github.com> Date: Sun, 5 Dec 2021 11:56:42 +0100 Subject: [PATCH] + Added an UpdateChecker.kt + Bump version to v1.0.2 --- build.gradle.kts | 4 +- .../ambientstars/AmbientStarsPlugin.kt | 2 + .../ambientstars/utils/UpdateChecker.kt | 51 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/thelecrafter/plugins/ambientstars/utils/UpdateChecker.kt diff --git a/build.gradle.kts b/build.gradle.kts index 3eee065..9445b39 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "dev.thelecrafter.plugins.ambientstars" -version = "1.0.1" +version = "1.0.2" val versionType: String = "stable" repositories { @@ -25,6 +25,7 @@ dependencies { implementation("io.papermc:paperlib:1.0.7") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0") implementation("io.github.classgraph:classgraph:4.8.137") + implementation("org.kohsuke:github-api:1.301") } tasks.shadowJar { @@ -32,6 +33,7 @@ tasks.shadowJar { relocate("org.jetbrains", "$group.org.jetbrains") relocate("org.reflections", "$group.org.reflections") relocate("io.papermc.lib", "$group.io.papermc.lib") + relocate("org.kohsuke", "$group.org.kohsuke") archiveFileName.set("${project.name}-$versionType.jar") } diff --git a/src/main/java/dev/thelecrafter/plugins/ambientstars/AmbientStarsPlugin.kt b/src/main/java/dev/thelecrafter/plugins/ambientstars/AmbientStarsPlugin.kt index 34e5167..96fabd9 100644 --- a/src/main/java/dev/thelecrafter/plugins/ambientstars/AmbientStarsPlugin.kt +++ b/src/main/java/dev/thelecrafter/plugins/ambientstars/AmbientStarsPlugin.kt @@ -2,6 +2,7 @@ package dev.thelecrafter.plugins.ambientstars import dev.thelecrafter.plugins.ambientstars.commands.ReloadCommand import dev.thelecrafter.plugins.ambientstars.utils.EventCollector +import dev.thelecrafter.plugins.ambientstars.utils.UpdateChecker import io.papermc.lib.PaperLib import org.bukkit.Bukkit import org.bukkit.command.CommandExecutor @@ -43,6 +44,7 @@ class AmbientStarsPlugin : JavaPlugin() { override fun onEnable() { getInstance = this getLogger = logger + UpdateChecker.checkForUpdates() PaperLib.suggestPaper(getInstance, Level.WARNING) getInstance.saveDefaultConfig() getDefaultConfig = YamlConfiguration.loadConfiguration(getTextResource("config.yml")!!) diff --git a/src/main/java/dev/thelecrafter/plugins/ambientstars/utils/UpdateChecker.kt b/src/main/java/dev/thelecrafter/plugins/ambientstars/utils/UpdateChecker.kt new file mode 100644 index 0000000..ab1b1ca --- /dev/null +++ b/src/main/java/dev/thelecrafter/plugins/ambientstars/utils/UpdateChecker.kt @@ -0,0 +1,51 @@ +package dev.thelecrafter.plugins.ambientstars.utils + +import dev.thelecrafter.plugins.ambientstars.AmbientStarsPlugin +import net.kyori.adventure.text.Component +import net.kyori.adventure.text.event.ClickEvent +import net.kyori.adventure.text.event.HoverEvent +import net.kyori.adventure.text.format.NamedTextColor +import net.kyori.adventure.text.format.TextDecoration +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerJoinEvent +import org.kohsuke.github.GHRelease +import org.kohsuke.github.GHRepository +import org.kohsuke.github.GitHub +import java.util.logging.Level + +class UpdateChecker : Listener { + + companion object { + private var upToDate: Boolean = true + + fun checkForUpdates() { + val version: String = "v" + AmbientStarsPlugin.getInstance.description.version + val gitHub: GitHub = GitHub.connectAnonymously() + val repository: GHRepository = gitHub.getRepository("TheLeCrafter/ambientstars")!! + val latestRelease: GHRelease = repository.latestRelease + if (!latestRelease.isDraft && !latestRelease.isPrerelease) { + if (version != latestRelease.tagName) { + upToDate = false + AmbientStarsPlugin.getLogger.log(Level.WARNING, "The plugin is not up to date! Download the latest release at https://github.com/TheLeCrafter/ambientstars/releases") + } + } else AmbientStarsPlugin.getLogger.log(Level.WARNING, "Couldn't check the latest release because it's a draft/pre release! Check yourself at https://github.com/TheLeCrafter/ambientstars/releases") + } + + fun getUpToDate(): Boolean { + return upToDate + } + } + + @EventHandler + fun sendUpdateMessageOnOPJoin(event: PlayerJoinEvent) { + if (event.player.isOp || event.player.hasPermission("*")) { + if (!getUpToDate()) { + event.player.sendMessage(Component.text("[AmbientStars] The plugin is not up to date! Download the latest release ").color(NamedTextColor.YELLOW).append(Component.text("here").decorate(TextDecoration.UNDERLINED).color(NamedTextColor.BLUE).clickEvent( + ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/TheLeCrafter/ambientstars/releases")).hoverEvent(HoverEvent.showText( + Component.text("Opens https://github.com/TheLeCrafter/ambientstars/releases").color(NamedTextColor.GRAY))))) + } + } + } + +} \ No newline at end of file