Skip to content

Commit ce3b6ab

Browse files
committed
Make getOnlinePlayers unmodifiable and getPlayerWorld return option
1 parent 58ac0f5 commit ce3b6ab

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/main/java/org/mvplugins/multiverse/core/listeners/MVChatListener.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,9 @@ void asyncPlayerChat(AsyncPlayerChatEvent event) {
9696
}
9797

9898
private String getWorldName(Player player) {
99-
String world = playerWorldTracker.getPlayerWorld(player.getName());
100-
if (world == null) {
101-
world = player.getWorld().getName();
102-
}
103-
return this.worldManager.getLoadedWorld(world)
99+
String worldName = playerWorldTracker.getPlayerWorld(player.getName())
100+
.getOrElse(() -> player.getWorld().getName());
101+
return this.worldManager.getLoadedWorld(worldName)
104102
.map(mvworld -> mvworld.isHidden() ? "" : mvworld.getAliasOrName())
105103
.getOrElse("");
106104
}

src/main/java/org/mvplugins/multiverse/core/world/helpers/ConcurrentPlayerWorldTracker.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.mvplugins.multiverse.core.world.helpers;
22

3+
import io.vavr.control.Option;
34
import jakarta.inject.Inject;
45
import org.bukkit.entity.Player;
56
import org.bukkit.event.EventHandler;
@@ -10,14 +11,19 @@
1011
import org.bukkit.event.player.PlayerQuitEvent;
1112
import org.jetbrains.annotations.ApiStatus;
1213
import org.jetbrains.annotations.NotNull;
13-
import org.jetbrains.annotations.Nullable;
14+
import org.jetbrains.annotations.UnmodifiableView;
1415
import org.jvnet.hk2.annotations.Service;
1516
import org.mvplugins.multiverse.core.MultiverseCore;
1617

17-
import java.util.List;
18+
import java.util.Collection;
19+
import java.util.Collections;
1820
import java.util.Map;
1921
import 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
2329
public 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

Comments
 (0)