From 1716098622ce90c7a651758cac3b7d52ee7db376 Mon Sep 17 00:00:00 2001 From: "Blue (Lukas Rieger)" Date: Sun, 16 Aug 2020 13:49:40 +0200 Subject: [PATCH] Add live-api configurations --- .../resources/bluemap-bukkit-defaults.conf | 6 +++ .../src/main/resources/bluemap-bukkit.conf | 21 ++++++++++ .../bluemap/common/BlueMapWebServer.java | 5 ++- .../common/live/LiveAPIRequestHandler.java | 16 ++++--- .../bluemap/common/plugin/Plugin.java | 8 ++-- .../plugin/serverinterface/Gamemode.java | 28 +++++++++++-- .../bluemap/core/config/LiveAPISettings.java | 39 +++++++++++++++++ .../bluemap/core/config/MainConfig.java | 42 ++++++++++++++++++- .../resources/bluemap-fabric-defaults.conf | 6 +++ .../src/main/resources/bluemap-fabric.conf | 21 ++++++++++ .../resources/bluemap-forge-defaults.conf | 6 +++ .../src/main/resources/bluemap-forge.conf | 21 ++++++++++ .../resources/bluemap-sponge-defaults.conf | 6 +++ .../src/main/resources/bluemap-sponge.conf | 20 +++++++++ 14 files changed, 230 insertions(+), 15 deletions(-) create mode 100644 BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/LiveAPISettings.java diff --git a/BlueMapBukkit/src/main/resources/bluemap-bukkit-defaults.conf b/BlueMapBukkit/src/main/resources/bluemap-bukkit-defaults.conf index c6f20621..98e58691 100644 --- a/BlueMapBukkit/src/main/resources/bluemap-bukkit-defaults.conf +++ b/BlueMapBukkit/src/main/resources/bluemap-bukkit-defaults.conf @@ -9,3 +9,9 @@ webserver { port: 8100 maxConnectionCount: 100 } +liveUpdates { + enabled: true + hiddenGameModes: [] + hideInvisible: true + hideSneaking: false +} diff --git a/BlueMapBukkit/src/main/resources/bluemap-bukkit.conf b/BlueMapBukkit/src/main/resources/bluemap-bukkit.conf index c3a60ee9..bf44441c 100644 --- a/BlueMapBukkit/src/main/resources/bluemap-bukkit.conf +++ b/BlueMapBukkit/src/main/resources/bluemap-bukkit.conf @@ -38,6 +38,7 @@ webroot: "bluemap/web" #webdata: "path/to/data/folder" # If the web-application should use cookies to save the configurations of a user. +# Default is true useCookies: true webserver { @@ -165,3 +166,23 @@ maps: [ } ] + +liveUpdates { + # If the server should send live-updates and player-positions. + # Default is true + enabled: true + + # A list of gamemodes that will prevent a player from appearing on the map. + # Possible values are: survival, creative, spectator, adventure + hiddenGameModes: [ + "spectator" + ] + + # If this is true, players that have an invisibility (potion-)effect will be hidden on the map. + # Default is true + hideInvisible: true + + # If this is true, players that are sneaking will be hidden on the map. + # Default is false + hideSneaking: false +} diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapWebServer.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapWebServer.java index e51dd030..bdfd1fe9 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapWebServer.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapWebServer.java @@ -28,6 +28,7 @@ import de.bluecolored.bluemap.common.live.LiveAPIRequestHandler; import de.bluecolored.bluemap.common.plugin.serverinterface.ServerInterface; +import de.bluecolored.bluemap.core.config.LiveAPISettings; import de.bluecolored.bluemap.core.logger.Logger; import de.bluecolored.bluemap.core.web.FileRequestHandler; import de.bluecolored.bluemap.core.web.WebFilesManager; @@ -50,12 +51,12 @@ public BlueMapWebServer(WebServerConfig config) { this.webFilesManager = new WebFilesManager(config.getWebRoot()); } - public BlueMapWebServer(WebServerConfig config, ServerInterface server) { + public BlueMapWebServer(WebServerConfig config, LiveAPISettings liveSettings, ServerInterface server) { super( config.getWebserverPort(), config.getWebserverMaxConnections(), config.getWebserverBindAdress(), - new LiveAPIRequestHandler(server, new FileRequestHandler(config.getWebRoot(), "BlueMap/Webserver")) + new LiveAPIRequestHandler(server, liveSettings, new FileRequestHandler(config.getWebRoot(), "BlueMap/Webserver")) ); this.webFilesManager = new WebFilesManager(config.getWebRoot()); diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/live/LiveAPIRequestHandler.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/live/LiveAPIRequestHandler.java index 94dbdf3f..2483cb71 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/live/LiveAPIRequestHandler.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/live/LiveAPIRequestHandler.java @@ -31,9 +31,9 @@ import com.google.gson.stream.JsonWriter; -import de.bluecolored.bluemap.common.plugin.serverinterface.Gamemode; import de.bluecolored.bluemap.common.plugin.serverinterface.Player; import de.bluecolored.bluemap.common.plugin.serverinterface.ServerInterface; +import de.bluecolored.bluemap.core.config.LiveAPISettings; import de.bluecolored.bluemap.core.webserver.HttpRequest; import de.bluecolored.bluemap.core.webserver.HttpRequestHandler; import de.bluecolored.bluemap.core.webserver.HttpResponse; @@ -45,7 +45,9 @@ public class LiveAPIRequestHandler implements HttpRequestHandler { private Map liveAPIRequests; private ServerInterface server; - public LiveAPIRequestHandler(ServerInterface server, HttpRequestHandler notFoundHandler) { + private LiveAPISettings config; + + public LiveAPIRequestHandler(ServerInterface server, LiveAPISettings config, HttpRequestHandler notFoundHandler) { this.server = server; this.notFoundHandler = notFoundHandler; @@ -53,10 +55,14 @@ public LiveAPIRequestHandler(ServerInterface server, HttpRequestHandler notFound this.liveAPIRequests.put("live", this::handleLivePingRequest); this.liveAPIRequests.put("live/players", this::handlePlayersRequest); + + this.config = config; } @Override public HttpResponse handle(HttpRequest request) { + if (!config.isLiveUpdatesEnabled()) this.notFoundHandler.handle(request); + HttpRequestHandler handler = liveAPIRequests.get(request.getPath()); if (handler != null) return handler.handle(request); @@ -81,9 +87,9 @@ public HttpResponse handlePlayersRequest(HttpRequest request) { json.name("players").beginArray(); for (Player player : server.getOnlinePlayers()) { - if (player.isInvisible()) continue; - if (player.isSneaking()) continue; - if (player.getGamemode() == Gamemode.SPECTATOR) continue; + if (config.isHideInvisible() && player.isInvisible()) continue; + if (config.isHideSneaking() && player.isSneaking()) continue; + if (config.getHiddenGameModes().contains(player.getGamemode().getId())) continue; json.beginObject(); json.name("uuid").value(player.getUuid().toString()); diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java index 5bdfff37..4c99bdac 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java @@ -272,8 +272,10 @@ public synchronized void load() throws IOException, ParseResourceException { serverInterface.registerListener(updateHandler); //start skin updater - this.skinUpdater = new PlayerSkinUpdater(config.getWebRoot().resolve("assets").resolve("playerheads").toFile()); - serverInterface.registerListener(skinUpdater); + if (config.isLiveUpdatesEnabled()) { + this.skinUpdater = new PlayerSkinUpdater(config.getWebRoot().resolve("assets").resolve("playerheads").toFile()); + serverInterface.registerListener(skinUpdater); + } //create/update webfiles WebFilesManager webFilesManager = new WebFilesManager(config.getWebRoot()); @@ -299,7 +301,7 @@ public synchronized void load() throws IOException, ParseResourceException { //start webserver if (config.isWebserverEnabled()) { - webServer = new BlueMapWebServer(config, serverInterface); + webServer = new BlueMapWebServer(config, config, serverInterface); webServer.updateWebfiles(); webServer.start(); } diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/serverinterface/Gamemode.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/serverinterface/Gamemode.java index 9012e4b9..2ed0e03a 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/serverinterface/Gamemode.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/serverinterface/Gamemode.java @@ -26,9 +26,29 @@ public enum Gamemode { - SURVIVAL, - CREATIVE, - ADVENTURE, - SPECTATOR + SURVIVAL ("survival"), + CREATIVE ("creative"), + ADVENTURE ("adventure"), + SPECTATOR ("spectator"); + + private final String id; + + Gamemode(String id){ + this.id = id; + } + + public String getId() { + return id; + } + + public static Gamemode getById(String id) { + if (id == null) throw new NullPointerException("id cannot be null"); + + for (Gamemode gamemode : values()) { + if (gamemode.id.equals(id)) return gamemode; + } + + throw new IllegalArgumentException("There is no Gamemode with id: '" + id + "'"); + } } diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/LiveAPISettings.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/LiveAPISettings.java new file mode 100644 index 00000000..121008e9 --- /dev/null +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/LiveAPISettings.java @@ -0,0 +1,39 @@ +/* + * 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.core.config; + +import java.util.Collection; + +public interface LiveAPISettings { + + boolean isLiveUpdatesEnabled(); + + Collection getHiddenGameModes(); + + boolean isHideInvisible(); + + boolean isHideSneaking(); + +} diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/MainConfig.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/MainConfig.java index 1e1b820e..c7da1a5d 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/MainConfig.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/config/MainConfig.java @@ -31,6 +31,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.regex.Pattern; @@ -43,7 +45,7 @@ import de.bluecolored.bluemap.core.web.WebServerConfig; import ninja.leaping.configurate.ConfigurationNode; -public class MainConfig implements WebServerConfig { +public class MainConfig implements WebServerConfig, LiveAPISettings { private static final Pattern VALID_ID_PATTERN = Pattern.compile("[a-zA-Z0-9_]+"); private boolean downloadAccepted = false; @@ -64,6 +66,11 @@ public class MainConfig implements WebServerConfig { private List mapConfigs = new ArrayList<>(); + private boolean liveUpdatesEnabled = false; + private Collection hiddenGameModes = Collections.emptyList(); + private boolean hideInvisible = false; + private boolean hideSneaking = false; + public MainConfig(ConfigurationNode node) throws OutdatedConfigException, IOException { checkOutdated(node); @@ -101,6 +108,19 @@ public MainConfig(ConfigurationNode node) throws OutdatedConfigException, IOExce //maps loadMapConfigs(node.getNode("maps")); + + //live-updates + ConfigurationNode liveNode = node.getNode("liveUpdates"); + liveUpdatesEnabled = liveNode.getNode("enabled").getBoolean(true); + + hiddenGameModes = new ArrayList<>(); + for (ConfigurationNode gameModeNode : liveNode.getNode("hiddenGameModes").getChildrenList()) { + hiddenGameModes.add(gameModeNode.getString()); + } + hiddenGameModes = Collections.unmodifiableCollection(hiddenGameModes); + + hideInvisible = liveNode.getNode("hideInvisible").getBoolean(true); + hideSneaking = liveNode.getNode("hideSneaking").getBoolean(false); } private void loadWebConfig(ConfigurationNode node) throws IOException { @@ -196,6 +216,26 @@ public List getMapConfigs(){ return mapConfigs; } + @Override + public boolean isLiveUpdatesEnabled() { + return this.liveUpdatesEnabled; + } + + @Override + public Collection getHiddenGameModes() { + return this.hiddenGameModes; + } + + @Override + public boolean isHideInvisible() { + return this.hideInvisible; + } + + @Override + public boolean isHideSneaking() { + return this.hideSneaking; + } + public class MapConfig implements RenderSettings { private String id; diff --git a/BlueMapFabric/src/main/resources/bluemap-fabric-defaults.conf b/BlueMapFabric/src/main/resources/bluemap-fabric-defaults.conf index c6f20621..98e58691 100644 --- a/BlueMapFabric/src/main/resources/bluemap-fabric-defaults.conf +++ b/BlueMapFabric/src/main/resources/bluemap-fabric-defaults.conf @@ -9,3 +9,9 @@ webserver { port: 8100 maxConnectionCount: 100 } +liveUpdates { + enabled: true + hiddenGameModes: [] + hideInvisible: true + hideSneaking: false +} diff --git a/BlueMapFabric/src/main/resources/bluemap-fabric.conf b/BlueMapFabric/src/main/resources/bluemap-fabric.conf index af9edf92..c56e51a9 100644 --- a/BlueMapFabric/src/main/resources/bluemap-fabric.conf +++ b/BlueMapFabric/src/main/resources/bluemap-fabric.conf @@ -38,6 +38,7 @@ webroot: "bluemap/web" #webdata: "path/to/data/folder" # If the web-application should use cookies to save the configurations of a user. +# Default is true useCookies: true webserver { @@ -165,3 +166,23 @@ maps: [ } ] + +liveUpdates { + # If the server should send live-updates and player-positions. + # Default is true + enabled: true + + # A list of gamemodes that will prevent a player from appearing on the map. + # Possible values are: survival, creative, spectator, adventure + hiddenGameModes: [ + "spectator" + ] + + # If this is true, players that have an invisibility (potion-)effect will be hidden on the map. + # Default is true + hideInvisible: true + + # If this is true, players that are sneaking will be hidden on the map. + # Default is false + hideSneaking: false +} diff --git a/BlueMapForge/src/main/resources/bluemap-forge-defaults.conf b/BlueMapForge/src/main/resources/bluemap-forge-defaults.conf index c6f20621..98e58691 100644 --- a/BlueMapForge/src/main/resources/bluemap-forge-defaults.conf +++ b/BlueMapForge/src/main/resources/bluemap-forge-defaults.conf @@ -9,3 +9,9 @@ webserver { port: 8100 maxConnectionCount: 100 } +liveUpdates { + enabled: true + hiddenGameModes: [] + hideInvisible: true + hideSneaking: false +} diff --git a/BlueMapForge/src/main/resources/bluemap-forge.conf b/BlueMapForge/src/main/resources/bluemap-forge.conf index af9edf92..c56e51a9 100644 --- a/BlueMapForge/src/main/resources/bluemap-forge.conf +++ b/BlueMapForge/src/main/resources/bluemap-forge.conf @@ -38,6 +38,7 @@ webroot: "bluemap/web" #webdata: "path/to/data/folder" # If the web-application should use cookies to save the configurations of a user. +# Default is true useCookies: true webserver { @@ -165,3 +166,23 @@ maps: [ } ] + +liveUpdates { + # If the server should send live-updates and player-positions. + # Default is true + enabled: true + + # A list of gamemodes that will prevent a player from appearing on the map. + # Possible values are: survival, creative, spectator, adventure + hiddenGameModes: [ + "spectator" + ] + + # If this is true, players that have an invisibility (potion-)effect will be hidden on the map. + # Default is true + hideInvisible: true + + # If this is true, players that are sneaking will be hidden on the map. + # Default is false + hideSneaking: false +} diff --git a/BlueMapSponge/src/main/resources/bluemap-sponge-defaults.conf b/BlueMapSponge/src/main/resources/bluemap-sponge-defaults.conf index f0a3bdb0..cf42dfc8 100644 --- a/BlueMapSponge/src/main/resources/bluemap-sponge-defaults.conf +++ b/BlueMapSponge/src/main/resources/bluemap-sponge-defaults.conf @@ -9,3 +9,9 @@ webserver { port: 8100 maxConnectionCount: 100 } +liveUpdates { + enabled: true + hiddenGameModes: [] + hideInvisible: true + hideSneaking: false +} diff --git a/BlueMapSponge/src/main/resources/bluemap-sponge.conf b/BlueMapSponge/src/main/resources/bluemap-sponge.conf index c97e6ea8..c126f710 100644 --- a/BlueMapSponge/src/main/resources/bluemap-sponge.conf +++ b/BlueMapSponge/src/main/resources/bluemap-sponge.conf @@ -160,3 +160,23 @@ maps: [ } ] + +liveUpdates { + # If the server should send live-updates and player-positions. + # Default is true + enabled: true + + # A list of gamemodes that will prevent a player from appearing on the map. + # Possible values are: survival, creative, spectator, adventure + hiddenGameModes: [ + "spectator" + ] + + # If this is true, players that have an invisibility (potion-)effect will be hidden on the map. + # Default is true + hideInvisible: true + + # If this is true, players that are sneaking will be hidden on the map. + # Default is false + hideSneaking: false +}