aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-10-18 16:35:18 +0200
committerLinnea Gräf <nea@nea.moe>2024-10-18 16:35:18 +0200
commitc71fca6cc633416162b2dd010c52182662ceff85 (patch)
tree71b13f7f666d6b3ade6cded1266b5856a47e3c21 /src
parentc89b663acad487caeb15f7521be3dd14342dd4e7 (diff)
downloadFirmament-c71fca6cc633416162b2dd010c52182662ceff85.tar.gz
Firmament-c71fca6cc633416162b2dd010c52182662ceff85.tar.bz2
Firmament-c71fca6cc633416162b2dd010c52182662ceff85.zip
Add custom inventory text color texture pack support
Diffstat (limited to 'src')
-rw-r--r--src/main/java/moe/nea/firmament/mixins/ReplaceTextColorInHandledScreen.java43
-rw-r--r--src/main/kotlin/features/texturepack/CustomSkyBlockTextures.kt1
-rw-r--r--src/main/kotlin/features/texturepack/CustomTextColors.kt66
-rw-r--r--src/main/resources/assets/firmament/lang/en_us.json1
4 files changed, 111 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/firmament/mixins/ReplaceTextColorInHandledScreen.java b/src/main/java/moe/nea/firmament/mixins/ReplaceTextColorInHandledScreen.java
new file mode 100644
index 0000000..190fda0
--- /dev/null
+++ b/src/main/java/moe/nea/firmament/mixins/ReplaceTextColorInHandledScreen.java
@@ -0,0 +1,43 @@
+package moe.nea.firmament.mixins;
+
+
+import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
+import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
+import moe.nea.firmament.features.texturepack.CustomTextColors;
+import net.minecraft.client.font.TextRenderer;
+import net.minecraft.client.gui.DrawContext;
+import net.minecraft.client.gui.screen.ingame.AnvilScreen;
+import net.minecraft.client.gui.screen.ingame.BeaconScreen;
+import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
+import net.minecraft.client.gui.screen.ingame.HandledScreen;
+import net.minecraft.client.gui.screen.ingame.InventoryScreen;
+import net.minecraft.client.gui.screen.ingame.MerchantScreen;
+import net.minecraft.text.Text;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+
+@Mixin({HandledScreen.class, InventoryScreen.class, CreativeInventoryScreen.class, MerchantScreen.class,
+ AnvilScreen.class, BeaconScreen.class})
+public class ReplaceTextColorInHandledScreen {
+
+ @WrapOperation(
+ method = "drawForeground",
+ at = @At(
+ value = "INVOKE",
+ target = "Lnet/minecraft/client/gui/DrawContext;drawText(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/text/Text;IIIZ)I"),
+ expect = 0)
+ private int replaceTextColorWithVariableShadow(DrawContext instance, TextRenderer textRenderer, Text text, int x, int y, int color, boolean shadow, Operation<Integer> original) {
+ return original.call(instance, textRenderer, text, x, y, CustomTextColors.INSTANCE.mapTextColor(text, color), shadow);
+ }
+
+ @WrapOperation(
+ method = "drawForeground",
+ at = @At(
+ value = "INVOKE",
+ target = "Lnet/minecraft/client/gui/DrawContext;drawTextWithShadow(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/text/Text;III)I"),
+ expect = 0)
+ private int replaceTextColorWithShadow(DrawContext instance, TextRenderer textRenderer, Text text, int x, int y, int color, Operation<Integer> original) {
+ return original.call(instance, textRenderer, text, x, y, CustomTextColors.INSTANCE.mapTextColor(text, color));
+ }
+
+}
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
+ }
+}
diff --git a/src/main/resources/assets/firmament/lang/en_us.json b/src/main/resources/assets/firmament/lang/en_us.json
index cd4d2d4..9a204f7 100644
--- a/src/main/resources/assets/firmament/lang/en_us.json
+++ b/src/main/resources/assets/firmament/lang/en_us.json
@@ -191,6 +191,7 @@
"firmament.config.custom-skyblock-textures.cache-duration": "Model Cache Duration",
"firmament.config.custom-skyblock-textures.model-overrides": "Enable model overrides/predicates",
"firmament.config.custom-skyblock-textures.legacy-cit": "Enable legacy CIT Resewn compat",
+ "firmament.config.custom-skyblock-textures.recolor-text": "Allow packs to recolor text",
"firmament.config.custom-skyblock-textures.armor-overrides": "Enable Armor re-texturing",
"firmament.config.custom-skyblock-textures.block-overrides": "Enable Block re-modelling",
"firmament.config.custom-skyblock-textures.enabled": "Enable Custom Item Textures",