3
3
import java .nio .charset .StandardCharsets ;
4
4
import java .util .List ;
5
5
import java .util .Optional ;
6
+ import java .util .concurrent .Semaphore ;
6
7
7
8
import com .comphenix .protocol .events .InternalStructure ;
8
9
import com .comphenix .protocol .reflect .EquivalentConverter ;
@@ -29,30 +30,38 @@ public final class ServerPingRecord implements ServerPingImpl {
29
30
private static EquivalentConverter <List <WrappedGameProfile >> PROFILE_LIST_CONVERTER ;
30
31
31
32
private static boolean initialized = false ;
33
+ private static final Object lock = new Object ();
32
34
33
35
private static void initialize () {
34
36
if (initialized ) {
35
37
return ;
36
38
}
37
39
38
- initialized = true ;
40
+ synchronized (lock ) {
41
+ // may have been initialized while waiting for the lock
42
+ if (initialized ) {
43
+ return ;
44
+ }
39
45
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 ();
44
50
45
- PING_CTOR = Accessors .getConstructorAccessor (SERVER_PING .getConstructors ()[0 ]);
51
+ PING_CTOR = Accessors .getConstructorAccessor (SERVER_PING .getConstructors ()[0 ]);
46
52
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" ));
50
56
51
- PROFILE_LIST_CONVERTER = BukkitConverters .getListConverter (BukkitConverters .getWrappedGameProfileConverter ());
57
+ PROFILE_LIST_CONVERTER = BukkitConverters .getListConverter (BukkitConverters .getWrappedGameProfileConverter ());
52
58
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
+ }
56
65
}
57
66
}
58
67
0 commit comments