From 86d7516f3f619a0368b36af6dc5cad13babb5cb5 Mon Sep 17 00:00:00 2001 From: nea Date: Mon, 17 Jul 2023 03:53:57 +0200 Subject: Add update notifications --- src/main/kotlin/dulkirmod/DulkirMod.kt | 2 + src/main/kotlin/dulkirmod/command/UpdateCommand.kt | 23 +++++++ .../features/UpdateNotificationFeature.kt | 79 ++++++++++++++++++++++ .../kotlin/dulkirmod/utils/MinecraftExecutor.kt | 10 +++ 4 files changed, 114 insertions(+) create mode 100644 src/main/kotlin/dulkirmod/command/UpdateCommand.kt create mode 100644 src/main/kotlin/dulkirmod/features/UpdateNotificationFeature.kt create mode 100644 src/main/kotlin/dulkirmod/utils/MinecraftExecutor.kt (limited to 'src/main/kotlin') diff --git a/src/main/kotlin/dulkirmod/DulkirMod.kt b/src/main/kotlin/dulkirmod/DulkirMod.kt index 1637404..a7dcf24 100644 --- a/src/main/kotlin/dulkirmod/DulkirMod.kt +++ b/src/main/kotlin/dulkirmod/DulkirMod.kt @@ -55,6 +55,7 @@ class DulkirMod { cch.registerCommand(JoinDungeonCommand()) cch.registerCommand(LeapNameCommand()) cch.registerCommand(HurtCamCommand()) + cch.registerCommand(UpdateCommand()) cch.registerCommand(FarmingControlSchemeCommand()) cch.registerCommand(DynamicKeyCommand()) cch.registerCommand(ResetSlayerTracker()) @@ -83,6 +84,7 @@ class DulkirMod { mcBus.register(HideHealerFairy) mcBus.register(SecretSounds) mcBus.register(BlazeSlayerFeatures) + mcBus.register(UpdateNotificationFeature) mcBus.register(WorldRenderUtils) mcBus.register(IchorHighlight) mcBus.register(SteakDisplay) diff --git a/src/main/kotlin/dulkirmod/command/UpdateCommand.kt b/src/main/kotlin/dulkirmod/command/UpdateCommand.kt new file mode 100644 index 0000000..813fcdb --- /dev/null +++ b/src/main/kotlin/dulkirmod/command/UpdateCommand.kt @@ -0,0 +1,23 @@ +package dulkirmod.command + +import dulkirmod.features.UpdateNotificationFeature +import dulkirmod.utils.TextUtils +import net.minecraft.command.ICommandSender + +class UpdateCommand : ClientCommandBase("updatedulkir") { + override fun processCommand(sender: ICommandSender?, args: Array) { + when (val a = args.singleOrNull()) { + "check" -> { + UpdateNotificationFeature.checkUpdate(true) + } + + null -> { + TextUtils.info("Usage: /updatedulkir check") + } + + else -> { + UpdateNotificationFeature.downloadUpdate(a) + } + } + } +} \ No newline at end of file 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() + + 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 diff --git a/src/main/kotlin/dulkirmod/utils/MinecraftExecutor.kt b/src/main/kotlin/dulkirmod/utils/MinecraftExecutor.kt new file mode 100644 index 0000000..6939f88 --- /dev/null +++ b/src/main/kotlin/dulkirmod/utils/MinecraftExecutor.kt @@ -0,0 +1,10 @@ +package dulkirmod.utils + +import dulkirmod.DulkirMod +import java.util.concurrent.Executor + +object MinecraftExecutor : Executor { + override fun execute(command: Runnable) { + DulkirMod.mc.addScheduledTask(command) + } +} -- cgit