1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

feat(color): 添加清理颜色代码的方法

This commit is contained in:
Carm Jos 2022-11-28 16:12:01 +08:00
parent 62e7370622
commit d9b0689e63
2 changed files with 37 additions and 6 deletions

View File

@ -25,15 +25,46 @@ public class ColorParser {
public static final Pattern COLOR_PATTERN = Pattern.compile("([&§][0-9a-fA-FrRxX])+"); // 会影响颜色的代码
public static final Pattern FORMAT_PATTERN = Pattern.compile("([&§][0-9a-fA-Fk-oK-OrRxX])+"); // MC可用的格式化代码
public static String parse(String text) {
/**
* 清除一条消息中的全部颜色代码 (包括RGB颜色代码与渐变颜色代码)
*
* @param text 源消息内容
* @return 清理颜色后的消息
*/
public static @NotNull String clear(@NotNull String text) {
text = HEX_PATTERN.matcher(text).replaceAll("");
text = GRADIENT_PATTERN.matcher(text).replaceAll("");
text = COLOR_PATTERN.matcher(text).replaceAll("");
return text;
}
/**
* 对一条消息进行颜色解析包括普通颜色代码RGB颜色代码与RBG渐变代码
*
* @param text 源消息内容
* @return 解析后的消息
*/
public static @NotNull String parse(@NotNull String text) {
return parseBaseColor(parseGradientColor(parseHexColor(text)));
}
public static String[] parse(String... texts) {
/**
* 对多条消息进行颜色解析包括普通颜色代码RGB颜色代码与RBG渐变代码
*
* @param texts 源消息内容
* @return 解析后的消息
*/
public static @NotNull String[] parse(@NotNull String... texts) {
return parse(Arrays.asList(texts)).toArray(new String[0]);
}
public static List<String> parse(List<String> texts) {
/**
* 对多条消息进行颜色解析包括普通颜色代码RGB颜色代码与RBG渐变代码
*
* @param texts 源消息内容
* @return 解析后的消息
*/
public static @NotNull List<String> parse(@NotNull Collection<String> texts) {
return texts.stream().map(ColorParser::parse).collect(Collectors.toList());
}

View File

@ -4,10 +4,9 @@ import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import static cc.carm.lib.easyplugin.utils.ColorParser.parse;
import static cc.carm.lib.easyplugin.utils.ColorParser.parseGradientColor;
import static cc.carm.lib.easyplugin.utils.ColorParser.*;
public class GradientTest {
public class ColorTest {
@Test
@ -20,6 +19,7 @@ public class GradientTest {
System.out.println(parse("&<#AAAAAA>&l我&r真&(#666666)的&<#BBBBBB>&o爱死&<#111111>你&<#FFFFFF>了&r"));
System.out.println(parse("&r正常的颜色理应&c&l不受影响&r。"));
System.out.println(clear("&f测试&<#AAAAAA>清理颜色代码&<#111111> &&这样应该&(#666666)不被影响 &f。"));
}
@Test