Working on moving settings.json

This commit is contained in:
Lukas Rieger (Blue) 2022-06-03 18:52:15 +02:00
parent b28b4e8c1f
commit b92536da64
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
10 changed files with 131 additions and 179 deletions

View File

@ -31,7 +31,6 @@
import de.bluecolored.bluemap.common.plugin.Plugin;
import de.bluecolored.bluemap.common.plugin.serverinterface.ServerInterface;
import de.bluecolored.bluemap.common.plugin.serverinterface.ServerWorld;
import de.bluecolored.bluemap.common.web.WebSettings;
import de.bluecolored.bluemap.core.MinecraftVersion;
import de.bluecolored.bluemap.core.debug.DebugDump;
import de.bluecolored.bluemap.core.debug.StateDumper;

View File

@ -8,6 +8,7 @@
import org.spongepowered.configurate.objectmapping.meta.Required;
import java.nio.file.Path;
import java.util.Optional;
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
@DebugDump
@ -19,6 +20,8 @@ public class MapConfig implements MapSettings {
@Required
private Path world = Path.of("world");
private int sorting = 0;
private Vector2i startPos = null;
private String skyColor = "#7dabff";
@ -60,8 +63,12 @@ public Path getWorld() {
return world;
}
public Vector2i getStartPos() {
return startPos;
public int getSorting() {
return sorting;
}
public Optional<Vector2i> getStartPos() {
return Optional.of(startPos);
}
public String getSkyColor() {

View File

@ -1,172 +0,0 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* 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.web;
import com.flowpowered.math.vector.Vector2i;
import de.bluecolored.bluemap.common.config.MapConfig;
import de.bluecolored.bluemap.core.map.BmMap;
import de.bluecolored.bluemap.core.util.ConfigUtils;
import de.bluecolored.bluemap.core.util.math.Color;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.gson.GsonConfigurationLoader;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.serialize.SerializationException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.stream.Collectors;
public class WebSettings {
private final ConfigurationLoader<? extends ConfigurationNode> configLoader;
private ConfigurationNode rootNode;
public WebSettings(Path settingsFile) throws IOException {
if (!Files.exists(settingsFile)) {
Files.createDirectories(settingsFile.getParent());
Files.createFile(settingsFile);
}
configLoader = GsonConfigurationLoader.builder()
.path(settingsFile)
.build();
load();
}
public void load() throws IOException {
rootNode = configLoader.load();
}
public void save() throws IOException {
configLoader.save(rootNode);
}
public void set(Object value, Object... path) throws SerializationException {
rootNode.node(path).set(value);
}
public Object get(Object... path) {
return rootNode.node(path).raw();
}
public String getString(Object... path) {
return rootNode.node(path).getString();
}
public int getInt(Object... path) {
return rootNode.node(path).getInt();
}
public long getLong(Object... path) {
return rootNode.node(path).getLong();
}
public float getFloat(Object... path) {
return rootNode.node(path).getFloat();
}
public double getDouble(Object... path) {
return rootNode.node(path).getDouble();
}
public Collection<String> getMapIds() {
return rootNode.node("maps").childrenMap().keySet().stream().map(Object::toString).collect(Collectors.toSet());
}
public void setAllMapsEnabled(boolean enabled) throws SerializationException {
for (ConfigurationNode mapNode : rootNode.node("maps").childrenMap().values()) {
mapNode.node("enabled").set(enabled);
}
}
public void setMapEnabled(boolean enabled, String mapId) throws SerializationException {
set(enabled, "maps", mapId, "enabled");
}
public void setFrom(BmMap map) throws SerializationException {
Vector2i hiresTileSize = map.getHiresModelManager().getTileGrid().getGridSize();
Vector2i gridOrigin = map.getHiresModelManager().getTileGrid().getOffset();
Vector2i lowresTileSize = map.getLowresModelManager().getTileSize();
Vector2i lowresPointsPerHiresTile = map.getLowresModelManager().getPointsPerHiresTile();
set(hiresTileSize.getX(), "maps", map.getId(), "hires", "tileSize", "x");
set(hiresTileSize.getY(), "maps", map.getId(), "hires", "tileSize", "z");
set(1, "maps", map.getId(), "hires", "scale", "x");
set(1, "maps", map.getId(), "hires", "scale", "z");
set(gridOrigin.getX(), "maps", map.getId(), "hires", "translate", "x");
set(gridOrigin.getY(), "maps", map.getId(), "hires", "translate", "z");
Vector2i pointSize = hiresTileSize.div(lowresPointsPerHiresTile);
Vector2i tileSize = pointSize.mul(lowresTileSize);
set(tileSize.getX(), "maps", map.getId(), "lowres", "tileSize", "x");
set(tileSize.getY(), "maps", map.getId(), "lowres", "tileSize", "z");
set(pointSize.getX(), "maps", map.getId(), "lowres", "scale", "x");
set(pointSize.getY(), "maps", map.getId(), "lowres", "scale", "z");
set(pointSize.getX() / 2, "maps", map.getId(), "lowres", "translate", "x");
set(pointSize.getY() / 2, "maps", map.getId(), "lowres", "translate", "z");
set(map.getWorld().getSpawnPoint().getX(), "maps", map.getId(), "startPos", "x");
set(map.getWorld().getSpawnPoint().getZ(), "maps", map.getId(), "startPos", "z");
set(map.getWorldId(), "maps", map.getId(), "world");
}
public void setFrom(MapConfig mapConfig, String mapId) throws SerializationException {
Vector2i startPos = mapConfig.getStartPos();
if (startPos != null) {
set(startPos.getX(), "maps", mapId, "startPos", "x");
set(startPos.getY(), "maps", mapId, "startPos", "z");
}
Color skyColor = new Color().set(ConfigUtils.parseColorFromString(mapConfig.getSkyColor()));
set(skyColor.r, "maps", mapId, "skyColor", "r");
set(skyColor.g, "maps", mapId, "skyColor", "g");
set(skyColor.b, "maps", mapId, "skyColor", "b");
set(mapConfig.getAmbientLight(), "maps", mapId, "ambientLight");
setName(mapConfig.getName(), mapId);
}
public void setOrdinal(int ordinal, String mapId) throws SerializationException {
set(ordinal, "maps", mapId, "ordinal");
}
public int getOrdinal(String mapId) {
return getInt("maps", mapId, "ordinal");
}
public void setName(String name, String mapId) throws SerializationException {
set(name, "maps", mapId, "name");
}
public String getName(String mapId) {
return getString("maps", mapId, "name");
}
}

View File

@ -11,6 +11,10 @@ name: "${name}"
# The path to the save-folder of the world to render.
world: "${world}"
# A higher value makes the map sorted first (in lists and menus), a lower value makes it sorted later.
# Default is 0
sorting: 0
# The position on the world where the map will be centered if you open it.
# You can change this at any time.
# This defaults to the world-spawn if you don't set it.

View File

@ -30,6 +30,7 @@
import de.bluecolored.bluemap.core.map.hires.HiresModelManager;
import de.bluecolored.bluemap.core.map.hires.HiresTileMeta;
import de.bluecolored.bluemap.core.map.lowres.LowresModelManager;
import de.bluecolored.bluemap.core.resources.adapter.ResourcesGson;
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.storage.CompressedInputStream;
import de.bluecolored.bluemap.core.storage.MetaType;
@ -38,9 +39,7 @@
import de.bluecolored.bluemap.core.world.Grid;
import de.bluecolored.bluemap.core.world.World;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
@ -84,6 +83,8 @@ public BmMap(String id, String name, String worldId, World world, Storage storag
this.textureGallery.put(resourcePack);
saveTextureGallery();
saveMapSettings();
this.hiresModelManager = new HiresModelManager(
storage.tileStorage(id, TileType.HIRES),
this.resourcePack,
@ -164,6 +165,17 @@ private void saveTextureGallery() {
}
}
private void saveMapSettings() {
try (
OutputStream out = storage.writeMeta(id, MetaType.SETTINGS);
Writer writer = new OutputStreamWriter(out)
) {
ResourcesGson.INSTANCE.toJson(this, writer);
} catch (Exception ex) {
Logger.global.logError("Failed to save settings for map '" + getId() + "'!", ex);
}
}
public String getId() {
return id;
}

View File

@ -24,10 +24,19 @@
*/
package de.bluecolored.bluemap.core.map;
import com.flowpowered.math.vector.Vector2i;
import de.bluecolored.bluemap.core.map.hires.RenderSettings;
import java.util.Optional;
public interface MapSettings extends RenderSettings {
int getSorting();
Optional<Vector2i> getStartPos();
String getSkyColor();
int getHiresTileSize();
int getLowresPointsPerLowresTile();

View File

@ -0,0 +1,60 @@
package de.bluecolored.bluemap.core.resources.adapter;
import com.flowpowered.math.vector.Vector2i;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import de.bluecolored.bluemap.core.map.BmMap;
import de.bluecolored.bluemap.core.util.ConfigUtils;
import de.bluecolored.bluemap.core.util.math.Color;
import java.lang.reflect.Type;
public class MapSettingsSerializer implements JsonSerializer<BmMap> {
@Override
public JsonElement serialize(BmMap map, Type typeOfSrc, JsonSerializationContext context) {
JsonObject root = new JsonObject();
// name
root.addProperty("name", map.getName());
// hires
Vector2i hiresTileSize = map.getHiresModelManager().getTileGrid().getGridSize();
Vector2i gridOrigin = map.getHiresModelManager().getTileGrid().getOffset();
Vector2i lowresTileSize = map.getLowresModelManager().getTileSize();
Vector2i lowresPointsPerHiresTile = map.getLowresModelManager().getPointsPerHiresTile();
JsonObject hires = new JsonObject();
hires.add("tileSize", context.serialize(hiresTileSize));
hires.add("scale", context.serialize(Vector2i.ONE));
hires.add("translate", context.serialize(gridOrigin));
root.add("hires", hires);
// lowres
Vector2i pointSize = hiresTileSize.div(lowresPointsPerHiresTile);
Vector2i tileSize = pointSize.mul(lowresTileSize);
JsonObject lowres = new JsonObject();
lowres.add("tileSize", context.serialize(tileSize));
lowres.add("scale", context.serialize(pointSize));
lowres.add("translate", context.serialize(pointSize.div(2)));
root.add("lowres", lowres);
// startPos
Vector2i startPos = map.getMapSettings().getStartPos()
.orElse(map.getWorld().getSpawnPoint().toVector2(true));
root.add("startPos", context.serialize(startPos));
// skyColor
Color skyColor = new Color().set(ConfigUtils.parseColorFromString(map.getMapSettings().getSkyColor()));
root.add("skyColor", context.serialize(skyColor));
// ambientLight
root.addProperty("ambientLight", map.getMapSettings().getAmbientLight());
return root;
}
}

View File

@ -9,6 +9,7 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import de.bluecolored.bluemap.core.map.BmMap;
import de.bluecolored.bluemap.core.resources.resourcepack.blockmodel.Face;
import de.bluecolored.bluemap.core.util.Direction;
import de.bluecolored.bluemap.core.util.math.Axis;
@ -32,6 +33,7 @@ private static Gson createGson() {
.registerTypeAdapter(Vector3f.class, new Vector3fAdapter())
.registerTypeAdapter(Vector4d.class, new Vector4dAdapter())
.registerTypeAdapter(Vector4f.class, new Vector4fAdapter())
.registerTypeAdapter(BmMap.class, new MapSettingsSerializer())
.registerTypeAdapter(
new TypeToken<EnumMap<Direction, Face>>(){}.getType(),
new EnumMapInstanceCreator<Direction, Face>(Direction.class)

View File

@ -0,0 +1,31 @@
package de.bluecolored.bluemap.core.resources.adapter;
import com.flowpowered.math.vector.Vector2i;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
public class Vector2iAdapter extends TypeAdapter<Vector2i> {
@Override
public void write(JsonWriter out, Vector2i value) throws IOException {
out.beginArray();
out.value(value.getX());
out.value(value.getY());
out.endArray();
}
@Override
public Vector2i read(JsonReader in) throws IOException {
in.beginArray();
Vector2i value = new Vector2i(
in.nextDouble(),
in.nextDouble()
);
in.endArray();
return value;
}
}

View File

@ -27,7 +27,7 @@
public enum MetaType {
TEXTURES ("textures", "textures.json", "application/json"),
//SETTINGS ("settings", "settings.json", "application/json"),
SETTINGS ("settings", "settings.json", "application/json"),
//MARKERS ("markers", "markers.json", "application/json"),
RENDER_STATE ("render_state", ".rstate", "application/octet-stream");