aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/dulkirmod/features
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/dulkirmod/features')
-rw-r--r--src/main/kotlin/dulkirmod/features/UpdateNotificationFeature.kt79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/main/kotlin/dulkirmod/features/UpdateNotificationFeature.kt b/src/main/kotlin/dulkirmod/features/UpdateNotificationFeature.kt
new file mode 100644
index 0000000..81814dd
--- /dev/null
+++ b/src/main/kotlin/dulkirmod/features/UpdateNotificationFeature.kt
@@ -0,0 +1,79 @@
+package dulkirmod.features
+
+import dulkirmod.DulkirMod
+import dulkirmod.utils.MinecraftExecutor
+import dulkirmod.utils.TextUtils
+import moe.nea.libautoupdate.*
+import net.minecraft.event.ClickEvent
+import net.minecraft.event.HoverEvent
+import net.minecraft.util.ChatComponentText
+import net.minecraft.util.ChatStyle
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent
+import java.util.concurrent.CompletableFuture
+
+object UpdateNotificationFeature {
+ val context = UpdateContext(
+ UpdateSource.githubUpdateSource("inglettronald", "DulkirMod"),
+ UpdateTarget.deleteAndSaveInTheSameFolder(UpdateNotificationFeature::class.java),
+ CurrentVersion.ofTag("v${DulkirMod.MOD_VERSION}"),
+ DulkirMod.MOD_ID,
+ )
+
+ init {
+ context.cleanup()
+ }
+
+ val updates = mutableMapOf<String, PotentialUpdate>()
+
+ var hasCheckedOnStartup = false
+
+ @SubscribeEvent
+ fun updateOnStartup(event: ClientTickEvent) {
+ if (!hasCheckedOnStartup && DulkirMod.mc.thePlayer != null) {
+ hasCheckedOnStartup = true
+ checkUpdate(false)
+ }
+ }
+
+ fun checkUpdate(alwaysNotify: Boolean) {
+ context.checkUpdate("full")
+ .thenAcceptAsync({
+ if (it.isUpdateAvailable) {
+ updates[it.updateUUID.toString()] = it
+ DulkirMod.mc.thePlayer?.addChatMessage(
+ ChatComponentText("${DulkirMod.CHAT_PREFIX} A DulkirMod update is available: ${DulkirMod.MOD_VERSION} ยป ${it.update.versionName}. Click here to download it.").setChatStyle(
+ ChatStyle().setChatClickEvent(
+ ClickEvent(
+ ClickEvent.Action.RUN_COMMAND,
+ "/updatedulkir ${it.updateUUID}"
+ )
+ ).setChatHoverEvent(
+ HoverEvent(
+ HoverEvent.Action.SHOW_TEXT,
+ ChatComponentText("Click to download ${it.update.versionName}")
+ )
+ )
+ )
+ )
+ } else if (alwaysNotify) {
+ TextUtils.info("No DulkirMod update found! You are on the latest version.")
+ }
+ }, MinecraftExecutor)
+ }
+
+ fun downloadUpdate(uuid: String) {
+ val update = updates[uuid]
+ if (update == null) {
+ TextUtils.info("Unknown update.")
+ return
+ }
+ CompletableFuture.runAsync {
+ update.prepareUpdate()
+ }.thenRunAsync({
+ TextUtils.info("Update ${update.update.versionName} downloaded. It will be applied after your next restart.")
+ update.executePreparedUpdate()
+ }, MinecraftExecutor)
+ }
+
+} \ No newline at end of file