Skip to content

Commit 8ddcbee

Browse files
committed
feat: add distinction between mutable and immutable texts
1 parent ed76e6c commit 8ddcbee

File tree

9 files changed

+130
-101
lines changed

9 files changed

+130
-101
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/Config.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public HorizontalAlign alignCategory(ProcessedCategory category, boolean isSelec
3939

4040
public StructuredText formatCategoryName(ProcessedCategory category, boolean isSelected) {
4141
if (isSelected) {
42-
return category.getDisplayName().underlined().aqua();
42+
return category.getDisplayName().copyShallow().underlined().aqua();
4343
} else if (category.getParentCategoryId() == null) {
44-
return category.getDisplayName().grey();
44+
return category.getDisplayName().copyShallow().grey();
4545
} else {
46-
return category.getDisplayName().darkGrey();
46+
return category.getDisplayName().copyShallow().darkGrey();
4747
}
4848
}
4949

common/src/main/java/io/github/notenoughupdates/moulconfig/common/IMinecraft.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ default void sendChatMessage(StructuredText message) {
8888

8989
StructuredText getKeyName(int keyCode);
9090

91-
StructuredText createLiteral(String text);
91+
StructuredText.Mutable createLiteral(String text);
9292

93-
StructuredText createTranslatable(String key, StructuredText... args);
93+
StructuredText.Mutable createTranslatable(String key, StructuredText... args);
9494

9595
/**
9696
* Create a structured text from an untyped platform object. Must be a platform type exactly, not a string or a structured text.

common/src/main/java/io/github/notenoughupdates/moulconfig/common/text/StructuredText.java

Lines changed: 91 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -9,123 +9,129 @@
99

1010
@ApiStatus.NonExtendable
1111
public interface StructuredText {
12-
static @NotNull StructuredText of(@NotNull String text) {
13-
return IMinecraft.INSTANCE.createLiteral(text);
14-
}
12+
interface Mutable extends StructuredText {
13+
@NotNull Mutable append(@NotNull StructuredText text);
1514

16-
static @NotNull StructuredText empty() {
17-
return of("");
18-
}
15+
default @NotNull Mutable append(@NotNull String text) {
16+
return append(StructuredText.of(text));
17+
}
1918

20-
static @NotNull StructuredText translatable(@NotNull String translationKey, @NotNull StructuredText @NotNull ... args) {
21-
return IMinecraft.INSTANCE.createTranslatable(translationKey, args);
22-
}
19+
default @NotNull Mutable withStyle(@NotNull StructuredStyle style) {
20+
setStyle(style);
21+
return this;
22+
}
2323

24-
/**
25-
* @return a string containing the text of this and any children. this is a lossy conversion.
26-
*/
27-
@NotNull String getText();
24+
default @NotNull Mutable modifyStyle(@NotNull Function<@NotNull StructuredStyle, @NotNull StructuredStyle> operator) {
25+
// ew, non linear types
26+
return withStyle(operator.apply(getStyle()));
27+
}
2828

29-
@NotNull
30-
Stream<@NotNull StructuredText> getChildren();
29+
default @NotNull Mutable withColour(int rgb) {
30+
return modifyStyle(it -> it.withColour(rgb));
31+
}
3132

32-
@NotNull StructuredText append(@NotNull StructuredText text);
33-
default @NotNull StructuredText append(@NotNull String text) {
34-
return append(StructuredText.of(text));
35-
}
33+
default @NotNull Mutable withColour(@NotNull DefaultFormattingColour colour) {
34+
return withColour(colour.getRgb());
35+
}
3636

37-
@NotNull StructuredStyle getStyle();
37+
default @NotNull Mutable bold() {
38+
return modifyStyle(it -> it.withBold(true));
39+
}
3840

39-
void setStyle(StructuredStyle style);
41+
default @NotNull Mutable underlined() {
42+
return modifyStyle(it -> it.withUnderline(true));
43+
}
4044

41-
default @NotNull StructuredText withStyle(@NotNull StructuredStyle style) {
42-
setStyle(style);
43-
return this;
44-
}
45+
default @NotNull Mutable black() {
46+
return withColour(DefaultFormattingColour.BLACK);
47+
}
4548

46-
default @NotNull StructuredText modifyStyle(@NotNull Function<@NotNull StructuredStyle, @NotNull StructuredStyle> operator) {
47-
// ew, non linear types
48-
return withStyle(operator.apply(getStyle()));
49-
}
49+
default @NotNull Mutable darkBlue() {
50+
return withColour(DefaultFormattingColour.DARK_BLUE);
51+
}
5052

51-
default @NotNull StructuredText withColour(int rgb) {
52-
return modifyStyle(it -> it.withColour(rgb));
53-
}
53+
default @NotNull Mutable darkGreen() {
54+
return withColour(DefaultFormattingColour.DARK_GREEN);
55+
}
5456

55-
default @NotNull StructuredText withColour(@NotNull DefaultFormattingColour colour) {
56-
return withColour(colour.getRgb());
57-
}
57+
default @NotNull Mutable darkAqua() {
58+
return withColour(DefaultFormattingColour.DARK_AQUA);
59+
}
5860

59-
default @NotNull StructuredText bold() {
60-
return modifyStyle(it -> it.withBold(true));
61-
}
61+
default @NotNull Mutable darkRed() {
62+
return withColour(DefaultFormattingColour.DARK_RED);
63+
}
6264

63-
default @NotNull StructuredText underlined() {
64-
return modifyStyle(it -> it.withUnderline(true));
65-
}
65+
default @NotNull Mutable darkPurple() {
66+
return withColour(DefaultFormattingColour.DARK_PURPLE);
67+
}
6668

67-
default @NotNull StructuredText black() {
68-
return withColour(DefaultFormattingColour.BLACK);
69-
}
69+
default @NotNull Mutable gold() {
70+
return withColour(DefaultFormattingColour.GOLD);
71+
}
7072

71-
default @NotNull StructuredText darkBlue() {
72-
return withColour(DefaultFormattingColour.DARK_BLUE);
73-
}
73+
default @NotNull Mutable grey() {
74+
return withColour(DefaultFormattingColour.GREY);
75+
}
7476

75-
default @NotNull StructuredText darkGreen() {
76-
return withColour(DefaultFormattingColour.DARK_GREEN);
77-
}
77+
default @NotNull Mutable darkGrey() {
78+
return withColour(DefaultFormattingColour.DARK_GREY);
79+
}
7880

79-
default @NotNull StructuredText darkAqua() {
80-
return withColour(DefaultFormattingColour.DARK_AQUA);
81-
}
81+
default @NotNull Mutable blue() {
82+
return withColour(DefaultFormattingColour.BLUE);
83+
}
8284

83-
default @NotNull StructuredText darkRed() {
84-
return withColour(DefaultFormattingColour.DARK_RED);
85-
}
85+
default @NotNull Mutable green() {
86+
return withColour(DefaultFormattingColour.GREEN);
87+
}
8688

87-
default @NotNull StructuredText darkPurple() {
88-
return withColour(DefaultFormattingColour.DARK_PURPLE);
89-
}
89+
default @NotNull Mutable aqua() {
90+
return withColour(DefaultFormattingColour.AQUA);
91+
}
9092

91-
default @NotNull StructuredText gold() {
92-
return withColour(DefaultFormattingColour.GOLD);
93-
}
93+
default @NotNull Mutable red() {
94+
return withColour(DefaultFormattingColour.RED);
95+
}
9496

95-
default @NotNull StructuredText grey() {
96-
return withColour(DefaultFormattingColour.GREY);
97-
}
97+
default @NotNull Mutable lightPurple() {
98+
return withColour(DefaultFormattingColour.LIGHT_PURPLE);
99+
}
98100

99-
default @NotNull StructuredText darkGrey() {
100-
return withColour(DefaultFormattingColour.DARK_GREY);
101-
}
101+
default @NotNull Mutable yellow() {
102+
return withColour(DefaultFormattingColour.YELLOW);
103+
}
102104

103-
default @NotNull StructuredText blue() {
104-
return withColour(DefaultFormattingColour.BLUE);
105-
}
105+
default @NotNull Mutable white() {
106+
return withColour(DefaultFormattingColour.WHITE);
107+
}
106108

107-
default @NotNull StructuredText green() {
108-
return withColour(DefaultFormattingColour.GREEN);
109109
}
110110

111-
default @NotNull StructuredText aqua() {
112-
return withColour(DefaultFormattingColour.AQUA);
111+
static @NotNull StructuredText.Mutable of(@NotNull String text) {
112+
return IMinecraft.INSTANCE.createLiteral(text);
113113
}
114114

115-
default @NotNull StructuredText red() {
116-
return withColour(DefaultFormattingColour.RED);
115+
static @NotNull StructuredText.Mutable empty() {
116+
return of("");
117117
}
118118

119-
default @NotNull StructuredText lightPurple() {
120-
return withColour(DefaultFormattingColour.LIGHT_PURPLE);
119+
static @NotNull StructuredText.Mutable translatable(@NotNull String translationKey, @NotNull StructuredText @NotNull ... args) {
120+
return IMinecraft.INSTANCE.createTranslatable(translationKey, args);
121121
}
122122

123-
default @NotNull StructuredText yellow() {
124-
return withColour(DefaultFormattingColour.YELLOW);
125-
}
123+
Mutable copyShallow();
126124

127-
default @NotNull StructuredText white() {
128-
return withColour(DefaultFormattingColour.WHITE);
129-
}
125+
/**
126+
* @return a string containing the text of this and any children. this is a lossy conversion.
127+
*/
128+
@NotNull String getText();
129+
130+
@NotNull
131+
Stream<@NotNull StructuredText> getChildren();
132+
133+
@NotNull StructuredStyle getStyle();
134+
135+
void setStyle(StructuredStyle style);
130136

131137
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ class ForgeMinecraft : IMinecraft {
148148
return StructuredText.of(KeybindHelper.getKeyName(keyCode))
149149
}
150150

151-
override fun createLiteral(text: String): StructuredText {
151+
override fun createLiteral(text: String): StructuredText.Mutable {
152152
return StructuredTextImpl.wrap(ChatComponentText(text))
153153
}
154154

155-
override fun createTranslatable(key: String, vararg args: StructuredText): StructuredText {
155+
override fun createTranslatable(key: String, vararg args: StructuredText): StructuredText.Mutable {
156156
return StructuredTextImpl.wrap(ChatComponentTranslation(key, *args))
157157
}
158158

legacy/src/main/java/io/github/notenoughupdates/moulconfig/internal/StructuredStyleImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class StructuredStyleImpl implements StructuredStyle {
1919
return new StructuredStyleImpl(chatStyle);
2020
}
2121

22+
public static ChatStyle unwrap(StructuredStyle style) {
23+
return ((StructuredStyleImpl) style).chatStyle;
24+
}
25+
2226
@Override
2327
public @NotNull StructuredStyle withColour(int rgb) {
2428
return withColour(DefaultFormattingColour.estimate(rgb));

legacy/src/main/java/io/github/notenoughupdates/moulconfig/internal/StructuredTextImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
import java.util.stream.Stream;
1010

1111
@Value
12-
public class StructuredTextImpl implements StructuredText {
12+
public class StructuredTextImpl implements StructuredText.Mutable {
1313
IChatComponent chatComponent;
1414

1515
public static @NotNull IChatComponent unwrap(@NotNull StructuredText structuredText) {
1616
return ((StructuredTextImpl) structuredText).chatComponent;
1717
}
1818

19-
public static @NotNull StructuredText wrap(@NotNull IChatComponent it) {
19+
public static @NotNull StructuredText.Mutable wrap(@NotNull IChatComponent it) {
2020
return new StructuredTextImpl(it);
2121
}
2222

23+
@Override
24+
public Mutable copyShallow() {
25+
return new StructuredTextImpl(chatComponent.createCopy());
26+
}
27+
2328
@Override
2429
public @NotNull String getText() {
2530
return chatComponent.getUnformattedText();
@@ -31,7 +36,7 @@ public class StructuredTextImpl implements StructuredText {
3136
}
3237

3338
@Override
34-
public @NotNull StructuredText append(@NotNull StructuredText text) {
39+
public @NotNull StructuredText.Mutable append(@NotNull StructuredText text) {
3540
chatComponent.appendSibling(unwrap(text));
3641
return this;
3742
}
@@ -43,6 +48,6 @@ public class StructuredTextImpl implements StructuredText {
4348

4449
@Override
4550
public void setStyle(StructuredStyle style) {
46-
51+
chatComponent.setChatStyle(StructuredStyleImpl.unwrap(style));
4752
}
4853
}

legacy/src/main/java/io/github/notenoughupdates/moulconfig/test/TestConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public HorizontalAlign alignCategory(ProcessedCategory category, boolean isSelec
4646

4747
@Override
4848
public StructuredText formatCategoryName(ProcessedCategory category, boolean isSelected) {
49-
return super.formatCategoryName(category, isSelected).append("AAAAAAAAAAAA");
49+
return super.formatCategoryName(category, isSelected).copyShallow().append("AAAAAAAAAAAA");
5050
}
5151

5252
@Override

modern/templates/java/io/github/notenoughupdates/moulconfig/platform/MoulConfigPlatform.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.minecraft.client.util.InputUtil;
2020
import net.minecraft.item.ItemStack;
2121
import net.minecraft.text.ClickEvent;
22+
import net.minecraft.text.MutableText;
2223
import net.minecraft.text.Text;
2324
import net.minecraft.util.Identifier;
2425
import org.apache.logging.log4j.LogManager;
@@ -73,6 +74,10 @@ public static StructuredText wrap(Text text) {
7374
return MoulConfigText.wrap(text);
7475
}
7576

77+
public static StructuredText.Mutable wrap(MutableText text) {
78+
return MoulConfigText.wrap(text);
79+
}
80+
7681
public static TextRenderer unwrap(IFontRenderer fontRenderer) {
7782
return ((MoulConfigFontRenderer) fontRenderer).getFont();
7883
}
@@ -234,12 +239,12 @@ public StructuredText getKeyName(int keyCode) {
234239
}
235240

236241
@Override
237-
public StructuredText createLiteral(String text) {
242+
public StructuredText.Mutable createLiteral(String text) {
238243
return wrap(Text.literal(text));
239244
}
240245

241246
@Override
242-
public StructuredText createTranslatable(String key, StructuredText... args) {
247+
public StructuredText.Mutable createTranslatable(String key, StructuredText... args) {
243248
return wrap(Text.translatable(key, Stream.of(args).map(MoulConfigPlatform::unwrap).toArray()));
244249
}
245250

modern/templates/java/io/github/notenoughupdates/moulconfig/platform/MoulConfigText.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@
1010
import java.util.stream.Stream;
1111

1212
@Value
13-
public class MoulConfigText implements StructuredText {
13+
public class MoulConfigText implements StructuredText.Mutable {
1414
Text text;
1515

16+
@Override
17+
public Mutable copyShallow() {
18+
return new MoulConfigText(text.copy());
19+
}
20+
1621
@Override
1722
public @NotNull String getText() {
1823
return text.getString();
1924
}
2025

26+
public static StructuredText.Mutable wrap(MutableText text) {
27+
return new MoulConfigText(text);
28+
}
29+
2130
public static StructuredText wrap(Text text) {
2231
return new MoulConfigText(text);
2332
}
@@ -33,7 +42,7 @@ public static Text unwrap(StructuredText wrappedText) {
3342

3443
@NotNull
3544
@Override
36-
public StructuredText append(@NotNull StructuredText text) {
45+
public StructuredText.Mutable append(@NotNull StructuredText text) {
3746
((MutableText) this.text).append(unwrap(text));
3847
return this;
3948
}

0 commit comments

Comments
 (0)