Skip to content

Commit c7753a9

Browse files
committed
Better server ping concurrency
Addresses #2289
1 parent b518126 commit c7753a9

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/main/java/com/comphenix/protocol/wrappers/ping/ServerPingRecord.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.nio.charset.StandardCharsets;
44
import java.util.List;
55
import java.util.Optional;
6+
import java.util.concurrent.Semaphore;
67

78
import com.comphenix.protocol.events.InternalStructure;
89
import com.comphenix.protocol.reflect.EquivalentConverter;
@@ -29,30 +30,38 @@ public final class ServerPingRecord implements ServerPingImpl {
2930
private static EquivalentConverter<List<WrappedGameProfile>> PROFILE_LIST_CONVERTER;
3031

3132
private static boolean initialized = false;
33+
private static final Object lock = new Object();
3234

3335
private static void initialize() {
3436
if (initialized) {
3537
return;
3638
}
3739

38-
initialized = true;
40+
synchronized (lock) {
41+
// may have been initialized while waiting for the lock
42+
if (initialized) {
43+
return;
44+
}
3945

40-
try {
41-
SERVER_PING = MinecraftReflection.getServerPingClass();
42-
PLAYER_SAMPLE_CLASS = MinecraftReflection.getServerPingPlayerSampleClass();
43-
SERVER_DATA_CLASS = MinecraftReflection.getServerPingServerDataClass();
46+
try {
47+
SERVER_PING = MinecraftReflection.getServerPingClass();
48+
PLAYER_SAMPLE_CLASS = MinecraftReflection.getServerPingPlayerSampleClass();
49+
SERVER_DATA_CLASS = MinecraftReflection.getServerPingServerDataClass();
4450

45-
PING_CTOR = Accessors.getConstructorAccessor(SERVER_PING.getConstructors()[0]);
51+
PING_CTOR = Accessors.getConstructorAccessor(SERVER_PING.getConstructors()[0]);
4652

47-
DATA_WRAPPER = AutoWrapper.wrap(ServerData.class, SERVER_DATA_CLASS);
48-
SAMPLE_WRAPPER = AutoWrapper.wrap(PlayerSample.class, PLAYER_SAMPLE_CLASS);
49-
FAVICON_WRAPPER = AutoWrapper.wrap(Favicon.class, MinecraftReflection.getMinecraftClass("network.protocol.status.ServerPing$a"));
53+
DATA_WRAPPER = AutoWrapper.wrap(ServerData.class, SERVER_DATA_CLASS);
54+
SAMPLE_WRAPPER = AutoWrapper.wrap(PlayerSample.class, PLAYER_SAMPLE_CLASS);
55+
FAVICON_WRAPPER = AutoWrapper.wrap(Favicon.class, MinecraftReflection.getMinecraftClass("network.protocol.status.ServerPing$a"));
5056

51-
PROFILE_LIST_CONVERTER = BukkitConverters.getListConverter(BukkitConverters.getWrappedGameProfileConverter());
57+
PROFILE_LIST_CONVERTER = BukkitConverters.getListConverter(BukkitConverters.getWrappedGameProfileConverter());
5258

53-
DEFAULT_DESCRIPTION = WrappedChatComponent.fromLegacyText("A Minecraft Server");
54-
} catch (Exception ex) {
55-
ex.printStackTrace(); // TODO
59+
DEFAULT_DESCRIPTION = WrappedChatComponent.fromLegacyText("A Minecraft Server");
60+
} catch (Exception ex) {
61+
throw new RuntimeException("Failed to initialize Server Ping", ex);
62+
} finally {
63+
initialized = true;
64+
}
5665
}
5766
}
5867

0 commit comments

Comments
 (0)