Skip to content

Commit

Permalink
Improved: User login handling (hopefully fixes an issue with the clie…
Browse files Browse the repository at this point in the history
…nt version being null)
  • Loading branch information
Bram1903 committed Sep 22, 2024
1 parent 4b91a03 commit c80be71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
31 changes: 15 additions & 16 deletions src/main/java/com/deathmotion/totemguard/data/TotemPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}

Expand All @@ -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();
Expand Down

0 comments on commit c80be71

Please sign in to comment.