fix Chat
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-07-14
|
||||
*/
|
||||
package mc.core.network.proto_125.serializers;
|
||||
|
||||
import mc.core.text.Text;
|
||||
import mc.core.text.TextStyle;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class TextSerializer {
|
||||
private static final char SPECIAL_CHAR = '\u00a7'; // §;
|
||||
private static final String BOLD = SPECIAL_CHAR + "l";
|
||||
private static final String ITALIC = SPECIAL_CHAR + "o";
|
||||
private static final String UNDERLINE = SPECIAL_CHAR + "n";
|
||||
private static final String STRIKETHOUGH = SPECIAL_CHAR + "m";
|
||||
private static final String OBFUSCATED = SPECIAL_CHAR + "k";
|
||||
private static final String RESET = SPECIAL_CHAR + "r";
|
||||
|
||||
private static void processText(StringJoiner sj, Text text) {
|
||||
if (text.getStyle() != null) {
|
||||
if (text.getStyle().equals(TextStyle.RESET)) {
|
||||
sj.add(RESET);
|
||||
} else {
|
||||
sj.add(text.getStyle().getBold().orElse(false) ? BOLD : "");
|
||||
sj.add(text.getStyle().getItalic().orElse(false) ? ITALIC : "");
|
||||
sj.add(text.getStyle().getUnderline().orElse(false) ? UNDERLINE : "");
|
||||
sj.add(text.getStyle().getStrikethrough().orElse(false) ? STRIKETHOUGH : "");
|
||||
sj.add(text.getStyle().getObfuscated().orElse(false) ? OBFUSCATED : "");
|
||||
}
|
||||
}
|
||||
|
||||
if (text.getColor() != null) {
|
||||
sj.add(SPECIAL_CHAR + text.getColor().getCode() + "");
|
||||
}
|
||||
|
||||
if (text.getContent() != null) {
|
||||
sj.add(text.getContent());
|
||||
}
|
||||
}
|
||||
|
||||
public static String serialize(Text text) {
|
||||
StringJoiner sj = new StringJoiner("");
|
||||
|
||||
processText(sj, text);
|
||||
|
||||
for (Text childText : text.getChildren()) {
|
||||
processText(sj, childText);
|
||||
}
|
||||
|
||||
return sj.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user