Skip to content

Commit e0ea99d

Browse files
committed
(1.8.9) fix formatting for wrapped text
1 parent ff5cdaa commit e0ea99d

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright © 2025 moehreag <[email protected]> & Contributors
3+
*
4+
* This file is part of AxolotlClient.
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*
20+
* For more information, see the LICENSE file.
21+
*/
22+
23+
package io.github.axolotlclient.mixin;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.Locale;
28+
import java.util.Map;
29+
30+
import com.llamalad7.mixinextras.sugar.Local;
31+
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
32+
import io.github.axolotlclient.AxolotlClient;
33+
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
34+
import net.minecraft.client.render.TextRenderUtils;
35+
import net.minecraft.client.render.TextRenderer;
36+
import net.minecraft.text.Formatting;
37+
import net.minecraft.text.LiteralText;
38+
import net.minecraft.text.Text;
39+
import org.spongepowered.asm.mixin.Mixin;
40+
import org.spongepowered.asm.mixin.Unique;
41+
import org.spongepowered.asm.mixin.injection.At;
42+
import org.spongepowered.asm.mixin.injection.Inject;
43+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
44+
45+
@Mixin(TextRenderUtils.class)
46+
public class TextRenderUtilsMixin {
47+
48+
@Unique
49+
private static final Map<Character, Formatting> formattingCodes;
50+
51+
static {
52+
var map = new Object2ObjectOpenHashMap<Character, Formatting>();
53+
map.put('0', Formatting.BLACK);
54+
map.put('1', Formatting.DARK_BLUE);
55+
map.put('2', Formatting.DARK_GREEN);
56+
map.put('3', Formatting.DARK_AQUA);
57+
map.put('4', Formatting.DARK_RED);
58+
map.put('5', Formatting.DARK_PURPLE);
59+
map.put('6', Formatting.GOLD);
60+
map.put('7', Formatting.GRAY);
61+
map.put('8', Formatting.DARK_GRAY);
62+
map.put('9', Formatting.BLUE);
63+
map.put('a', Formatting.GREEN);
64+
map.put('b', Formatting.AQUA);
65+
map.put('c', Formatting.RED);
66+
map.put('d', Formatting.LIGHT_PURPLE);
67+
map.put('e', Formatting.YELLOW);
68+
map.put('f', Formatting.WHITE);
69+
map.put('k', Formatting.OBFUSCATED);
70+
map.put('l', Formatting.BOLD);
71+
map.put('m', Formatting.STRIKETHROUGH);
72+
map.put('n', Formatting.UNDERLINE);
73+
map.put('o', Formatting.ITALIC);
74+
map.put('r', Formatting.RESET);
75+
formattingCodes = map;
76+
}
77+
78+
79+
@Inject(method = "wrapText", at = @At("HEAD"))
80+
private static void reformatText(Text pText, int i, TextRenderer textRenderer, boolean bl, boolean bl2, CallbackInfoReturnable<List<Text>> cir, @Local(argsOnly = true) LocalRef<Text> text) {
81+
text.set(formatFromCodes(text.get().getFormattedString()));
82+
}
83+
84+
@Unique
85+
private static Formatting byCodeOfFirstChar(String code) {
86+
char c = code.toLowerCase(Locale.ROOT).charAt(0);
87+
88+
for (Map.Entry<Character, Formatting> formatting : formattingCodes.entrySet()) {
89+
if (formatting.getKey() == c) {
90+
return formatting.getValue();
91+
}
92+
}
93+
94+
return null;
95+
}
96+
97+
@Unique
98+
private static Text formatFromCodes(String formattedString) {
99+
Text text = new LiteralText("");
100+
String[] arr = formattedString.split("§");
101+
102+
List<Formatting> modifiers = new ArrayList<>();
103+
for (String s : arr) {
104+
Formatting formatting = s.isEmpty() ? null : byCodeOfFirstChar(s);
105+
if (formatting != null && formatting.isModifier()) {
106+
modifiers.add(formatting);
107+
}
108+
Text part = new LiteralText(!s.isEmpty() ? s.substring(1) : "");
109+
if (formatting != null) {
110+
part.getStyle().setColor(formatting);
111+
112+
if (!modifiers.isEmpty()) {
113+
for (Formatting mod : modifiers) {
114+
switch (mod) {
115+
case OBFUSCATED -> part.getStyle().setObfuscated(true);
116+
case BOLD -> part.getStyle().setBold(true);
117+
case ITALIC -> part.getStyle().setItalic(true);
118+
case UNDERLINE -> part.getStyle().setUnderlined(true);
119+
default -> AxolotlClient.LOGGER.warn("Unexpected modifier: " + mod);
120+
}
121+
}
122+
if (formatting.equals(Formatting.RESET)) {
123+
modifiers.clear();
124+
}
125+
}
126+
}
127+
text.append(part);
128+
}
129+
return text;
130+
}
131+
}

1.8.9/src/main/java/io/github/axolotlclient/mixin/TextRendererMixin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public abstract class TextRendererMixin {
4949

5050
// Pain at its finest
5151

52+
@Unique
5253
private final Identifier texture_g = new Identifier("axolotlclient", "textures/font/g_breve_capital.png");
5354
@Shadow
5455
public int fontHeight;
@@ -66,6 +67,7 @@ public abstract class TextRendererMixin {
6667
private float y;
6768
@Shadow
6869
private int color;
70+
@Unique
6971
private boolean shouldHaveShadow;
7072

7173
@Inject(method = "drawLayer(Ljava/lang/String;FFIZ)I", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/TextRenderer;drawLayer(Ljava/lang/String;Z)V"))
@@ -93,6 +95,7 @@ public abstract class TextRendererMixin {
9395
}
9496
}
9597

98+
@Unique
9699
private void drawTexture(float x, float y) {
97100
Tessellator tessellator = Tessellator.getInstance();
98101
BufferBuilder bufferBuilder = tessellator.getBuilder();

1.8.9/src/main/resources/axolotlclient.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"SoundManagerAccessor",
5757
"SoundSystemAccessor",
5858
"TextRendererMixin",
59+
"TextRenderUtilsMixin",
5960
"TextureManagerAccessor",
6061
"TitleScreenMixin",
6162
"TntEntityRendererMixin",

0 commit comments

Comments
 (0)