9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2025-12-25 01:49:16 +00:00
This commit is contained in:
Xiao-MoMi
2022-06-24 00:46:46 +08:00
parent a9a02a3eaa
commit 7cd20ecd5f
15 changed files with 720 additions and 7 deletions

View File

@@ -35,6 +35,7 @@ public class ConfigManager {
public static boolean show_after;
public static String lang;
public static Long preview;
public static boolean thin_font;
public static void ReloadConfig(){
CustomNameplates.instance.saveDefaultConfig();
@@ -55,6 +56,7 @@ public class ConfigManager {
show_after = config.getBoolean("config.show-after-load-resourcepack");
key = Key.key(fontName);
preview = config.getLong("config.preview-duration");
thin_font = config.getBoolean("config.use-thin-font",false);
}
}
//消息文件

View File

@@ -6,6 +6,7 @@ import net.momirealms.customnameplates.commands.Execute;
import net.momirealms.customnameplates.commands.TabComplete;
import net.momirealms.customnameplates.data.DataManager;
import net.momirealms.customnameplates.data.SqlHandler;
import net.momirealms.customnameplates.hook.HookManager;
import net.momirealms.customnameplates.listener.PlayerListener;
import net.momirealms.customnameplates.listener.PacketsListener;
import net.momirealms.customnameplates.resource.ResourceManager;

View File

@@ -0,0 +1,78 @@
package net.momirealms.customnameplates.font;
public enum FontWidthThin {
A('A', 3), a('a', 3), B('B', 3), b('b', 3),
C('C', 3), c('c', 3), D('D', 3), d('d', 3),
E('E', 3), e('e', 3), F('F', 3), f('f', 2),
G('G', 3), g('g', 3), H('H', 3), h('h', 3),
I('I', 2), i('i', 2), J('J', 3), j('j', 2),
K('K', 3), k('k', 3), L('L', 3), l('l', 2),
M('M', 3), m('m', 3), N('N', 3), n('n', 3),
O('O', 3), o('o', 3), P('P', 3), p('p', 3),
Q('Q', 3), q('q', 3), R('R', 3), r('r', 3),
S('S', 3), s('s', 3), T('T', 3), t('t', 2),
U('U', 3), u('u', 3), V('V', 3), v('v', 3),
W('W', 3), w('w', 3), X('X', 3), x('x', 3),
Y('Y', 3), y('y', 3), Z('Z', 3), z('z', 3),
NUM_1('1', 2), NUM_2('2', 3), NUM_3('3', 3), NUM_4('4', 3),
NUM_5('5', 3), NUM_6('6', 3), NUM_7('7', 3), NUM_8('8', 3),
NUM_9('9', 3), NUM_0('0', 3), EXCLAMATION_POINT('!', 1), AT_SYMBOL('@', 3),
NUM_SIGN('#', 3), DOLLAR_SIGN('$', 3), PERCENT('%', 3), UP_ARROW('^', 3),
AMPERSAND('&', 3), ASTERISK('*', 3), LEFT_PARENTHESIS('(', 2),
RIGHT_PARENTHESIS(')', 2), MINUS('-', 3), UNDERSCORE('_', 3), PLUS_SIGN('+', 3),
EQUALS_SIGN('=', 3), LEFT_CURL_BRACE('{', 1), RIGHT_CURL_BRACE('}', 1),
LEFT_BRACKET('[', 2), RIGHT_BRACKET(']', 2), COLON(':', 1), SEMI_COLON(';', 1),
DOUBLE_QUOTE('\"', 2), SINGLE_QUOTE('\'', 1), LEFT_ARROW('<', 2),
RIGHT_ARROW('>', 2), QUESTION_MARK('?', 3), SLASH('/', 3),
BACK_SLASH('\\', 3), LINE('|', 1), TILDE('~', 3), TICK('`', 1),
PERIOD('.', 1), COMMA(',', 1), SPACE(' ', 3),
IN_BETWEEN(' ', 1), DEFAULT('默', 8);
private final char character;
private final int length;
FontWidthThin(char character, int length) {
this.character = character;
this.length = length;
}
public char getCharacter() {
return this.character;
}
public int getLength() {
return this.length;
}
public int getBoldLength() {
if (this == FontWidthThin.SPACE) {
return this.getLength();
}
return this.getLength() + 1;
}
/*
获取每个字符的像素宽度
*/
public static FontWidthThin getInfo(char c) {
for (FontWidthThin minecraftFontWidth : values()) {
if (minecraftFontWidth.getCharacter() == c) {
return minecraftFontWidth;
}
}
return FontWidthThin.DEFAULT;
}
/*
计算一个字符串的总宽度
*/
public static int getTotalWidth(String s) {
int length = s.length();
int n = 0;
for (int i = 0; i < length; i++) {
n += getInfo(s.charAt(i)).getLength();
}
return n + FontWidthThin.IN_BETWEEN.getLength() * (length - 1); //总长还需加上字符间距
}
}

View File

@@ -0,0 +1,57 @@
package net.momirealms.customnameplates.hook;
import me.clip.placeholderapi.PlaceholderAPI;
import net.momirealms.customnameplates.AdventureManager;
import net.momirealms.customnameplates.ConfigManager;
import net.momirealms.customnameplates.CustomNameplates;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
public class HookManager {
private boolean placeholderAPI;
private boolean itemsAdder;
public boolean hasPlaceholderAPI() {
return this.placeholderAPI;
}
public boolean hasItemsAdder() {
return this.itemsAdder;
}
public HookManager(CustomNameplates plugin) {
this.initializePlaceholderAPI();
this.initializeItemsAdder();
}
//Papi Hook检测
private void initializePlaceholderAPI() {
if(!ConfigManager.MainConfig.placeholderAPI){
this.placeholderAPI = false;
return;
}
if(CustomNameplates.instance.getServer().getPluginManager().getPlugin("PlaceholderAPI") != null){
this.placeholderAPI = true;
AdventureManager.consoleMessage("<gradient:#DDE4FF:#8DA2EE>[CustomNameplates]</gradient> " + "<color:#F5F5F5>PlaceholderAPI Hooked!");
}
}
//ItemsAdder Hook检测
private void initializeItemsAdder() {
if (!ConfigManager.MainConfig.itemsAdder) {
this.itemsAdder = false;
}
if(CustomNameplates.instance.getServer().getPluginManager().getPlugin("ItemsAdder") != null){
this.itemsAdder = true;
AdventureManager.consoleMessage("<gradient:#DDE4FF:#8DA2EE>[CustomNameplates]</gradient> " + "<color:#F5F5F5>ItemsAdder Hooked!");
}
}
/*
解析prefix与suffix
*/
public String parsePlaceholders(Player player, String papi) {
String s = StringUtils.replace(StringUtils.replace(papi, "%player_name%", player.getName()), "%player_displayname%", player.getDisplayName());
s = PlaceholderAPI.setPlaceholders(player, s);
return ChatColor.translateAlternateColorCodes('&', s);
}
}

View File

@@ -0,0 +1,36 @@
package net.momirealms.customnameplates.hook;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.momirealms.customnameplates.CustomNameplates;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
public class PapiPlate extends PlaceholderExpansion{
private CustomNameplates plugin;
public PapiPlate(CustomNameplates plugin) {
this.plugin = plugin;
}
@Override
public @NotNull String getIdentifier() {
return "nameplates";
}
@Override
public @NotNull String getAuthor() {
return "XiaoMoMi";
}
@Override
public @NotNull String getVersion() {
return "1.0";
}
@Override
public String onRequest(OfflinePlayer player, String params) {
return null;
}
}

View File

@@ -2,12 +2,12 @@ package net.momirealms.customnameplates.nameplates;
import org.bukkit.ChatColor;
public record NameplateConfig(ChatColor color, int height, String name) {
public record NameplateConfig(ChatColor color, int height, String name, int yoffset) {
public static NameplateConfig EMPTY;
static {
EMPTY = new NameplateConfig(ChatColor.WHITE, 16, "none");
EMPTY = new NameplateConfig(ChatColor.WHITE, 16, "none", 12);
}
//获取Team颜色
@@ -24,4 +24,6 @@ public record NameplateConfig(ChatColor color, int height, String name) {
public String getName() {
return this.name;
}
public int getyoffset() {return this.yoffset; }
}

View File

@@ -1,8 +1,10 @@
package net.momirealms.customnameplates.nameplates;
import net.momirealms.customnameplates.ConfigManager;
import net.momirealms.customnameplates.font.FontCache;
import net.momirealms.customnameplates.font.FontNegative;
import net.momirealms.customnameplates.font.FontWidth;
import net.momirealms.customnameplates.font.FontWidthThin;
import org.bukkit.ChatColor;
public class NameplateUtil {
@@ -18,7 +20,12 @@ public class NameplateUtil {
当然这个玩家名是带上前缀与后缀的
*/
public String makeCustomNameplate(String prefix, String name, String suffix) {
int totalWidth = FontWidth.getTotalWidth(ChatColor.stripColor(prefix + name + suffix));
int totalWidth;
if (ConfigManager.MainConfig.thin_font){
totalWidth = FontWidthThin.getTotalWidth(ChatColor.stripColor(prefix + name + suffix));
}else {
totalWidth = FontWidth.getTotalWidth(ChatColor.stripColor(prefix + name + suffix));
}
boolean isEven = totalWidth % 2 == 0; //奇偶判断
char left = this.fontcache.getChar().getLeft();
char middle = this.fontcache.getChar().getMiddle();
@@ -44,7 +51,12 @@ public class NameplateUtil {
保证铭牌总是位于玩家头顶中央的位置
*/
public String getSuffixLength(String name) {
final int totalWidth = FontWidth.getTotalWidth(ChatColor.stripColor(name));
int totalWidth;
if (ConfigManager.MainConfig.thin_font){
totalWidth = FontWidthThin.getTotalWidth(ChatColor.stripColor(name));
}else {
totalWidth = FontWidth.getTotalWidth(ChatColor.stripColor(name));
}
return FontNegative.getShortestNegChars(totalWidth + totalWidth % 2 + 1);
}

View File

@@ -23,7 +23,7 @@ import java.util.*;
public class ResourceManager {
public final HashMap<String, FontCache> caches;
private CustomNameplates plugin;
private final CustomNameplates plugin;
public ResourceManager(CustomNameplates plugin) {
this.caches = new HashMap<>();
@@ -78,7 +78,7 @@ public class ResourceManager {
caches.put(pngName, new FontCache(pngName, fontChar, config));
jsonObject_2.add("type", new JsonPrimitive("bitmap"));
jsonObject_2.add("file", new JsonPrimitive(ConfigManager.MainConfig.namespace + ":" + ConfigManager.MainConfig.folder_path.replaceAll("\\\\","/") + png.getName().toLowerCase()));
jsonObject_2.add("ascent", new JsonPrimitive(12));
jsonObject_2.add("ascent", new JsonPrimitive(config.getyoffset()));
jsonObject_2.add("height", new JsonPrimitive(config.getHeight()));
JsonArray jsonArray_2 = new JsonArray();
jsonArray_2.add(native2ascii(fontChar.getLeft()) + native2ascii(fontChar.getMiddle()) + native2ascii(fontChar.getRight()));
@@ -170,6 +170,9 @@ public class ResourceManager {
if (!config.contains("size")){
config.set("size", 16);
}
if (!config.contains("yoffset")){
config.set("yoffset", 12);
}
ChatColor color = ChatColor.WHITE;
try {
color = ChatColor.valueOf(Objects.requireNonNull(config.getString("color")).toUpperCase());
@@ -178,9 +181,10 @@ public class ResourceManager {
AdventureManager.consoleMessage("<red>[CustomNameplates] Invalid Color of " + nameplate + "</red>");
}
int size = config.getInt("size");
int yoffset = config.getInt("yoffset");
String name = config.getString("name");
config.save(file);
return new NameplateConfig(color, size, name);
return new NameplateConfig(color, size, name, yoffset);
}
catch (Exception e) {
return NameplateConfig.EMPTY;