Skip to content

Commit 903ab64

Browse files
committed
Add --markers option to cli
1 parent 47f4aea commit 903ab64

File tree

3 files changed

+81
-25
lines changed

3 files changed

+81
-25
lines changed

common/src/main/java/de/bluecolored/bluemap/common/BlueMapService.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,27 +249,9 @@ private synchronized void loadMap(String id, MapConfig mapConfig) throws Configu
249249
);
250250
maps.put(id, map);
251251

252-
// load marker-config by converting it first from hocon to json and then loading it with MarkerGson
253-
ConfigurationNode markerSetNode = mapConfig.getMarkerSets();
254-
if (markerSetNode != null && !markerSetNode.empty()) {
255-
String markerJson = GsonConfigurationLoader.builder()
256-
.headerMode(HeaderMode.NONE)
257-
.lenient(false)
258-
.indent(0)
259-
.buildAndSaveString(markerSetNode);
260-
Gson gson = MarkerGson.addAdapters(new GsonBuilder())
261-
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
262-
.create();
263-
Type markerSetType = new TypeToken<Map<String, MarkerSet>>() {}.getType();
264-
Map<String, MarkerSet> markerSets = gson.fromJson(markerJson, markerSetType);
265-
map.getMarkerSets().putAll(markerSets);
266-
}
252+
// load markers
253+
map.getMarkerSets().putAll(mapConfig.parseMarkerSets());
267254

268-
} catch (ConfigurateException | JsonParseException ex) {
269-
throw new ConfigurationException(
270-
"Failed to create the markers for map '" + id + "'!\n" +
271-
"Make sure your marker-configuration for this map is valid.",
272-
ex);
273255
} catch (IOException | ConfigurationException ex) {
274256
throw new ConfigurationException("Failed to load map '" + id + "'!", ex);
275257
}

common/src/main/java/de/bluecolored/bluemap/common/config/MapConfig.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,27 @@
2626

2727
import com.flowpowered.math.vector.Vector2i;
2828
import com.flowpowered.math.vector.Vector3i;
29+
import com.google.gson.FieldNamingPolicy;
30+
import com.google.gson.Gson;
31+
import com.google.gson.GsonBuilder;
32+
import com.google.gson.JsonParseException;
33+
import com.google.gson.reflect.TypeToken;
34+
import de.bluecolored.bluemap.api.gson.MarkerGson;
35+
import de.bluecolored.bluemap.api.markers.MarkerSet;
2936
import de.bluecolored.bluemap.core.map.MapSettings;
3037
import de.bluecolored.bluemap.core.util.Key;
3138
import lombok.AccessLevel;
3239
import lombok.Getter;
3340
import org.jetbrains.annotations.Nullable;
41+
import org.spongepowered.configurate.ConfigurateException;
3442
import org.spongepowered.configurate.ConfigurationNode;
43+
import org.spongepowered.configurate.gson.GsonConfigurationLoader;
44+
import org.spongepowered.configurate.loader.HeaderMode;
3545
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
3646

47+
import java.lang.reflect.Type;
3748
import java.nio.file.Path;
49+
import java.util.Map;
3850

3951
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
4052
@ConfigSerializable
@@ -104,4 +116,26 @@ public Vector3i getMaxPos() {
104116
return max;
105117
}
106118

119+
/**
120+
* parse marker-config by converting it first from hocon to json and then loading it with MarkerGson
121+
*/
122+
public Map<String, MarkerSet> parseMarkerSets() throws ConfigurationException {
123+
if (markerSets == null || markerSets.empty()) return Map.of();
124+
try {
125+
String markerJson = GsonConfigurationLoader.builder()
126+
.headerMode(HeaderMode.NONE)
127+
.lenient(false)
128+
.indent(0)
129+
.buildAndSaveString(markerSets);
130+
Gson gson = MarkerGson.addAdapters(new GsonBuilder())
131+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
132+
.create();
133+
Type markerSetType = new TypeToken<Map<String, MarkerSet>>() {}.getType();
134+
return gson.fromJson(markerJson, markerSetType);
135+
} catch (ConfigurateException | JsonParseException ex) {
136+
throw new ConfigurationException("Failed to parse marker-sets." +
137+
"Make sure your marker-configuration for this map is valid.", ex);
138+
}
139+
}
140+
107141
}

implementations/cli/src/main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@
2424
*/
2525
package de.bluecolored.bluemap.cli;
2626

