11package org .mvplugins .multiverse .core .world .helpers ;
22
3+ import io .vavr .control .Option ;
34import jakarta .inject .Inject ;
45import org .bukkit .entity .Player ;
56import org .bukkit .event .EventHandler ;
1011import org .bukkit .event .player .PlayerQuitEvent ;
1112import org .jetbrains .annotations .ApiStatus ;
1213import org .jetbrains .annotations .NotNull ;
13- import org .jetbrains .annotations .Nullable ;
14+ import org .jetbrains .annotations .UnmodifiableView ;
1415import org .jvnet .hk2 .annotations .Service ;
1516import org .mvplugins .multiverse .core .MultiverseCore ;
1617
17- import java .util .List ;
18+ import java .util .Collection ;
19+ import java .util .Collections ;
1820import java .util .Map ;
1921import java .util .concurrent .ConcurrentHashMap ;
2022
23+ /**
24+ * Tracks which players are in which worlds, using a thread-safe map.
25+ * This allows async access to online players list and the world they are in.
26+ */
2127@ ApiStatus .AvailableSince ("5.4" )
2228@ Service
2329public final class ConcurrentPlayerWorldTracker implements Listener {
@@ -30,16 +36,28 @@ public final class ConcurrentPlayerWorldTracker implements Listener {
3036 plugin .getServer ().getPluginManager ().registerEvents (this , plugin );
3137 }
3238
39+ /**
40+ * Get an unmodifiable collection of all online player names on the server.
41+ *
42+ * @return Unmodifiable collection of online player names.
43+ */
3344 @ ApiStatus .AvailableSince ("5.4" )
3445 @ NotNull
35- public List <String > getOnlinePlayers () {
36- return List .copyOf (playerWorldMap .keySet ());
46+ @ UnmodifiableView
47+ public Collection <String > getOnlinePlayers () {
48+ return Collections .unmodifiableCollection (playerWorldMap .keySet ());
3749 }
3850
51+ /**
52+ * Get the world name a player is currently in.
53+ *
54+ * @param playerName Name of the player.
55+ * @return World name the player is in, or null if the player is not online.
56+ */
3957 @ ApiStatus .AvailableSince ("5.4" )
40- @ Nullable
41- public String getPlayerWorld (String playerName ) {
42- return playerWorldMap .get (playerName );
58+ @ NotNull
59+ public Option < String > getPlayerWorld (String playerName ) {
60+ return Option . of ( playerWorldMap .get (playerName ) );
4361 }
4462
4563 @ EventHandler (priority = EventPriority .LOWEST )
0 commit comments