From 1fd2bd9ee446e9b13bb7068ff7fdac01e9069e6c Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 15 Jun 2019 17:08:05 -0400 Subject: [PATCH] Avoid sending duplicate set latency/gamemode/display name packets --- .../proxy/tablist/VelocityTabList.java | 20 ++++++++++--------- .../proxy/tablist/VelocityTabListEntry.java | 12 +++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index 5a0d5db2a..2851e6e84 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -21,7 +21,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class VelocityTabList implements TabList { private final MinecraftConnection connection; - private final Map entries = new ConcurrentHashMap<>(); + private final Map entries = new ConcurrentHashMap<>(); public VelocityTabList(MinecraftConnection connection) { this.connection = connection; @@ -46,11 +46,13 @@ public class VelocityTabList implements TabList { "The provided entry was not created by this tab list"); Preconditions.checkArgument(!entries.containsKey(entry.getProfile().getId()), "this TabList already contains an entry with the same uuid"); + Preconditions.checkArgument(entry instanceof VelocityTabListEntry, + "Not a Velocity tab list entry"); PlayerListItem.Item packetItem = PlayerListItem.Item.from(entry); connection.write( new PlayerListItem(PlayerListItem.ADD_PLAYER, Collections.singletonList(packetItem))); - entries.put(entry.getProfile().getId(), entry); + entries.put(entry.getProfile().getId(), (VelocityTabListEntry) entry); } @Override @@ -111,7 +113,7 @@ public class VelocityTabList implements TabList { if (name == null || properties == null) { throw new IllegalStateException("Got null game profile for ADD_PLAYER"); } - entries.put(item.getUuid(), TabListEntry.builder() + entries.put(item.getUuid(), (VelocityTabListEntry) TabListEntry.builder() .tabList(this) .profile(new GameProfile(uuid, name, properties)) .displayName(item.getDisplayName()) @@ -124,23 +126,23 @@ public class VelocityTabList implements TabList { entries.remove(uuid); break; case PlayerListItem.UPDATE_DISPLAY_NAME: { - TabListEntry entry = entries.get(uuid); + VelocityTabListEntry entry = entries.get(uuid); if (entry != null) { - entry.setDisplayName(item.getDisplayName()); + entry.setDisplayNameInternal(item.getDisplayName()); } break; } case PlayerListItem.UPDATE_LATENCY: { - TabListEntry entry = entries.get(uuid); + VelocityTabListEntry entry = entries.get(uuid); if (entry != null) { - entry.setLatency(item.getLatency()); + entry.setLatencyInternal(item.getLatency()); } break; } case PlayerListItem.UPDATE_GAMEMODE: { - TabListEntry entry = entries.get(uuid); + VelocityTabListEntry entry = entries.get(uuid); if (entry != null) { - entry.setLatency(item.getGameMode()); + entry.setGameModeInternal(item.getGameMode()); } break; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java index 1f2d774ec..647ce3d7a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java @@ -47,6 +47,10 @@ public class VelocityTabListEntry implements TabListEntry { return this; } + void setDisplayNameInternal(@Nullable Component displayName) { + this.displayName = displayName; + } + @Override public int getLatency() { return latency; @@ -59,6 +63,10 @@ public class VelocityTabListEntry implements TabListEntry { return this; } + void setLatencyInternal(int latency) { + this.latency = latency; + } + @Override public int getGameMode() { return gameMode; @@ -70,4 +78,8 @@ public class VelocityTabListEntry implements TabListEntry { tabList.updateEntry(PlayerListItem.UPDATE_GAMEMODE, this); return this; } + + void setGameModeInternal(int gameMode) { + this.gameMode = gameMode; + } }