So i’ve been searching for the past year and out of all the solutions to change the nametag of a player 2 have been standing out one is using reflection and another is just using protocollib.
The problem im facing is that i cant get the nametag right just as i want it!
ProtocolLib Solution
public static void changePlayerNameTag(Player player, String newName) {
// Update nametag for other players
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(EssentialsNicks.getPlugin(), PacketType.Play.Server.PLAYER_INFO) {
@Override
public void onPacketSending(PacketEvent event) {
if (event.getPacket().getPlaye`your text`rInfoAction().read(0) != EnumWrappers.PlayerInfoAction.ADD_PLAYER) return;
List<PlayerInfoData> newPlayerInfoDataList = new ArrayList<PlayerInfoData>();
List<PlayerInfoData> playerInfoDataList = event.getPacket().getPlayerInfoDataLists().read(0);
for (PlayerInfoData playerInfoData : playerInfoDataList) {
if (playerInfoData == null || playerInfoData.getProfile() == null || Bukkit.getPlayer(playerInfoData.getProfile().getUUID()) == null) { //Unknown Player
newPlayerInfoDataList.add(playerInfoData);
continue;
}
// Check if the player's UUID matches the specific player's UUID
if (!playerInfoData.getProfile().getUUID().equals(player.getUniqueId())) {
newPlayerInfoDataList.add(playerInfoData);
continue;
}
WrappedGameProfile profile = playerInfoData.getProfile();
// Create a new profile with the desired name and the original profile's properties (including the skin)
WrappedGameProfile newProfile = new WrappedGameProfile(profile.getUUID(), ChatColor.translateAlternateColorCodes('&', newName));
newProfile.getProperties().putAll(profile.getProperties());
PlayerInfoData newPlayerInfoData = new PlayerInfoData(newProfile, playerInfoData.getLatency(), playerInfoData.getGameMode(), playerInfoData.getDisplayName());
newPlayerInfoDataList.add(newPlayerInfoData);
}
event.getPacket().getPlayerInfoDataLists().write(0, newPlayerInfoDataList);
}
});
Bukkit.getOnlinePlayers().forEach(onlinePlayer -> {
protocolManager.sendServerPacket(onlinePlayer, packet);
onlinePlayer.hidePlayer(player);
onlinePlayer.showPlayer(player);
});
}
Reflection
public static void changeName(String name, Player player) {
try {
Method getHandle = player.getClass().getMethod("getHandle");
Object entityPlayer = getHandle.invoke(player);
Class<?> entityHuman = entityPlayer.getClass().getSuperclass();
Field gameProfileField;
int majVersion = Integer.parseInt(Bukkit.getServer().getClass().getPackage().getName().split("\.")[3].replaceAll("(v|R[0-9]+)", "").split("_")[0]);
int minVersion = Integer.parseInt(Bukkit.getServer().getClass().getPackage().getName().split("\.")[3].replaceAll("(v|R[0-9]+)", "").split("_")[1]);
if (majVersion >= 1 && minVersion >= 9) {
gameProfileField = entityHuman.getDeclaredField("bS");
} else {
gameProfileField = entityHuman.getDeclaredField("bH");
}
gameProfileField.setAccessible(true);
gameProfileField.set(entityPlayer, new GameProfile(player.getUniqueId(), name));
for (Player players : Bukkit.getOnlinePlayers()) {
players.hidePlayer(player);
players.showPlayer(player);
}
} catch (NoSuchMethodException | SecurityException | InvocationTargetException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
Each own has its own problem and im tryna just have the best of both worlds kinda like hypixel
Common Problems
Player’s own nametag doesn’t change (Client Side)
ProtocolLib’s Problems
Changed nametag doesn’t work with teams
Reflection’s Problems
For Reflection it changes your very own minecraft name (obviously only on the server) the problem im facing with Reflection is that its gonna make minecraft and all of the other plugins commands unusuable also you can’t even tab to get your or the changed name.
Honestly if i can just fix the problem with protocolLib nametag that doesn’t work with teams i would be happy but i kinda want the player’s own nametag to change also ????.
If anybody knows something i don’t please i beg for your help!
ludgerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.