Avoid sending duplicate set latency/gamemode/display name packets

This commit is contained in:
Andrew Steinborn
2019-06-15 17:08:05 -04:00
parent 335c34a679
commit 1fd2bd9ee4
2 changed files with 23 additions and 9 deletions

View File

@@ -21,7 +21,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class VelocityTabList implements TabList { public class VelocityTabList implements TabList {
private final MinecraftConnection connection; private final MinecraftConnection connection;
private final Map<UUID, TabListEntry> entries = new ConcurrentHashMap<>(); private final Map<UUID, VelocityTabListEntry> entries = new ConcurrentHashMap<>();
public VelocityTabList(MinecraftConnection connection) { public VelocityTabList(MinecraftConnection connection) {
this.connection = connection; this.connection = connection;
@@ -46,11 +46,13 @@ public class VelocityTabList implements TabList {
"The provided entry was not created by this tab list"); "The provided entry was not created by this tab list");
Preconditions.checkArgument(!entries.containsKey(entry.getProfile().getId()), Preconditions.checkArgument(!entries.containsKey(entry.getProfile().getId()),
"this TabList already contains an entry with the same uuid"); "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); PlayerListItem.Item packetItem = PlayerListItem.Item.from(entry);
connection.write( connection.write(
new PlayerListItem(PlayerListItem.ADD_PLAYER, Collections.singletonList(packetItem))); new PlayerListItem(PlayerListItem.ADD_PLAYER, Collections.singletonList(packetItem)));
entries.put(entry.getProfile().getId(), entry); entries.put(entry.getProfile().getId(), (VelocityTabListEntry) entry);
} }
@Override @Override
@@ -111,7 +113,7 @@ public class VelocityTabList implements TabList {
if (name == null || properties == null) { if (name == null || properties == null) {
throw new IllegalStateException("Got null game profile for ADD_PLAYER"); 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) .tabList(this)
.profile(new GameProfile(uuid, name, properties)) .profile(new GameProfile(uuid, name, properties))
.displayName(item.getDisplayName()) .displayName(item.getDisplayName())
@@ -124,23 +126,23 @@ public class VelocityTabList implements TabList {
entries.remove(uuid); entries.remove(uuid);
break; break;
case PlayerListItem.UPDATE_DISPLAY_NAME: { case PlayerListItem.UPDATE_DISPLAY_NAME: {
TabListEntry entry = entries.get(uuid); VelocityTabListEntry entry = entries.get(uuid);
if (entry != null) { if (entry != null) {
entry.setDisplayName(item.getDisplayName()); entry.setDisplayNameInternal(item.getDisplayName());
} }
break; break;
} }
case PlayerListItem.UPDATE_LATENCY: { case PlayerListItem.UPDATE_LATENCY: {
TabListEntry entry = entries.get(uuid); VelocityTabListEntry entry = entries.get(uuid);
if (entry != null) { if (entry != null) {
entry.setLatency(item.getLatency()); entry.setLatencyInternal(item.getLatency());
} }
break; break;
} }
case PlayerListItem.UPDATE_GAMEMODE: { case PlayerListItem.UPDATE_GAMEMODE: {
TabListEntry entry = entries.get(uuid); VelocityTabListEntry entry = entries.get(uuid);
if (entry != null) { if (entry != null) {
entry.setLatency(item.getGameMode()); entry.setGameModeInternal(item.getGameMode());
} }
break; break;
} }

View File

@@ -47,6 +47,10 @@ public class VelocityTabListEntry implements TabListEntry {
return this; return this;
} }
void setDisplayNameInternal(@Nullable Component displayName) {
this.displayName = displayName;
}
@Override @Override
public int getLatency() { public int getLatency() {
return latency; return latency;
@@ -59,6 +63,10 @@ public class VelocityTabListEntry implements TabListEntry {
return this; return this;
} }
void setLatencyInternal(int latency) {
this.latency = latency;
}
@Override @Override
public int getGameMode() { public int getGameMode() {
return gameMode; return gameMode;
@@ -70,4 +78,8 @@ public class VelocityTabListEntry implements TabListEntry {
tabList.updateEntry(PlayerListItem.UPDATE_GAMEMODE, this); tabList.updateEntry(PlayerListItem.UPDATE_GAMEMODE, this);
return this; return this;
} }
void setGameModeInternal(int gameMode) {
this.gameMode = gameMode;
}
} }