From 9d9b3c4349f1b39bdf3efdadf88bb74e349a6908 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 15 Oct 2023 21:57:13 +0800 Subject: [PATCH] Micro optimisations --- .../net/citizensnpcs/ProtocolLibListener.java | 21 ++++++++++++------- .../nms/v1_19_R3/util/NMSImpl.java | 13 +++++++++++- .../nms/v1_20_R2/util/NMSImpl.java | 12 ++++++++++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/ProtocolLibListener.java b/main/src/main/java/net/citizensnpcs/ProtocolLibListener.java index 35df97632..39ca5d48c 100644 --- a/main/src/main/java/net/citizensnpcs/ProtocolLibListener.java +++ b/main/src/main/java/net/citizensnpcs/ProtocolLibListener.java @@ -131,6 +131,9 @@ public class ProtocolLibListener implements Listener { return; boolean changed = false; + GameProfile playerProfile = null; + WrappedGameProfile wgp = null; + WrappedChatComponent playerName = null; for (int i = 0; i < list.size(); i++) { PlayerInfoData npcInfo = list.get(i); if (npcInfo == null) @@ -139,19 +142,22 @@ public class ProtocolLibListener implements Listener { if (trait == null || !trait.isMirroring(event.getPlayer())) continue; - GameProfile playerProfile = NMS.getProfile(event.getPlayer()); + if (playerProfile == null) { + playerProfile = NMS.getProfile(event.getPlayer()); + wgp = WrappedGameProfile.fromPlayer(event.getPlayer()); + playerName = WrappedChatComponent.fromText(event.getPlayer().getDisplayName()); + } + if (trait.mirrorName()) { - list.set(i, - new PlayerInfoData( - WrappedGameProfile.fromPlayer(event.getPlayer()) - .withId(npcInfo.getProfile().getId()), - npcInfo.getLatency(), npcInfo.getGameMode(), - WrappedChatComponent.fromText(event.getPlayer().getDisplayName()))); + list.set(i, new PlayerInfoData(wgp.withId(npcInfo.getProfile().getId()), npcInfo.getLatency(), + npcInfo.getGameMode(), playerName)); continue; } + Collection textures = playerProfile.getProperties().get("textures"); if (textures == null || textures.size() == 0) continue; + npcInfo.getProfile().getProperties().clear(); for (String key : playerProfile.getProperties().keySet()) { npcInfo.getProfile().getProperties().putAll(key, @@ -162,6 +168,7 @@ public class ProtocolLibListener implements Listener { } changed = true; } + if (changed) { event.getPacket().getPlayerInfoDataLists().write(0, list); } diff --git a/v1_19_R3/src/main/java/net/citizensnpcs/nms/v1_19_R3/util/NMSImpl.java b/v1_19_R3/src/main/java/net/citizensnpcs/nms/v1_19_R3/util/NMSImpl.java index 3c895dee6..4bb307f2c 100644 --- a/v1_19_R3/src/main/java/net/citizensnpcs/nms/v1_19_R3/util/NMSImpl.java +++ b/v1_19_R3/src/main/java/net/citizensnpcs/nms/v1_19_R3/util/NMSImpl.java @@ -1179,14 +1179,17 @@ public class NMSImpl implements NMSBridge { ClientboundPlayerInfoUpdatePacket packet = (ClientboundPlayerInfoUpdatePacket) raw; List list = Lists.newArrayList(packet.entries()); boolean changed = false; + GameProfile playerProfile = null; for (int i = 0; i < list.size(); i++) { ClientboundPlayerInfoUpdatePacket.Entry npcInfo = list.get(i); if (npcInfo == null) continue; + MirrorTrait trait = mirrorTraits.apply(npcInfo.profileId()); if (trait == null || !trait.isMirroring(player)) { continue; } + if (Setting.DISABLE_TABLIST.asBoolean() != npcInfo.listed()) { list.set(i, new ClientboundPlayerInfoUpdatePacket.Entry(npcInfo.profileId(), npcInfo.profile(), @@ -1195,7 +1198,11 @@ public class NMSImpl implements NMSBridge { npcInfo.chatSession())); changed = true; } - GameProfile playerProfile = NMS.getProfile(player); + + if (playerProfile == null) { + playerProfile = NMS.getProfile(player); + } + if (trait.mirrorName()) { list.set(i, new ClientboundPlayerInfoUpdatePacket.Entry(npcInfo.profileId(), playerProfile, @@ -1204,15 +1211,19 @@ public class NMSImpl implements NMSBridge { changed = true; continue; } + Collection textures = playerProfile.getProperties().get("textures"); if (textures == null || textures.size() == 0) continue; + npcInfo.profile().getProperties().clear(); for (String key : playerProfile.getProperties().keySet()) { npcInfo.profile().getProperties().putAll(key, playerProfile.getProperties().get(key)); } + changed = true; } + if (changed) { try { PLAYER_INFO_ENTRIES_LIST.invoke(packet, list); diff --git a/v1_20_R2/src/main/java/net/citizensnpcs/nms/v1_20_R2/util/NMSImpl.java b/v1_20_R2/src/main/java/net/citizensnpcs/nms/v1_20_R2/util/NMSImpl.java index 9e6bc27e8..615df781b 100644 --- a/v1_20_R2/src/main/java/net/citizensnpcs/nms/v1_20_R2/util/NMSImpl.java +++ b/v1_20_R2/src/main/java/net/citizensnpcs/nms/v1_20_R2/util/NMSImpl.java @@ -1155,6 +1155,7 @@ public class NMSImpl implements NMSBridge { ClientboundPlayerInfoUpdatePacket packet = (ClientboundPlayerInfoUpdatePacket) raw; List list = Lists.newArrayList(packet.entries()); boolean changed = false; + GameProfile playerProfile = null; for (int i = 0; i < list.size(); i++) { ClientboundPlayerInfoUpdatePacket.Entry npcInfo = list.get(i); if (npcInfo == null) @@ -1164,6 +1165,7 @@ public class NMSImpl implements NMSBridge { if (trait == null || !trait.isMirroring(player)) { continue; } + if (Setting.DISABLE_TABLIST.asBoolean() != npcInfo.listed()) { list.set(i, new ClientboundPlayerInfoUpdatePacket.Entry(npcInfo.profileId(), npcInfo.profile(), @@ -1172,7 +1174,11 @@ public class NMSImpl implements NMSBridge { npcInfo.chatSession())); changed = true; } - GameProfile playerProfile = NMS.getProfile(player); + + if (playerProfile == null) { + playerProfile = NMS.getProfile(player); + } + if (trait.mirrorName()) { list.set(i, new ClientboundPlayerInfoUpdatePacket.Entry(npcInfo.profileId(), playerProfile, @@ -1181,15 +1187,19 @@ public class NMSImpl implements NMSBridge { changed = true; continue; } + Collection textures = playerProfile.getProperties().get("textures"); if (textures == null || textures.size() == 0) continue; + npcInfo.profile().getProperties().clear(); for (String key : playerProfile.getProperties().keySet()) { npcInfo.profile().getProperties().putAll(key, playerProfile.getProperties().get(key)); } + changed = true; } + if (changed) { try { PLAYER_INFO_ENTRIES_LIST.invoke(packet, list);