Skip to content

Commit c068225

Browse files
committed
Implement ConcurrentPlayerWorldTracker to allow for async online player names and worlds lookup
1 parent 14d8ad7 commit c068225

5 files changed

Lines changed: 91 additions & 28 deletions

File tree

src/main/java/org/mvplugins/multiverse/core/destination/core/BedDestination.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import co.aikar.locales.MessageKey;
88
import co.aikar.locales.MessageKeyProvider;
9-
import org.bukkit.Bukkit;
9+
import jakarta.inject.Inject;
1010
import org.bukkit.command.CommandSender;
1111
import org.bukkit.entity.Player;
1212
import org.jetbrains.annotations.NotNull;
@@ -20,8 +20,7 @@
2020
import org.mvplugins.multiverse.core.utils.PlayerFinder;
2121
import org.mvplugins.multiverse.core.utils.result.Attempt;
2222
import org.mvplugins.multiverse.core.utils.result.FailureReason;
23-
24-
import static org.mvplugins.multiverse.core.locale.message.MessageReplacement.replace;
23+
import org.mvplugins.multiverse.core.world.helpers.ConcurrentPlayerWorldTracker;
2524

2625
/**
2726
* {@link Destination} implementation for beds.
@@ -30,7 +29,11 @@
3029
public final class BedDestination implements Destination<BedDestination, BedDestinationInstance, BedDestination.InstanceFailureReason> {
3130
static final String OWN_BED_STRING = "playerbed";
3231

33-
BedDestination() {
32+
private final ConcurrentPlayerWorldTracker worldTracker;
33+
34+
@Inject
35+
BedDestination(@NotNull ConcurrentPlayerWorldTracker worldTracker) {
36+
this.worldTracker = worldTracker;
3437
}
3538

3639
/**
@@ -62,8 +65,8 @@ public final class BedDestination implements Destination<BedDestination, BedDest
6265
@Override
6366
public @NotNull Collection<DestinationSuggestionPacket> suggestDestinations(
6467
@NotNull CommandSender sender, @Nullable String destinationParams) {
65-
List<DestinationSuggestionPacket> collect = Bukkit.getOnlinePlayers().stream()
66-
.map(player -> new DestinationSuggestionPacket(this, player.getName(), player.getName()))
68+
List<DestinationSuggestionPacket> collect = worldTracker.getOnlinePlayers().stream()
69+
.map(player -> new DestinationSuggestionPacket(this, player, player))
6770
.collect(Collectors.toList());
6871
if (sender instanceof Player) {
6972
collect.add(new DestinationSuggestionPacket(this, OWN_BED_STRING, OWN_BED_STRING));

src/main/java/org/mvplugins/multiverse/core/destination/core/PlayerDestination.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import co.aikar.locales.MessageKey;
66
import co.aikar.locales.MessageKeyProvider;
7-
import org.bukkit.Bukkit;
7+
import jakarta.inject.Inject;
88
import org.bukkit.command.CommandSender;
99
import org.bukkit.entity.Player;
1010
import org.jetbrains.annotations.NotNull;
@@ -14,21 +14,26 @@
1414
import org.mvplugins.multiverse.core.destination.Destination;
1515
import org.mvplugins.multiverse.core.destination.DestinationSuggestionPacket;
1616
import org.mvplugins.multiverse.core.locale.MVCorei18n;
17-
import org.mvplugins.multiverse.core.locale.message.MessageReplacement;
1817
import org.mvplugins.multiverse.core.locale.message.MessageReplacement.Replace;
1918
import org.mvplugins.multiverse.core.utils.PlayerFinder;
2019
import org.mvplugins.multiverse.core.utils.result.Attempt;
2120
import org.mvplugins.multiverse.core.utils.result.FailureReason;
21+
import org.mvplugins.multiverse.core.world.helpers.ConcurrentPlayerWorldTracker;
2222

2323
/**
2424
* {@link Destination} implementation for players.s
2525
*/
2626
@Service
2727
public final class PlayerDestination implements Destination<PlayerDestination, PlayerDestinationInstance, PlayerDestination.InstanceFailureReason> {
28+
29+
private final ConcurrentPlayerWorldTracker playerWorldTracker;
30+
2831
/**
2932
* Creates a new instance of the PlayerDestination.
3033
*/
31-
PlayerDestination() {
34+
@Inject
35+
PlayerDestination(@NotNull ConcurrentPlayerWorldTracker playerWorldTracker) {
36+
this.playerWorldTracker = playerWorldTracker;
3237
}
3338

3439
/**
@@ -60,8 +65,8 @@ public final class PlayerDestination implements Destination<PlayerDestination, P
6065
@Override
6166
public @NotNull Collection<DestinationSuggestionPacket> suggestDestinations(
6267
@NotNull CommandSender sender, @Nullable String destinationParams) {
63-
return Bukkit.getOnlinePlayers().stream()
64-
.map(p -> new DestinationSuggestionPacket(this, p.getName(), p.getName()))
68+
return playerWorldTracker.getOnlinePlayers().stream()
69+
.map(player -> new DestinationSuggestionPacket(this, player, player))
6570
.toList();
6671
}
6772

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.mvplugins.multiverse.core.dynamiclistener.annotations.SkipIfEventExist;
1717
import org.mvplugins.multiverse.core.utils.text.ChatTextFormatter;
1818
import org.mvplugins.multiverse.core.world.WorldManager;
19+
import org.mvplugins.multiverse.core.world.helpers.ConcurrentPlayerWorldTracker;
1920

2021
/**
2122
* Multiverse's Listener for players.
@@ -24,16 +25,17 @@
2425
final class MVChatListener implements CoreListener {
2526
private final CoreConfig config;
2627
private final WorldManager worldManager;
27-
private final MVPlayerListener playerListener;
28+
private final ConcurrentPlayerWorldTracker playerWorldTracker;
2829

2930
@Inject
3031
MVChatListener(
3132
CoreConfig config,
3233
WorldManager worldManager,
33-
MVPlayerListener playerListener) {
34+
ConcurrentPlayerWorldTracker playerWorldTracker
35+
) {
3436
this.config = config;
3537
this.worldManager = worldManager;
36-
this.playerListener = playerListener;
38+
this.playerWorldTracker = playerWorldTracker;
3739
}
3840

3941
@EventClass("io.papermc.paper.event.player.AsyncChatEvent")
@@ -94,10 +96,9 @@ void asyncPlayerChat(AsyncPlayerChatEvent event) {
9496
}
9597

9698
private String getWorldName(Player player) {
97-
String world = playerListener.getPlayerWorld().get(player.getName());
99+
String world = playerWorldTracker.getPlayerWorld(player.getName());
98100
if (world == null) {
99101
world = player.getWorld().getName();
100-
playerListener.getPlayerWorld().put(player.getName(), world);
101102
}
102103
return this.worldManager.getLoadedWorld(world)
103104
.map(mvworld -> mvworld.isHidden() ? "" : mvworld.getAliasOrName())

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ final class MVPlayerListener implements CoreListener {
7373
private final DimensionFinder dimensionFinder;
7474
private final CorePermissionsChecker corePermissionsChecker;
7575

76-
private final Map<String, String> playerWorld = new ConcurrentHashMap<>();
77-
7876
@Inject
7977
MVPlayerListener(
8078
MultiverseCore plugin,
@@ -117,15 +115,6 @@ private PluginLocales getLocales() {
117115
return getCommandManager().getLocales();
118116
}
119117

120-
/**
121-
* Gets the map of player and the world name they are in.
122-
*
123-
* @return the playerWorld-map
124-
*/
125-
Map<String, String> getPlayerWorld() {
126-
return playerWorld;
127-
}
128-
129118
/**
130119
* This method is called when a player respawns.
131120
*
@@ -274,7 +263,6 @@ private void handleJoinLocation(PlayerSpawnLocationEvent event) {
274263
void playerChangedWorld(PlayerChangedWorldEvent event) {
275264
// Permissions now determine whether or not to handle a gamemode.
276265
this.handleGameModeAndFlight(event.getPlayer(), event.getPlayer().getWorld());
277-
playerWorld.put(event.getPlayer().getName(), event.getPlayer().getWorld().getName());
278266
}
279267

280268
/**
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.mvplugins.multiverse.core.world.helpers;
2+
3+
import jakarta.inject.Inject;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.event.EventHandler;
6+
import org.bukkit.event.EventPriority;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.player.PlayerChangedWorldEvent;
9+
import org.bukkit.event.player.PlayerJoinEvent;
10+
import org.bukkit.event.player.PlayerQuitEvent;
11+
import org.jetbrains.annotations.ApiStatus;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
import org.jvnet.hk2.annotations.Service;
15+
import org.mvplugins.multiverse.core.MultiverseCore;
16+
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.concurrent.ConcurrentHashMap;
20+
21+
@ApiStatus.AvailableSince("5.4")
22+
@Service
23+
public final class ConcurrentPlayerWorldTracker implements Listener {
24+
25+
private final Map<String, String> playerWorldMap;
26+
27+
@Inject
28+
ConcurrentPlayerWorldTracker(@NotNull MultiverseCore plugin) {
29+
this.playerWorldMap = new ConcurrentHashMap<>();
30+
plugin.getServer().getPluginManager().registerEvents(this, plugin);
31+
}
32+
33+
@ApiStatus.AvailableSince("5.4")
34+
@NotNull
35+
public List<String> getOnlinePlayers() {
36+
return List.copyOf(playerWorldMap.keySet());
37+
}
38+
39+
@ApiStatus.AvailableSince("5.4")
40+
@Nullable
41+
public String getPlayerWorld(String playerName) {
42+
return playerWorldMap.get(playerName);
43+
}
44+
45+
@EventHandler(priority = EventPriority.LOWEST)
46+
private void onPlayerJoin(PlayerJoinEvent event) {
47+
setPlayerWorld(event.getPlayer());
48+
}
49+
50+
@EventHandler(priority = EventPriority.LOWEST)
51+
private void onPlayerChangedWorld(@NotNull PlayerChangedWorldEvent event) {
52+
setPlayerWorld(event.getPlayer());
53+
}
54+
55+
private void setPlayerWorld(Player player) {
56+
String playerName = player.getName();
57+
String worldName = player.getWorld().getName();
58+
playerWorldMap.put(playerName, worldName);
59+
}
60+
61+
@EventHandler
62+
private void onPlayerQuit(@NotNull PlayerQuitEvent event) {
63+
String playerName = event.getPlayer().getName();
64+
playerWorldMap.remove(playerName);
65+
}
66+
}

0 commit comments

Comments
 (0)