From 3bfec3033e9d905514d5c1c6c62953c2a1646af0 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Fri, 1 Mar 2024 21:31:48 +0100 Subject: Add mob drop viewer to item list --- .../moe/nea/firmament/repo/RepoModResourcePack.kt | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt (limited to 'src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt') diff --git a/src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt b/src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt new file mode 100644 index 0000000..c511c90 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt @@ -0,0 +1,101 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.repo + +import java.io.InputStream +import java.nio.file.Files +import java.nio.file.Path +import net.fabricmc.fabric.api.resource.ModResourcePack +import net.fabricmc.loader.api.FabricLoader +import net.fabricmc.loader.api.metadata.ModMetadata +import kotlin.io.path.exists +import kotlin.io.path.isRegularFile +import kotlin.io.path.relativeTo +import kotlin.streams.asSequence +import net.minecraft.resource.AbstractFileResourcePack +import net.minecraft.resource.InputSupplier +import net.minecraft.resource.ResourcePack +import net.minecraft.resource.ResourceType +import net.minecraft.resource.metadata.ResourceMetadataReader +import net.minecraft.util.Identifier +import net.minecraft.util.PathUtil + +class RepoModResourcePack(val basePath: Path) : ModResourcePack { + companion object { + fun append(packs: MutableList) { + packs.add(RepoModResourcePack(RepoDownloadManager.repoSavedLocation)) + } + } + + override fun close() { + } + + override fun openRoot(vararg segments: String): InputSupplier? { + return getFile(segments)?.let { InputSupplier.create(it) } + } + + fun getFile(segments: Array): Path? { + PathUtil.validatePath(*segments) + val path = segments.fold(basePath, Path::resolve) + if (!path.isRegularFile()) return null + return path + } + + override fun open(type: ResourceType?, id: Identifier): InputSupplier? { + if (type != ResourceType.CLIENT_RESOURCES) return null + if (id.namespace != "neurepo") return null + val file = getFile(id.path.split("/").toTypedArray()) + return file?.let { InputSupplier.create(it) } + } + + override fun findResources( + type: ResourceType?, + namespace: String, + prefix: String, + consumer: ResourcePack.ResultConsumer + ) { + if (namespace != "neurepo") return + if (type != ResourceType.CLIENT_RESOURCES) return + + val prefixPath = basePath.resolve(prefix) + if (!prefixPath.exists()) + return + Files.walk(prefixPath) + .asSequence() + .map { it.relativeTo(basePath) } + .forEach { + consumer.accept(Identifier.of("neurepo", it.toString()), InputSupplier.create(it)) + } + } + + override fun getNamespaces(type: ResourceType?): Set { + if (type != ResourceType.CLIENT_RESOURCES) return emptySet() + return setOf("neurepo") + } + + override fun parseMetadata(metaReader: ResourceMetadataReader): T? { + return AbstractFileResourcePack.parseMetadata( + metaReader, """ +{ + "pack": { + "pack_format": 12, + "description": "NEU Repo Resources" + } +} +""".trimIndent().byteInputStream() + ) + } + + override fun getName(): String { + return "NEU Repo Resources" + } + + override fun getFabricModMetadata(): ModMetadata { + return FabricLoader.getInstance().getModContainer("firmament") + .get().metadata + } +} -- cgit