diff --git a/build.gradle b/build.gradle index 88447dd..622e9db 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'net.momirealms' -version = '1.8.1' +version = '1.8.4' repositories { mavenCentral() @@ -44,7 +44,7 @@ dependencies { implementation('net.kyori:adventure-api:4.11.0') implementation('net.kyori:adventure-platform-bukkit:4.1.1') implementation('net.kyori:adventure-text-minimessage:4.11.0') - implementation("net.kyori:adventure-text-serializer-gson:4.11.0") + implementation('net.kyori:adventure-text-serializer-gson:4.11.0') } def targetJavaVersion = 16 diff --git a/src/main/java/net/momirealms/customnameplates/ConfigManager.java b/src/main/java/net/momirealms/customnameplates/ConfigManager.java index f020492..97b6e02 100644 --- a/src/main/java/net/momirealms/customnameplates/ConfigManager.java +++ b/src/main/java/net/momirealms/customnameplates/ConfigManager.java @@ -41,6 +41,7 @@ public class ConfigManager { public static TreeMap bossbarsP = new TreeMap<>(); public static HashMap papiBG = new HashMap<>(); public static HashMap papiNP = new HashMap<>(); + public static HashMap fontWidth = new HashMap<>(); public static YamlConfiguration getConfig(String configName) { File file = new File(CustomNameplates.instance.getDataFolder(), configName); @@ -65,6 +66,18 @@ public class ConfigManager { useAdventure = bossbarmode.getString("mode").equalsIgnoreCase("Adventure"); } + + public static void loadWidth(){ + + fontWidth.clear(); + + YamlConfiguration config = getConfig("char-width.yml"); + config.getConfigurationSection("").getKeys(false).forEach(key -> { + fontWidth.put(key.charAt(0), config.getInt(key)); + }); + AdventureManager.consoleMessage("[CustomNameplates] Loaded " + fontWidth.size() + " custom char width"); + } + public static class MainConfig{ public static String namespace; public static String fontName; diff --git a/src/main/java/net/momirealms/customnameplates/CustomNameplates.java b/src/main/java/net/momirealms/customnameplates/CustomNameplates.java index 887d402..c254da1 100644 --- a/src/main/java/net/momirealms/customnameplates/CustomNameplates.java +++ b/src/main/java/net/momirealms/customnameplates/CustomNameplates.java @@ -30,7 +30,6 @@ import net.momirealms.customnameplates.data.SqlHandler; import net.momirealms.customnameplates.helper.LibraryLoader; import net.momirealms.customnameplates.hook.Placeholders; import net.momirealms.customnameplates.listener.PlayerListener; -import net.momirealms.customnameplates.listener.PacketsListener; import net.momirealms.customnameplates.resource.ResourceManager; import net.momirealms.customnameplates.scoreboard.ScoreBoardManager; import org.bukkit.Bukkit; @@ -48,11 +47,10 @@ public final class CustomNameplates extends JavaPlugin { private ResourceManager resourceManager; private DataManager dataManager; private ScoreBoardManager scoreBoardManager; + private Placeholders placeholders; private Timer timer; - public ResourceManager getResourceManager() { - return this.resourceManager; - } + public ResourceManager getResourceManager() {return this.resourceManager;} public DataManager getDataManager() { return this.dataManager; } public ScoreBoardManager getScoreBoardManager() { return this.scoreBoardManager; } @@ -71,6 +69,7 @@ public final class CustomNameplates extends JavaPlugin { ConfigManager.loadModule(); ConfigManager.MainConfig.ReloadConfig(); ConfigManager.Message.ReloadConfig(); + ConfigManager.loadWidth(); if (ConfigManager.bossbar){ ConfigManager.loadBossBar(); if (ConfigManager.useAdventure){ @@ -89,17 +88,17 @@ public final class CustomNameplates extends JavaPlugin { if (ConfigManager.nameplate){ ConfigManager.DatabaseConfig.LoadConfig(); Bukkit.getPluginManager().registerEvents(new PlayerListener(this),this); - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketsListener(this)); } if (ConfigManager.MainConfig.placeholderAPI){ - new Placeholders().register(); + placeholders = new Placeholders(); + placeholders.register(); AdventureManager.consoleMessage("[CustomNameplates] PlaceholderAPI Hooked!"); } if (ConfigManager.MainConfig.tab){ AdventureManager.consoleMessage("[CustomNameplates] TAB Hooked!"); } Objects.requireNonNull(Bukkit.getPluginCommand("customnameplates")).setExecutor(new Execute(this)); - Objects.requireNonNull(Bukkit.getPluginCommand("customnameplates")).setTabCompleter(new TabComplete(this)); + Objects.requireNonNull(Bukkit.getPluginCommand("customnameplates")).setTabCompleter(new TabComplete()); this.resourceManager = new ResourceManager(this); this.dataManager = new DataManager(this); this.scoreBoardManager = new ScoreBoardManager(this); @@ -122,9 +121,27 @@ public final class CustomNameplates extends JavaPlugin { if (timer != null){ timer.stopTimer(timer.getTaskID()); } - if(adventure != null) { + if (adventure != null) { adventure.close(); adventure = null; } + if (protocolManager != null){ + protocolManager = null; + } + if (placeholders != null){ + placeholders.unregister(); + } + if (resourceManager != null){ + resourceManager = null; + } + if (scoreBoardManager != null){ + scoreBoardManager = null; + } + if (dataManager != null){ + dataManager = null; + } + if (instance != null){ + instance = null; + } } } diff --git a/src/main/java/net/momirealms/customnameplates/bossbar/adventure/BossBarSenderA.java b/src/main/java/net/momirealms/customnameplates/bossbar/adventure/BossBarSenderA.java index a7c9bbf..c4162c1 100644 --- a/src/main/java/net/momirealms/customnameplates/bossbar/adventure/BossBarSenderA.java +++ b/src/main/java/net/momirealms/customnameplates/bossbar/adventure/BossBarSenderA.java @@ -21,7 +21,6 @@ import me.clip.placeholderapi.PlaceholderAPI; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.minimessage.MiniMessage; import net.momirealms.customnameplates.ConfigManager; import net.momirealms.customnameplates.CustomNameplates; diff --git a/src/main/java/net/momirealms/customnameplates/bossbar/protocollib/BossBarSenderP.java b/src/main/java/net/momirealms/customnameplates/bossbar/protocollib/BossBarSenderP.java index 6950f57..561ee64 100644 --- a/src/main/java/net/momirealms/customnameplates/bossbar/protocollib/BossBarSenderP.java +++ b/src/main/java/net/momirealms/customnameplates/bossbar/protocollib/BossBarSenderP.java @@ -37,9 +37,9 @@ public class BossBarSenderP extends BukkitRunnable { public void showBossbar(){ this.packet = new PacketContainer(PacketType.Play.Server.BOSS); - packet.setMeta("id", UUID.randomUUID()); this.overlay = bossbarConfig.getOverlay(); this.barColor = bossbarConfig.getColor(); + packet.getModifier().write(0, UUID.randomUUID()); InternalStructure internalStructure = packet.getStructures().read(1); if (ConfigManager.MainConfig.placeholderAPI){ this.text = PlaceholderAPI.setPlaceholders(player, bossbarConfig.getText()); diff --git a/src/main/java/net/momirealms/customnameplates/commands/Execute.java b/src/main/java/net/momirealms/customnameplates/commands/Execute.java index 9e6554f..0814023 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/Execute.java +++ b/src/main/java/net/momirealms/customnameplates/commands/Execute.java @@ -31,10 +31,11 @@ import net.momirealms.customnameplates.AdventureManager; import net.momirealms.customnameplates.CustomNameplates; import net.momirealms.customnameplates.data.DataManager; import net.momirealms.customnameplates.font.FontCache; -import net.momirealms.customnameplates.hook.ParsePapi; +import net.momirealms.customnameplates.hook.PapiHook; import net.momirealms.customnameplates.hook.TABHook; import net.momirealms.customnameplates.nameplates.NameplateUtil; import net.momirealms.customnameplates.scoreboard.NameplatesTeam; +import net.momirealms.customnameplates.scoreboard.ScoreBoardManager; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -80,6 +81,7 @@ public class Execute implements CommandExecutor { if (sender.hasPermission("customnameplates.reload") || sender.isOp()) { ConfigManager.MainConfig.ReloadConfig(); ConfigManager.Message.ReloadConfig(); + ConfigManager.loadWidth(); if (ConfigManager.actionbar){ ConfigManager.ActionbarConfig.LoadConfig(); } @@ -123,9 +125,9 @@ public class Execute implements CommandExecutor { } DataManager.cache.get(player.getUniqueId()).equipNameplate(args[1]); if (ConfigManager.MainConfig.tab){ - this.plugin.getScoreBoardManager().getTeam(TABHook.getTABTeam(player.getName())).updateNameplates(); + ScoreBoardManager.teams.get(TABHook.getTABTeam(player.getName())).updateNameplates(); }else { - this.plugin.getScoreBoardManager().getTeam(player.getName()).updateNameplates(); + ScoreBoardManager.teams.get(player.getName()).updateNameplates(); } this.plugin.getDataManager().savePlayer(player.getUniqueId()); AdventureManager.playerMessage((Player) sender, ConfigManager.Message.prefix + ConfigManager.Message.equip.replace("{Nameplate}", plugin.getResourceManager().getNameplateInfo(args[1]).getConfig().getName())); @@ -160,9 +162,9 @@ public class Execute implements CommandExecutor { } DataManager.cache.get(player.getUniqueId()).equipNameplate(args[2]); if (ConfigManager.MainConfig.tab){ - this.plugin.getScoreBoardManager().getTeam(TABHook.getTABTeam(args[1])).updateNameplates(); + ScoreBoardManager.teams.get(TABHook.getTABTeam(args[1])).updateNameplates(); }else { - this.plugin.getScoreBoardManager().getTeam(args[1]).updateNameplates(); + ScoreBoardManager.teams.get(args[1]).updateNameplates(); } this.plugin.getDataManager().savePlayer(player.getUniqueId()); if (sender instanceof Player){ @@ -187,9 +189,9 @@ public class Execute implements CommandExecutor { if (sender instanceof Player player){ DataManager.cache.get(player.getUniqueId()).equipNameplate("none"); if (ConfigManager.MainConfig.tab){ - this.plugin.getScoreBoardManager().getTeam(TABHook.getTABTeam(player.getName())).updateNameplates(); + ScoreBoardManager.teams.get(TABHook.getTABTeam(player.getName())).updateNameplates(); }else { - this.plugin.getScoreBoardManager().getTeam(player.getName()).updateNameplates(); + ScoreBoardManager.teams.get(player.getName()).updateNameplates(); } this.plugin.getDataManager().savePlayer(player.getUniqueId()); AdventureManager.playerMessage(player, ConfigManager.Message.prefix + ConfigManager.Message.unequip); @@ -212,9 +214,9 @@ public class Execute implements CommandExecutor { Player player = Bukkit.getPlayer(args[1]); DataManager.cache.get(player.getUniqueId()).equipNameplate("none"); if (ConfigManager.MainConfig.tab){ - this.plugin.getScoreBoardManager().getTeam(TABHook.getTABTeam(args[1])).updateNameplates(); + ScoreBoardManager.teams.get(TABHook.getTABTeam(args[1])).updateNameplates(); }else { - this.plugin.getScoreBoardManager().getTeam(args[1]).updateNameplates(); + ScoreBoardManager.teams.get(args[1]).updateNameplates(); } this.plugin.getDataManager().savePlayer(player.getUniqueId()); if (sender instanceof Player){ @@ -293,8 +295,8 @@ public class Execute implements CommandExecutor { String playerPrefix; String playerSuffix; if (ConfigManager.MainConfig.placeholderAPI) { - playerPrefix = ParsePapi.parsePlaceholders(player, ConfigManager.MainConfig.player_prefix); - playerSuffix = ParsePapi.parsePlaceholders(player, ConfigManager.MainConfig.player_suffix); + playerPrefix = PapiHook.parsePlaceholders(player, ConfigManager.MainConfig.player_prefix); + playerSuffix = PapiHook.parsePlaceholders(player, ConfigManager.MainConfig.player_suffix); }else { playerPrefix = ConfigManager.MainConfig.player_prefix; playerSuffix = ConfigManager.MainConfig.player_suffix; @@ -345,6 +347,7 @@ public class Execute implements CommandExecutor { AdventureManager.playerMessage(player,"/nameplates preview - preview your nameplate"); AdventureManager.playerMessage(player,"/nameplates forcepreview - force a player to preview a nameplate"); AdventureManager.playerMessage(player,"/nameplates list - list your available nameplates"); + AdventureManager.playerMessage(player,"/nameplates generate - generate the RP"); } }else { AdventureManager.consoleMessage("/nameplates help - show the command list"); @@ -356,6 +359,7 @@ public class Execute implements CommandExecutor { AdventureManager.consoleMessage("/nameplates preview - preview your nameplate"); AdventureManager.consoleMessage("/nameplates forcepreview - force a player to preview a nameplate"); AdventureManager.consoleMessage("/nameplates list - list your available nameplates"); + AdventureManager.consoleMessage("/nameplates generate - generate the RP"); } return true; } @@ -364,6 +368,7 @@ public class Execute implements CommandExecutor { } private void showNameplate(Player player, Component component) { + ArmorStand entity = player.getWorld().spawn(player.getLocation().add(0,0.8,0), ArmorStand.class, a -> { a.setInvisible(true); a.setCollidable(false); diff --git a/src/main/java/net/momirealms/customnameplates/commands/TabComplete.java b/src/main/java/net/momirealms/customnameplates/commands/TabComplete.java index fe4368d..27bd2b1 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/TabComplete.java +++ b/src/main/java/net/momirealms/customnameplates/commands/TabComplete.java @@ -17,7 +17,7 @@ package net.momirealms.customnameplates.commands; -import net.momirealms.customnameplates.CustomNameplates; +import net.momirealms.customnameplates.resource.ResourceManager; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -33,12 +33,6 @@ import java.util.List; public class TabComplete implements TabCompleter { - private final CustomNameplates plugin; - - public TabComplete(CustomNameplates plugin){ - this.plugin = plugin; - } - @Override @ParametersAreNonnullByDefault public @Nullable List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { @@ -95,7 +89,7 @@ public class TabComplete implements TabCompleter { String permission = info.getPermission().toLowerCase(); if (permission.startsWith("customnameplates.equip.")) { permission = StringUtils.replace(permission, "customnameplates.equip.", ""); - if (this.plugin.getResourceManager().caches.get(permission) != null){ + if (ResourceManager.caches.get(permission) != null){ availableNameplates.add(permission); } } @@ -105,6 +99,6 @@ public class TabComplete implements TabCompleter { } private List nameplates(){ - return new ArrayList<>(this.plugin.getResourceManager().caches.keySet()); + return new ArrayList<>(ResourceManager.caches.keySet()); } } diff --git a/src/main/java/net/momirealms/customnameplates/font/FontWidth.java b/src/main/java/net/momirealms/customnameplates/font/FontWidth.java index 12b6b5a..94ebdd1 100644 --- a/src/main/java/net/momirealms/customnameplates/font/FontWidth.java +++ b/src/main/java/net/momirealms/customnameplates/font/FontWidth.java @@ -17,6 +17,8 @@ package net.momirealms.customnameplates.font; +import net.momirealms.customnameplates.ConfigManager; + public enum FontWidth { A('A', 5), a('a', 5), B('B', 5), b('b', 5), @@ -72,13 +74,18 @@ public enum FontWidth { /* 获取每个字符的像素宽度 */ - public static FontWidth getInfo(char c) { + public static int getInfo(char c) { for (FontWidth minecraftFontWidth : values()) { if (minecraftFontWidth.getCharacter() == c) { - return minecraftFontWidth; + return minecraftFontWidth.length; } } - return FontWidth.DEFAULT; + int custom = ConfigManager.fontWidth.get(c); + if (custom != 0){ + return custom; + }else { + return 8; + } } /* @@ -88,8 +95,8 @@ public enum FontWidth { int length = s.length(); int n = 0; for (int i = 0; i < length; i++) { - n += getInfo(s.charAt(i)).getLength(); + n += getInfo(s.charAt(i)); } - return n + FontWidth.IN_BETWEEN.getLength() * (length - 1); //总长还需加上字符间距 + return n + length - 1; //总长还需加上字符间距 } } diff --git a/src/main/java/net/momirealms/customnameplates/font/FontWidthThin.java b/src/main/java/net/momirealms/customnameplates/font/FontWidthThin.java index 11c7801..97a1020 100644 --- a/src/main/java/net/momirealms/customnameplates/font/FontWidthThin.java +++ b/src/main/java/net/momirealms/customnameplates/font/FontWidthThin.java @@ -17,6 +17,8 @@ package net.momirealms.customnameplates.font; +import net.momirealms.customnameplates.ConfigManager; + public enum FontWidthThin { A('A', 3), a('a', 3), B('B', 3), b('b', 3), @@ -72,13 +74,18 @@ public enum FontWidthThin { /* 获取每个字符的像素宽度 */ - public static FontWidthThin getInfo(char c) { + public static int getInfo(char c) { for (FontWidthThin minecraftFontWidth : values()) { if (minecraftFontWidth.getCharacter() == c) { - return minecraftFontWidth; + return minecraftFontWidth.length; } } - return FontWidthThin.DEFAULT; + int custom = ConfigManager.fontWidth.get(c); + if (custom != 0){ + return custom; + }else { + return 8; + } } /* @@ -88,8 +95,8 @@ public enum FontWidthThin { int length = s.length(); int n = 0; for (int i = 0; i < length; i++) { - n += getInfo(s.charAt(i)).getLength(); + n += getInfo(s.charAt(i)); } - return n + FontWidthThin.IN_BETWEEN.getLength() * (length - 1); //总长还需加上字符间距 + return n + length - 1; //总长还需加上字符间距 } } diff --git a/src/main/java/net/momirealms/customnameplates/hook/ParsePapi.java b/src/main/java/net/momirealms/customnameplates/hook/PapiHook.java similarity index 76% rename from src/main/java/net/momirealms/customnameplates/hook/ParsePapi.java rename to src/main/java/net/momirealms/customnameplates/hook/PapiHook.java index 831c41a..a83060b 100644 --- a/src/main/java/net/momirealms/customnameplates/hook/ParsePapi.java +++ b/src/main/java/net/momirealms/customnameplates/hook/PapiHook.java @@ -18,13 +18,11 @@ package net.momirealms.customnameplates.hook; import me.clip.placeholderapi.PlaceholderAPI; -import org.apache.commons.lang.StringUtils; import org.bukkit.entity.Player; -public class ParsePapi { +public class PapiHook { public static String parsePlaceholders(Player player, String papi) { - String s = StringUtils.replace(StringUtils.replace(papi, "%player_name%", player.getName()), "%player_displayname%", player.getDisplayName()); - return PlaceholderAPI.setPlaceholders(player, s); + return PlaceholderAPI.setPlaceholders(player, papi); } } diff --git a/src/main/java/net/momirealms/customnameplates/listener/PacketsListener.java b/src/main/java/net/momirealms/customnameplates/listener/PacketsListener.java deleted file mode 100644 index bd87a96..0000000 --- a/src/main/java/net/momirealms/customnameplates/listener/PacketsListener.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) <2022> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.momirealms.customnameplates.listener; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.events.InternalStructure; -import com.comphenix.protocol.events.ListenerPriority; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.utility.MinecraftReflection; -import com.comphenix.protocol.wrappers.WrappedChatComponent; -import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import net.momirealms.customnameplates.ConfigManager; -import net.momirealms.customnameplates.CustomNameplates; -import net.momirealms.customnameplates.data.DataManager; -import net.momirealms.customnameplates.scoreboard.NameplatesTeam; -import org.bukkit.ChatColor; - -import java.util.Optional; - -public class PacketsListener extends PacketAdapter { - - private final CustomNameplates plugin; - - public PacketsListener(CustomNameplates plugin) { - super(plugin, ListenerPriority.HIGHEST, PacketType.Play.Server.SCOREBOARD_TEAM); - this.plugin = plugin; - } - - public void onPacketSending(PacketEvent event) { - Integer n = event.getPacket().getIntegers().read(0); - if (n != 2) { - return; - } - //if (n == 0) System.out.println("对玩家" + event.getPlayer().getName() + "发送team创建包"); - //if (n == 2) System.out.println("对玩家"+ event.getPlayer().getName() + "发送team更新包"); - Optional optional = event.getPacket().getOptionalStructures().read(0); - if (optional.isEmpty()) { - return; - } - InternalStructure internalStructure = optional.get(); - String teamName = event.getPacket().getStrings().read(0); - //System.out.println("本次创建/更新的队伍名是" + teamName); - NameplatesTeam team = this.plugin.getScoreBoardManager().getTeam(teamName); - if (team == null) { - //System.out.println("但是这个队伍不存在于缓存中哦,说明那个玩家还没上线"); - return; - } - //System.out.println("这个队伍确实存在于缓存中呢!"); - if (ConfigManager.MainConfig.show_after && (DataManager.cache.get(event.getPlayer().getUniqueId()) == null || DataManager.cache.get(event.getPlayer().getUniqueId()).getAccepted() == 0)) { - //System.out.println("玩家" +event.getPlayer().getName() +"因为没有接受资源包所以没有被显示铭牌"); - internalStructure.getChatComponents().write(1, WrappedChatComponent.fromJson("{\"text\":\"\"}")); - internalStructure.getChatComponents().write(2, WrappedChatComponent.fromJson("{\"text\":\"\"}")); - internalStructure.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat")).write(0,ChatColor.WHITE); - return; - } - //System.out.println("玩家" +event.getPlayer().getName() +"可以看见队伍" + teamName + "的铭牌"); - if (team.getPrefix() != null) { - internalStructure.getChatComponents().write(1, WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(team.getPrefix()))); - } - if (team.getSuffix() != null) { - internalStructure.getChatComponents().write(2, WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(team.getSuffix()))); - } - internalStructure.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat")).write(0,team.getColor()); - } -} diff --git a/src/main/java/net/momirealms/customnameplates/listener/PlayerListener.java b/src/main/java/net/momirealms/customnameplates/listener/PlayerListener.java index 0e17b62..24a39ec 100644 --- a/src/main/java/net/momirealms/customnameplates/listener/PlayerListener.java +++ b/src/main/java/net/momirealms/customnameplates/listener/PlayerListener.java @@ -17,20 +17,31 @@ package net.momirealms.customnameplates.listener; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.InternalStructure; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.utility.MinecraftReflection; +import com.comphenix.protocol.wrappers.WrappedChatComponent; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.momirealms.customnameplates.ConfigManager; import net.momirealms.customnameplates.CustomNameplates; import net.momirealms.customnameplates.data.DataManager; import net.momirealms.customnameplates.data.PlayerData; import net.momirealms.customnameplates.hook.TABHook; +import net.momirealms.customnameplates.scoreboard.NameplatesTeam; import net.momirealms.customnameplates.scoreboard.ScoreBoardManager; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerResourcePackStatusEvent; -import org.bukkit.scoreboard.Scoreboard; -import org.bukkit.scoreboard.Team; + +import java.lang.reflect.InvocationTargetException; +import java.util.Collections; +import java.util.Optional; public record PlayerListener(CustomNameplates plugin) implements Listener { @@ -38,30 +49,20 @@ public record PlayerListener(CustomNameplates plugin) implements Listener { public void onJoin(PlayerJoinEvent event) { this.plugin.getDataManager().loadData(event.getPlayer()); Bukkit.getScheduler().runTaskLaterAsynchronously(CustomNameplates.instance, ()-> { - if (ConfigManager.MainConfig.tab){ - Bukkit.getOnlinePlayers().forEach(player -> this.plugin.getScoreBoardManager().getTeam(TABHook.getTABTeam(player.getName())).updateNameplates()); - }else { - Bukkit.getOnlinePlayers().forEach(player -> this.plugin.getScoreBoardManager().getTeam(player.getName()).updateNameplates()); - } - }, 50); + sendPacketsToPlayer(event.getPlayer()); + }, 40); } @EventHandler public void onQuit(PlayerQuitEvent event) { this.plugin.getDataManager().unloadPlayer(event.getPlayer().getUniqueId()); - Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); - Team team; String teamName; if (ConfigManager.MainConfig.tab){ teamName = TABHook.getTABTeam(event.getPlayer().getName()); }else { teamName = event.getPlayer().getName(); } - team = scoreboard.getTeam(teamName); ScoreBoardManager.teams.remove(teamName); - if (team != null){ - team.unregister(); - } } @EventHandler @@ -73,19 +74,58 @@ public record PlayerListener(CustomNameplates plugin) implements Listener { } if (event.getStatus() == PlayerResourcePackStatusEvent.Status.SUCCESSFULLY_LOADED) { playerData.setAccepted(1); - if (ConfigManager.MainConfig.tab){ - Bukkit.getOnlinePlayers().forEach(player -> this.plugin.getScoreBoardManager().getTeam(TABHook.getTABTeam(player.getName())).updateNameplates()); - }else { - Bukkit.getOnlinePlayers().forEach(player -> this.plugin.getScoreBoardManager().getTeam(player.getName()).updateNameplates()); - } - } else if(event.getStatus() == PlayerResourcePackStatusEvent.Status.DECLINED || event.getStatus() == PlayerResourcePackStatusEvent.Status.FAILED_DOWNLOAD) { - playerData.setAccepted(0); - if (ConfigManager.MainConfig.tab){ - Bukkit.getOnlinePlayers().forEach(player -> this.plugin.getScoreBoardManager().getTeam(TABHook.getTABTeam(player.getName())).updateNameplates()); - }else { - Bukkit.getOnlinePlayers().forEach(player -> this.plugin.getScoreBoardManager().getTeam(player.getName()).updateNameplates()); - } } + else if(event.getStatus() == PlayerResourcePackStatusEvent.Status.DECLINED || event.getStatus() == PlayerResourcePackStatusEvent.Status.FAILED_DOWNLOAD) { + playerData.setAccepted(0); + } + sendPacketsToPlayer(event.getPlayer()); }, 20); } + + private void sendPacketsToPlayer(Player player){ + + if (ConfigManager.MainConfig.show_after && DataManager.cache.get(player.getUniqueId()).getAccepted() != 1) return; + + Bukkit.getOnlinePlayers().forEach(onlinePlayer -> { + + String teamName; + + if (ConfigManager.MainConfig.tab){ + teamName = TABHook.getTABTeam(onlinePlayer.getName()); + }else { + teamName = onlinePlayer.getName(); + } + + NameplatesTeam team = ScoreBoardManager.teams.get(teamName); + + if (team == null) return; + + PacketContainer packetContainer = new PacketContainer(PacketType.Play.Server.SCOREBOARD_TEAM); + packetContainer.getStrings().write(0, teamName); + + Optional optional = packetContainer.getOptionalStructures().read(0); + if (optional.isEmpty()) { + return; + } + InternalStructure internalStructure1 = optional.get(); + internalStructure1.getChatComponents().write(0, WrappedChatComponent.fromJson("{\"text\":\" "+ onlinePlayer.getName() +" \"}")); + + if (team.getPrefix() != null){ + internalStructure1.getChatComponents().write(1, WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(team.getPrefix()))); + } + + if (team.getSuffix() != null){ + internalStructure1.getChatComponents().write(2, WrappedChatComponent.fromJson(GsonComponentSerializer.gson().serialize(team.getSuffix()))); + } + + internalStructure1.getEnumModifier(ChatColor.class, MinecraftReflection.getMinecraftClass("EnumChatFormat")).write(0,team.getColor()); + packetContainer.getModifier().write(2, Collections.singletonList(onlinePlayer.getName())); + + try { + CustomNameplates.protocolManager.sendServerPacket(player, packetContainer); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + }); + } } diff --git a/src/main/java/net/momirealms/customnameplates/resource/ResourceManager.java b/src/main/java/net/momirealms/customnameplates/resource/ResourceManager.java index 7db6835..443aa29 100644 --- a/src/main/java/net/momirealms/customnameplates/resource/ResourceManager.java +++ b/src/main/java/net/momirealms/customnameplates/resource/ResourceManager.java @@ -96,6 +96,14 @@ public class ResourceManager { if (ConfigManager.MainConfig.anotherFont){ + JsonObject jsonObject_3 = new JsonObject(); + jsonObject_3.add("type", new JsonPrimitive("space")); + JsonObject jsonObject_4 = new JsonObject(); + jsonObject_4.add(" ", new JsonPrimitive(4)); + jsonObject_4.add("\\u200c", new JsonPrimitive(0)); + jsonObject_3.add("advances", jsonObject_4); + jsonArray_1.add(jsonObject_3); + JsonObject jsonObject_2 = new JsonObject(); jsonObject_2.add("type", new JsonPrimitive("bitmap")); jsonObject_2.add("file", new JsonPrimitive("minecraft:font/ascii.png")); @@ -120,14 +128,6 @@ public class ResourceManager { jsonArray_2.add("\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"); jsonObject_2.add("chars", jsonArray_2); jsonArray_1.add(jsonObject_2); - - JsonObject jsonObject_3 = new JsonObject(); - jsonObject_3.add("type", new JsonPrimitive("space")); - JsonObject jsonObject_4 = new JsonObject(); - jsonObject_4.add(" ", new JsonPrimitive(4)); - jsonObject_4.add("", new JsonPrimitive(0)); - jsonObject_3.add("advances", jsonObject_4); - jsonArray_1.add(jsonObject_3); } if (ConfigManager.nameplate){ diff --git a/src/main/java/net/momirealms/customnameplates/scoreboard/NameplatesTeam.java b/src/main/java/net/momirealms/customnameplates/scoreboard/NameplatesTeam.java index fa031c9..1b9d9c3 100644 --- a/src/main/java/net/momirealms/customnameplates/scoreboard/NameplatesTeam.java +++ b/src/main/java/net/momirealms/customnameplates/scoreboard/NameplatesTeam.java @@ -24,14 +24,11 @@ import net.momirealms.customnameplates.CustomNameplates; import net.momirealms.customnameplates.data.DataManager; import net.momirealms.customnameplates.data.PlayerData; import net.momirealms.customnameplates.font.FontCache; -import net.momirealms.customnameplates.hook.ParsePapi; +import net.momirealms.customnameplates.hook.PapiHook; import net.momirealms.customnameplates.hook.TABHook; import net.momirealms.customnameplates.nameplates.NameplateUtil; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import org.bukkit.scoreboard.Scoreboard; -import org.bukkit.scoreboard.Team; import java.util.Optional; @@ -39,7 +36,6 @@ public class NameplatesTeam { private final CustomNameplates plugin; private final Player player; - private final Team team; private Component prefix; private Component suffix; private String prefixText; @@ -58,16 +54,11 @@ public class NameplatesTeam { this.color = ChatColor.WHITE; this.plugin = plugin; this.player = player; - Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard(); String name = player.getName(); if (ConfigManager.MainConfig.tab){ this.teamName = TABHook.getTABTeam(name); - this.team = Optional.ofNullable(Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName)).orElseGet(() -> scoreboard.registerNewTeam(teamName)); - team.addEntry(player.getName()); }else { this.teamName = name; - this.team = Optional.ofNullable(Bukkit.getScoreboardManager().getMainScoreboard().getTeam(name)).orElseGet(() -> scoreboard.registerNewTeam(name)); - team.addEntry(player.getName()); } } @@ -81,8 +72,8 @@ public class NameplatesTeam { } if (nameplate.equals("none")) { if (ConfigManager.MainConfig.placeholderAPI) { - this.prefix = MiniMessage.miniMessage().deserialize(ParsePapi.parsePlaceholders(this.player, ConfigManager.MainConfig.player_prefix)); - this.suffix = MiniMessage.miniMessage().deserialize(ParsePapi.parsePlaceholders(this.player, ConfigManager.MainConfig.player_suffix)); + this.prefix = MiniMessage.miniMessage().deserialize(PapiHook.parsePlaceholders(this.player, ConfigManager.MainConfig.player_prefix)); + this.suffix = MiniMessage.miniMessage().deserialize(PapiHook.parsePlaceholders(this.player, ConfigManager.MainConfig.player_suffix)); this.prefixText = ""; this.suffixText = ""; } else { @@ -92,7 +83,6 @@ public class NameplatesTeam { this.suffixText = ""; } this.color = ChatColor.WHITE; - this.team.setPrefix(""); return; } FontCache fontCache = this.plugin.getResourceManager().getNameplateInfo(nameplate); @@ -100,7 +90,6 @@ public class NameplatesTeam { this.prefix = Component.text(""); this.suffix = Component.text(""); this.color = ChatColor.WHITE; - this.team.setPrefix(""); DataManager.cache.get(player.getUniqueId()).equipNameplate("none"); return; } @@ -110,16 +99,17 @@ public class NameplatesTeam { String playerSuffix; if (ConfigManager.MainConfig.placeholderAPI) { if (!ConfigManager.MainConfig.hidePrefix){ - playerPrefix = ParsePapi.parsePlaceholders(this.player, ConfigManager.MainConfig.player_prefix); + playerPrefix = PapiHook.parsePlaceholders(this.player, ConfigManager.MainConfig.player_prefix); }else { playerPrefix = ""; } if (!ConfigManager.MainConfig.hideSuffix){ - playerSuffix = ParsePapi.parsePlaceholders(this.player, ConfigManager.MainConfig.player_suffix); + playerSuffix = PapiHook.parsePlaceholders(this.player, ConfigManager.MainConfig.player_suffix); }else { playerSuffix = ""; } - }else { + } + else { if (!ConfigManager.MainConfig.hidePrefix){ playerPrefix = ConfigManager.MainConfig.player_prefix; }else { @@ -135,11 +125,6 @@ public class NameplatesTeam { this.suffixText = nameplateUtil.getSuffixLength(MiniMessage.miniMessage().stripTags(playerPrefix) + name + MiniMessage.miniMessage().stripTags(playerSuffix)); this.prefix = Component.text(nameplateUtil.makeCustomNameplate(MiniMessage.miniMessage().stripTags(playerPrefix), name, MiniMessage.miniMessage().stripTags(playerSuffix))).font(ConfigManager.MainConfig.key).append(MiniMessage.miniMessage().deserialize(playerPrefix)); this.suffix = MiniMessage.miniMessage().deserialize(playerSuffix).append(Component.text(nameplateUtil.getSuffixLength(MiniMessage.miniMessage().stripTags(playerPrefix) + name + MiniMessage.miniMessage().stripTags(playerSuffix))).font(ConfigManager.MainConfig.key)); -// this.prefixText = nameplateUtil.makeCustomNameplate(playerPrefix, name, playerSuffix) + playerPrefix; -// this.suffixText = playerSuffix + nameplateUtil.getSuffixLength(playerPrefix + name + playerSuffix); -// this.prefix = Component.text(nameplateUtil.makeCustomNameplate(playerPrefix, name, playerSuffix)).font(ConfigManager.MainConfig.key).append(Component.text(playerPrefix).font(Key.key("default"))); -// this.suffix = Component.text(playerSuffix).append(Component.text(nameplateUtil.getSuffixLength(playerPrefix + name + playerSuffix)).font(ConfigManager.MainConfig.key)); this.color = nameplateUtil.getColor(); - this.team.setPrefix(""); } } \ No newline at end of file diff --git a/src/main/java/net/momirealms/customnameplates/scoreboard/ScoreBoardManager.java b/src/main/java/net/momirealms/customnameplates/scoreboard/ScoreBoardManager.java index efaa37c..05fbe8e 100644 --- a/src/main/java/net/momirealms/customnameplates/scoreboard/ScoreBoardManager.java +++ b/src/main/java/net/momirealms/customnameplates/scoreboard/ScoreBoardManager.java @@ -35,22 +35,15 @@ public record ScoreBoardManager(CustomNameplates plugin) { if (!teams.containsKey(tabTeamName)) { teams.put(tabTeamName, new NameplatesTeam(this.plugin, player)); } - this.getTeam(tabTeamName).updateNameplates(); + teams.get(tabTeamName).updateNameplates(); return teams.get(tabTeamName); - } else { + } + else { if (!teams.containsKey(player.getName())) { teams.put(player.getName(), new NameplatesTeam(this.plugin, player)); } - this.getTeam(player.getName()).updateNameplates(); + teams.get(player.getName()).updateNameplates(); return teams.get(player.getName()); } } - - public void removeTeam(String teamName) { - teams.remove(teamName); - } - - public NameplatesTeam getTeam(String teamName) { - return teams.get(teamName); - } } diff --git a/src/main/resources/backgrounds/b0.png b/src/main/resources/backgrounds/b0.png index 45cb173..1130ab3 100644 Binary files a/src/main/resources/backgrounds/b0.png and b/src/main/resources/backgrounds/b0.png differ diff --git a/src/main/resources/backgrounds/b1.png b/src/main/resources/backgrounds/b1.png index 86ff93d..ae76d6a 100644 Binary files a/src/main/resources/backgrounds/b1.png and b/src/main/resources/backgrounds/b1.png differ diff --git a/src/main/resources/backgrounds/b128.png b/src/main/resources/backgrounds/b128.png index 9a1e971..7a4c1ad 100644 Binary files a/src/main/resources/backgrounds/b128.png and b/src/main/resources/backgrounds/b128.png differ diff --git a/src/main/resources/backgrounds/b16.png b/src/main/resources/backgrounds/b16.png index 009ec0e..83d0f34 100644 Binary files a/src/main/resources/backgrounds/b16.png and b/src/main/resources/backgrounds/b16.png differ diff --git a/src/main/resources/backgrounds/b2.png b/src/main/resources/backgrounds/b2.png index 4469ef4..9daf46c 100644 Binary files a/src/main/resources/backgrounds/b2.png and b/src/main/resources/backgrounds/b2.png differ diff --git a/src/main/resources/backgrounds/b32.png b/src/main/resources/backgrounds/b32.png index ca23db5..31ce74f 100644 Binary files a/src/main/resources/backgrounds/b32.png and b/src/main/resources/backgrounds/b32.png differ diff --git a/src/main/resources/backgrounds/b4.png b/src/main/resources/backgrounds/b4.png index f1bdd43..0532849 100644 Binary files a/src/main/resources/backgrounds/b4.png and b/src/main/resources/backgrounds/b4.png differ diff --git a/src/main/resources/backgrounds/b64.png b/src/main/resources/backgrounds/b64.png index 217322e..9188308 100644 Binary files a/src/main/resources/backgrounds/b64.png and b/src/main/resources/backgrounds/b64.png differ diff --git a/src/main/resources/backgrounds/b8.png b/src/main/resources/backgrounds/b8.png index 496b4b5..87d0de7 100644 Binary files a/src/main/resources/backgrounds/b8.png and b/src/main/resources/backgrounds/b8.png differ diff --git a/src/main/resources/bossbar.yml b/src/main/resources/bossbar.yml index 344680b..41c5591 100644 --- a/src/main/resources/bossbar.yml +++ b/src/main/resources/bossbar.yml @@ -4,12 +4,11 @@ # mode: Adventure / ProtocolLib # There's a bug that font can't be applied to Adventure bossbar. This is an Adventure API bug that I can't fix for the moment. # So I made the ProtocolLib mode and I will remove ProtocolLib mode when Adventure API is fixed! -# ProtocolLib mode doesn't support multiple bossbars for the moment. # It requires a restart to change mode mode: ProtocolLib bossbar: - example: + example_1: text: '%nameplates_bg_player%Hello! %player_name% %nameplates_bg_pos%You are now at: %player_x%, %player_y%, %player_z%' refresh-rate: 5 # PINK @@ -25,4 +24,21 @@ bossbar: # NOTCHED_10 # NOTCHED_12 # NOTCHED_20 - overlay: PROGRESS \ No newline at end of file + overlay: PROGRESS + example_2: + text: 'Another BossBar' + refresh-rate: 5 + # PINK + # YELLOW + # WHITE + # RED + # PURPLE + # GREEN + # BLUE + color: RED + # PROGRESS + # NOTCHED_6 + # NOTCHED_10 + # NOTCHED_12 + # NOTCHED_20 + overlay: NOTCHED_10 \ No newline at end of file diff --git a/src/main/resources/char-width.yml b/src/main/resources/char-width.yml new file mode 100644 index 0000000..d3b1c9e --- /dev/null +++ b/src/main/resources/char-width.yml @@ -0,0 +1,6 @@ +# Font is processed by client +# Server side doesn't know the width of you custom font +# So you can tell the plugin each character's width here +小: 8 +默: 8 +米: 8 \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c93b844..2315d04 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -56,7 +56,10 @@ config: # Placeholder based prefix and suffix system. When enabled, it is recommended # to use PlaceholderAPI to be able to use this feature to the fullest extent. # keep it empty if you don't want to enable this feature. - prefix: 'Hello! ' + # You should make sure the papi doesn't contain "&" (legacy color code) + # Nameplates work on the custom font system where legacy color code is not supported! + # Please use minimessage format: https://docs.adventure.kyori.net/minimessage/format.html + prefix: 'Hello! ' suffix: '' # should prefix/suffix be hidden when player is equipping a nameplate diff --git a/src/main/resources/custom-papi.yml b/src/main/resources/custom-papi.yml index 98fb949..7a51108 100644 --- a/src/main/resources/custom-papi.yml +++ b/src/main/resources/custom-papi.yml @@ -13,6 +13,9 @@ papi: pos: text: 'You are now at: %player_x%, %player_y%, %player_z%' background: bedrock_1 + text: + text: 'Thanks for your purchase!' + background: bedrock_2 #This papi will not return text with nameplate #It will only return the nameplate diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9a702e5..63637ef 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -12,6 +12,38 @@ softdepend: - Oraxen commands: customnameplates: - usage: /customnameplates help + usage: /customnameplates aliases: - - nameplates \ No newline at end of file + - nameplates + +permissions: + nameplates.*: + description: Gives access to all nameplates commands + children: + nameplates.generate: true + nameplates.reload: true + nameplates.forceequip: true + nameplates.forceunequip: true + nameplates.help: true + nameplates.unequip: true + nameplates.preview: true + nameplates.list: true + + nameplates.generate: + default: op + nameplates.reload: + default: op + nameplates.forceequip: + default: op + nameplates.forceunequip: + default: op + nameplates.help: + default: op + nameplates.unequip: + default: true + nameplates.preview: + default: true + nameplates.list: + default: true + nameplates.equip: + default: true \ No newline at end of file