From 8c66016bb3041793961375178d470546519e0e69 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sat, 18 May 2024 17:33:19 +0200 Subject: Add more screens at once [no changelog] --- docs/Texture Pack Format.md | 3 ++ .../features/texturepack/CustomGlobalTextures.kt | 14 +++++---- .../util/json/SingletonSerializableList.kt | 36 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt diff --git a/docs/Texture Pack Format.md b/docs/Texture Pack Format.md index 914e157..ab072b3 100644 --- a/docs/Texture Pack Format.md +++ b/docs/Texture Pack Format.md @@ -268,6 +268,9 @@ avoid collisions with other texture packs that might use the same id for a gui. The `predicate` is just a normal [predicate](#predicates). This one does not support the vanilla predicates. You can still use vanilla predicates in the resolved model, but this will not allow you to fall back to other global overrides. +The `screen` specifies which screens your override will work on. This is purely for performance reasons, your filter +should work purely based on predicates if possible. You can specify multiply screens by using a json array. + ### Global item texture Screens In order to improve performance not all overrides are tested all the time. Instead you can prefilter by the screen that diff --git a/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomGlobalTextures.kt b/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomGlobalTextures.kt index 2eb4ee1..c2a003a 100644 --- a/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomGlobalTextures.kt +++ b/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomGlobalTextures.kt @@ -33,6 +33,7 @@ import moe.nea.firmament.events.subscription.SubscriptionOwner import moe.nea.firmament.features.FirmamentFeature import moe.nea.firmament.util.IdentifierSerializer import moe.nea.firmament.util.MC +import moe.nea.firmament.util.json.SingletonSerializableList import moe.nea.firmament.util.runNull object CustomGlobalTextures : SinglePreparationResourceReloader(), @@ -46,7 +47,7 @@ object CustomGlobalTextures : SinglePreparationResourceReloader, val model: Identifier, val predicate: FirmamentModelPredicate, ) @@ -107,7 +108,8 @@ object CustomGlobalTextures : SinglePreparationResourceReloader override.screen.toSet().map { it to override } } + .groupBy { it.first } val guiClasses = byGuiClass.entries .mapNotNull { val key = it.key @@ -123,7 +125,7 @@ object CustomGlobalTextures : SinglePreparationResourceReloader = listOf() + var matchingOverrides: Set = setOf() @Subscribe fun onOpenGui(event: ScreenChangeEvent) { val newTitle = event.new?.title matchingOverrides = - if (newTitle == null) listOf() - else guiClassOverrides.classes.filter { it.screenFilter.title.matches(newTitle) } + if (newTitle == null) setOf() + else guiClassOverrides.classes.filterTo(mutableSetOf()) { it.screenFilter.title.matches(newTitle) } } @JvmStatic diff --git a/src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt b/src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt new file mode 100644 index 0000000..5474b35 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util.json + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.JsonDecoder +import kotlinx.serialization.json.JsonElement + +class SingletonSerializableList(val child: KSerializer) : KSerializer> { + override val descriptor: SerialDescriptor + get() = JsonElement.serializer().descriptor + + override fun deserialize(decoder: Decoder): List { + decoder as JsonDecoder + val list = JsonElement.serializer().deserialize(decoder) + if (list is JsonArray) { + return list.map { + decoder.json.decodeFromJsonElement(child, it) + } + } + return listOf(decoder.json.decodeFromJsonElement(child, list)) + } + + override fun serialize(encoder: Encoder, value: List) { + ListSerializer(child).serialize(encoder, value) + } +} -- cgit