diff options
author | nea <nea@nea.moe> | 2023-07-17 03:53:57 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-07-17 03:53:57 +0200 |
commit | 86d7516f3f619a0368b36af6dc5cad13babb5cb5 (patch) | |
tree | 7f59d4dd2553a0c21834f4818bdf312bb76381ce /src/main/kotlin/dulkirmod/features | |
parent | 69db365139428bd0c433ccbb9d31dfded7216572 (diff) | |
download | DulkirMod-updatenotifs.tar.gz DulkirMod-updatenotifs.tar.bz2 DulkirMod-updatenotifs.zip |
Add update notificationsupdatenotifs
Diffstat (limited to 'src/main/kotlin/dulkirmod/features')
-rw-r--r-- | src/main/kotlin/dulkirmod/features/UpdateNotificationFeature.kt | 79 |
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 |