aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/dulkirmod/features/UpdateNotificationFeature.kt
blob: 81814ddffd6b99bfbae5d9240dbd2428b6bf3e14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)
    }

}