From 7e82f19961bd00e2c415939cf5b3496dcb2bf9c6 Mon Sep 17 00:00:00 2001 From: SamB440 Date: Tue, 22 Oct 2024 18:15:10 +0100 Subject: [PATCH] Correctly handle reEncodeByDefault = false by last commit --- .../checks/impl/groundspoof/NoFallA.java | 15 ++++-- ...perNull.java => PacketHidePlayerInfo.java} | 49 ++++++------------- .../packets/PacketSelfMetadataListener.java | 2 + .../manager/init/start/PacketManager.java | 2 +- 4 files changed, 30 insertions(+), 38 deletions(-) rename src/main/java/ac/grim/grimac/events/packets/{PacketSetWrapperNull.java => PacketHidePlayerInfo.java} (72%) diff --git a/src/main/java/ac/grim/grimac/checks/impl/groundspoof/NoFallA.java b/src/main/java/ac/grim/grimac/checks/impl/groundspoof/NoFallA.java index 68777e3135..12bc91e689 100644 --- a/src/main/java/ac/grim/grimac/checks/impl/groundspoof/NoFallA.java +++ b/src/main/java/ac/grim/grimac/checks/impl/groundspoof/NoFallA.java @@ -43,7 +43,10 @@ public void onPacketReceive(PacketReceiveEvent event) { if (!isNearGround(wrapper.isOnGround())) { // If player isn't near ground // 1.8 boats have a mind on their own... only flag if they're not near a boat or are on 1.9+ if (!GhostBlockDetector.isGhostBlock(player) && flagWithSetback()) alert(""); - if (shouldModifyPackets()) wrapper.setOnGround(false); + if (shouldModifyPackets()) { + wrapper.setOnGround(false); + event.markForReEncode(true); + } } } } @@ -59,10 +62,16 @@ public void onPacketReceive(PacketReceiveEvent event) { // Also flip teleports because I don't trust vanilla's handling of teleports and ground if (flipPlayerGroundStatus) { flipPlayerGroundStatus = false; - if (shouldModifyPackets()) wrapper.setOnGround(!wrapper.isOnGround()); + if (shouldModifyPackets()) { + wrapper.setOnGround(!wrapper.isOnGround()); + event.markForReEncode(true); + } } if (player.packetStateData.lastPacketWasTeleport) { - if (shouldModifyPackets()) wrapper.setOnGround(false); + if (shouldModifyPackets()) { + wrapper.setOnGround(false); + event.markForReEncode(true); + } } } } diff --git a/src/main/java/ac/grim/grimac/events/packets/PacketSetWrapperNull.java b/src/main/java/ac/grim/grimac/events/packets/PacketHidePlayerInfo.java similarity index 72% rename from src/main/java/ac/grim/grimac/events/packets/PacketSetWrapperNull.java rename to src/main/java/ac/grim/grimac/events/packets/PacketHidePlayerInfo.java index ae5573e798..9a2711121c 100644 --- a/src/main/java/ac/grim/grimac/events/packets/PacketSetWrapperNull.java +++ b/src/main/java/ac/grim/grimac/events/packets/PacketHidePlayerInfo.java @@ -5,14 +5,11 @@ import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.event.PacketListenerAbstract; import com.github.retrooper.packetevents.event.PacketListenerPriority; -import com.github.retrooper.packetevents.event.PacketReceiveEvent; import com.github.retrooper.packetevents.event.PacketSendEvent; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.packettype.PacketType; -import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon; import com.github.retrooper.packetevents.protocol.player.GameMode; -import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerFlying; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata; +import com.github.retrooper.packetevents.protocol.player.UserProfile; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate; @@ -20,22 +17,15 @@ import java.util.EnumSet; import java.util.List; -public class PacketSetWrapperNull extends PacketListenerAbstract { - // It's faster (and less buggy) to simply not re-encode the wrapper unless we changed something - // The two packets we change are clientbound entity metadata (to fix a netcode issue) - // and the serverbound player flying packets (to patch NoFall) - public PacketSetWrapperNull() { +public class PacketHidePlayerInfo extends PacketListenerAbstract { + + public PacketHidePlayerInfo() { super(PacketListenerPriority.HIGHEST); } @Override public void onPacketSend(PacketSendEvent event) { - if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) { - WrapperPlayServerEntityMetadata wrapper = new WrapperPlayServerEntityMetadata(event); - if (wrapper.getEntityId() != event.getUser().getEntityId()) { - event.setLastUsedWrapper(null); - } - } else if (event.getPacketType() == PacketType.Play.Server.PLAYER_INFO) { + if (event.getPacketType() == PacketType.Play.Server.PLAYER_INFO) { //iterate through players and fake their game mode if they are spectating via grim spectate if (PacketEvents.getAPI().getServerManager().getVersion().isOlderThanOrEquals(ServerVersion.V_1_12_2)) return; @@ -62,8 +52,8 @@ public void onPacketSend(PacketSendEvent event) { //if amount of hidden players is the amount of players updated & is an update game mode action just cancel it if (hideCount == nmsPlayerInfoDataList.size() && info.getAction() == WrapperPlayServerPlayerInfo.Action.UPDATE_GAME_MODE) { event.setCancelled(true); - } else if (hideCount <= 0) { - event.setLastUsedWrapper(null); + } else if (hideCount > 0) { + event.markForReEncode(true); } } } else if (event.getPacketType() == PacketType.Play.Server.PLAYER_INFO_UPDATE) { @@ -81,12 +71,13 @@ public void onPacketSend(PacketSendEvent event) { for (WrapperPlayServerPlayerInfoUpdate.PlayerInfo entry : wrapper.getEntries()) { //check if the player should be hidden WrapperPlayServerPlayerInfoUpdate.PlayerInfo modifiedPacket = null; - if (GrimAPI.INSTANCE.getSpectateManager().shouldHidePlayer(receiver, entry.getProfileId())) { + final UserProfile gameProfile = entry.getGameProfile(); + if (GrimAPI.INSTANCE.getSpectateManager().shouldHidePlayer(receiver, gameProfile.getUUID())) { hideCount++; //modify & create a new packet from pre-existing one if they are a spectator if (entry.getGameMode() == GameMode.SPECTATOR) { modifiedPacket = new WrapperPlayServerPlayerInfoUpdate.PlayerInfo( - entry.getGameProfile(), + gameProfile, entry.isListed(), entry.getLatency(), GameMode.SURVIVAL, @@ -104,30 +95,20 @@ public void onPacketSend(PacketSendEvent event) { } //if only the game mode was updated and the packet was modified, don't send anything } - //if no hidden players, don't modify packet - if (hideCount <= 0) { - event.setLastUsedWrapper(null); - } else if (hideCount == modified.size()) { //if the amount of hidden players & modified entries are the same + + // if the amount of hidden players & modified entries are the same + if (hideCount == modified.size()) { if (onlyGameMode) { // if only the game mode changed, cancel event.setCancelled(true); } else { //if more than the game mode changed, remove the action wrapper.getActions().remove(WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_GAME_MODE); + event.markForReEncode(true); } } else { //modify entries wrapper.setEntries(modified); + event.markForReEncode(true); } } - - } else if (event.getPacketType() != PacketType.Play.Server.PLAYER_POSITION_AND_LOOK) { - event.setLastUsedWrapper(null); - } - } - - @Override - public void onPacketReceive(PacketReceiveEvent event) { - PacketTypeCommon packetType = event.getPacketType(); - if (!WrapperPlayClientPlayerFlying.isFlying(packetType) && packetType != PacketType.Play.Client.CLIENT_SETTINGS && !event.isCancelled()) { - event.setLastUsedWrapper(null); } } } diff --git a/src/main/java/ac/grim/grimac/events/packets/PacketSelfMetadataListener.java b/src/main/java/ac/grim/grimac/events/packets/PacketSelfMetadataListener.java index bc62f3fdc0..88f832a055 100644 --- a/src/main/java/ac/grim/grimac/events/packets/PacketSelfMetadataListener.java +++ b/src/main/java/ac/grim/grimac/events/packets/PacketSelfMetadataListener.java @@ -24,6 +24,7 @@ import java.util.Optional; public class PacketSelfMetadataListener extends PacketListenerAbstract { + public PacketSelfMetadataListener() { super(PacketListenerPriority.HIGH); } @@ -68,6 +69,7 @@ public void onPacketSend(PacketSendEvent event) { // Remove the pose metadata from the list metadataStuff.removeIf(element -> element.getIndex() == 6); entityMetadata.setEntityMetadata(metadataStuff); + event.markForReEncode(true); } EntityData watchable = WatchableIndexUtil.getIndex(entityMetadata.getEntityMetadata(), 0); diff --git a/src/main/java/ac/grim/grimac/manager/init/start/PacketManager.java b/src/main/java/ac/grim/grimac/manager/init/start/PacketManager.java index e5f9aae25e..4b9d2a131a 100644 --- a/src/main/java/ac/grim/grimac/manager/init/start/PacketManager.java +++ b/src/main/java/ac/grim/grimac/manager/init/start/PacketManager.java @@ -43,7 +43,7 @@ public void start() { } PacketEvents.getAPI().getEventManager().registerListener(new ProxyAlertMessenger()); - PacketEvents.getAPI().getEventManager().registerListener(new PacketSetWrapperNull()); + PacketEvents.getAPI().getEventManager().registerListener(new PacketHidePlayerInfo()); PacketEvents.getAPI().init(); }