支持渐变语法
All checks were successful
Java CI-CD with Maven / build (push) Successful in 10m56s

This commit is contained in:
zhangyuheng 2024-08-12 16:42:54 +08:00
parent 466dd84fac
commit 78de7dca65
3 changed files with 9 additions and 64 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.lunadeer</groupId> <groupId>cn.lunadeer</groupId>
<artifactId>MiniPlayerTitle</artifactId> <artifactId>MiniPlayerTitle</artifactId>
<version>4.6.2</version> <version>4.7.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MiniPlayerTitle</name> <name>MiniPlayerTitle</name>
@ -82,7 +82,7 @@
<dependency> <dependency>
<groupId>cn.lunadeer</groupId> <groupId>cn.lunadeer</groupId>
<artifactId>MinecraftPluginUtils</artifactId> <artifactId>MinecraftPluginUtils</artifactId>
<version>1.3.4-SNAPSHOT</version> <version>1.3.7-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>me.clip</groupId> <groupId>me.clip</groupId>

View File

@ -34,7 +34,7 @@ public class Expansion extends PlaceholderExpansion {
if (t == null) { if (t == null) {
return ""; return "";
} }
return ChatColor.translateAlternateColorCodes('&', t.getTitleColoredBukkit()); return t.getTitleColoredBukkit();
} }
return null; // return null; //

View File

@ -1,21 +1,16 @@
package cn.lunadeer.miniplayertitle.dtos; package cn.lunadeer.miniplayertitle.dtos;
import cn.lunadeer.minecraftpluginutils.XLogger; import cn.lunadeer.minecraftpluginutils.ColorParser;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager; import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
import cn.lunadeer.minecraftpluginutils.databse.Field; import cn.lunadeer.minecraftpluginutils.databse.Field;
import cn.lunadeer.minecraftpluginutils.databse.FieldType; import cn.lunadeer.minecraftpluginutils.databse.FieldType;
import cn.lunadeer.miniplayertitle.Color;
import cn.lunadeer.miniplayertitle.MiniPlayerTitle; import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static cn.lunadeer.minecraftpluginutils.databse.DatabaseManager.handleDatabaseError; import static cn.lunadeer.minecraftpluginutils.databse.DatabaseManager.handleDatabaseError;
@ -80,32 +75,8 @@ public class TitleDTO {
} }
public TextComponent getTitleColored() { public TextComponent getTitleColored() {
TextComponent prefix = Component.text(MiniPlayerTitle.config.getPrefix(), new Color("#ffffff").getStyle()); String with_pre_suf = "&#ffffff" + MiniPlayerTitle.config.getPrefix() + getTitleRaw() + "&#ffffff" + MiniPlayerTitle.config.getSuffix();
TextComponent suffix = Component.text(MiniPlayerTitle.config.getSuffix(), new Color("#ffffff").getStyle()); return ColorParser.getComponentType(with_pre_suf);
String[] parts = getTitleRaw().split("&#");
List<TextComponent> components = new ArrayList<>();
components.add(prefix);
for (String part : parts) {
if (part.isEmpty()) {
continue;
}
Color color = new Color("#ffffff");
String content;
if (part.length() > 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
String color_str = part.substring(0, 6);
color = new Color("#" + color_str);
content = part.substring(6);
} else {
content = part;
}
components.add(Component.text(content, color.getStyle()));
}
components.add(suffix);
TextComponent.Builder title_component = Component.text();
for (TextComponent component : components) {
title_component.append(component);
}
return title_component.build().hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text(getDescription())));
} }
/** /**
@ -116,38 +87,12 @@ public class TitleDTO {
* @return String * @return String
*/ */
public String getTitleColoredBukkit() { public String getTitleColoredBukkit() {
String title = "&f" + MiniPlayerTitle.config.getPrefix() + getTitleRaw() + "&f" + MiniPlayerTitle.config.getSuffix(); String with_pre_suf = "&#ffffff" + MiniPlayerTitle.config.getPrefix() + getTitleRaw() + "&#ffffff" + MiniPlayerTitle.config.getSuffix();
title = title.replaceAll("&#", "#"); return ColorParser.getBukkitType(with_pre_suf);
Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}");
Matcher matcher = pattern.matcher(title);
while (matcher.find()) {
String hexCode = matcher.group();
StringBuilder builder = new StringBuilder("&x");
for (char c : hexCode.substring(1).toCharArray()) {
builder.append('&').append(c);
}
title = title.replace(hexCode, builder.toString());
}
XLogger.debug("TitleDTO.getTitleColoredBukkit: %s", title);
return title;
} }
public String getTitlePlainText() { public String getTitlePlainText() {
String[] parts = getTitleRaw().split("&#"); return ColorParser.getPlainText(getTitleRaw());
StringBuilder res = new StringBuilder();
for (String part : parts) {
if (part.isEmpty()) {
continue;
}
String content;
if (part.length() > 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
content = part.substring(6);
} else {
content = part;
}
res.append(content);
}
return res.toString();
} }
public Integer getId() { public Integer getId() {