Skip to content

Commit f31d731

Browse files
Cleanup player info logic 1.8->1.7 (#608)
* Cleanup player info logic * Don't update ping if it's the same * Cleanup --------- Co-authored-by: FlorianMichael <florian.michael07@gmail.com>
1 parent 2ddd0d8 commit f31d731

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/rewriter/PlayerPacketRewriter1_8.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -245,34 +245,28 @@ public void register() {
245245
final int count = wrapper.read(Types.VAR_INT);
246246
for (int i = 0; i < count; i++) {
247247
UUID uuid = wrapper.read(Types.UUID);
248-
if (action == 0) {
249-
String name = wrapper.read(Types.STRING);
250-
251-
GameProfileStorage.GameProfile gameProfile = gameProfileStorage.get(uuid);
252-
if (gameProfile == null) gameProfile = gameProfileStorage.put(uuid, name);
248+
if (action == 0) { // Add player
249+
final String name = wrapper.read(Types.STRING);
250+
final GameProfile.Property[] properties = wrapper.read(Types.PROFILE_PROPERTY_ARRAY);
251+
final int gamemode = wrapper.read(Types.VAR_INT);
252+
final int ping = wrapper.read(Types.VAR_INT);
253+
final JsonElement displayNameComponent = wrapper.read(Types.OPTIONAL_COMPONENT);
254+
final String displayName = displayNameComponent != null ? ChatUtil.jsonToLegacy(displayNameComponent) : null;
253255

254-
GameProfile.Property[] properties = wrapper.read(Types.PROFILE_PROPERTY_ARRAY);
256+
final GameProfileStorage.GameProfile gameProfile = gameProfileStorage.put(uuid, name, displayName, ping, gamemode);
255257
for (GameProfile.Property property : properties) {
256258
gameProfile.properties.add(new GameProfileStorage.Property(property.name(), property.value(), property.signature()));
257259
}
258260

259-
int gamemode = wrapper.read(Types.VAR_INT);
260-
int ping = wrapper.read(Types.VAR_INT);
261-
gameProfile.ping = ping;
262-
gameProfile.gamemode = gamemode;
263-
JsonElement displayName = wrapper.read(Types.OPTIONAL_COMPONENT);
264-
if (displayName != null) {
265-
gameProfile.setDisplayName(ChatUtil.jsonToLegacy(displayName));
266-
}
267-
268261
final PacketWrapper playerInfo = PacketWrapper.create(ClientboundPackets1_7_2_5.PLAYER_INFO, wrapper.user());
269-
playerInfo.write(Types.STRING, gameProfile.getDisplayName());
262+
playerInfo.write(Types.STRING, gameProfile.getLegacyDisplayName());
270263
playerInfo.write(Types.BOOLEAN, true);
271264
playerInfo.write(Types.SHORT, (short) ping);
272265
playerInfo.scheduleSend(Protocol1_8To1_7_6_10.class);
273-
} else if (action == 1) {
266+
} else if (action == 1) { // Update game mode
274267
final int gamemode = wrapper.read(Types.VAR_INT);
275-
GameProfileStorage.GameProfile gameProfile = gameProfileStorage.get(uuid);
268+
269+
final GameProfileStorage.GameProfile gameProfile = gameProfileStorage.get(uuid);
276270
if (gameProfile == null || gameProfile.gamemode == gamemode) {
277271
continue;
278272
}
@@ -303,52 +297,60 @@ public void register() {
303297
}
304298

305299
gameProfile.gamemode = gamemode;
306-
} else if (action == 2) {
300+
} else if (action == 2) { // Update latency
307301
final int ping = wrapper.read(Types.VAR_INT);
308302

309303
final GameProfileStorage.GameProfile gameProfile = gameProfileStorage.get(uuid);
310-
if (gameProfile == null) {
304+
if (gameProfile == null || gameProfile.ping == ping) {
311305
continue;
312306
}
307+
313308
gameProfile.ping = ping;
314309

315310
PacketWrapper packet = PacketWrapper.create(ClientboundPackets1_7_2_5.PLAYER_INFO, wrapper.user());
316-
packet.write(Types.STRING, gameProfile.getDisplayName());
311+
packet.write(Types.STRING, gameProfile.getLegacyDisplayName());
317312
packet.write(Types.BOOLEAN, true);
318313
packet.write(Types.SHORT, (short) ping);
319314
packet.scheduleSend(Protocol1_8To1_7_6_10.class);
320-
} else if (action == 3) {
315+
} else if (action == 3) { // Update display name
316+
GameProfileStorage.GameProfile gameProfile = gameProfileStorage.get(uuid);
317+
318+
if (gameProfile == null) {
319+
continue;
320+
}
321+
321322
JsonElement displayNameComponent = wrapper.read(Types.OPTIONAL_COMPONENT);
322323
String displayName = displayNameComponent != null ? ChatUtil.jsonToLegacy(displayNameComponent) : null;
323324

324-
GameProfileStorage.GameProfile gameProfile = gameProfileStorage.get(uuid);
325-
if (gameProfile == null || gameProfile.displayName == null && displayName == null) continue;
325+
// Don't update if display name is the same
326+
if (Objects.equals(gameProfile.displayName, displayName)) {
327+
continue;
328+
}
326329

327330
PacketWrapper playerInfo = PacketWrapper.create(ClientboundPackets1_7_2_5.PLAYER_INFO, wrapper.user());
328-
playerInfo.write(Types.STRING, gameProfile.getDisplayName());
331+
playerInfo.write(Types.STRING, gameProfile.getLegacyDisplayName());
329332
playerInfo.write(Types.BOOLEAN, false);
330-
playerInfo.write(Types.SHORT, (short) gameProfile.ping);
333+
playerInfo.write(Types.SHORT, (short) 0);
331334
playerInfo.scheduleSend(Protocol1_8To1_7_6_10.class);
332335

333-
if (gameProfile.displayName == null && displayName != null || gameProfile.displayName != null && displayName == null || !gameProfile.displayName.equals(displayName)) {
334-
gameProfile.setDisplayName(displayName);
335-
}
336+
gameProfile.setDisplayName(displayName);
336337

337338
playerInfo = PacketWrapper.create(ClientboundPackets1_7_2_5.PLAYER_INFO, wrapper.user());
338-
playerInfo.write(Types.STRING, gameProfile.getDisplayName());
339+
playerInfo.write(Types.STRING, gameProfile.getLegacyDisplayName());
339340
playerInfo.write(Types.BOOLEAN, true);
340341
playerInfo.write(Types.SHORT, (short) gameProfile.ping);
341342
playerInfo.scheduleSend(Protocol1_8To1_7_6_10.class);
342-
} else if (action == 4) {
343+
} else if (action == 4) { // Remove player
343344
final GameProfileStorage.GameProfile gameProfile = gameProfileStorage.remove(uuid);
345+
344346
if (gameProfile == null) {
345347
continue;
346348
}
347349

348350
final PacketWrapper playerInfo = PacketWrapper.create(ClientboundPackets1_7_2_5.PLAYER_INFO, wrapper.user());
349-
playerInfo.write(Types.STRING, gameProfile.getDisplayName());
351+
playerInfo.write(Types.STRING, gameProfile.getLegacyDisplayName());
350352
playerInfo.write(Types.BOOLEAN, false);
351-
playerInfo.write(Types.SHORT, (short) gameProfile.ping);
353+
playerInfo.write(Types.SHORT, (short) 0);
352354
playerInfo.scheduleSend(Protocol1_8To1_7_6_10.class);
353355
}
354356
}

common/src/main/java/com/viaversion/viarewind/protocol/v1_8to1_7_6_10/storage/GameProfileStorage.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public GameProfileStorage(UserConnection user) {
3939
super(user);
4040
}
4141

42-
public GameProfile put(UUID uuid, String name) {
43-
GameProfile gameProfile = new GameProfile(uuid, name);
42+
public GameProfile put(UUID uuid, String name, String displayName, int ping, int gamemode) {
43+
GameProfile gameProfile = new GameProfile(uuid, name, displayName, ping, gamemode);
4444
properties.put(uuid, gameProfile);
4545
return gameProfile;
4646
}
@@ -86,16 +86,19 @@ public GameProfile remove(UUID uuid) {
8686

8787

8888
public static class GameProfile {
89-
public final String name;
9089
public final UUID uuid;
90+
public final String name;
9191
public String displayName;
92-
public int ping;
9392
public List<Property> properties = new ArrayList<>();
93+
public int ping;
9494
public int gamemode = 0;
9595

96-
public GameProfile(UUID uuid, String name) {
97-
this.name = name;
96+
public GameProfile(UUID uuid, String name, String displayName, int ping, int gamemode) {
9897
this.uuid = uuid;
98+
this.name = name;
99+
this.displayName = displayName;
100+
this.ping = ping;
101+
this.gamemode = gamemode;
99102
}
100103

101104
public Item getSkull() {
@@ -121,7 +124,7 @@ public Item getSkull() {
121124
return new DataItem(397, (byte) 1, (short) 3, tag);
122125
}
123126

124-
public String getDisplayName() {
127+
public String getLegacyDisplayName() {
125128
String displayName = this.displayName == null ? name : this.displayName;
126129
if (displayName.length() > 16) displayName = ChatUtil.removeUnusedColor(displayName, 'f');
127130
if (displayName.length() > 16) displayName = ChatColorUtil.stripColor(displayName);

0 commit comments

Comments
 (0)