diff options
author | nea <nea@nea.moe> | 2023-05-16 01:23:43 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-05-16 01:23:43 +0200 |
commit | ead6762eb1c005914b05f9d3c29f334989c67513 (patch) | |
tree | cd1409756be2bc4a93195c31d432fef053afe002 /src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt | |
parent | 96c546cc73880a7c502c17aadda6ca84c847692d (diff) | |
download | firmament-ead6762eb1c005914b05f9d3c29f334989c67513.tar.gz firmament-ead6762eb1c005914b05f9d3c29f334989c67513.tar.bz2 firmament-ead6762eb1c005914b05f9d3c29f334989c67513.zip |
Replace references to NEU with Firmament
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt b/src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt new file mode 100644 index 0000000..ac880b5 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt @@ -0,0 +1,102 @@ +package moe.nea.firmament.repo + +import io.github.cottonmc.cotton.gui.client.CottonHud +import io.github.moulberry.repo.NEURecipeCache +import io.github.moulberry.repo.NEURepository +import io.github.moulberry.repo.NEURepositoryException +import io.github.moulberry.repo.data.NEURecipe +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents +import kotlinx.coroutines.launch +import kotlinx.serialization.Serializable +import kotlinx.serialization.serializer +import net.minecraft.client.MinecraftClient +import net.minecraft.network.packet.s2c.play.SynchronizeRecipesS2CPacket +import net.minecraft.text.Text +import moe.nea.firmament.Firmament +import moe.nea.firmament.Firmament.logger +import moe.nea.firmament.hud.ProgressBar +import moe.nea.firmament.util.SkyblockId +import moe.nea.firmament.util.data.DataHolder + +object RepoManager : DataHolder<RepoManager.Config>(serializer(), "repo", ::Config) { + @Serializable + data class Config( + var user: String = "NotEnoughUpdates", + var repo: String = "NotEnoughUpdates-REPO", + var autoUpdate: Boolean = true, + var branch: String = "dangerous", + ) + + val currentDownloadedSha by RepoDownloadManager::latestSavedVersionHash + + var recentlyFailedToUpdateItemList = false + + val progressBar = ProgressBar("", null, 0).also { + it.setSize(180, 22) + } + + val neuRepo: NEURepository = NEURepository.of(RepoDownloadManager.repoSavedLocation).apply { + registerReloadListener(ItemCache) + registerReloadListener { + if (!trySendClientboundUpdateRecipesPacket()) { + logger.warn("Failed to issue a ClientboundUpdateRecipesPacket (to reload REI). This may lead to an outdated item list.") + recentlyFailedToUpdateItemList = true + } + } + } + + private val recipeCache = NEURecipeCache.forRepo(neuRepo) + + fun getAllRecipes() = neuRepo.items.items.values.asSequence().flatMap { it.recipes } + + fun getRecipesFor(skyblockId: SkyblockId): Set<NEURecipe> = recipeCache.recipes[skyblockId.neuItem] ?: setOf() + fun getUsagesFor(skyblockId: SkyblockId): Set<NEURecipe> = recipeCache.usages[skyblockId.neuItem] ?: setOf() + + private fun trySendClientboundUpdateRecipesPacket(): Boolean { + return MinecraftClient.getInstance().world != null && MinecraftClient.getInstance().networkHandler?.onSynchronizeRecipes( + SynchronizeRecipesS2CPacket(mutableListOf()) + ) != null + } + + init { + ClientTickEvents.START_WORLD_TICK.register(ClientTickEvents.StartWorldTick { + if (recentlyFailedToUpdateItemList && trySendClientboundUpdateRecipesPacket()) + recentlyFailedToUpdateItemList = false + }) + } + + fun getNEUItem(skyblockId: SkyblockId) = neuRepo.items.getItemBySkyblockId(skyblockId.neuItem) + + fun launchAsyncUpdate(force: Boolean = false) { + Firmament.coroutineScope.launch { + progressBar.reportProgress("Downloading", 0, null) + CottonHud.add(progressBar) + RepoDownloadManager.downloadUpdate(force) + progressBar.reportProgress("Download complete", 1, 1) + reload() + } + } + + fun reload() { + try { + progressBar.reportProgress("Reloading from Disk", 0, null) + CottonHud.add(progressBar) + neuRepo.reload() + } catch (exc: NEURepositoryException) { + MinecraftClient.getInstance().player?.sendMessage( + Text.literal("Failed to reload repository. This will result in some mod features not working.") + ) + CottonHud.remove(progressBar) + exc.printStackTrace() + } + } + + fun initialize() { + if (data.autoUpdate) { + launchAsyncUpdate() + } else { + reload() + } + } + +} |