Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
cd9f4549b2 | |||
6937a15225 | |||
937f9fcd8f | |||
06aeb33f50 |
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>cn.lunadeer</groupId>
|
||||
<artifactId>EssentialsD</artifactId>
|
||||
<version>2.2.3</version>
|
||||
<version>2.3.1</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>EssentialsD</name>
|
||||
|
@ -22,6 +22,7 @@ import cn.lunadeer.essentialsd.commands.weather.Sun;
|
||||
import cn.lunadeer.essentialsd.events.*;
|
||||
import cn.lunadeer.essentialsd.managers.ConfigManager;
|
||||
import cn.lunadeer.essentialsd.managers.DatabaseTables;
|
||||
import cn.lunadeer.essentialsd.managers.TabListUpdater;
|
||||
import cn.lunadeer.essentialsd.managers.TeleportManager;
|
||||
import cn.lunadeer.essentialsd.recipes.*;
|
||||
import cn.lunadeer.minecraftpluginutils.*;
|
||||
@ -53,6 +54,7 @@ public final class EssentialsD extends JavaPlugin {
|
||||
if (config.getPrefixEnable()) {
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
Bukkit.getPluginManager().registerEvents(new ChatPrefixEvent(), this);
|
||||
new TabListUpdater(); // 更新 TabList
|
||||
} else {
|
||||
XLogger.warn("未找到 PlaceholderAPI 插件, 无法使用聊天前缀功能, 已自动关闭前缀功能");
|
||||
config.setPrefixEnable(false);
|
||||
|
@ -3,7 +3,6 @@ package cn.lunadeer.essentialsd.commands.warp;
|
||||
import cn.lunadeer.essentialsd.dtos.WarpPoint;
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -18,6 +17,10 @@ public class DelWarp implements TabExecutor {
|
||||
*/
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
|
||||
if (strings.length != 1) {
|
||||
Notification.error(commandSender, "用法: /delwarp <name>");
|
||||
return true;
|
||||
}
|
||||
WarpPoint point = WarpPoint.selectByName(strings[0]);
|
||||
if (point == null) {
|
||||
Notification.error(commandSender, "传送点 %s 不存在", strings[0]);
|
||||
|
@ -2,10 +2,8 @@ package cn.lunadeer.essentialsd.commands.warp;
|
||||
|
||||
import cn.lunadeer.essentialsd.EssentialsD;
|
||||
import cn.lunadeer.essentialsd.dtos.WarpPoint;
|
||||
import cn.lunadeer.essentialsd.managers.TeleportManager;
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -26,6 +24,10 @@ public class Warp implements TabExecutor {
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) commandSender;
|
||||
if (strings.length != 1) {
|
||||
Notification.error(commandSender, "用法: /warp <name>");
|
||||
return true;
|
||||
}
|
||||
WarpPoint point = WarpPoint.selectByName(strings[0]);
|
||||
if (point == null) {
|
||||
Notification.error(commandSender, "传送点 %s 不存在", strings[0]);
|
||||
|
@ -335,6 +335,16 @@ public class ConfigManager {
|
||||
_plugin.saveConfig();
|
||||
}
|
||||
|
||||
public String getPrefixTabFormat() {
|
||||
return _prefix_tab_format;
|
||||
}
|
||||
|
||||
public void setPrefixTabFormat(String format) {
|
||||
_prefix_tab_format = format;
|
||||
_file.set("Prefix.TabFormat", format);
|
||||
_plugin.saveConfig();
|
||||
}
|
||||
|
||||
|
||||
private final EssentialsD _plugin;
|
||||
private FileConfiguration _file;
|
||||
|
@ -0,0 +1,67 @@
|
||||
package cn.lunadeer.essentialsd.managers;
|
||||
|
||||
import cn.lunadeer.essentialsd.EssentialsD;
|
||||
import cn.lunadeer.minecraftpluginutils.Scheduler;
|
||||
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;
|
||||
|
||||
public TabListUpdater() {
|
||||
instance = this;
|
||||
Scheduler.runTaskRepeatAsync(this::update, 0, 40);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
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());
|
||||
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