27+
import de.bluecolored.bluemap.api.gson.MarkerGson;
2728
import de.bluecolored.bluemap.common.BlueMapConfiguration;
2829
import de.bluecolored.bluemap.common.BlueMapService;
2930
import de.bluecolored.bluemap.common.MissingResourcesException;
3031
import de.bluecolored.bluemap.common.addons.Addons;
3132
import de.bluecolored.bluemap.common.api.BlueMapAPIImpl;
3233
import de.bluecolored.bluemap.common.commands.TextFormat;
33-
import de.bluecolored.bluemap.common.config.BlueMapConfigManager;
34-
import de.bluecolored.bluemap.common.config.ConfigurationException;
35-
import de.bluecolored.bluemap.common.config.CoreConfig;
36-
import de.bluecolored.bluemap.common.config.WebserverConfig;
34+
import de.bluecolored.bluemap.common.config.*;
3735
import de.bluecolored.bluemap.common.metrics.Metrics;
3836
import de.bluecolored.bluemap.common.plugin.MapUpdateService;
39-
import de.bluecolored.bluemap.common.rendermanager.*;
37+
import de.bluecolored.bluemap.common.rendermanager.MapUpdatePreparationTask;
38+
import de.bluecolored.bluemap.common.rendermanager.RenderManager;
39+
import de.bluecolored.bluemap.common.rendermanager.RenderTask;
40+
import de.bluecolored.bluemap.common.rendermanager.TileUpdateStrategy;
4041
import de.bluecolored.bluemap.common.web.*;
4142
import de.bluecolored.bluemap.common.web.http.HttpRequestHandler;
4243
import de.bluecolored.bluemap.common.web.http.HttpServer;
@@ -49,9 +50,13 @@
4950
import org.checkerframework.checker.nullness.qual.Nullable;
5051

5152
import java.io.IOException;
53+
import java.io.OutputStream;
54+
import java.io.OutputStreamWriter;
55+
import java.io.Writer;
5256
import java.net.BindException;
5357
import java.net.InetSocketAddress;
5458
import java.net.UnknownHostException;
59+
import java.nio.charset.StandardCharsets;
5560
import java.nio.file.Files;
5661
import java.nio.file.Path;
5762
import java.time.Duration;
@@ -216,6 +221,34 @@ public void run() {
216221
}
217222
}
218223

224+
public void updateMarkers(BlueMapService blueMap, @Nullable String mapsToUpdate) {
225+
Predicate<String> mapFilter = mapId -> true;
226+
if (mapsToUpdate != null) {
227+
Set<String> mapsToRenderSet = Set.of(mapsToUpdate.split(","));
228+
mapFilter = mapsToRenderSet::contains;
229+
}
230+
231+
for (Map.Entry<String, MapConfig> entry : blueMap.getConfig().getMapConfigs().entrySet()) {
232+
String mapId = entry.getKey();
233+
MapConfig mapConfig = entry.getValue();
234+
235+
if (!mapFilter.test(mapId)) return;
236+
237+
try {
238+
MapStorage storage = blueMap.getOrLoadStorage(mapConfig.getStorage()).map(mapId);
239+
try (
240+
OutputStream out = storage.markers().write();
241+
Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)
242+
) {
243+
MarkerGson.INSTANCE.toJson(mapConfig.parseMarkerSets(), writer);
244+
}
245+
Logger.global.logInfo("Updated markers for map '" + mapId + "'");
246+
} catch (Exception ex) {
247+
Logger.global.logError("Failed to save markers for map '" + mapId + "'!", ex);
248+
}
249+
}
250+
}
251+
219252
public void startWebserver(BlueMapService blueMap, boolean verbose) throws IOException, ConfigurationException, InterruptedException {
220253
Logger.global.logInfo("Starting webserver ...");
221254

@@ -374,6 +407,11 @@ public static void main(String[] args) {
374407
String mapsToRender = cmd.getOptionValue("m", null);
375408
cli.renderMaps(blueMap, watch, force, generateWebappFiles, mapsToRender);
376409
} else {
410+
if (cmd.hasOption("markers")) {
411+
noActions = false;
412+
String mapsToUpdate = cmd.getOptionValue("m", null);
413+
cli.updateMarkers(blueMap, mapsToUpdate);
414+
}
377415
if (cmd.hasOption("g")) {
378416
noActions = false;
379417
blueMap.createOrUpdateWebApp(true);
@@ -480,6 +518,8 @@ private static Options createOptions() {
480518
options.addOption("f", "force-render", false, "Forces rendering everything, instead of only rendering chunks that have been modified since the last render");
481519
options.addOption("m", "maps", true, "A comma-separated list of map-id's that should be rendered. Example: 'world,nether'");
482520

521+
options.addOption(null, "markers", false, "Updates the map-markers based on the map configs");
522+
483523
options.addOption("u", "watch", false, "Watches for file-changes after rendering and updates the map");
484524

485525
options.addOption("V", "version", false, "Print the current BlueMap version");

0 commit comments

Comments
 (0)