From c80be71f422012bb9e52ed21359f1587d4fc59ae Mon Sep 17 00:00:00 2001 From: Bram Date: Sun, 22 Sep 2024 19:12:56 +0200 Subject: [PATCH] Improved: User login handling (hopefully fixes an issue with the client version being null) --- .../totemguard/data/TotemPlayer.java | 31 +++++++++---------- .../packetlisteners/UserTracker.java | 23 +++++--------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/deathmotion/totemguard/data/TotemPlayer.java b/src/main/java/com/deathmotion/totemguard/data/TotemPlayer.java index a91bf13..cb69ff0 100644 --- a/src/main/java/com/deathmotion/totemguard/data/TotemPlayer.java +++ b/src/main/java/com/deathmotion/totemguard/data/TotemPlayer.java @@ -23,22 +23,21 @@ import java.util.UUID; -public record TotemPlayer(UUID uuid, String username, ClientVersion clientVersion, boolean isBedrockPlayer, - String clientBrand, TotemData totemData) { - // Primary constructor - public TotemPlayer(UUID uuid, String username, ClientVersion clientVersion, boolean isBedrockPlayer) { - this(uuid, username, clientVersion, isBedrockPlayer, null, new TotemData()); - } - - // Full constructor - public TotemPlayer(UUID uuid, String username, ClientVersion clientVersion, boolean isBedrockPlayer, String clientBrand) { +public record TotemPlayer( + UUID uuid, + String username, + ClientVersion clientVersion, + boolean isBedrockPlayer, + String clientBrand, + TotemData totemData +) { + public TotemPlayer( + UUID uuid, + String username, + ClientVersion clientVersion, + boolean isBedrockPlayer, + String clientBrand + ) { this(uuid, username, clientVersion, isBedrockPlayer, clientBrand, new TotemData()); } - - // Constructor to update client brand - public TotemPlayer withClientBrand(String clientBrand) { - return new TotemPlayer(this.uuid, this.username, this.clientVersion, this.isBedrockPlayer, clientBrand, this.totemData); - } } - - diff --git a/src/main/java/com/deathmotion/totemguard/packetlisteners/UserTracker.java b/src/main/java/com/deathmotion/totemguard/packetlisteners/UserTracker.java index b29f48f..64689f1 100644 --- a/src/main/java/com/deathmotion/totemguard/packetlisteners/UserTracker.java +++ b/src/main/java/com/deathmotion/totemguard/packetlisteners/UserTracker.java @@ -59,15 +59,10 @@ public void onUserLogin(UserLoginEvent event) { plugin.getAlertManager().enableAlerts(player); } - // Create a new or update the existing TotemPlayer with full details + // Compute the TotemPlayer based on userUUID, updating or creating as necessary TotemPlayer totemPlayer = totemPlayers.compute(userUUID, (uuid, existing) -> { - if (existing == null) { - // `onPacketReceive` was never called, create a new TotemPlayer with brand "Unknown" - return new TotemPlayer(userUUID, user.getName(), user.getClientVersion(), userUUID.getMostSignificantBits() == 0L, "Unknown"); - } else { - // Update the existing TotemPlayer with remaining fields - return new TotemPlayer(userUUID, user.getName(), user.getClientVersion(), userUUID.getMostSignificantBits() == 0L, existing.clientBrand()); - } + String clientBrand = existing != null ? existing.clientBrand() : "Unknown"; + return new TotemPlayer(userUUID, user.getName(), user.getClientVersion(), userUUID.getMostSignificantBits() == 0L, clientBrand); }); announceClientBrand(player.getName(), totemPlayer.clientBrand()); @@ -77,8 +72,7 @@ public void onUserLogin(UserLoginEvent event) { @Override public void onPacketReceive(PacketReceiveEvent event) { - if (event.getPacketType() != PacketType.Play.Client.PLUGIN_MESSAGE - && event.getPacketType() != PacketType.Configuration.Client.PLUGIN_MESSAGE) { + if (event.getPacketType() != PacketType.Play.Client.PLUGIN_MESSAGE && event.getPacketType() != PacketType.Configuration.Client.PLUGIN_MESSAGE) { return; } @@ -89,16 +83,15 @@ public void onPacketReceive(PacketReceiveEvent event) { byte[] data = packet.getData(); if (data.length == 0 || data.length > 64) return; + String brand = new String(data, 1, data.length - 1).replace(" (Velocity)", ""); if (brand.isEmpty()) brand = "Unknown"; - UUID userUUID = event.getUser().getUUID(); - if (userUUID == null) return; - - totemPlayers.put(userUUID, new TotemPlayer(userUUID, null, null, false, brand)); + User user = event.getUser(); + UUID userUUID = user.getUUID(); + totemPlayers.put(userUUID, new TotemPlayer(userUUID, user.getName(), user.getClientVersion(), userUUID.getMostSignificantBits() == 0L, brand)); } - @Override public void onUserDisconnect(UserDisconnectEvent event) { UUID userUUID = event.getUser().getUUID();