From 86d7516f3f619a0368b36af6dc5cad13babb5cb5 Mon Sep 17 00:00:00 2001 From: nea Date: Mon, 17 Jul 2023 03:53:57 +0200 Subject: Add update notifications --- build.gradle.kts | 7 +- 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 +++ 5 files changed, 120 insertions(+), 1 deletion(-) 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 diff --git a/build.gradle.kts b/build.gradle.kts index e3d6c46..bac63fc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -58,6 +58,7 @@ repositories { // If you don't want to log in with your real minecraft account, remove this line maven("https://pkgs.dev.azure.com/djtheredstoner/DevAuth/_packaging/public/maven/v1") maven("https://repo.essential.gg/repository/maven-public/") + maven("https://repo.nea.moe/releases") maven("https://repo.polyfrost.cc/releases") } @@ -85,6 +86,9 @@ dependencies { compileOnly("cc.polyfrost:oneconfig-1.8.9-forge:0.2.0-alpha+") // Should not be included in jar // include should be replaced with a configuration that includes this in the jar shadowImpl("cc.polyfrost:oneconfig-wrapper-launchwrapper:1.0.0-beta+") // Should be included in jar + + // Auto updating library + shadowImpl("moe.nea:libautoupdate:1.1.0") } // Configures our shadow/shade configuration, so we can @@ -129,12 +133,13 @@ tasks.shadowJar { configurations = listOf(shadowImpl) doLast { configurations.forEach { - println("Config: ${it.files}") + println("Copying into JAR: ${it.files}") } } // If you want to include other dependencies and shadow them, you can relocate them in here fun relocate(name: String) = relocate(name, "com.dulkirmod.deps.$name") + relocate("moe.nea.libautoupdate") } tasks.withType { duplicatesStrategy = DuplicatesStrategy.EXCLUDE } 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