我一直在尝试找出如何更改玩家的名字(他们头顶的标签)。我尝试了很多事情,但是最接近的代码是:
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对象。基本上,这是为了更改播放器名称而需要更改的内容。不用担心数据包,我已经全部工作了。
我的问题一直是:“如何更改EntityPlayer的名称标签?”我意识到从Bukkit论坛获得的代码已经过时了。 GameProfile不再存储在EntityPlayer中,而是存储在EntityHuman中。因此,我现在的问题是,如何正确更改GameProfile字段“ bH”?
我将在net.minecraft.server.v1_8_R3.EntityHuman中向您展示一些代码;
private final GameProfile bH;
同样,这是需要更改的内容,因为GameProfile接受UUID和String名称。所以,我得到的错误:
[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)
[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中使用此特定行时,没有错误。我如何在此行上“修复”此问题?注意:我无法使用ProtocolLib / iTag / TagAPI
任何帮助表示赞赏,谢谢!
最佳答案
更改profileField.set(eh.getClass().getSuperclass, new GameProfile(p.getUniqueId(), newName));
至profileField.set(eh, new GameProfile(p.getUniqueId(), newName));
现在可以工作了:D
关于java - 使用Field.set设置GameProfile(更改玩家名称),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38416728/