diff --git a/common/src/main/java/de/bluecolored/bluemap/common/api/PluginImpl.java b/common/src/main/java/de/bluecolored/bluemap/common/api/PluginImpl.java index 1ee3f0edd..50e85f8f7 100644 --- a/common/src/main/java/de/bluecolored/bluemap/common/api/PluginImpl.java +++ b/common/src/main/java/de/bluecolored/bluemap/common/api/PluginImpl.java @@ -26,6 +26,7 @@ import de.bluecolored.bluemap.api.plugin.PlayerIconFactory; +import de.bluecolored.bluemap.api.plugin.PlayerDisplayNameProvider; import de.bluecolored.bluemap.api.plugin.SkinProvider; import de.bluecolored.bluemap.common.plugin.Plugin; import lombok.NonNull; @@ -58,6 +59,16 @@ public void setPlayerMarkerIconFactory(PlayerIconFactory playerMarkerIconFactory plugin.getSkinUpdater().setPlayerMarkerIconFactory(playerMarkerIconFactory); } + @Override + public PlayerDisplayNameProvider getPlayerDisplayNameProvider() { + return plugin.getPlayerDisplayNameProvider(); + } + + @Override + public void setPlayerDisplayNameProvider(PlayerDisplayNameProvider playerNameProvider) { + plugin.setPlayerDisplayNameProvider(playerNameProvider); + } + public Plugin getPlugin() { return plugin; } diff --git a/common/src/main/java/de/bluecolored/bluemap/common/live/LivePlayersDataSupplier.java b/common/src/main/java/de/bluecolored/bluemap/common/live/LivePlayersDataSupplier.java index 65465ae6d..022013345 100644 --- a/common/src/main/java/de/bluecolored/bluemap/common/live/LivePlayersDataSupplier.java +++ b/common/src/main/java/de/bluecolored/bluemap/common/live/LivePlayersDataSupplier.java @@ -26,6 +26,7 @@ import com.google.gson.stream.JsonWriter; import de.bluecolored.bluemap.common.config.PluginConfig; +import de.bluecolored.bluemap.common.plugin.Plugin; import de.bluecolored.bluemap.common.serverinterface.Player; import de.bluecolored.bluemap.common.serverinterface.Server; import de.bluecolored.bluemap.common.serverinterface.ServerWorld; @@ -41,6 +42,7 @@ public class LivePlayersDataSupplier implements Supplier { + private final Plugin plugin; private final Server server; private final PluginConfig config; private final World world; @@ -48,7 +50,8 @@ public class LivePlayersDataSupplier implements Supplier { private transient @Nullable ServerWorld serverWorld; - public LivePlayersDataSupplier(Server server, PluginConfig config, World world, Predicate playerFilter) { + public LivePlayersDataSupplier(Plugin plugin, Server server, PluginConfig config, World world, Predicate playerFilter) { + this.plugin = plugin; this.server = server; this.config = config; this.world = world; @@ -83,7 +86,7 @@ public String get() { json.beginObject(); json.name("uuid").value(player.getUuid().toString()); - json.name("name").value(player.getName().toPlainString()); + json.name("name").value(this.plugin.getPlayerDisplayNameProvider().get(player.getUuid())); json.name("foreign").value(!isCorrectWorld); json.name("position").beginObject(); diff --git a/common/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java b/common/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java index 1f94fdb8c..23822c0cc 100644 --- a/common/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java +++ b/common/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java @@ -24,6 +24,7 @@ */ package de.bluecolored.bluemap.common.plugin; +import de.bluecolored.bluemap.api.plugin.PlayerDisplayNameProvider; import de.bluecolored.bluemap.common.BlueMapConfiguration; import de.bluecolored.bluemap.common.BlueMapService; import de.bluecolored.bluemap.common.InterruptableReentrantLock; @@ -101,6 +102,7 @@ public class Plugin implements ServerEventListener { private Timer daemonTimer; private Map mapUpdateServices; private PlayerSkinUpdater skinUpdater; + private PlayerDisplayNameProvider playerDisplayNameProvider; private boolean loaded = false; @@ -206,7 +208,7 @@ private void load(@Nullable ResourcePack preloadedResourcePack) throws IOExcepti MapRequestHandler mapRequestHandler; BmMap map = maps.get(id); if (map != null) { - mapRequestHandler = new MapRequestHandler(map, serverInterface, pluginConfig, Predicate.not(pluginState::isPlayerHidden)); + mapRequestHandler = new MapRequestHandler(this, map, serverInterface, pluginConfig, Predicate.not(pluginState::isPlayerHidden)); } else { Storage storage = blueMap.getOrLoadStorage(mapConfig.getStorage()); mapRequestHandler = new MapRequestHandler(storage.map(id)); @@ -279,6 +281,8 @@ private void load(@Nullable ResourcePack preloadedResourcePack) throws IOExcepti serverInterface.registerListener(skinUpdater); } + this.playerDisplayNameProvider = new ServerPlayerDisplayNameProvider(this.serverInterface); + //init timer daemonTimer = new Timer("BlueMap-Plugin-DaemonTimer", true); @@ -549,6 +553,7 @@ public void savePlayerStates() { var maps = blueMap.getMaps(); for (BmMap map : maps.values()) { var dataSupplier = new LivePlayersDataSupplier( + this, serverInterface, getBlueMap().getConfig().getPluginConfig(), map.getWorld(), @@ -657,4 +662,7 @@ public boolean isLoading() { return loadingLock.isLocked(); } + public void setPlayerDisplayNameProvider(PlayerDisplayNameProvider playerDisplayNameProvider) { + this.playerDisplayNameProvider = Objects.requireNonNull(playerDisplayNameProvider, "playerDisplayNameProvider can not be null"); + } } diff --git a/common/src/main/java/de/bluecolored/bluemap/common/plugin/ServerPlayerDisplayNameProvider.java b/common/src/main/java/de/bluecolored/bluemap/common/plugin/ServerPlayerDisplayNameProvider.java new file mode 100644 index 000000000..6f15c947d --- /dev/null +++ b/common/src/main/java/de/bluecolored/bluemap/common/plugin/ServerPlayerDisplayNameProvider.java @@ -0,0 +1,53 @@ +/* + * This file is part of BlueMap, licensed under the MIT License (MIT). + * + * Copyright (c) Blue (Lukas Rieger) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package de.bluecolored.bluemap.common.plugin; + +import de.bluecolored.bluemap.api.plugin.PlayerDisplayNameProvider; +import de.bluecolored.bluemap.common.serverinterface.Player; +import de.bluecolored.bluemap.common.serverinterface.Server; + +import java.util.UUID; + +public class ServerPlayerDisplayNameProvider implements PlayerDisplayNameProvider { + private final Server server; + + public ServerPlayerDisplayNameProvider(Server server) { + this.server = server; + } + + @Override + public String get(UUID playerUUID) { + // TODO: I am not happy with needing to do this here. + // The place that calls this function already loops over the online players, so this loops double! + // I can't really think of a better way to do this right now, though. + for (Player onlinePlayer : this.server.getOnlinePlayers()) { + if (onlinePlayer.getUuid().equals(playerUUID)) { + return onlinePlayer.getName().toPlainString(); + } + } + + return playerUUID.toString(); + } +} diff --git a/common/src/main/java/de/bluecolored/bluemap/common/web/MapRequestHandler.java b/common/src/main/java/de/bluecolored/bluemap/common/web/MapRequestHandler.java index befc9b16c..2b9250ebe 100644 --- a/common/src/main/java/de/bluecolored/bluemap/common/web/MapRequestHandler.java +++ b/common/src/main/java/de/bluecolored/bluemap/common/web/MapRequestHandler.java @@ -27,6 +27,7 @@ import de.bluecolored.bluemap.common.config.PluginConfig; import de.bluecolored.bluemap.common.live.LiveMarkersDataSupplier; import de.bluecolored.bluemap.common.live.LivePlayersDataSupplier; +import de.bluecolored.bluemap.common.plugin.Plugin; import de.bluecolored.bluemap.common.serverinterface.Server; import de.bluecolored.bluemap.common.serverinterface.ServerWorld; import de.bluecolored.bluemap.core.map.BmMap; @@ -40,9 +41,9 @@ public class MapRequestHandler extends RoutingRequestHandler { - public MapRequestHandler(BmMap map, Server serverInterface, PluginConfig pluginConfig, Predicate playerFilter) { + public MapRequestHandler(Plugin plugin, BmMap map, Server serverInterface, PluginConfig pluginConfig, Predicate playerFilter) { this(map.getStorage(), - new LivePlayersDataSupplier(serverInterface, pluginConfig, map.getWorld(), playerFilter), + new LivePlayersDataSupplier(plugin, serverInterface, pluginConfig, map.getWorld(), playerFilter), new LiveMarkersDataSupplier(map.getMarkerSets())); }