From 8e3f750c7cfc4ecb35ffccd8a7ed39755c08d529 Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 18 Jan 2013 11:33:44 +1100 Subject: [PATCH] Make the tab list implementations pruddy. --- .../bungee/tablist/GlobalPingTabList.java | 14 +++++++---- .../md_5/bungee/tablist/GlobalTabList.java | 6 +++-- .../bungee/tablist/ServerUniqueTabList.java | 23 +++++++++++-------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java b/proxy/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java index 056ecc28..ba108cec 100644 --- a/proxy/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java +++ b/proxy/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java @@ -1,8 +1,7 @@ package net.md_5.bungee.tablist; -import java.util.Collections; import java.util.Map; -import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.packet.PacketC9PlayerListItem; @@ -10,8 +9,15 @@ import net.md_5.bungee.packet.PacketC9PlayerListItem; public class GlobalPingTabList extends GlobalTabList { - public static final int PING_THRESHOLD = 20; - private Map lastPings = Collections.synchronizedMap(new WeakHashMap()); + private static final int PING_THRESHOLD = 20; + private final Map lastPings = new ConcurrentHashMap<>(); + + @Override + public void onDisconnect(ProxiedPlayer player) + { + lastPings.remove(player); + super.onDisconnect(player); + } @Override public void onPingChange(ProxiedPlayer player, int ping) diff --git a/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java b/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java index ec4e551a..f22d6ee6 100644 --- a/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java +++ b/proxy/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java @@ -1,8 +1,8 @@ package net.md_5.bungee.tablist; -import java.util.Collections; import java.util.HashSet; import java.util.Set; +import lombok.Synchronized; import net.md_5.bungee.BungeeCord; import net.md_5.bungee.UserConnection; import net.md_5.bungee.api.TabListHandler; @@ -12,7 +12,7 @@ import net.md_5.bungee.packet.PacketC9PlayerListItem; public class GlobalTabList implements TabListHandler { - private Set sentPings = Collections.synchronizedSet(new HashSet()); + private final Set sentPings = new HashSet<>(); @Override public void onConnect(ProxiedPlayer player) @@ -24,6 +24,7 @@ public class GlobalTabList implements TabListHandler } @Override + @Synchronized(value = "sentPings") public void onPingChange(ProxiedPlayer player, int ping) { if (!sentPings.contains(player)) @@ -34,6 +35,7 @@ public class GlobalTabList implements TabListHandler } @Override + @Synchronized(value = "sentPings") public void onDisconnect(ProxiedPlayer player) { BungeeCord.getInstance().broadcast(new PacketC9PlayerListItem(player.getDisplayName(), false, 9999)); diff --git a/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java b/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java index bff35075..f5cef3a8 100644 --- a/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java +++ b/proxy/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java @@ -1,10 +1,9 @@ package net.md_5.bungee.tablist; -import java.util.Collections; -import java.util.LinkedHashSet; +import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; import net.md_5.bungee.UserConnection; import net.md_5.bungee.api.TabListHandler; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -13,7 +12,7 @@ import net.md_5.bungee.packet.PacketC9PlayerListItem; public class ServerUniqueTabList implements TabListHandler { - private Map> sentUsernames = Collections.synchronizedMap(new WeakHashMap>()); + private final Map> sentUsernames = new ConcurrentHashMap<>(); @Override public void onConnect(ProxiedPlayer player) @@ -28,6 +27,7 @@ public class ServerUniqueTabList implements TabListHandler @Override public void onDisconnect(ProxiedPlayer player) { + sentUsernames.remove(player); } @Override @@ -53,16 +53,19 @@ public class ServerUniqueTabList implements TabListHandler Set usernames = sentUsernames.get(player); if (usernames == null) { - usernames = new LinkedHashSet<>(); + usernames = new HashSet<>(); sentUsernames.put(player, usernames); } - if (online) + synchronized (usernames) { - usernames.add(name); - } else - { - usernames.remove(name); + if (online) + { + usernames.add(name); + } else + { + usernames.remove(name); + } } return true;