From 05a4a5b3817ea3e70662d3834fd56a1b249fa70e Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 4 Aug 2022 04:26:51 +0200 Subject: screen padding --- .../moe/nea/notenoughupdates/NotEnoughUpdates.kt | 26 +++++++++++++---- .../moe/nea/notenoughupdates/repo/ItemCache.kt | 13 +++++++++ .../moe/nea/notenoughupdates/repo/RepoManager.kt | 27 +++++++++++++---- .../moe/nea/notenoughupdates/util/ConfigHolder.kt | 34 ++++++++++++++++------ 4 files changed, 79 insertions(+), 21 deletions(-) (limited to 'src/main') diff --git a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt index 1986339..a7a8a19 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt @@ -3,6 +3,7 @@ package moe.nea.notenoughupdates import com.mojang.brigadier.Command import com.mojang.brigadier.CommandDispatcher import io.github.cottonmc.cotton.gui.client.CottonClientScreen +import io.github.moulberry.repo.NEURepositoryException import io.ktor.client.* import io.ktor.client.plugins.* import io.ktor.client.plugins.contentnegotiation.* @@ -18,6 +19,7 @@ import net.fabricmc.api.ModInitializer import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents import net.fabricmc.loader.api.FabricLoader import net.fabricmc.loader.api.Version import net.fabricmc.loader.api.metadata.ModMetadata @@ -67,14 +69,20 @@ object NotEnoughUpdates : ModInitializer, ClientModInitializer { ) { dispatcher.register(ClientCommandManager.literal("neureload").executes { it.source.sendFeedback(Component.literal("Reloading repository from disk. This may lag a bit.")) - RepoManager.neuRepo.reload() + try { + RepoManager.reload() + } catch (exc: NEURepositoryException) { + it.source.sendError(Component.literal("There has been an error reloading the repository. Please try again. IF this persists, delete the .notenoughupdates folder in your mincraft folder")) + exc.printStackTrace() + } Command.SINGLE_SUCCESS }) - dispatcher.register(ClientCommandManager.literal("neu") - .then(ClientCommandManager.literal("repo").executes { - setScreenLater(CottonClientScreen(RepoManagementGui())) - Command.SINGLE_SUCCESS - }) + dispatcher.register( + ClientCommandManager.literal("neu") + .then(ClientCommandManager.literal("repo").executes { + setScreenLater(CottonClientScreen(RepoManagementGui())) + Command.SINGLE_SUCCESS + }) ) } @@ -82,6 +90,12 @@ object NotEnoughUpdates : ModInitializer, ClientModInitializer { RepoManager.initialize() ConfigHolder.registerEvents() ClientCommandRegistrationCallback.EVENT.register(this::registerCommands) + ClientLifecycleEvents.CLIENT_STOPPING.register(ClientLifecycleEvents.ClientStopping { + runBlocking { + logger.info("Shutting down NEU coroutines") + globalJob.cancel() + } + }) } override fun onInitializeClient() { diff --git a/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt b/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt index 893b1c0..8deb4c3 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt @@ -4,6 +4,8 @@ import com.mojang.serialization.Dynamic import io.github.moulberry.repo.IReloadable import io.github.moulberry.repo.NEURepository import io.github.moulberry.repo.data.NEUItem +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch import moe.nea.notenoughupdates.NotEnoughUpdates import moe.nea.notenoughupdates.util.LegacyTagParser import moe.nea.notenoughupdates.util.appendLore @@ -70,7 +72,18 @@ object ItemCache : IReloadable { ResourceLocation("skyblockitem", skyblockItemId.lowercase().replace(";", "__")) + var job: Job? = null + override fun reload(repository: NEURepository) { cache.clear() + val j = job + if (j != null && j.isActive) { + j.cancel() + job = NotEnoughUpdates.coroutineScope.launch { + repository.items?.items?.values?.forEach { + it.asItemStack() // Rebuild cache + } + } + } } } diff --git a/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt b/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt index 70ee18e..375b2bb 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/repo/RepoManager.kt @@ -1,6 +1,7 @@ package moe.nea.notenoughupdates.repo import io.github.moulberry.repo.NEURepository +import io.github.moulberry.repo.NEURepositoryException import kotlinx.coroutines.launch import kotlinx.serialization.Serializable import kotlinx.serialization.serializer @@ -9,15 +10,16 @@ import moe.nea.notenoughupdates.NotEnoughUpdates.logger import moe.nea.notenoughupdates.util.ConfigHolder import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents import net.minecraft.client.Minecraft +import net.minecraft.network.chat.Component import net.minecraft.network.protocol.game.ClientboundUpdateRecipesPacket object RepoManager : ConfigHolder(serializer(), "repo", ::Config) { @Serializable data class Config( - var user: String = "NotEnoughUpdates", - var repo: String = "NotEnoughUpdates-REPO", - var autoUpdate: Boolean = true, - var branch: String = "dangerous", + var user: String = "NotEnoughUpdates", + var repo: String = "NotEnoughUpdates-REPO", + var autoUpdate: Boolean = true, + var branch: String = "dangerous", ) var recentlyFailedToUpdateItemList = false @@ -33,7 +35,9 @@ object RepoManager : ConfigHolder(serializer(), "repo", ::Co } private fun trySendClientboundUpdateRecipesPacket(): Boolean { - return Minecraft.getInstance().level != null && Minecraft.getInstance().connection?.handleUpdateRecipes(ClientboundUpdateRecipesPacket(mutableListOf())) != null + return Minecraft.getInstance().level != null && Minecraft.getInstance().connection?.handleUpdateRecipes( + ClientboundUpdateRecipesPacket(mutableListOf()) + ) != null } init { @@ -46,7 +50,18 @@ object RepoManager : ConfigHolder(serializer(), "repo", ::Co fun launchAsyncUpdate() { NotEnoughUpdates.coroutineScope.launch { RepoDownloadManager.downloadUpdate() + reload() + } + } + + fun reload() { + try { neuRepo.reload() + } catch (exc: NEURepositoryException) { + Minecraft.getInstance().player?.sendSystemMessage( + Component.literal("Failed to reload repository. This will result in some mod features not working.") + ) + exc.printStackTrace() } } @@ -54,7 +69,7 @@ object RepoManager : ConfigHolder(serializer(), "repo", ::Co if (config.autoUpdate) { launchAsyncUpdate() } else { - neuRepo.reload() + reload() } } diff --git a/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt index 003de6c..07a9b4c 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/util/ConfigHolder.kt @@ -4,6 +4,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.SerializationException import moe.nea.notenoughupdates.NotEnoughUpdates import moe.nea.notenoughupdates.events.NEUScreenEvents +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents import net.minecraft.client.Minecraft import net.minecraft.commands.CommandSource import net.minecraft.network.chat.Component @@ -15,9 +16,11 @@ import kotlin.io.path.readText import kotlin.io.path.writeText import kotlin.reflect.KClass -abstract class ConfigHolder(val serializer: KSerializer, - val name: String, - val default: () -> T) { +abstract class ConfigHolder( + val serializer: KSerializer, + val name: String, + val default: () -> T +) { var config: T private set @@ -33,15 +36,21 @@ abstract class ConfigHolder(val serializer: KSerializer, if (file.exists()) try { return NotEnoughUpdates.json.decodeFromString( - serializer, - file.readText() + serializer, + file.readText() ) } catch (e: IOException) { badLoads.add(name) - NotEnoughUpdates.logger.error("IO exception during loading of config file $name. This will reset this config.", e) + NotEnoughUpdates.logger.error( + "IO exception during loading of config file $name. This will reset this config.", + e + ) } catch (e: SerializationException) { badLoads.add(name) - NotEnoughUpdates.logger.error("Serialization exception during loading of config file $name. This will reset this config.", e) + NotEnoughUpdates.logger.error( + "Serialization exception during loading of config file $name. This will reset this config.", + e + ) } return default() } @@ -95,8 +104,12 @@ abstract class ConfigHolder(val serializer: KSerializer, private fun warnForResetConfigs(player: CommandSource) { if (badLoads.isNotEmpty()) { - player.sendSystemMessage(Component.literal("The following configs have been reset: ${badLoads.joinToString(", ")}. " + - "This can be intentional, but probably isn't.")) + player.sendSystemMessage( + Component.literal( + "The following configs have been reset: ${badLoads.joinToString(", ")}. " + + "This can be intentional, but probably isn't." + ) + ) badLoads.clear() } } @@ -110,6 +123,9 @@ abstract class ConfigHolder(val serializer: KSerializer, } false }) + ClientLifecycleEvents.CLIENT_STOPPING.register(ClientLifecycleEvents.ClientStopping { + performSaves() + }) } -- cgit