Skip to content

Commit 2bae9dc

Browse files
authored
Merge pull request #529 from AsOne2020/v2
修复(rainbow/gradient): 渐变时 emoji 字符被拆分问题
2 parents 3301c71 + 1602f20 commit 2bae9dc

1 file changed

Lines changed: 33 additions & 37 deletions

File tree

  • project/runtime-bukkit/src/main/java/me/arasple/mc/trchat/util/color

project/runtime-bukkit/src/main/java/me/arasple/mc/trchat/util/color/HexUtils.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,31 @@ public static String parseRainbow(String message) {
106106

107107
int stop = findStop(parsed, matcher.end());
108108
String content = parsed.substring(matcher.end(), stop);
109-
int contentLength = content.length();
110-
char[] chars = content.toCharArray();
111-
for (int i = 0; i < chars.length - 1; i++)
112-
if (chars[i] == '&' && "KkLlMmNnOoRr".indexOf(chars[i + 1]) > -1)
113-
contentLength -= 2;
114109

115-
int length = looping ? Math.min(contentLength, CHARS_UNTIL_LOOP) : contentLength;
110+
// 使用 codePoint 遍历,避免 emoji 被拆开
111+
int length = content.codePointCount(0, content.length());
112+
if (looping) length = Math.min(length, CHARS_UNTIL_LOOP);
116113

117-
ColorGenerator rainbow;
118-
if (speed == -1) {
119-
rainbow = new Rainbow(length, saturation, brightness);
120-
} else {
121-
rainbow = new AnimatedRainbow(length, saturation, brightness, speed);
122-
}
114+
ColorGenerator rainbow = (speed == -1)
115+
? new Rainbow(length, saturation, brightness)
116+
: new AnimatedRainbow(length, saturation, brightness, speed);
123117

124118
String compoundedFormat = ""; // Carry the format codes through the rainbow gradient
125-
for (int i = 0; i < chars.length; i++) {
126-
char c = chars[i];
127-
if (c == '&' && i + 1 < chars.length) {
128-
char next = chars[i + 1];
119+
for (int i = 0; i < content.length(); ) {
120+
int cp = content.codePointAt(i);
121+
int charCount = Character.charCount(cp);
122+
123+
if (cp == '&' && i + 1 < content.length()) {
124+
char next = content.charAt(i + 1);
129125
if (HexKt.isFormat(next)) {
130126
compoundedFormat += String.valueOf(ChatColor.COLOR_CHAR) + next;
131-
i++; // Skip next character
127+
i += 2; // Skip next character
132128
continue;
133129
}
134130
}
135-
parsedRainbow.append(rainbow.nextChatColor()).append(compoundedFormat).append(c);
131+
132+
parsedRainbow.append(rainbow.nextChatColor()).append(compoundedFormat).appendCodePoint(cp);
133+
i += charCount;
136134
}
137135

138136
String before = parsed.substring(0, matcher.start());
@@ -168,32 +166,30 @@ public static String parseGradients(String message) {
168166

169167
int stop = findStop(parsed, matcher.end());
170168
String content = parsed.substring(matcher.end(), stop);
171-
int contentLength = content.length();
172-
char[] chars = content.toCharArray();
173-
for (int i = 0; i < chars.length - 1; i++)
174-
if (chars[i] == '&' && "KkLlMmNnOoRr".indexOf(chars[i + 1]) > -1)
175-
contentLength -= 2;
176-
177-
int length = looping ? Math.min(contentLength, CHARS_UNTIL_LOOP) : contentLength;
178-
ColorGenerator gradient;
179-
if (speed == -1) {
180-
gradient = new Gradient(hexSteps, length);
181-
} else {
182-
gradient = new AnimatedGradient(hexSteps, length, speed);
183-
}
169+
170+
int length = content.codePointCount(0, content.length());
171+
if (looping) length = Math.min(length, CHARS_UNTIL_LOOP);
172+
173+
ColorGenerator gradient = (speed == -1)
174+
? new Gradient(hexSteps, length)
175+
: new AnimatedGradient(hexSteps, length, speed);
184176

185177
String compoundedFormat = ""; // Carry the format codes through the gradient
186-
for (int i = 0; i < chars.length; i++) {
187-
char c = chars[i];
188-
if (c == '&' && i + 1 < chars.length) {
189-
char next = chars[i + 1];
178+
for (int i = 0; i < content.length(); ) {
179+
int cp = content.codePointAt(i);
180+
int charCount = Character.charCount(cp);
181+
182+
if (cp == '&' && i + 1 < content.length()) {
183+
char next = content.charAt(i + 1);
190184
if (HexKt.isFormat(next)) {
191185
compoundedFormat += String.valueOf(ChatColor.COLOR_CHAR) + next;
192-
i++; // Skip next character
186+
i += 2; // Skip next character
193187
continue;
194188
}
195189
}
196-
parsedGradient.append(gradient.nextChatColor()).append(compoundedFormat).append(c);
190+
191+
parsedGradient.append(gradient.nextChatColor()).append(compoundedFormat).appendCodePoint(cp);
192+
i += charCount;
197193
}
198194

199195
String before = parsed.substring(0, matcher.start());

0 commit comments

Comments
 (0)