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 {
private final MinecraftConnection connection;
private final Map<UUID, TabListEntry> entries = new ConcurrentHashMap<>();
private final Map<UUID, VelocityTabListEntry> 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;
}

View File

@@ -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;
}
}