Skip to content

Commit

Permalink
Instead of using my own updatechecker I copied the skbee updatechecke…
Browse files Browse the repository at this point in the history
…r ;) Full credits to Shane
  • Loading branch information
byPixelTV committed May 1, 2024
1 parent 3f1486a commit a9011ac
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 139 deletions.
46 changes: 8 additions & 38 deletions src/main/kotlin/de/bypixeltv/skcloudnet/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package de.bypixeltv.skcloudnet
import ch.njol.skript.Skript
import ch.njol.skript.SkriptAddon
import de.bypixeltv.skcloudnet.commands.SkCloudnetCommands
import de.bypixeltv.skcloudnet.tasks.UpdateCheck
import de.bypixeltv.skcloudnet.utils.VersionUtils
import de.bypixeltv.skcloudnet.utils.IngameUpdateMessage
import de.bypixeltv.skcloudnet.utils.IngameUpdateChecker
import de.bypixeltv.skcloudnet.utils.UpdateChecker
import dev.jorel.commandapi.CommandAPI
import dev.jorel.commandapi.CommandAPIBukkitConfig
import net.axay.kspigot.main.KSpigot
Expand Down Expand Up @@ -50,44 +49,15 @@ class Main : KSpigot() {
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> <aqua>Successfully enabled SkCloudnet v${this.description.version}!</aqua>"))

IngameUpdateChecker

val githubVersion = VersionUtils().getLatestAddonVersion()?.replace("v", "")
val currentVersion = this.description.version.replace("v", "")

if (githubVersion != null) {
if (VersionUtils().isVersionGreater(githubVersion, currentVersion)) {
// There is an update available
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
// You're on the latest version
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> <color:#43fa00>You're on the latest version of SkCloudnet!</color> <aqua>Version <yellow>v${this.description.version}</yellow></aqua>"))
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
} else if (githubVersion == currentVersion) {
// You're running a development version
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> <color:#43fa00>There is an update available for SkCloudnet!</color> <aqua>You're on version <yellow>v${this.description.version}</yellow> and the latest version is <yellow>$githubVersion</yellow></aqua>!\n\n<color:#43fa00>Download the latest version here:</color> <blue>https://github.com/byPixelTV/SkCloudnet/releases</blue> <aqua>"))
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
} else {
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> <color:#ff0000>You're running a development version of SkCloudnet! Please note that this version may contain bugs!</color> <aqua>Version <color:#ff0000>v${this.description.version}</color> > <color:#43fa00>${VersionUtils().getLatestAddonVersion()}</color></aqua>"))
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
}
} else {
// Unable to fetch the latest version from GitHub
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> <color:#ff0000>Unable to fetch the latest version from Github!</color> <aqua>Are you rate limited?</aqua>"))
server.consoleSender.sendMessage(" ")
server.consoleSender.sendMessage(" ")
val version = description.version
if (version.contains("-")) {
server.consoleSender.sendMessage(miniMessages.deserialize("<yellow>This is a BETA build, things may not work as expected, please report any bugs on GitHub</yellow>"))
server.consoleSender.sendMessage(miniMessages.deserialize("<yellow>https://github.com/byPixelTV/SkCloudnet/issues</yellow>"))
}

IngameUpdateMessage
UpdateCheck
UpdateChecker.checkForUpdate(version)

Metrics(this, 21526)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package de.bypixeltv.skcloudnet.commands

import ch.njol.skript.Skript
import ch.njol.skript.util.Version
import de.bypixeltv.skcloudnet.Main
import de.bypixeltv.skcloudnet.utils.UpdateChecker
import de.bypixeltv.skcloudnet.utils.UpdateChecker.Companion.getLatestReleaseVersion
import de.bypixeltv.skcloudnet.utils.VersionUtils
import dev.jorel.commandapi.kotlindsl.anyExecutor
import dev.jorel.commandapi.kotlindsl.commandTree
import dev.jorel.commandapi.kotlindsl.literalArgument
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.Bukkit
import java.nio.file.Files
import java.nio.file.Paths

Expand All @@ -28,7 +32,8 @@ class SkCloudnetCommands {
}
}

val addonsList = if (addonMessages.isNotEmpty()) addonMessages.joinToString("\n") else "<color:#ff0000>No other addons found</color>"
val addonsList =
if (addonMessages.isNotEmpty()) addonMessages.joinToString("\n") else "<color:#ff0000>No other addons found</color>"
player.sendMessage(
miniMessages.deserialize(
"<dark_grey>--- <aqua>SkCloudnet</aqua> <grey>Info:</grey> ---</dark_grey>\n\n<grey>SkCloudnet Version: <aqua>${Main.INSTANCE.description.version}</aqua>\nSkript Version: <aqua>${VersionUtils().getSkriptVersion()}</aqua>\nServer Version: <aqua>${Main.INSTANCE.server.minecraftVersion}</aqua>\nServer Implementation: <aqua>${Main.INSTANCE.server.version}</aqua>\nAddons:\n$addonsList</grey>"
Expand All @@ -49,19 +54,23 @@ class SkCloudnetCommands {
literalArgument("version") {
withPermission("skcloudnet.admin.version")
anyExecutor { player, _ ->
val githubVersion = VersionUtils().getLatestAddonVersion()?.replace("v", "")
val currentVersion = Main.INSTANCE.description.version.replace("v", "")
val currentVersion = Main.INSTANCE.description.version
val updateVersion = UpdateChecker(Main.INSTANCE).getUpdateVersion(currentVersion)

if (githubVersion != null) {
if (VersionUtils().isVersionGreater(githubVersion, currentVersion)) {
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> <color:#43fa00>There is an update available for SkCloudnet!</color> <aqua>You're on version <yellow>${Main.INSTANCE.description.version}</yellow> and the latest version is <yellow>$githubVersion</yellow></aqua>!<color:#43fa00>Download the latest version here:</color> <blue>https://github.com/byPixelTV/SkCloudnet/releases</blue> <aqua>"))
} else if (githubVersion == currentVersion) {
return@anyExecutor
getLatestReleaseVersion { version ->
val plugVer = Version(Main.INSTANCE.description.version)
val curVer = Version(version)
if (curVer.compareTo(plugVer) <= 0) {
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> <green>The plugin is up to date!</green>"))
} else {
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> <color:#ff0000>You're running a development version of SkCloudnet! Please note that this version may contain bugs!</color> <aqua>Version <color:#ff0000>${Main.INSTANCE.description.version}</color> > <color:#43fa00>${VersionUtils().getLatestAddonVersion()}</color></aqua>"))
Bukkit.getScheduler().runTaskLater(Main.INSTANCE, Runnable {
updateVersion.thenApply { version ->
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> update available: <green>$version</green>"))
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> download at <aqua><click:open_url:'https://github.com/byPixelTV/SkCloudnet/releases'>https://github.com/byPixelTV/SkCloudnet/releases</click></aqua>"))
true
}
}, 30)
}
} else {
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> <color:#ff0000>Unable to fetch the latest version from Github!</color> <aqua>Are you rate limited?</aqua>"))
}
}
}
Expand Down
56 changes: 0 additions & 56 deletions src/main/kotlin/de/bypixeltv/skcloudnet/tasks/UpdateCheck.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.bypixeltv.skcloudnet.utils

import de.bypixeltv.skcloudnet.Main
import net.axay.kspigot.event.listen
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.Bukkit
import org.bukkit.event.player.PlayerJoinEvent

object IngameUpdateChecker {
private val miniMessages = MiniMessage.miniMessage()

@Suppress("DEPRECATION", "UNUSED")
val joinEvent = listen<PlayerJoinEvent> {
val player = it.player
if (Main.INSTANCE.config.getBoolean("update-checker")) {
if (player.hasPermission("skcloudnet.admin.version") || player.isOp) {
val currentVersion = Main.INSTANCE.description.version
val updateVersion = UpdateChecker(Main.INSTANCE).getUpdateVersion(currentVersion)

Bukkit.getScheduler().runTaskLater(Main.INSTANCE, Runnable {
updateVersion.thenApply { version ->
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> update available: <green>$version</green>"))
player.sendMessage(miniMessages.deserialize("<dark_grey>[<gradient:aqua:blue:aqua>SkCloudnet</gradient>]</dark_grey> download at <aqua><click:open_url:'https://github.com/byPixelTV/SkCloudnet/releases'>https://github.com/byPixelTV/SkCloudnet/releases</click></aqua>"))
true
}
}, 30)
}
}
}
}

This file was deleted.

83 changes: 83 additions & 0 deletions src/main/kotlin/de/bypixeltv/skcloudnet/utils/UpdateChecker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package de.bypixeltv.skcloudnet.utils

import ch.njol.skript.util.Version
import com.google.gson.Gson
import com.google.gson.JsonObject
import de.bypixeltv.skcloudnet.Main
import net.axay.kspigot.extensions.server
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.Bukkit
import org.bukkit.event.Listener
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.URL
import java.util.concurrent.CompletableFuture
import java.util.function.Consumer

/*
This got ported into Kotlin by @byPixelTV and was originally written in Java by ShaneBeee
You can find this code at https://github.com/ShaneBeee/SkBee/blob/master/src/main/java/com/shanebeestudios/skbee/api/util/UpdateChecker.java
Checkout https://github.com/ShaneBeee/SkBee
*/

class UpdateChecker(private val plugin: Main) : Listener {

companion object {
private var UPDATE_VERSION: Version? = null

fun checkForUpdate(pluginVersion: String) {
val miniMessages = MiniMessage.miniMessage()
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> Checking for updates..."))
getLatestReleaseVersion { version ->
val plugVer = Version(pluginVersion)
val curVer = Version(version)
if (curVer.compareTo(plugVer) <= 0) {
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> <green>The plugin is up to date!</green>"))
} else {
server.consoleSender.sendMessage(miniMessages.deserialize("<grey>[<aqua>SkCloudnet</aqua>]</grey> <red>The plugin is not up to date!</red>"))
server.consoleSender.sendMessage(miniMessages.deserialize(" - Current version: <red>v${pluginVersion}</red>"))
server.consoleSender.sendMessage(miniMessages.deserialize(" - Available update: <green>v${version}</green>"))
server.consoleSender.sendMessage(miniMessages.deserialize(" - Download available at: <aqua>https://github.com/byPixelTV/SkCloudnet/releases</aqua>"))
UPDATE_VERSION = curVer
}
}
}

fun getLatestReleaseVersion(consumer: Consumer<String>) {
val miniMessages = MiniMessage.miniMessage()
try {
val url = URL("https://api.github.com/repos/byPixelTV/SkCloudnet/releases/latest")
val reader = BufferedReader(InputStreamReader(url.openStream()))
val jsonObject = Gson().fromJson(reader, JsonObject::class.java)
var tagName = jsonObject["tag_name"].asString
tagName = tagName.removePrefix("v")
consumer.accept(tagName)
} catch (e: IOException) {
server.consoleSender.sendMessage(miniMessages.deserialize("<red>Checking for updates failed!</red>"))
}
}
}

@Suppress("RedundantSamConstructor")
fun getUpdateVersion(currentVersion: String): CompletableFuture<Version> {
val future = CompletableFuture<Version>()
if (UPDATE_VERSION != null) {
future.complete(UPDATE_VERSION)
} else {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, Runnable {
getLatestReleaseVersion(Consumer { version ->
val plugVer = Version(currentVersion)
val curVer = Version(version)
if (curVer.compareTo(plugVer) <= 0) {
future.cancel(true)
} else {
UPDATE_VERSION = curVer
future.complete(UPDATE_VERSION)
}
})
})
}
return future
}
}
Loading

0 comments on commit a9011ac

Please sign in to comment.