Skip to content

Commit 9d183d2

Browse files
committed
feat: add structured texts to 1.21.7
1 parent ff0edf4 commit 9d183d2

File tree

13 files changed

+205
-83
lines changed

13 files changed

+205
-83
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/common/IFontRenderer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ interface IFontRenderer {
1111
fun getStringWidth(string: String): Int = getStringWidth(StructuredText.of(string))
1212
fun getCharWidth(char: Char): Int
1313
fun splitText(text: StructuredText, width: Int): List<StructuredText>
14-
fun splitLines(text: StructuredText): List<StructuredText>
14+
fun splitLines(text: StructuredText): List<StructuredText> {
15+
return splitText(text, Int.MAX_VALUE)
16+
}
1517

1618
fun trimStringToWidth(string: String, width: Int) = trimStringToWidth(string, width, false)
1719
fun trimStringToWidth(string: String, width: Int, reverse: Boolean): String

legacy/src/main/java/io/github/notenoughupdates/moulconfig/internal/ForgeFontRenderer.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ class ForgeFontRenderer(val font: FontRenderer) : IFontRenderer {
2929
return iChatComponents.map { StructuredTextImpl.wrap(it) }
3030
}
3131

32-
override fun splitLines(text: StructuredText): List<StructuredText> {
33-
return splitText(text, Integer.MAX_VALUE)
34-
}
35-
3632
override fun trimStringToWidth(string: String, width: Int, reverse: Boolean): String {
3733
return font.trimStringToWidth(string, width, reverse)
3834
}

modern/1.21.5/src/main/java/io/github/notenoughupdates/moulconfig/platform/ModernFontRenderer.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import io.github.notenoughupdates.moulconfig.common.IFontRenderer
44
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
55
import net.minecraft.client.font.TextRenderer
66
import net.minecraft.client.util.ChatMessages
7+
import net.minecraft.text.StringVisitable
8+
import net.minecraft.text.Style
9+
import net.minecraft.text.Text
10+
import java.util.Optional
711

812
class ModernFontRenderer(val textRenderer: TextRenderer) :
913
IFontRenderer {
@@ -19,24 +23,19 @@ class ModernFontRenderer(val textRenderer: TextRenderer) :
1923
}
2024

2125
override fun splitText(text: StructuredText, width: Int): List<StructuredText> {
22-
val lines = ChatMessages.breakRenderedChatMessageLines(MoulConfigText.unwrap(text), width, textRenderer)
23-
val strings: MutableList<StructuredText> = ArrayList(lines.size)
24-
for (iChatComponent in lines) {
25-
var formattedText = ""
26-
iChatComponent.accept { i, style, j ->
27-
formattedText += j.toChar() // TODO: Implement a proper reconstitution
28-
true
29-
}
30-
strings.add(StructuredText.of(formattedText))
26+
val lines = mutableListOf<StructuredText>()
27+
textRenderer.textHandler.wrapLines(MoulConfigText.unwrap(text), width, Style.EMPTY) { visitable, lastLineWrapped ->
28+
val text = Text.empty()
29+
visitable.visit(StringVisitable.StyledVisitor<Unit> { arg, string ->
30+
text.append(Text.literal(string).setStyle(arg))
31+
Optional.empty()
32+
}, Style.EMPTY)
33+
lines.add(MoulConfigText.wrap(text))
3134
}
32-
return strings
35+
return lines
3336

3437
}
3538

36-
override fun splitLines(text: StructuredText): List<StructuredText> {
37-
return listOf(text) // TODO: this is very much wrong
38-
}
39-
4039
override fun trimStringToWidth(string: String, width: Int, reverse: Boolean): String {
4140
return textRenderer.trimToWidth(string, width, reverse)
4241
}

modern/1.21.7/src/main/kotlin/io/github/notenoughupdates/moulconfig/gui/GuiComponentWrapper.kt renamed to modern/1.21.7/src/main/java/io/github/notenoughupdates/moulconfig/gui/GuiComponentWrapper.kt

File renamed without changes.

modern/1.21.7/src/main/kotlin/io/github/notenoughupdates/moulconfig/gui/GuiElementWrapper.kt renamed to modern/1.21.7/src/main/java/io/github/notenoughupdates/moulconfig/gui/GuiElementWrapper.kt

File renamed without changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.notenoughupdates.moulconfig.platform
2+
3+
import io.github.notenoughupdates.moulconfig.common.IFontRenderer
4+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
5+
import net.minecraft.client.font.TextRenderer
6+
import net.minecraft.text.StringVisitable
7+
import net.minecraft.text.Style
8+
import net.minecraft.text.Text
9+
import java.util.*
10+
11+
class ModernFontRenderer(val textRenderer: TextRenderer) :
12+
IFontRenderer {
13+
override val height: Int
14+
get() = textRenderer.fontHeight
15+
16+
override fun getStringWidth(string: StructuredText): Int {
17+
return textRenderer.getWidth(MoulConfigText.unwrap(string))
18+
}
19+
20+
override fun getStringWidth(string: String): Int {
21+
return textRenderer.getWidth(string)
22+
}
23+
24+
override fun getCharWidth(char: Char): Int {
25+
return textRenderer.getWidth(char + "")
26+
}
27+
28+
override fun splitText(text: StructuredText, width: Int): List<StructuredText> {
29+
val lines = mutableListOf<StructuredText>()
30+
textRenderer.textHandler.wrapLines(MoulConfigText.unwrap(text), width, Style.EMPTY) { visitable, lastLineWrapped ->
31+
val text = Text.empty()
32+
visitable.visit(StringVisitable.StyledVisitor<Unit> { arg, string ->
33+
text.append(Text.literal(string).setStyle(arg))
34+
Optional.empty()
35+
}, Style.EMPTY)
36+
lines.add(MoulConfigText.wrap(text))
37+
}
38+
return lines
39+
}
40+
41+
override fun trimStringToWidth(string: String, width: Int, reverse: Boolean): String {
42+
return textRenderer.trimToWidth(string, width, reverse)
43+
}
44+
}

modern/1.21.7/src/main/kotlin/io/github/notenoughupdates/moulconfig/platform/ModernItemStack.kt renamed to modern/1.21.7/src/main/java/io/github/notenoughupdates/moulconfig/platform/ModernItemStack.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.notenoughupdates.moulconfig.platform
22

33
import io.github.notenoughupdates.moulconfig.common.IItemStack
44
import io.github.notenoughupdates.moulconfig.common.MyResourceLocation
5+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
56
import net.minecraft.client.MinecraftClient
67
import net.minecraft.item.Item
78
import net.minecraft.item.ItemStack
@@ -10,12 +11,12 @@ import net.minecraft.registry.Registries
1011
import net.minecraft.world.World
1112

1213
class ModernItemStack private constructor(val backing: ItemStack) : IItemStack {
13-
override fun getLore(): List<String> {
14-
return backing.getTooltip(Item.TooltipContext.create(null as World?), MinecraftClient.getInstance().player, TooltipType.BASIC).map { it.string }
14+
override fun getLore(): List<StructuredText> {
15+
return backing.getTooltip(Item.TooltipContext.create(null as World?), MinecraftClient.getInstance().player, TooltipType.BASIC).map { MoulConfigText.wrap(it) }
1516
}
1617

17-
override fun getDisplayName(): String {
18-
return backing.name.string
18+
override fun getDisplayName(): StructuredText {
19+
return MoulConfigText.wrap(backing.name)
1920
}
2021

2122
override fun getStackSize(): Int {

modern/1.21.7/src/main/kotlin/io/github/notenoughupdates/moulconfig/platform/ModernRenderContext.kt renamed to modern/1.21.7/src/main/java/io/github/notenoughupdates/moulconfig/platform/ModernRenderContext.kt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.mojang.blaze3d.pipeline.RenderPipeline
44
import com.mojang.blaze3d.platform.DepthTestFunction
55
import com.mojang.blaze3d.vertex.VertexFormat
66
import io.github.notenoughupdates.moulconfig.common.*
7+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
78
import io.github.notenoughupdates.moulconfig.internal.ColourUtil
89
import io.github.notenoughupdates.moulconfig.internal.FilterAssertionCache
910
import io.github.notenoughupdates.moulconfig.internal.Rect
@@ -19,7 +20,10 @@ import net.minecraft.client.render.VertexConsumer
1920
import net.minecraft.client.render.VertexFormats
2021
import net.minecraft.client.texture.TextureSetup
2122
import net.minecraft.client.util.InputUtil
23+
import net.minecraft.text.OrderedText
2224
import net.minecraft.text.Text
25+
import net.minecraft.util.Language
26+
import net.minecraft.world.gen.structure.Structure
2327
import org.joml.Matrix3x2f
2428
import org.lwjgl.glfw.GLFW
2529
import java.util.*
@@ -100,15 +104,8 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
100104
})
101105
}
102106

103-
override fun drawString(
104-
fontRenderer: IFontRenderer,
105-
text: String,
106-
x: Int,
107-
y: Int,
108-
color: Int,
109-
shadow: Boolean
110-
) {
111-
drawContext.drawText((fontRenderer as ModernFontRenderer).textRenderer, text, x, y, ColourUtil.makeOpaque(color), shadow)
107+
override fun drawString(fontRenderer: IFontRenderer, text: StructuredText, x: Int, y: Int, color: Int, shadow: Boolean) {
108+
drawContext.drawText((fontRenderer as ModernFontRenderer).textRenderer, MoulConfigText.unwrap(text), x, y, ColourUtil.makeOpaque(color), shadow)
112109
}
113110

114111
override fun drawColoredRect(left: Float, top: Float, right: Float, bottom: Float, color: Int) {
@@ -204,22 +201,22 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
204201
}
205202
}
206203

207-
override fun renderItemStack(itemStack: IItemStack, x: Int, y: Int, overlayText: String?) {
204+
override fun renderItemStack(itemStack: IItemStack, x: Int, y: Int, overlayText: StructuredText?) {
208205
val item = (itemStack as ModernItemStack).backing
209206
drawContext.drawItem(item, x, y)
210207
drawContext.drawStackOverlay(
211208
MinecraftClient.getInstance().textRenderer,
212209
item,
213210
x,
214211
y,
215-
overlayText ?: ""
212+
overlayText?.text ?: ""
216213
)
217214
}
218215

219-
override fun drawTooltipNow(x: Int, y: Int, tooltipLines: List<String>) {
216+
override fun drawTooltipNow(x: Int, y: Int, tooltipLines: List<StructuredText>) {
220217
drawContext.drawTooltipImmediately(
221218
MinecraftClient.getInstance().textRenderer,
222-
tooltipLines.map { TooltipComponent.of(Text.literal(it).asOrderedText()) },
219+
tooltipLines.map { TooltipComponent.of(Language.getInstance().reorder(MoulConfigText.unwrap(it))) },
223220
x, y,
224221
HoveredTooltipPositioner.INSTANCE,
225222
null,

modern/1.21.7/src/main/kotlin/io/github/notenoughupdates/moulconfig/platform/MoulConfigPlatform.kt renamed to modern/1.21.7/src/main/java/io/github/notenoughupdates/moulconfig/platform/MoulConfigPlatform.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.notenoughupdates.moulconfig.platform
22

33
import io.github.notenoughupdates.moulconfig.common.*
4+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
45
import io.github.notenoughupdates.moulconfig.gui.GuiComponentWrapper
56
import io.github.notenoughupdates.moulconfig.gui.GuiContext
67
import io.github.notenoughupdates.moulconfig.gui.GuiElement
@@ -152,8 +153,8 @@ class MoulConfigPlatform : IMinecraft {
152153
return MinecraftClient.getInstance().keyboard.clipboard ?: ""
153154
}
154155

155-
override fun sendClickableChatMessage(message: String, action: String, type: ClickType) {
156-
MinecraftClient.getInstance().inGameHud.chatHud.addMessage(Text.literal(message).styled {
156+
override fun sendClickableChatMessage(message: StructuredText, action: String, type: ClickType) {
157+
MinecraftClient.getInstance().inGameHud.chatHud.addMessage(MoulConfigText.unwrap(message).copy().styled {
157158
it.withClickEvent(
158159
when (type) {
159160
ClickType.OPEN_LINK -> ClickEvent.OpenUrl(URI(action))
@@ -163,10 +164,24 @@ class MoulConfigPlatform : IMinecraft {
163164
})
164165
}
165166

166-
override fun getKeyName(keyCode: Int): String {
167+
override fun getKeyName(keyCode: Int): StructuredText {
167168
return ModernKeybindHelper.getKeyName(keyCode)
168169
}
169170

171+
override fun createLiteral(text: String): StructuredText {
172+
return MoulConfigText.wrap(Text.literal(text))
173+
}
174+
175+
override fun createTranslatable(key: String, vararg args: StructuredText): StructuredText {
176+
return MoulConfigText.wrap(Text.translatable(key, *args))
177+
}
178+
179+
override fun createStructuredTextInternal(obj: Any): StructuredText? {
180+
if (obj is Text)
181+
return MoulConfigText.wrap(obj)
182+
return null
183+
}
184+
170185
fun NativeImageBackedTexture.setData(img: BufferedImage) {
171186
for (i in (0 until img.width)) {
172187
for (j in (0 until img.height)) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.notenoughupdates.moulconfig.platform;
2+
3+
import io.github.notenoughupdates.moulconfig.common.text.StructuredStyle;
4+
import lombok.Value;
5+
import net.minecraft.text.Style;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
@Value
9+
public class MoulConfigStyle implements StructuredStyle {
10+
@NotNull Style style;
11+
12+
public static MoulConfigStyle wrap(Style style) {
13+
return new MoulConfigStyle(style);
14+
}
15+
16+
public static Style unwrap(StructuredStyle style) {
17+
return ((MoulConfigStyle) style).getStyle();
18+
}
19+
20+
@NotNull
21+
@Override
22+
public StructuredStyle withColour(int rgb) {
23+
return new MoulConfigStyle(style.withColor(rgb));
24+
}
25+
26+
@NotNull
27+
@Override
28+
public StructuredStyle withBold(boolean bold) {
29+
return new MoulConfigStyle(style.withBold(bold));
30+
}
31+
32+
@NotNull
33+
@Override
34+
public StructuredStyle withItalic(boolean italic) {
35+
return new MoulConfigStyle(style.withItalic(italic));
36+
}
37+
38+
@NotNull
39+
@Override
40+
public StructuredStyle withUnderline(boolean underline) {
41+
return new MoulConfigStyle(style.withUnderline(underline));
42+
}
43+
44+
@NotNull
45+
@Override
46+
public StructuredStyle withStrikethrough(boolean strikethrough) {
47+
return new MoulConfigStyle(style.withStrikethrough(strikethrough));
48+
}
49+
50+
@NotNull
51+
@Override
52+
public StructuredStyle withObfuscated(boolean obfuscated) {
53+
return new MoulConfigStyle(style.withObfuscated(obfuscated));
54+
}
55+
}

0 commit comments

Comments
 (0)