aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/features')
-rw-r--r--src/main/kotlin/features/texturepack/CustomSkyBlockTextures.kt1
-rw-r--r--src/main/kotlin/features/texturepack/CustomTextColors.kt66
2 files changed, 67 insertions, 0 deletions
diff --git a/src/main/kotlin/features/texturepack/CustomSkyBlockTextures.kt b/src/main/kotlin/features/texturepack/CustomSkyBlockTextures.kt
index b641959..627d39a 100644
--- a/src/main/kotlin/features/texturepack/CustomSkyBlockTextures.kt
+++ b/src/main/kotlin/features/texturepack/CustomSkyBlockTextures.kt
@@ -35,6 +35,7 @@ object CustomSkyBlockTextures : FirmamentFeature {
val enableArmorOverrides by toggle("armor-overrides") { true }
val enableBlockOverrides by toggle("block-overrides") { true }
val enableLegacyCIT by toggle("legacy-cit") { true }
+ val allowRecoloringUiText by toggle("recolor-text") { true }
}
override val config: ManagedConfig
diff --git a/src/main/kotlin/features/texturepack/CustomTextColors.kt b/src/main/kotlin/features/texturepack/CustomTextColors.kt
new file mode 100644
index 0000000..4ca1796
--- /dev/null
+++ b/src/main/kotlin/features/texturepack/CustomTextColors.kt
@@ -0,0 +1,66 @@
+package moe.nea.firmament.features.texturepack
+
+import java.util.Optional
+import kotlinx.serialization.Serializable
+import kotlin.jvm.optionals.getOrNull
+import net.minecraft.resource.ResourceManager
+import net.minecraft.resource.SinglePreparationResourceReloader
+import net.minecraft.text.Text
+import net.minecraft.util.Identifier
+import net.minecraft.util.profiler.Profiler
+import moe.nea.firmament.Firmament
+import moe.nea.firmament.annotations.Subscribe
+import moe.nea.firmament.events.FinalizeResourceManagerEvent
+import moe.nea.firmament.util.collections.WeakCache
+
+object CustomTextColors : SinglePreparationResourceReloader<CustomTextColors.TextOverrides?>() {
+ @Serializable
+ data class TextOverrides(
+ val defaultColor: Int,
+ val overrides: List<TextOverride> = listOf()
+ )
+
+ @Serializable
+ data class TextOverride(
+ val predicate: StringMatcher,
+ val override: Int,
+ )
+
+ @Subscribe
+ fun registerTextColorReloader(event: FinalizeResourceManagerEvent) {
+ event.resourceManager.registerReloader(this)
+ }
+
+ val cache = WeakCache.memoize<Text, Optional<Int>>("CustomTextColor") { text ->
+ val override = textOverrides ?: return@memoize Optional.empty()
+ Optional.of(override.overrides.find { it.predicate.matches(text) }?.override ?: override.defaultColor)
+ }
+
+ fun mapTextColor(text: Text, oldColor: Int): Int {
+ if (textOverrides == null) return oldColor
+ return cache(text).getOrNull() ?: oldColor
+ }
+
+ override fun prepare(
+ manager: ResourceManager,
+ profiler: Profiler
+ ): TextOverrides? {
+ val resource = manager.getResource(Identifier.of("firmskyblock", "overrides/text_colors.json")).getOrNull()
+ ?: return null
+ return Firmament.tryDecodeJsonFromStream<TextOverrides>(resource.inputStream)
+ .getOrElse {
+ Firmament.logger.error("Could not parse text_colors.json", it)
+ null
+ }
+ }
+
+ var textOverrides: TextOverrides? = null
+
+ override fun apply(
+ prepared: TextOverrides?,
+ manager: ResourceManager,
+ profiler: Profiler
+ ) {
+ textOverrides = prepared
+ }
+}