-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of using my own updatechecker I copied the skbee updatechecke…
…r ;) Full credits to Shane
- Loading branch information
Showing
7 changed files
with
143 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
src/main/kotlin/de/bypixeltv/skcloudnet/tasks/UpdateCheck.kt
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/de/bypixeltv/skcloudnet/utils/IngameUpdateChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
33 changes: 0 additions & 33 deletions
33
src/main/kotlin/de/bypixeltv/skcloudnet/utils/IngameUpdateMessage.kt
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
src/main/kotlin/de/bypixeltv/skcloudnet/utils/UpdateChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.