lundi 18 juillet 2016

Using Field.set to set a GameProfile (Changing a player's name)

I've been trying to work out how to change a player's name (the tag above their head). I've tried many things, but the closest I got was with this code:

EntityHuman eh = ((CraftPlayer) p).getHandle();

PacketPlayOutEntityDestroy p29 = new PacketPlayOutEntityDestroy(new int[] {
  p.getEntityId()
});
PacketPlayOutNamedEntitySpawn p20 = new PacketPlayOutNamedEntitySpawn(eh);

try {

  Field profileField = eh.getClass().getSuperclass.getDeclaredField("bH");

  profileField.setAccessible(true);

  profileField.set(eh.getClass().getSuperclass, new GameProfile(p.getUniqueId(), newName));

} catch (Exception e) {

  e.printStackTrace();
  Bukkit.broadcastMessage("Not Work!");
}

I'll get to the Stack Trace it prints in a little bit... So I figured out that the old methods people were using wouldn't work anymore due to some file shifting around and such. After digging around in code for a while, I finally found what I needed to find, which is the GameProfile object named "bH" within net.minecraft.server.v1_8_R3.EntityHuman. This is basically what needs to be changed in order for the name of the player to be changed. Don't worry about the packets, I've got that all working.

My problem has been, "how do I change the name tag of an EntityPlayer?" and I realized that the code I got from the Bukkit forums was outdated. The GameProfile was no longer stored within EntityPlayer, but EntityHuman. So, my question now is, how do I correctly change the GameProfile field, "bH"?

I'll show you some code within net.minecraft.server.v1_8_R3.EntityHuman; private final GameProfile bH; Again, this is what needs to be changed, because a GameProfile takes in a UUID and a String, name.

So, onto the error I'm getting:

[19:10:23 WARN]: java.lang.IllegalArgumentException: Can not set final com.mojang.authlib.GameProfile field net.minecraft.server.v1_8_R3.EntityHuman.bH to java.lang.Class
[19:10:23 WARN]:        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
[19:10:23 WARN]:        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
[19:10:23 WARN]:        at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
[19:10:23 WARN]:        at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.set(Unknown Source)
[19:10:23 WARN]:        at java.lang.reflect.Field.set(Unknown Source)
[19:10:23 WARN]:        at yt.Kaelinator.commands.Rename.disguisePlayer(Rename.java:117)
[19:10:23 WARN]:        at yt.Kaelinator.commands.Rename.onCommand(Rename.java:70)
[19:10:23 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
[19:10:23 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140)
[19:10:23 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13)
[19:10:23 WARN]:        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
[19:10:23 WARN]:        at java.util.concurrent.FutureTask.run(Unknown Source)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629)
[19:10:23 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537)
[19:10:23 WARN]:        at java.lang.Thread.run(Unknown Source)

The line [19:10:23 WARN]: java.lang.IllegalArgumentException: Can not set final com.mojang.authlib.GameProfile field net.minecraft.server.v1_8_R3.EntityHuman.bH to java.lang.Class is particularly interesting but I'm not too sure how to read it.

I've done some testing, and the exact line that is causing this is profileField.set(eh.getClass(), new GameProfile(p.getUniqueId(), newName));, as when I take this specific line within the try/catch, there is no error. What can I do to this line to 'fix' this?

NOTE: I'm not able to use ProtocolLib/iTag/TagAPI

Any help is appreciated, thanks!





Aucun commentaire:

Enregistrer un commentaire