尝试增加tab列表对齐功能(暂未成功)
This commit is contained in:
parent
6937a15225
commit
cd9f4549b2
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>cn.lunadeer</groupId>
|
||||
<artifactId>EssentialsD</artifactId>
|
||||
<version>2.3.0</version>
|
||||
<version>2.3.1</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EssentialsD</name>
|
||||
|
@ -6,6 +6,11 @@ import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TabListUpdater {
|
||||
|
||||
public static TabListUpdater instance;
|
||||
@ -16,9 +21,47 @@ public class TabListUpdater {
|
||||
}
|
||||
|
||||
private void update() {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
Collection<? extends Player> players = Bukkit.getOnlinePlayers();
|
||||
Map<UUID, String> formatedNames = new HashMap<>();
|
||||
|
||||
int longestFormatedStringLength = 0;
|
||||
|
||||
for (Player player : players) {
|
||||
String formated = PlaceholderAPI.setPlaceholders(player, EssentialsD.config.getPrefixTabFormat());
|
||||
player.setPlayerListName(formated);
|
||||
if (length(formated) > longestFormatedStringLength) {
|
||||
longestFormatedStringLength = length(formated);
|
||||
}
|
||||
formatedNames.put(player.getUniqueId(), formated);
|
||||
}
|
||||
// <span> 将会被替换为填充空格 " " 用于保证长度一致
|
||||
for (Player player : players) {
|
||||
String formated = formatedNames.get(player.getUniqueId());
|
||||
int formatedLength = length(formated);
|
||||
int spaceLength = longestFormatedStringLength - formatedLength;
|
||||
StringBuilder space = new StringBuilder();
|
||||
for (int i = 0; i < spaceLength; i++) {
|
||||
space.append(" ");
|
||||
}
|
||||
player.setPlayerListName(formated.replace("<span>", space.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
private static int length(String value) {
|
||||
int valueLength = 0;
|
||||
String chinese = "[\u0391-\uFFE5]";
|
||||
/* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
/* 获取一个字符 */
|
||||
String temp = value.substring(i, i + 1);
|
||||
/* 判断是否为中文字符 */
|
||||
if (temp.matches(chinese)) {
|
||||
/* 中文字符长度为2 */
|
||||
valueLength += 2;
|
||||
} else {
|
||||
/* 其他字符长度为1 */
|
||||
valueLength += 1;
|
||||
}
|
||||
}
|
||||
return valueLength;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user