在 Minecraft 中使用 NMS 更改玩家皮肤(Bukkit/Spigot)

Red*_*mpt 5 java minecraft bukkit

我目前正在开发一个插件,让您可以假设另一个玩家的身份。它几乎完美地做到了这一点:您的 UUID 和用户名已更改为您在服务器端假定其身份的用户的用户名,并且据服务器和插件所知,您似乎就是该玩家。您将拥有与他们相同的等级、相同的权限以及一切。我一直无法得到的一件事是皮肤。我曾想过当 UUID 是其他玩家时,玩家的皮肤会改变,但事实并非如此。我正在使用反射来更改 GameProfile 和 EntityPlayer 中的 UUID(uniqueID 字段继承自 Entity),并且所有获取玩家 UUID 的方法都返回插件设置的 UUID。我挖掘了反编译的 NMS 和 Bukkit/Spigot 论坛,但所有这些似乎都表明皮肤应该随着 UUID 改变。我将 PlayerQuitEvent 和 PlayerJoinEvent 发送到插件以模拟真实玩家离开和假定玩家加入,并向所有玩家发送数据包以从选项卡和游戏中删除旧玩家,然后添加新玩家。如果可以避免,我宁愿不使用 ProtocolLib。任何帮助将不胜感激,有人能指出我正确的方向吗?

提前致谢!

Red*_*mpt 7

我自己想通了。事实证明,一个 GameProfile 包含一个皮肤纹理。必须从 Mojang 会话服务器请求此纹理。这是代码:

public static boolean setSkin(GameProfile profile, UUID uuid) {
    try {
        HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false", UUIDTypeAdapter.fromUUID(uuid))).openConnection();
        if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
            String reply = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
            String skin = reply.split("\"value\":\"")[1].split("\"")[0];
            String signature = reply.split("\"signature\":\"")[1].split("\"")[0];
            profile.getProperties().put("textures", new Property("textures", skin, signature));
            return true;
        } else {
            System.out.println("Connection could not be opened (Response code " + connection.getResponseCode() + ", " + connection.getResponseMessage() + ")");
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)