Search code examples
minecraftbukkit

What´s the specific function to fully change a players name?


I´ve been looking around the Internet for a week trying to discover a useable function that I can use to change a players name in my Plugin, and since most information is waaaaay to old, I was unsuccessful to find anything. This is what I have tried already:

 player.setCustomName(args[0]);
 player.setDisplayName(args[0]);
 player.setPlayerListName(args[0]);
 getConfig().set(player.getName(),args[0]);
 

Its not like I receive a Error or something, its just that not much happens to the player names (but the function is actually called, I checked).


Solution

  • Simply, you cannot change your full name that you entered in the client settings.

    These methods you are trying to use will only change the name of the in-game server for the player object. These also affect the names of the chat, tablist and possibly scoreboard teams. There is a solution that can be used to change the player name for a given player in half, but it will not affect other servers or the client either. Using GameProfile from Mojang, you can change its name and UUID, but this requires creating a new instance and adding it to the existing PlayerInfoData list. If you don't have Spigot/Paper or some software attached to your project, you'll need to use Java Reflections to modify the values/list and everything else, especially in PacketPlayOutPlayerInfo. Or, if you want to avoid Java Reflections, you can use the PlayerProfile interface implemented by Paper, using the Player#getPlayerProfile method.

    An example code using Reflections:

    Player player = ...; // Your player object here
    GameProfile gameProfile = new GameProfile(player.getUniqueId(), "newPlayerNameHere");
    
    // packet is the new instance of PacketPlayOutPlayerInfo
    // infoList is the list retrieved from PacketPlayOutPlayerInfo
    // playerInfoDataConstr is the PacketPlayOutPlayerInfo constructor
    // ping is the amount of ping the player have currently
    // gameMode is the EnumGameMode object of the player
    // text is the text parsed to IChatBaseComponent
    
    ((List<Object>) infoList.get(packet)).add(playerInfoDataConstr.newInstance(packet, gameProfile, ping, gameMode, getAsIChatBaseComponent(text)));
    
    // Send the packet object to every online player on the server
    

    For accessing to GameProfile class you'll need com.mojang.authlib dependency.

    Using Paper API is kinda easy to implement.