| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.UnaryOperator;
|
| 6 | import net.minecraft.ChatFormatting;
|
| 7 | import net.minecraft.locale.Language;
|
| 8 | import net.minecraft.util.FormattedCharSequence;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 |
|
| 11 | public final class MutableComponent implements Component {
|
| 12 | private final ComponentContents contents;
|
| 13 | private final List<Component> siblings;
|
| 14 | private Style style;
|
| 15 | private FormattedCharSequence visualOrderText = FormattedCharSequence.EMPTY;
|
| 16 | private @Nullable Language decomposedWith;
|
| 17 |
|
| 18 | MutableComponent(final ComponentContents contents, final List<Component> siblings, final Style style) {
|
| 19 | this.contents = contents;
|
| 20 | this.siblings = siblings;
|
| 21 | this.style = style;
|
| 22 | }
|
| 23 |
|
| 24 | public static MutableComponent create(final ComponentContents contents) {
|
| 25 | return new MutableComponent(contents, Lists.newArrayList(), Style.EMPTY);
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public ComponentContents getContents() {
|
| 30 | return this.contents;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public List<Component> getSiblings() {
|
| 35 | return this.siblings;
|
| 36 | }
|
| 37 |
|
| 38 | public MutableComponent setStyle(final Style style) {
|
| 39 | this.style = style;
|
| 40 | return this;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public Style getStyle() {
|
| 45 | return this.style;
|
| 46 | }
|
| 47 |
|
| 48 | public MutableComponent append(final String text) {
|
| 49 | return text.isEmpty() ? this : this.append(Component.literal(text));
|
| 50 | }
|
| 51 |
|
| 52 | public MutableComponent append(final Component component) {
|
| 53 | this.siblings.add(component);
|
| 54 | return this;
|
| 55 | }
|
| 56 |
|
| 57 | public MutableComponent withStyle(final UnaryOperator<Style> updater) {
|
| 58 | this.setStyle(updater.apply(this.getStyle()));
|
| 59 | return this;
|
| 60 | }
|
| 61 |
|
| 62 | public MutableComponent withStyle(final Style patch) {
|
| 63 | this.setStyle(patch.applyTo(this.getStyle()));
|
| 64 | return this;
|
| 65 | }
|
| 66 |
|
| 67 | public MutableComponent withStyle(final ChatFormatting... formats) {
|
| 68 | this.setStyle(this.getStyle().applyFormats(formats));
|
| 69 | return this;
|
| 70 | }
|
| 71 |
|
| 72 | public MutableComponent withStyle(final ChatFormatting format) {
|
| 73 | this.setStyle(this.getStyle().applyFormat(format));
|
| 74 | return this;
|
| 75 | }
|
| 76 |
|
| 77 | public MutableComponent withColor(final int color) {
|
| 78 | this.setStyle(this.getStyle().withColor(color));
|
| 79 | return this;
|
| 80 | }
|
| 81 |
|
| 82 | public MutableComponent withColor(final TextColor color) {
|
| 83 | this.setStyle(this.getStyle().withColor(color));
|
| 84 | return this;
|
| 85 | }
|
| 86 |
|
| 87 | public MutableComponent withoutShadow() {
|
| 88 | this.setStyle(this.getStyle().withoutShadow());
|
| 89 | return this;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public FormattedCharSequence getVisualOrderText() {
|
| 94 | Language currentLanguage = Language.getInstance();
|
| 95 | if (this.decomposedWith != currentLanguage) {
|
| 96 | this.visualOrderText = currentLanguage.getVisualOrder(this);
|
| 97 | this.decomposedWith = currentLanguage;
|
| 98 | }
|
| 99 |
|
| 100 | return this.visualOrderText;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public boolean equals(final Object o) {
|
| 105 | return this == o
|
| 106 | ? true
|
| 107 | : o instanceof MutableComponent that && this.contents.equals(that.contents) && this.style.equals(that.style) && this.siblings.equals(that.siblings);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public int hashCode() {
|
| 112 | int result = 1;
|
| 113 | result = 31 * result + this.contents.hashCode();
|
| 114 | result = 31 * result + this.style.hashCode();
|
| 115 | return 31 * result + this.siblings.hashCode();
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public String toString() {
|
| 120 | StringBuilder result = new StringBuilder(this.contents.toString());
|
| 121 | boolean hasStyle = !this.style.isEmpty();
|
| 122 | boolean hasSiblings = !this.siblings.isEmpty();
|
| 123 | if (hasStyle || hasSiblings) {
|
| 124 | result.append('[');
|
| 125 | if (hasStyle) {
|
| 126 | result.append("style=");
|
| 127 | result.append(this.style);
|
| 128 | }
|
| 129 |
|
| 130 | if (hasStyle && hasSiblings) {
|
| 131 | result.append(", ");
|
| 132 | }
|
| 133 |
|
| 134 | if (hasSiblings) {
|
| 135 | result.append("siblings=");
|
| 136 | result.append(this.siblings);
|
| 137 | }
|
| 138 |
|
| 139 | result.append(']');
|
| 140 | }
|
| 141 |
|
| 142 | return result.toString();
|
| 143 | }
|
| 144 | }
|
| 145 |
|