diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-01 21:31:48 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-02 19:43:00 +0100 |
commit | 3bfec3033e9d905514d5c1c6c62953c2a1646af0 (patch) | |
tree | afee0b3c20d0bdef9780e5fddb368c9d58fb3c70 /src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt | |
parent | f28dee0ef3a0dd4a0819a3d3a1c800a83a0f07f5 (diff) | |
download | Firmament-3bfec3033e9d905514d5c1c6c62953c2a1646af0.tar.gz Firmament-3bfec3033e9d905514d5c1c6c62953c2a1646af0.tar.bz2 Firmament-3bfec3033e9d905514d5c1c6c62953c2a1646af0.zip |
Add mob drop viewer to item list
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/repo/RepoModResourcePack.kt | 101 |
1 files changed, 101 insertions, 0 deletions
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 <nea@nea.moe> + * + * 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<in ModResourcePack>) { + packs.add(RepoModResourcePack(RepoDownloadManager.repoSavedLocation)) + } + } + + override fun close() { + } + + override fun openRoot(vararg segments: String): InputSupplier<InputStream>? { + return getFile(segments)?.let { InputSupplier.create(it) } + } + + fun getFile(segments: Array<out String>): 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<InputStream>? { + 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<String> { + if (type != ResourceType.CLIENT_RESOURCES) return emptySet() + return setOf("neurepo") + } + + override fun <T> parseMetadata(metaReader: ResourceMetadataReader<T>): 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 + } +} |