使用 Field.set 设置 GameProfile(更改玩家名称)
Using Field.set to set a GameProfile (Changing a player's name)
我一直在想办法改变玩家的名字(他们头上的标签)。我已经尝试了很多东西,但我得到的最接近的是这段代码:
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!");
}
稍后我将介绍它打印的 Stack Trace...所以我发现人们使用的旧方法由于某些文件移动等原因不再有效。在代码中挖掘了一段时间后,我终于找到了我需要找到的东西,即 net.minecraft.server.v1_8_R3.EntityHuman 中名为 "bH" 的 GameProfile 对象。这基本上是为了更改播放器的名称而需要更改的内容。不用担心数据包,我已经准备好了。
我的问题是,"how do I change the name tag of an EntityPlayer?" 我意识到我从 Bukkit 论坛获得的代码已经过时了。 GameProfile 不再存储在 EntityPlayer 中,而是存储在 EntityHuman 中。所以,我现在的问题是,如何正确更改 GameProfile 字段,"bH"?
我将在 net.minecraft.server.v1_8_R3.EntityHuman 中向您展示一些代码;
private final GameProfile bH;
同样,这是需要更改的地方,因为 GameProfile 接受 UUID 和字符串名称。
所以,我得到的错误是:
[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.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)
[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
行特别有趣,但我不太确定如何阅读它。
我已经做了一些测试,导致此问题的确切行是 profileField.set(eh.getClass(), new GameProfile(p.getUniqueId(), newName));
,因为当我在 try/catch 中使用此特定行时,没有错误。我可以对 'fix' 这条线做什么?
注意:我无法使用 ProtocolLib/iTag/TagAPI
感谢任何帮助,谢谢!
改变
profileField.set(eh.getClass().getSuperclass, new GameProfile(p.getUniqueId(), newName));
至 profileField.set(eh, new GameProfile(p.getUniqueId(), newName));
现在有效:D
我一直在想办法改变玩家的名字(他们头上的标签)。我已经尝试了很多东西,但我得到的最接近的是这段代码:
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!");
}
稍后我将介绍它打印的 Stack Trace...所以我发现人们使用的旧方法由于某些文件移动等原因不再有效。在代码中挖掘了一段时间后,我终于找到了我需要找到的东西,即 net.minecraft.server.v1_8_R3.EntityHuman 中名为 "bH" 的 GameProfile 对象。这基本上是为了更改播放器的名称而需要更改的内容。不用担心数据包,我已经准备好了。
我的问题是,"how do I change the name tag of an EntityPlayer?" 我意识到我从 Bukkit 论坛获得的代码已经过时了。 GameProfile 不再存储在 EntityPlayer 中,而是存储在 EntityHuman 中。所以,我现在的问题是,如何正确更改 GameProfile 字段,"bH"?
我将在 net.minecraft.server.v1_8_R3.EntityHuman 中向您展示一些代码;
private final GameProfile bH;
同样,这是需要更改的地方,因为 GameProfile 接受 UUID 和字符串名称。
所以,我得到的错误是:
[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.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)
[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
行特别有趣,但我不太确定如何阅读它。
我已经做了一些测试,导致此问题的确切行是 profileField.set(eh.getClass(), new GameProfile(p.getUniqueId(), newName));
,因为当我在 try/catch 中使用此特定行时,没有错误。我可以对 'fix' 这条线做什么?
注意:我无法使用 ProtocolLib/iTag/TagAPI
感谢任何帮助,谢谢!
改变
profileField.set(eh.getClass().getSuperclass, new GameProfile(p.getUniqueId(), newName));
至 profileField.set(eh, new GameProfile(p.getUniqueId(), newName));
现在有效:D