Style .java

net.minecraft.network.chat.Style (class): members: applyLegacyFormat, withStrikethrough, isStrikethrough, withShadowColor, getShadowColor, withClickEvent.

net.minecraft.network.chatclass618 linescommon15.9 KB

API Surface (28)

boolean isBold()
boolean isItalic()
boolean isStrikethrough()
boolean isUnderlined()
boolean isObfuscated()
boolean isEmpty()
FontDescription getFont()
Style withColor(final @Nullable TextColor color)
Style withColor(final @Nullable ChatFormatting color)
Style withColor(final int color)
Style withShadowColor(final int shadowColor)
Style withoutShadow()
Style withBold(final @Nullable Boolean bold)
Style withItalic(final @Nullable Boolean italic)
Style withUnderlined(final @Nullable Boolean underlined)
Style withStrikethrough(final @Nullable Boolean strikethrough)
Style withObfuscated(final @Nullable Boolean obfuscated)
Style withClickEvent(final @Nullable ClickEvent clickEvent)
Style withHoverEvent(final @Nullable HoverEvent hoverEvent)
Style withInsertion(final @Nullable String insertion)
Style withFont(final @Nullable FontDescription font)
Style applyFormat(final ChatFormatting format)
Style applyLegacyFormat(final ChatFormatting format)
Style applyFormats(final ChatFormatting... formats)
Style applyTo(final Style other)
String toString()
boolean equals(final Object o)
int hashCode()

Fields & Constants (13)

EMPTY
NO_SHADOW
color
shadowColor
bold
italic
underlined
strikethrough
obfuscated
clickEvent
hoverEvent
insertion
font

String literals (14)

"color"
"shadowColor"
"bold"
"italic"
"underlined"
"strikethrough"
"obfuscated"
"clickEvent"
"hoverEvent"
"insertion"
"font"
"shadow_color"
"click_event"
"hover_event"

Full Source

