File size: 5,937 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.mojang.realmsclient.util;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class TextRenderingUtils {
    private TextRenderingUtils() {
    }

    @VisibleForTesting
    protected static List<String> lineBreak(String p_90249_) {
        return Arrays.asList(p_90249_.split("\\n"));
    }

    public static List<TextRenderingUtils.Line> decompose(String p_90257_, TextRenderingUtils.LineSegment... p_90258_) {
        return decompose(p_90257_, Arrays.asList(p_90258_));
    }

    private static List<TextRenderingUtils.Line> decompose(String p_90254_, List<TextRenderingUtils.LineSegment> p_90255_) {
        List<String> list = lineBreak(p_90254_);
        return insertLinks(list, p_90255_);
    }

    private static List<TextRenderingUtils.Line> insertLinks(List<String> p_90260_, List<TextRenderingUtils.LineSegment> p_90261_) {
        int i = 0;
        List<TextRenderingUtils.Line> list = Lists.newArrayList();

        for (String s : p_90260_) {
            List<TextRenderingUtils.LineSegment> list1 = Lists.newArrayList();

            for (String s1 : split(s, "%link")) {
                if ("%link".equals(s1)) {
                    list1.add(p_90261_.get(i++));
                } else {
                    list1.add(TextRenderingUtils.LineSegment.text(s1));
                }
            }

            list.add(new TextRenderingUtils.Line(list1));
        }

        return list;
    }

    public static List<String> split(String p_90251_, String p_90252_) {
        if (p_90252_.isEmpty()) {
            throw new IllegalArgumentException("Delimiter cannot be the empty string");
        } else {
            List<String> list = Lists.newArrayList();
            int i = 0;

            int j;
            while ((j = p_90251_.indexOf(p_90252_, i)) != -1) {
                if (j > i) {
                    list.add(p_90251_.substring(i, j));
                }

                list.add(p_90252_);
                i = j + p_90252_.length();
            }

            if (i < p_90251_.length()) {
                list.add(p_90251_.substring(i));
            }

            return list;
        }
    }

    @OnlyIn(Dist.CLIENT)
    public static class Line {
        public final List<TextRenderingUtils.LineSegment> segments;

        Line(TextRenderingUtils.LineSegment... p_167625_) {
            this(Arrays.asList(p_167625_));
        }

        Line(List<TextRenderingUtils.LineSegment> p_90264_) {
            this.segments = p_90264_;
        }

        @Override
        public String toString() {
            return "Line{segments=" + this.segments + "}";
        }

        @Override
        public boolean equals(Object p_90266_) {
            if (this == p_90266_) {
                return true;
            } else if (p_90266_ != null && this.getClass() == p_90266_.getClass()) {
                TextRenderingUtils.Line textrenderingutils$line = (TextRenderingUtils.Line)p_90266_;
                return Objects.equals(this.segments, textrenderingutils$line.segments);
            } else {
                return false;
            }
        }

        @Override
        public int hashCode() {
            return Objects.hash(this.segments);
        }
    }

    @OnlyIn(Dist.CLIENT)
    public static class LineSegment {
        private final String fullText;
        @Nullable
        private final String linkTitle;
        @Nullable
        private final String linkUrl;

        private LineSegment(String p_90273_) {
            this.fullText = p_90273_;
            this.linkTitle = null;
            this.linkUrl = null;
        }

        private LineSegment(String p_90275_, @Nullable String p_90276_, @Nullable String p_90277_) {
            this.fullText = p_90275_;
            this.linkTitle = p_90276_;
            this.linkUrl = p_90277_;
        }

        @Override
        public boolean equals(Object p_90287_) {
            if (this == p_90287_) {
                return true;
            } else if (p_90287_ != null && this.getClass() == p_90287_.getClass()) {
                TextRenderingUtils.LineSegment textrenderingutils$linesegment = (TextRenderingUtils.LineSegment)p_90287_;
                return Objects.equals(this.fullText, textrenderingutils$linesegment.fullText)
                    && Objects.equals(this.linkTitle, textrenderingutils$linesegment.linkTitle)
                    && Objects.equals(this.linkUrl, textrenderingutils$linesegment.linkUrl);
            } else {
                return false;
            }
        }

        @Override
        public int hashCode() {
            return Objects.hash(this.fullText, this.linkTitle, this.linkUrl);
        }

        @Override
        public String toString() {
            return "Segment{fullText='" + this.fullText + "', linkTitle='" + this.linkTitle + "', linkUrl='" + this.linkUrl + "'}";
        }

        public String renderedText() {
            return this.isLink() ? this.linkTitle : this.fullText;
        }

        public boolean isLink() {
            return this.linkTitle != null;
        }

        public String getLinkUrl() {
            if (!this.isLink()) {
                throw new IllegalStateException("Not a link: " + this);
            } else {
                return this.linkUrl;
            }
        }

        public static TextRenderingUtils.LineSegment link(String p_90282_, String p_90283_) {
            return new TextRenderingUtils.LineSegment(null, p_90282_, p_90283_);
        }

        @VisibleForTesting
        protected static TextRenderingUtils.LineSegment text(String p_90280_) {
            return new TextRenderingUtils.LineSegment(p_90280_);
        }
    }
}