修复因接收聊天消息导致的 Minecraft 客户端卡顿
在 1.18 中你不再需要此模组
Mojang 已修复了其 API 中的错误,该错误原本会导致返回错误而非空列表。Minecraft 将在加入世界时获取一次屏蔽列表。由于发起 HTTP 请求需要时间,此模组仍可能通过节省这段时间来改善世界加载速度;尤其是在你的网络连接较慢的情况下。
Mojang 员工的这一评论证实了该错误已被修复:
此错误仍然影响 1.17 和 1.16,但仅影响首次接收到的聊天消息:
技术细节:在接收聊天消息时,Minecraft 会在渲染线程中发送一个 HTTP 请求以检查你的玩家屏蔽列表。在该请求完成之前,当前帧无法完成渲染,从而导致卡顿。此模组允许聊天消息直接通过,直到可以获取屏蔽列表为止。
为什么现在才发生,而以前没有?:Mojang 的 API 发生了某些变化。加入世界时,屏蔽列表通常能正常加载,但 Mojang 的 API 开始为已迁移至 Microsoft 账户的用户返回错误,导致在接收消息时每隔 2 分钟获取一次屏蔽列表。
关于错误发生原因的带注释 walkthrough
// 代码的简化视图。
private static final long BLOCKLIST_REQUEST_COOLDOWN = 120;
private Instant nextRequest;
private final blockList Set<UUID>;
// 此方法在接收到聊天消息时被调用
public boolean isBlockedPlayer(UUID playerID) {
// 如果我们还没有屏蔽列表,则获取它
// 注意:当出现错误时,fetchBlockList 返回 null
//
if (this.blockList == null) {
this.blockList = fetchBlockList();
// 如果仍然没有获取到,假设玩家未被屏蔽
if (this.blockList == null) {
return false;
}
}
return this.blockList.contains(playerID);
}
public Set<UUID> fetchBlockList() {
// 至少每 2 分钟检查一次。
// 这就是卡顿只在每 2 分钟或更长时间后才发生的原因
if (this.nextRequest == null || Instant.now().isAfter(this.nextRequest)) {
return null;
}
// 重置 2 分钟计时器
this.nextRequest = Instant.now().plusSeconds(BLOCKLIST_REQUEST_COOLDOWN);
try {
// 发起 HTTP 请求
BlockListResponse response = minecraftClient.get(routeBlocklist, BlockListResponse.class);
return response.getBlockedProfiles();
} catch (/* exception */) {
// 如果出现错误则返回 null
return null;
}
}
如果你想亲自查看未经修改的代码,请查看 com.mojang.authlib.yggdrasil.YggdrasilUserApiService,请记住这些方法是在渲染线程上调用的,因此会阻塞帧的完成。
GIF 演示该错误
Fixes Minecraft client lag from receiving chat messages
YOU DO NOT NEED THIS MOD ANYMORE ON 1.18
Mojang has fixed the bug in their API that was causing it to return an error instead of an empty list. Minecraft will fetch the block-list once when joining the world. This mod may still improve world loading times by the time it takes to make that HTTP request; especially if you have a slow internet connection.
This comment by a Mojang employee confirms that the bug was fixed:
This bug still affects 1.17 and 1.16 but only on the first chat message received:
Technical Details: When receiving a chat message, Minecraft sends a HTTP request in the render thread to check your player block list. The current frame does not finish rendering until this request has finished, causing a lag spike. This mod lets chat messages through until a block-list can be fetched.
Why is this happening now and not before?: something happened to Mojang's API. When joining a world, the blocklist would usually load fine, but Mojang's API has started returning errors for users who have migrated to Microsoft accounts, causing the block list to be fetched in 2 minute intervals when receiving a message.
A commented walkthrough of why the bug occurs
// A simplified view of the code.
private static final long BLOCKLIST_REQUEST_COOLDOWN = 120;
private Instant nextRequest;
private final blockList Set<UUID>;
// This method is called when receiving a chat message
public boolean isBlockedPlayer(UUID playerID) {
// If we don't have the blocklist yet, fetch it
// Note that when there's an error, fetchBlockList returns null
//
if (this.blockList == null) {
this.blockList = fetchBlockList();
// If we still don't have it, assume the player is not blocked
if (this.blockList == null) {
return false;
}
}
return this.blockList.contains(playerID);
}
public Set<UUID> fetchBlockList() {
// Only check at least every 2 minutes.
// This is why the lagspike only occurs every 2 minutes or later
if (this.nextRequest == null || Instant.now().isAfter(this.nextRequest)) {
return null;
}
// Reset the 2 minute timer
this.nextRequest = Instant.now().plusSeconds(BLOCKLIST_REQUEST_COOLDOWN);
try {
// Make the HTTP request
BlockListResponse response = minecraftClient.get(routeBlocklist, BlockListResponse.class);
return response.getBlockedProfiles();
} catch (/* exception */) {
// If there's an error return null
return null;
}
}
If you'd like to see the unmodified for yourself, check out com.mojang.authlib.yggdrasil.YggdrasilUserApiService, remember that these methods get called on the render thread, thus blocking the frame from finishing.
GIF demonstration of the bug
评论 0
登录后参与评分
还没有评论,来抢沙发吧