How to set Player name with Reflection?

Discussion in 'Spigot Plugin Development' started by LiveTeXon, Jul 10, 2018.

  1. Hello, i want to Change the name for a Nick Plugin. But i need help with the Refelctions in 1.12.2

    This is what i have:
    Code (Java):
        public boolean onCommand(CommandSender sender, Command command, String s, String[] strings) {

                    Player p = (Player)sender;

                Method getHandle = p.getClass().getMethod("getHandle");
                Object entityPlayer = getHandle.invoke(p);
                Field f = entityPlayer.getClass().getDeclaredField("name");

                f.set(entityPlayer, "THE_NEW_USERNAME");
            }catch(Exception e){

            return false;
  2. After setting the name, hide and show the player.

    Code (Java):
    for (Player players : Bukkit.getOnlinePlayers()) {
  3. Make sure you actually regsiter a command... everything else looks fine to me, however hi have little knowledge of reflection.

  4. I have regestier the command. But i became a Error in the Consol. (I forgot it)

    Code (Text):

    [18:44:48] [Server thread/INFO]: LiveTeXon issued server command: /test
    [18:44:48] [Server thread/WARN]: java.lang.NoSuchFieldException: name
    [18:44:48] [Server thread/WARN]:    at java.lang.Class.getDeclaredField(
    [18:44:48] [Server thread/WARN]:    at de.testplugin.commands.CMD_Test_ONE.onCommand(
    [18:44:48] [Server thread/WARN]:    at org.bukkit.command.PluginCommand.execute(
    [18:44:48] [Server thread/WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(
    [18:44:48] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PlayerConnection.a(
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$
    [18:44:48] [Server thread/WARN]:    at java.util.concurrent.Executors$
    [18:44:48] [Server thread/WARN]:    at
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.MinecraftServer.D(
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.DedicatedServer.D(
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.MinecraftServer.C(
    [18:44:48] [Server thread/WARN]:    at
    [18:44:48] [Server thread/WARN]:    at
  5. That's pretty self-explanatory, it couldn't find a field called 'name'. Have you looked at the source to see if the field exists?
  6. Okey, gives a list where i can see what fields i can use?
  7. The name field is in the GameProfile class.
  8. Okey, Now i have this:
    Code (Java):

    Player p = (Player)sender;
    GameProfile gp = new GameProfile(p.getUniqueId(), "IchBinBoss");

    But how i can set this for the user, or how i can set the Gameprofiel for the User?
  9. You don’t create a new GameProfile object.

    Cast Player to a CraftPlayer. Then get the CraftPlayer’s GameProfile.

    Then get the name Field from GameProfile.class
  10. Code (Java):
    package de.testplugin.commands;

    import com.mojang.authlib.GameProfile;
    import de.testplugin.main.Main;
    import de.testplugin.main.Skin;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;

    public class CMD_Test_ONE implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {

            if(sender instanceof Player) {
                if(args.length == 1) {
                    String name = args[0];

                    Player p = (Player)sender;
                    GameProfile gp = ((CraftPlayer)p).getProfile();
                    gp.getProperties().put("name", );

                    Skin skin = new Skin(Main.main.getUUID(name));

                    if(skin.getSkinName() != null) {
                        gp.getProperties().put(skin.getSkinName(), new Property(skin.getSkinName(), skin.getSkinValue(), skin.getSkinSignatur()));

                    for(Player all : Bukkit.getOnlinePlayers()) {


            return false;
    How i can put the name in gp.getProperties().put("name", ); ?
  11. Use protocollib & resend player info
    Afaik changing the playername is quite annoying to do, might wanna consider an hologram approach & hide the player Name.

    Also, Name cannot be longer then 16 chars.
  12. Yes i know with the langth, but i dont want to use protocollib or any other externel API