Skip to content

Commit 60d663e

Browse files
committed
Make player-marker supplier check for late loaded worlds
1 parent e8a9890 commit 60d663e

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

common/src/main/java/de/bluecolored/bluemap/common/live/LivePlayersDataSupplier.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import de.bluecolored.bluemap.common.serverinterface.Server;
3131
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
3232
import de.bluecolored.bluemap.core.logger.Logger;
33+
import de.bluecolored.bluemap.core.world.World;
34+
import org.jetbrains.annotations.Nullable;
3335

3436
import java.io.IOException;
3537
import java.io.StringWriter;
@@ -41,10 +43,12 @@ public class LivePlayersDataSupplier implements Supplier<String> {
4143

4244
private final Server server;
4345
private final PluginConfig config;
44-
private final ServerWorld world;
46+
private final World world;
4547
private final Predicate<UUID> playerFilter;
4648

47-
public LivePlayersDataSupplier(Server server, PluginConfig config, ServerWorld world, Predicate<UUID> playerFilter) {
49+
private transient @Nullable ServerWorld serverWorld;
50+
51+
public LivePlayersDataSupplier(Server server, PluginConfig config, World world, Predicate<UUID> playerFilter) {
4852
this.server = server;
4953
this.config = config;
5054
this.world = world;
@@ -53,6 +57,9 @@ public LivePlayersDataSupplier(Server server, PluginConfig config, ServerWorld w
5357

5458
@Override
5559
public String get() {
60+
if (serverWorld == null)
61+
serverWorld = server.getServerWorld(world).orElse(null);
62+
5663
try (StringWriter jsonString = new StringWriter();
5764
JsonWriter json = new JsonWriter(jsonString)) {
5865

@@ -61,7 +68,7 @@ public String get() {
6168

6269
if (config.isLivePlayerMarkers()) {
6370
for (Player player : this.server.getOnlinePlayers()) {
64-
boolean isCorrectWorld = player.getWorld().equals(this.world);
71+
boolean isCorrectWorld = player.getWorld().equals(serverWorld);
6572

6673
if (config.isHideInvisible() && player.isInvisible()) continue;
6774
if (config.isHideVanished() && player.isVanished()) continue;

common/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,10 @@ public void savePlayerStates() {
539539

540540
var maps = blueMap.getMaps();
541541
for (BmMap map : maps.values()) {
542-
var serverWorld = serverInterface.getServerWorld(map.getWorld()).orElse(null);
543-
if (serverWorld == null) continue;
544542
var dataSupplier = new LivePlayersDataSupplier(
545543
serverInterface,
546544
getBlueMap().getConfig().getPluginConfig(),
547-
serverWorld,
545+
map.getWorld(),
548546
Predicate.not(pluginState::isPlayerHidden)
549547
);
550548
try (

common/src/main/java/de/bluecolored/bluemap/common/serverinterface/Server.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
package de.bluecolored.bluemap.common.serverinterface;
2626

27+
import com.github.benmanes.caffeine.cache.Cache;
28+
import com.github.benmanes.caffeine.cache.Caffeine;
29+
import com.github.benmanes.caffeine.cache.LoadingCache;
2730
import de.bluecolored.bluemap.common.debug.DebugDump;
2831
import de.bluecolored.bluemap.core.util.Tristate;
2932
import de.bluecolored.bluemap.core.world.World;
@@ -33,9 +36,16 @@
3336
import java.nio.file.Path;
3437
import java.util.Collection;
3538
import java.util.Optional;
39+
import java.util.concurrent.TimeUnit;
3640

3741
public interface Server {
3842

43+
Cache<World, Optional<ServerWorld>> SERVER_WORLD_CACHE = Caffeine.newBuilder()
44+
.expireAfterWrite(10, TimeUnit.SECONDS)
45+
.weakKeys()
46+
.weakValues()
47+
.build();
48+
3949
@DebugDump
4050
@Nullable String getMinecraftVersion();
4151

@@ -63,17 +73,18 @@ default Tristate isMetricsEnabled() {
6373
* Returns the correct {@link ServerWorld} for a {@link World} if there is any.
6474
*/
6575
default Optional<ServerWorld> getServerWorld(World world) {
66-
if (world instanceof MCAWorld) {
67-
MCAWorld mcaWorld = (MCAWorld) world;
68-
return getLoadedServerWorlds().stream()
69-
.filter(serverWorld ->
70-
serverWorld.getWorldFolder().toAbsolutePath().normalize()
71-
.equals(mcaWorld.getWorldFolder().toAbsolutePath().normalize()) &&
72-
serverWorld.getDimension().equals(mcaWorld.getDimension())
73-
)
74-
.findAny();
75-
}
76-
return Optional.empty();
76+
return SERVER_WORLD_CACHE.get(world, w -> {
77+
if (w instanceof MCAWorld mcaWorld) {
78+
return getLoadedServerWorlds().stream()
79+
.filter(serverWorld ->
80+
serverWorld.getWorldFolder().toAbsolutePath().normalize()
81+
.equals(mcaWorld.getWorldFolder().toAbsolutePath().normalize()) &&
82+
serverWorld.getDimension().equals(mcaWorld.getDimension())
83+
)
84+
.findAny();
85+
}
86+
return Optional.empty();
87+
});
7788
}
7889

7990
/**

common/src/main/java/de/bluecolored/bluemap/common/web/MapRequestHandler.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class MapRequestHandler extends RoutingRequestHandler {
4242

4343
public MapRequestHandler(BmMap map, Server serverInterface, PluginConfig pluginConfig, Predicate<UUID> playerFilter) {
4444
this(map.getStorage(),
45-
createPlayersDataSupplier(map, serverInterface, pluginConfig, playerFilter),
45+
new LivePlayersDataSupplier(serverInterface, pluginConfig, map.getWorld(), playerFilter),
4646
new LiveMarkersDataSupplier(map.getMarkerSets()));
4747
}
4848

@@ -69,10 +69,4 @@ public MapRequestHandler(MapStorage mapStorage,
6969
}
7070
}
7171

72-
private static @Nullable LivePlayersDataSupplier createPlayersDataSupplier(BmMap map, Server serverInterface, PluginConfig pluginConfig, Predicate<UUID> playerFilter) {
73-
ServerWorld world = serverInterface.getServerWorld(map.getWorld()).orElse(null);
74-
if (world == null) return null;
75-
return new LivePlayersDataSupplier(serverInterface, pluginConfig, world, playerFilter);
76-
}
77-
7872
}

0 commit comments

Comments
 (0)