2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.util;
|
2016-08-04 17:37:11 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Provides utility methods around Minecraft and Web stuff.
|
|
|
|
*/
|
2016-08-04 17:37:11 +02:00
|
|
|
public class MinecraftWebUtil {
|
|
|
|
|
|
|
|
/**
|
2022-07-28 01:13:35 +02:00
|
|
|
* Convert a legacy Minecraft color coded String into HTML Format.
|
|
|
|
* <p>
|
|
|
|
* Each colored part of the text will be contained in a {@code <span>} tag with {@code class="cX"} where {@code X}
|
|
|
|
* is the color code from 0 to F in uppercase.
|
|
|
|
* The bold, striked, underlined and italic parts will be contained in a {@code <span>} tag with respectively the
|
|
|
|
* classes {@code cL}, {@code cM}, {@code cN} and {@code cO}.
|
|
|
|
* Some CSS properties are needed to apply the colors to the CSS classes.
|
|
|
|
* @param chatcolorPrefix the prefix used for the color codes
|
|
|
|
* @param legacyText the legacy text to convert to HTML.
|
|
|
|
* @return The text formated in HTML.
|
|
|
|
* @implNote the current implementation does not yet supports the RGB colors.
|
2016-08-04 17:37:11 +02:00
|
|
|
*/
|
2022-07-28 01:13:35 +02:00
|
|
|
// TODO update to support RGB colors (Bungee and Essentials notation). (see JS implementation at https://www.pandacube.fr/assets/js/global.js )
|
|
|
|
// TODO moves this to pandalib-chat and use Adventure API to help serializing to HTML
|
|
|
|
public static String fromMinecraftColorCodeToHTML(char chatcolorPrefix, String legacyText)
|
2016-08-04 17:37:11 +02:00
|
|
|
{
|
|
|
|
String color_char = "0123456789abcdefr";
|
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
StringBuilder builder = new StringBuilder();
|
2016-08-04 17:37:11 +02:00
|
|
|
char currentColor = 'r';
|
|
|
|
boolean bold = false, italic = false, underlined = false, strikethrough = false;
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
for (int i=0; i<legacyText.length(); i++) {
|
|
|
|
char c = legacyText.charAt(i);
|
2016-08-04 17:37:11 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
if (c == chatcolorPrefix && (i<legacyText.length()-1)) {
|
2016-08-04 17:37:11 +02:00
|
|
|
i++;
|
2022-07-28 01:13:35 +02:00
|
|
|
c = legacyText.charAt(i);
|
2022-07-10 00:55:56 +02:00
|
|
|
if (color_char.contains(String.valueOf(Character.toLowerCase(c)))) {
|
2016-08-04 17:37:11 +02:00
|
|
|
if (bold) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("</span>");
|
2016-08-04 17:37:11 +02:00
|
|
|
bold = false;
|
|
|
|
}
|
|
|
|
if (italic) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("</span>");
|
2016-08-04 17:37:11 +02:00
|
|
|
italic = false;
|
|
|
|
}
|
|
|
|
if (underlined) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("</span>");
|
2016-08-04 17:37:11 +02:00
|
|
|
underlined = false;
|
|
|
|
}
|
|
|
|
if (strikethrough) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("</span>");
|
2016-08-04 17:37:11 +02:00
|
|
|
strikethrough = false;
|
|
|
|
}
|
|
|
|
if (Character.toLowerCase(c) != currentColor) {
|
|
|
|
if (currentColor != 'r')
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("</span>");
|
2016-08-04 17:37:11 +02:00
|
|
|
if (Character.toLowerCase(c) != 'r')
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("<span class=\"c").append(Character.toUpperCase(c)).append("\">");
|
2016-08-04 17:37:11 +02:00
|
|
|
currentColor = Character.toLowerCase(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (Character.toLowerCase(c) == 'l') {
|
|
|
|
if (!bold) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("<span class=\"cL\">");
|
2016-08-04 17:37:11 +02:00
|
|
|
bold = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Character.toLowerCase(c) == 'm') {
|
|
|
|
if (!strikethrough) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("<span class=\"cM\">");
|
2016-08-04 17:37:11 +02:00
|
|
|
strikethrough = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Character.toLowerCase(c) == 'n') {
|
|
|
|
if (!underlined) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("<span class=\"cN\">");
|
2016-08-04 17:37:11 +02:00
|
|
|
underlined = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Character.toLowerCase(c) == 'o') {
|
|
|
|
if (!italic) {
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append("<span class=\"cO\">");
|
2016-08-04 17:37:11 +02:00
|
|
|
italic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2022-07-28 01:13:35 +02:00
|
|
|
builder.append(chatcolorPrefix + c);
|
2016-08-04 17:37:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2022-07-10 00:55:56 +02:00
|
|
|
builder.append(c);
|
2016-08-04 17:37:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
return builder.toString();
|
2016-08-04 17:37:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|