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):
        @Override
        public boolean onCommand(CommandSender sender, Command command, String s, String[] strings) {



                    Player p = (Player)sender;


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

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




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

    Code (Java):
    for (Player players : Bukkit.getOnlinePlayers()) {
                    players.hidePlayer(player);
                    players.showPlayer(player);
                }
     
  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(Class.java:2070)
    [18:44:48] [Server thread/WARN]:    at de.testplugin.commands.CMD_Test_ONE.onCommand(CMD_Test_ONE.java:28)
    [18:44:48] [Server thread/WARN]:    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [18:44:48] [Server thread/WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [18:44:48] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13)
    [18:44:48] [Server thread/WARN]:    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    [18:44:48] [Server thread/WARN]:    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    [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(MinecraftServer.java:748)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
    [18:44:48] [Server thread/WARN]:    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
    [18:44:48] [Server thread/WARN]:    at java.lang.Thread.run(Thread.java:745)
     
     
  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 com.mojang.authlib.properties.Property;
    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 {

        @Override
        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().clear();
                   
                    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()) {
                            all.hidePlayer((Player)sender);
                            all.showPlayer((Player)sender);
                    }

                }
            }



            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