2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.util;
|
2016-08-04 17:37:11 +02:00
|
|
|
|
|
|
|
public class MinecraftWebUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Convert a legacy Minecraft color coded String into HTML Format.
|
|
|
|
*/
|
2021-03-21 20:17:31 +01:00
|
|
|
// TODO update to support RGB colors (Bungee and Essentials notation).
|
|
|
|
// See JavaScript implementation at https://www.pandacube.fr/assets/js/global.js
|
2016-08-04 17:37:11 +02:00
|
|
|
public static String fromMinecraftColorCodeToHTML(char code_prefix, String ligne)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (int i=0; i<ligne.length(); i++) {
|
|
|
|
char c = ligne.charAt(i);
|
|
|
|
|
|
|
|
if (c == code_prefix && (i<ligne.length()-1)) {
|
|
|
|
i++;
|
|
|
|
c = ligne.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-10 00:55:56 +02:00
|
|
|
builder.append(code_prefix + 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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|