diff options
Diffstat (limited to 'src')
4 files changed, 114 insertions, 0 deletions
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<String>) { + 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<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 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) + } +} |