mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-03-09 13:19:07 +01:00
Color replacement to StringProvider values
This commit is contained in:
parent
b288182e42
commit
ed4dff350a
@ -41,6 +41,7 @@ public class ExtensionStringData implements ExtensionData {
|
||||
}
|
||||
|
||||
public String getFormattedValue() {
|
||||
return !playerName ? value : Html.LINK.parse(PlanAPI.getInstance().getPlayerInspectPageLink(value), value);
|
||||
String withColors = Html.swapColorCodesToSpan(value);
|
||||
return !playerName ? withColors : Html.LINK.parse(PlanAPI.getInstance().getPlayerInspectPageLink(value), withColors);
|
||||
}
|
||||
}
|
@ -110,14 +110,78 @@ public enum Html {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param p The replacement Strings
|
||||
* Changes Minecraft color codes to HTML span elements with correct color class assignments.
|
||||
*
|
||||
* @param string String to replace Minecraft color codes from
|
||||
* @return String with span elements.
|
||||
*/
|
||||
public static String swapColorCodesToSpan(String string) {
|
||||
Html[] replacer = new Html[]{
|
||||
Html.COLOR_0, Html.COLOR_1, Html.COLOR_2, Html.COLOR_3,
|
||||
Html.COLOR_4, Html.COLOR_5, Html.COLOR_6, Html.COLOR_7,
|
||||
Html.COLOR_8, Html.COLOR_9, Html.COLOR_A, Html.COLOR_B,
|
||||
Html.COLOR_C, Html.COLOR_D, Html.COLOR_E, Html.COLOR_F
|
||||
};
|
||||
Map<Character, String> colorMap = new HashMap<>();
|
||||
|
||||
for (Html html : replacer) {
|
||||
colorMap.put(Character.toLowerCase(html.name().charAt(6)), html.parse());
|
||||
colorMap.put('k', "");
|
||||
colorMap.put('l', "");
|
||||
colorMap.put('m', "");
|
||||
colorMap.put('n', "");
|
||||
colorMap.put('o', "");
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder(string.length());
|
||||
String[] split = string.split("§");
|
||||
|
||||
int placedSpans = 0;
|
||||
for (String part : split) {
|
||||
if (part.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
char colorChar = part.charAt(0);
|
||||
if (colorChar == 'r') {
|
||||
appendEndTags(result, placedSpans);
|
||||
placedSpans = 0; // Colors were reset
|
||||
result.append(part.substring(1));
|
||||
continue;
|
||||
}
|
||||
|
||||
String replacement = colorMap.get(colorChar);
|
||||
if (replacement != null) {
|
||||
result.append(replacement).append(part.substring(1));
|
||||
|
||||
if (!replacement.isEmpty()) {
|
||||
placedSpans++;
|
||||
}
|
||||
} else {
|
||||
result.append(part);
|
||||
}
|
||||
}
|
||||
|
||||
appendEndTags(result, placedSpans);
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static void appendEndTags(StringBuilder result, int placedSpans) {
|
||||
for (int i = 0; i < placedSpans; i++) {
|
||||
result.append("</span>");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param replacements The replacement Strings
|
||||
* @return The parsed HTML String
|
||||
*/
|
||||
public String parse(Serializable... p) {
|
||||
public String parse(Serializable... replacements) {
|
||||
Map<String, Serializable> replaceMap = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < p.length; i++) {
|
||||
replaceMap.put(String.valueOf(i), p[i]);
|
||||
for (int i = 0; i < replacements.length; i++) {
|
||||
replaceMap.put(String.valueOf(i), replacements[i]);
|
||||
}
|
||||
|
||||
StringSubstitutor sub = new StringSubstitutor(replaceMap);
|
||||
|
@ -42,22 +42,11 @@ public class HtmlUtils {
|
||||
* Changes Minecraft color codes to HTML span elements with correct color class assignments.
|
||||
*
|
||||
* @param string String to replace Minecraft color codes from
|
||||
* @deprecated Use {@link Html#swapColorCodesToSpan(String)} instead.
|
||||
* @return String with span elements.
|
||||
*/
|
||||
@Deprecated
|
||||
public static String swapColorsToSpan(String string) {
|
||||
Html[] replacer = new Html[]{Html.COLOR_0, Html.COLOR_1, Html.COLOR_2, Html.COLOR_3,
|
||||
Html.COLOR_4, Html.COLOR_5, Html.COLOR_6, Html.COLOR_7, Html.COLOR_8, Html.COLOR_9,
|
||||
Html.COLOR_A, Html.COLOR_B, Html.COLOR_C, Html.COLOR_D, Html.COLOR_E, Html.COLOR_F};
|
||||
|
||||
for (Html html : replacer) {
|
||||
string = string.replace("§" + Character.toLowerCase(html.name().charAt(6)), html.parse());
|
||||
}
|
||||
|
||||
int spans = string.split("<span").length - 1;
|
||||
for (int i = 0; i < spans; i++) {
|
||||
string = Html.SPAN.parse(string);
|
||||
}
|
||||
|
||||
return string.replace("§r", "").replace("§l", "").replace("§m", "").replace("§n", "").replace("§o", "").replace("§k", "");
|
||||
return Html.swapColorCodesToSpan(string);
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import com.djrapitops.plan.data.store.objects.DateHolder;
|
||||
import com.djrapitops.plan.data.store.objects.Nickname;
|
||||
import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator;
|
||||
import com.djrapitops.plan.utilities.formatting.Formatter;
|
||||
import com.djrapitops.plan.utilities.html.Html;
|
||||
import com.djrapitops.plan.utilities.html.HtmlUtils;
|
||||
|
||||
import java.util.List;
|
||||
@ -54,7 +55,7 @@ class NicknameTable extends TableContainer {
|
||||
UUID serverUUID = nickname.getServerUUID();
|
||||
String serverName = serverNames.getOrDefault(serverUUID, "Unknown");
|
||||
addRow(
|
||||
HtmlUtils.swapColorsToSpan(HtmlUtils.removeXSS(nickname.getName())),
|
||||
Html.swapColorCodesToSpan(HtmlUtils.removeXSS(nickname.getName())),
|
||||
serverName,
|
||||
yearFormatter.apply(nickname)
|
||||
);
|
||||
|
@ -16,18 +16,22 @@
|
||||
*/
|
||||
package com.djrapitops.plan.utilities.html;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for different functions of the {@link Html} class.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class HtmlTest {
|
||||
|
||||
@Test
|
||||
public void testParseWithZeroArgs() {
|
||||
void parsingWithNoArgsDoesNotReplacePlaceholder() {
|
||||
String expResult = "${0}</span>";
|
||||
String result = Html.SPAN.parse();
|
||||
|
||||
@ -35,7 +39,7 @@ public class HtmlTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseStringArr() {
|
||||
void parsingWithArgsReplacesPlaceholder() {
|
||||
String expResult = "Test</span>";
|
||||
String result = Html.SPAN.parse("Test");
|
||||
|
||||
@ -43,7 +47,10 @@ public class HtmlTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoBackSlash() {
|
||||
assertNotNull(Html.TABLELINE_2.parse("/\\", "0"));
|
||||
void colorsToSpanResetsColors() {
|
||||
String testString = "§fHello, §aPerson§r - How Are you?";
|
||||
String expected = Html.COLOR_F.parse() + "Hello, " + Html.COLOR_A.parse() + "Person</span></span> - How Are you?";
|
||||
String result = Html.swapColorCodesToSpan(testString);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user