Style.java618 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.chat;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.MapCodec;
5
import com.mojang.serialization.codecs.RecordCodecBuilder;
6
import java.util.Objects;
7
import java.util.Optional;
8
import net.minecraft.ChatFormatting;
9
import net.minecraft.network.RegistryFriendlyByteBuf;
10
import net.minecraft.network.codec.ByteBufCodecs;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.util.ExtraCodecs;
13
import org.jspecify.annotations.Nullable;
14
15
public final class Style {
16
	public static final Style EMPTY = new Style(null, null, null, null, null, null, null, null, null, null, null);
17
	public static final int NO_SHADOW = 0;
18
	private final @Nullable TextColor color;
19
	private final @Nullable Integer shadowColor;
20
	private final @Nullable Boolean bold;
21
	private final @Nullable Boolean italic;
22
	private final @Nullable Boolean underlined;
23
	private final @Nullable Boolean strikethrough;
24
	private final @Nullable Boolean obfuscated;
25
	private final @Nullable ClickEvent clickEvent;
26
	private final @Nullable HoverEvent hoverEvent;
27
	private final @Nullable String insertion;
28
	private final @Nullable FontDescription font;
29
30
	private static Style create(
31
		final Optional<TextColor> color,
32
		final Optional<Integer> shadowColor,
33
		final Optional<Boolean> bold,
34
		final Optional<Boolean> italic,
35
		final Optional<Boolean> underlined,
36
		final Optional<Boolean> strikethrough,
37
		final Optional<Boolean> obfuscated,
38
		final Optional<ClickEvent> clickEvent,
39
		final Optional<HoverEvent> hoverEvent,
40
		final Optional<String> insertion,
41
		final Optional<FontDescription> font
42
	) {
43
		Style result = new Style(
44
			color.orElse(null),
45
			shadowColor.orElse(null),
46
			bold.orElse(null),
47
			italic.orElse(null),
48
			underlined.orElse(null),
49
			strikethrough.orElse(null),
50
			obfuscated.orElse(null),
51
			clickEvent.orElse(null),
52
			hoverEvent.orElse(null),
53
			insertion.orElse(null),
54
			font.orElse(null)
55
		);
56
		return result.equals(EMPTY) ? EMPTY : result;
57
	}
58
59
	private Style(
60
		final @Nullable TextColor color,
61
		final @Nullable Integer shadowColor,
62
		final @Nullable Boolean bold,
63
		final @Nullable Boolean italic,
64
		final @Nullable Boolean underlined,
65
		final @Nullable Boolean strikethrough,
66
		final @Nullable Boolean obfuscated,
67
		final @Nullable ClickEvent clickEvent,
68
		final @Nullable HoverEvent hoverEvent,
69
		final @Nullable String insertion,
70
		final @Nullable FontDescription font
71
	) {
72
		this.color = color;
73
		this.shadowColor = shadowColor;
74
		this.bold = bold;
75
		this.italic = italic;
76
		this.underlined = underlined;
77
		this.strikethrough = strikethrough;
78
		this.obfuscated = obfuscated;
79
		this.clickEvent = clickEvent;
80
		this.hoverEvent = hoverEvent;
81
		this.insertion = insertion;
82
		this.font = font;
83
	}
84
85
	public @Nullable TextColor getColor() {
86
		return this.color;
87
	}
88
89
	public @Nullable Integer getShadowColor() {
90
		return this.shadowColor;
91
	}
92
93
	public boolean isBold() {
94
		return this.bold == Boolean.TRUE;
95
	}
96
97
	public boolean isItalic() {
98
		return this.italic == Boolean.TRUE;
99
	}
100
101
	public boolean isStrikethrough() {
102
		return this.strikethrough == Boolean.TRUE;
103
	}
104
105
	public boolean isUnderlined() {
106
		return this.underlined == Boolean.TRUE;
107
	}
108
109
	public boolean isObfuscated() {
110
		return this.obfuscated == Boolean.TRUE;
111
	}
112
113
	public boolean isEmpty() {
114
		return this == EMPTY;
115
	}
116
117
	public @Nullable ClickEvent getClickEvent() {
118
		return this.clickEvent;
119
	}
120
121
	public @Nullable HoverEvent getHoverEvent() {
122
		return this.hoverEvent;
123
	}
124
125
	public @Nullable String getInsertion() {
126
		return this.insertion;
127
	}
128
129
	public FontDescription getFont() {
130
		return this.font != null ? this.font : FontDescription.DEFAULT;
131
	}
132
133
	private static <T> Style checkEmptyAfterChange(final Style newStyle, final @Nullable T previous, final @Nullable T next) {
134
		return previous != null && next == null && newStyle.equals(EMPTY) ? EMPTY : newStyle;
135
	}
136
137
	public Style withColor(final @Nullable TextColor color) {
138
		return Objects.equals(this.color, color)
139
			? this
140
			: checkEmptyAfterChange(
141
				new Style(
142
					color,
143
					this.shadowColor,
144
					this.bold,
145
					this.italic,
146
					this.underlined,
147
					this.strikethrough,
148
					this.obfuscated,
149
					this.clickEvent,
150
					this.hoverEvent,
151
					this.insertion,
152
					this.font
153
				),
154
				this.color,
155
				color
156
			);
157
	}
158
159
	public Style withColor(final @Nullable ChatFormatting color) {
160
		return this.withColor(color != null ? TextColor.fromLegacyFormat(color) : null);
161
	}
162
163
	public Style withColor(final int color) {
164
		return this.withColor(TextColor.fromRgb(color));
165
	}
166
167
	public Style withShadowColor(final int shadowColor) {
168
		return Objects.equals(this.shadowColor, shadowColor)
169
			? this
170
			: checkEmptyAfterChange(
171
				new Style(
172
					this.color,
173
					shadowColor,
174
					this.bold,
175
					this.italic,
176
					this.underlined,
177
					this.strikethrough,
178
					this.obfuscated,
179
					this.clickEvent,
180
					this.hoverEvent,
181
					this.insertion,
182
					this.font
183
				),
184
				this.shadowColor,
185
				shadowColor
186
			);
187
	}
188
189
	public Style withoutShadow() {
190
		return this.withShadowColor(0);
191
	}
192
193
	public Style withBold(final @Nullable Boolean bold) {
194
		return Objects.equals(this.bold, bold)
195
			? this
196
			: checkEmptyAfterChange(
197
				new Style(
198
					this.color,
199
					this.shadowColor,
200
					bold,
201
					this.italic,
202
					this.underlined,
203
					this.strikethrough,
204
					this.obfuscated,
205
					this.clickEvent,
206
					this.hoverEvent,
207
					this.insertion,
208
					this.font
209
				),
210
				this.bold,
211
				bold
212
			);
213
	}
214
215
	public Style withItalic(final @Nullable Boolean italic) {
216
		return Objects.equals(this.italic, italic)
217
			? this
218
			: checkEmptyAfterChange(
219
				new Style(
220
					this.color,
221
					this.shadowColor,
222
					this.bold,
223
					italic,
224
					this.underlined,
225
					this.strikethrough,
226
					this.obfuscated,
227
					this.clickEvent,
228
					this.hoverEvent,
229
					this.insertion,
230
					this.font
231
				),
232
				this.italic,
233
				italic
234
			);
235
	}
236
237
	public Style withUnderlined(final @Nullable Boolean underlined) {
238
		return Objects.equals(this.underlined, underlined)
239
			? this
240
			: checkEmptyAfterChange(
241
				new Style(
242
					this.color,
243
					this.shadowColor,
244
					this.bold,
245
					this.italic,
246
					underlined,
247
					this.strikethrough,
248
					this.obfuscated,
249
					this.clickEvent,
250
					this.hoverEvent,
251
					this.insertion,
252
					this.font
253
				),
254
				this.underlined,
255
				underlined
256
			);
257
	}
258
259
	public Style withStrikethrough(final @Nullable Boolean strikethrough) {
260
		return Objects.equals(this.strikethrough, strikethrough)
261
			? this
262
			: checkEmptyAfterChange(
263
				new Style(
264
					this.color,
265
					this.shadowColor,
266
					this.bold,
267
					this.italic,
268
					this.underlined,
269
					strikethrough,
270
					this.obfuscated,
271
					this.clickEvent,
272
					this.hoverEvent,
273
					this.insertion,
274
					this.font
275
				),
276
				this.strikethrough,
277
				strikethrough
278
			);
279
	}
280
281
	public Style withObfuscated(final @Nullable Boolean obfuscated) {
282
		return Objects.equals(this.obfuscated, obfuscated)
283
			? this
284
			: checkEmptyAfterChange(
285
				new Style(
286
					this.color,
287
					this.shadowColor,
288
					this.bold,
289
					this.italic,
290
					this.underlined,
291
					this.strikethrough,
292
					obfuscated,
293
					this.clickEvent,
294
					this.hoverEvent,
295
					this.insertion,
296
					this.font
297
				),
298
				this.obfuscated,
299
				obfuscated
300
			);
301
	}
302
303
	public Style withClickEvent(final @Nullable ClickEvent clickEvent) {
304
		return Objects.equals(this.clickEvent, clickEvent)
305
			? this
306
			: checkEmptyAfterChange(
307
				new Style(
308
					this.color,
309
					this.shadowColor,
310
					this.bold,
311
					this.italic,
312
					this.underlined,
313
					this.strikethrough,
314
					this.obfuscated,
315
					clickEvent,
316
					this.hoverEvent,
317
					this.insertion,
318
					this.font
319
				),
320
				this.clickEvent,
321
				clickEvent
322
			);
323
	}
324
325
	public Style withHoverEvent(final @Nullable HoverEvent hoverEvent) {
326
		return Objects.equals(this.hoverEvent, hoverEvent)
327
			? this
328
			: checkEmptyAfterChange(
329
				new Style(
330
					this.color,
331
					this.shadowColor,
332
					this.bold,
333
					this.italic,
334
					this.underlined,
335
					this.strikethrough,
336
					this.obfuscated,
337
					this.clickEvent,
338
					hoverEvent,
339
					this.insertion,
340
					this.font
341
				),
342
				this.hoverEvent,
343
				hoverEvent
344
			);
345
	}
346
347
	public Style withInsertion(final @Nullable String insertion) {
348
		return Objects.equals(this.insertion, insertion)
349
			? this
350
			: checkEmptyAfterChange(
351
				new Style(
352
					this.color,
353
					this.shadowColor,
354
					this.bold,
355
					this.italic,
356
					this.underlined,
357
					this.strikethrough,
358
					this.obfuscated,
359
					this.clickEvent,
360
					this.hoverEvent,
361
					insertion,
362
					this.font
363
				),
364
				this.insertion,
365
				insertion
366
			);
367
	}
368
369
	public Style withFont(final @Nullable FontDescription font) {
370
		return Objects.equals(this.font, font)
371
			? this
372
			: checkEmptyAfterChange(
373
				new Style(
374
					this.color,
375
					this.shadowColor,
376
					this.bold,
377
					this.italic,
378
					this.underlined,
379
					this.strikethrough,
380
					this.obfuscated,
381
					this.clickEvent,
382
					this.hoverEvent,
383
					this.insertion,
384
					font
385
				),
386
				this.font,
387
				font
388
			);
389
	}
390
391
	public Style applyFormat(final ChatFormatting format) {
392
		TextColor color = this.color;
393
		Boolean bold = this.bold;
394
		Boolean italic = this.italic;
395
		Boolean strikethrough = this.strikethrough;
396
		Boolean underlined = this.underlined;
397
		Boolean obfuscated = this.obfuscated;
398
		switch (format) {
399
			case OBFUSCATED:
400
				obfuscated = true;
401
				break;
402
			case BOLD:
403
				bold = true;
404
				break;
405
			case STRIKETHROUGH:
406
				strikethrough = true;
407
				break;
408
			case UNDERLINE:
409
				underlined = true;
410
				break;
411
			case ITALIC:
412
				italic = true;
413
				break;
414
			case RESET:
415
				return EMPTY;
416
			default:
417
				color = TextColor.fromLegacyFormat(format);
418
		}
419
420
		return new Style(color, this.shadowColor, bold, italic, underlined, strikethrough, obfuscated, this.clickEvent, this.hoverEvent, this.insertion, this.font);
421
	}
422
423
	public Style applyLegacyFormat(final ChatFormatting format) {
424
		TextColor color = this.color;
425
		Boolean bold = this.bold;
426
		Boolean italic = this.italic;
427
		Boolean strikethrough = this.strikethrough;
428
		Boolean underlined = this.underlined;
429
		Boolean obfuscated = this.obfuscated;
430
		switch (format) {
431
			case OBFUSCATED:
432
				obfuscated = true;
433
				break;
434
			case BOLD:
435
				bold = true;
436
				break;
437
			case STRIKETHROUGH:
438
				strikethrough = true;
439
				break;
440
			case UNDERLINE:
441
				underlined = true;
442
				break;
443
			case ITALIC:
444
				italic = true;
445
				break;
446
			case RESET:
447
				return EMPTY;
448
			default:
449
				obfuscated = false;
450
				bold = false;
451
				strikethrough = false;
452
				underlined = false;
453
				italic = false;
454
				color = TextColor.fromLegacyFormat(format);
455
		}
456
457
		return new Style(color, this.shadowColor, bold, italic, underlined, strikethrough, obfuscated, this.clickEvent, this.hoverEvent, this.insertion, this.font);
458
	}
459
460
	public Style applyFormats(final ChatFormatting... formats) {
461
		TextColor color = this.color;
462
		Boolean bold = this.bold;
463
		Boolean italic = this.italic;
464
		Boolean strikethrough = this.strikethrough;
465
		Boolean underlined = this.underlined;
466
		Boolean obfuscated = this.obfuscated;
467
468
		for (ChatFormatting format : formats) {
469
			switch (format) {
470
				case OBFUSCATED:
471
					obfuscated = true;
472
					break;
473
				case BOLD:
474
					bold = true;
475
					break;
476
				case STRIKETHROUGH:
477
					strikethrough = true;
478
					break;
479
				case UNDERLINE:
480
					underlined = true;
481
					break;
482
				case ITALIC:
483
					italic = true;
484
					break;
485
				case RESET:
486
					return EMPTY;
487
				default:
488
					color = TextColor.fromLegacyFormat(format);
489
			}
490
		}
491
492
		return new Style(color, this.shadowColor, bold, italic, underlined, strikethrough, obfuscated, this.clickEvent, this.hoverEvent, this.insertion, this.font);
493
	}
494
495
	public Style applyTo(final Style other) {
496
		if (this == EMPTY) {
497
			return other;
498
		} else {
499
			return other == EMPTY
500
				? this
501
				: new Style(
502
					this.color != null ? this.color : other.color,
503
					this.shadowColor != null ? this.shadowColor : other.shadowColor,
504
					this.bold != null ? this.bold : other.bold,
505
					this.italic != null ? this.italic : other.italic,
506
					this.underlined != null ? this.underlined : other.underlined,
507
					this.strikethrough != null ? this.strikethrough : other.strikethrough,
508
					this.obfuscated != null ? this.obfuscated : other.obfuscated,
509
					this.clickEvent != null ? this.clickEvent : other.clickEvent,
510
					this.hoverEvent != null ? this.hoverEvent : other.hoverEvent,
511
					this.insertion != null ? this.insertion : other.insertion,
512
					this.font != null ? this.font : other.font
513
				);
514
		}
515
	}
516
517
	@Override
518
	public String toString() {
519
		final StringBuilder result = new StringBuilder("{");
520
521
		class Collector {
522
			private boolean isNotFirst;
523
524
			private void prependSeparator() {
525
				if (this.isNotFirst) {
526
					result.append(',');
527
				}
528
529
				this.isNotFirst = true;
530
			}
531
532
			private void addFlagString(final String name, final @Nullable Boolean value) {
533
				if (value != null) {
534
					this.prependSeparator();
535
					if (!value) {
536
						result.append('!');
537
					}
538
539
					result.append(name);
540
				}
541
			}
542
543
			private void addValueString(final String name, final @Nullable Object value) {
544
				if (value != null) {
545
					this.prependSeparator();
546
					result.append(name);
547
					result.append('=');
548
					result.append(value);
549
				}
550
			}
551
		}
552
553
		Collector collector = new Collector();
554
		collector.addValueString("color", this.color);
555
		collector.addValueString("shadowColor", this.shadowColor);
556
		collector.addFlagString("bold", this.bold);
557
		collector.addFlagString("italic", this.italic);
558
		collector.addFlagString("underlined", this.underlined);
559
		collector.addFlagString("strikethrough", this.strikethrough);
560
		collector.addFlagString("obfuscated", this.obfuscated);
561
		collector.addValueString("clickEvent", this.clickEvent);
562
		collector.addValueString("hoverEvent", this.hoverEvent);
563
		collector.addValueString("insertion", this.insertion);
564
		collector.addValueString("font", this.font);
565
		result.append("}");
566
		return result.toString();
567
	}
568
569
	@Override
570
	public boolean equals(final Object o) {
571
		if (this == o) {
572
			return true;
573
		} else {
574
			return !(o instanceof Style style)
575
				? false
576
				: this.bold == style.bold
577
					&& Objects.equals(this.getColor(), style.getColor())
578
					&& Objects.equals(this.getShadowColor(), style.getShadowColor())
579
					&& this.italic == style.italic
580
					&& this.obfuscated == style.obfuscated
581
					&& this.strikethrough == style.strikethrough
582
					&& this.underlined == style.underlined
583
					&& Objects.equals(this.clickEvent, style.clickEvent)
584
					&& Objects.equals(this.hoverEvent, style.hoverEvent)
585
					&& Objects.equals(this.insertion, style.insertion)
586
					&& Objects.equals(this.font, style.font);
587
		}
588
	}
589
590
	@Override
591
	public int hashCode() {
592
		return Objects.hash(
593
			this.color, this.shadowColor, this.bold, this.italic, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, this.hoverEvent, this.insertion
594
		);
595
	}
596
597
	public static class Serializer {
598
		public static final MapCodec<Style> MAP_CODEC = RecordCodecBuilder.mapCodec(
599
			i -> i.group(
600
					TextColor.CODEC.optionalFieldOf("color").forGetter(o -> Optional.ofNullable(o.color)),
601
					ExtraCodecs.ARGB_COLOR_CODEC.optionalFieldOf("shadow_color").forGetter(o -> Optional.ofNullable(o.shadowColor)),
602
					Codec.BOOL.optionalFieldOf("bold").forGetter(o -> Optional.ofNullable(o.bold)),
603
					Codec.BOOL.optionalFieldOf("italic").forGetter(o -> Optional.ofNullable(o.italic)),
604
					Codec.BOOL.optionalFieldOf("underlined").forGetter(o -> Optional.ofNullable(o.underlined)),
605
					Codec.BOOL.optionalFieldOf("strikethrough").forGetter(o -> Optional.ofNullable(o.strikethrough)),
606
					Codec.BOOL.optionalFieldOf("obfuscated").forGetter(o -> Optional.ofNullable(o.obfuscated)),
607
					ClickEvent.CODEC.optionalFieldOf("click_event").forGetter(o -> Optional.ofNullable(o.clickEvent)),
608
					HoverEvent.CODEC.optionalFieldOf("hover_event").forGetter(o -> Optional.ofNullable(o.hoverEvent)),
609
					Codec.STRING.optionalFieldOf("insertion").forGetter(o -> Optional.ofNullable(o.insertion)),
610
					FontDescription.CODEC.optionalFieldOf("font").forGetter(o -> Optional.ofNullable(o.font))
611
				)
612
				.apply(i, Style::create)
613
		);
614
		public static final Codec<Style> CODEC = MAP_CODEC.codec();
615
		public static final StreamCodec<RegistryFriendlyByteBuf, Style> TRUSTED_STREAM_CODEC = ByteBufCodecs.fromCodecWithRegistriesTrusted(CODEC);
616
	}
617
}
618

Decompiled Minecraft 26.3 source of Style (net/minecraft/network/chat/Style.java) with Mojang official mappings, stored verbatim on the site.