Move settings.json to map folder

This commit is contained in:
DreiMu 2021-08-30 17:34:32 +02:00
parent b9d55661b8
commit cac8acdc3f
3 changed files with 175 additions and 6 deletions

View File

@ -26,6 +26,7 @@
import de.bluecolored.bluemap.common.plugin.Plugin;
import de.bluecolored.bluemap.common.plugin.serverinterface.ServerInterface;
import de.bluecolored.bluemap.common.web.MapSettings;
import de.bluecolored.bluemap.common.web.WebSettings;
import de.bluecolored.bluemap.core.MinecraftVersion;
import de.bluecolored.bluemap.core.config.*;
@ -107,14 +108,19 @@ public synchronized WebSettings updateWebAppSettings() throws IOException, Inter
webSettings.set(getRenderConfig().isEnableFreeFlight(), "freeFlightEnabled");
webSettings.setAllMapsEnabled(false);
for (BmMap map : getMaps().values()) {
webSettings.setMap(map.getId(), map.getWorld().getUUID());
webSettings.setMapEnabled(true, map.getId());
webSettings.setFrom(map);
MapSettings mapSettings = new MapSettings(new File(getRenderConfig().getWebRoot(), "data" + File.separator + map.getId() + File.separator + "settings.json"));
mapSettings.setFrom(map);
mapSettings.save();
}
int ordinal = 0;
for (MapConfig map : getRenderConfig().getMapConfigs()) {
if (!getMaps().containsKey(map.getId())) continue; //don't add not loaded maps
webSettings.setOrdinal(ordinal++, map.getId());
webSettings.setFrom(map);
MapSettings mapSettings = new MapSettings(new File(getRenderConfig().getWebRoot(), "data" + File.separator + map.getId() + File.separator + "settings.json"));
mapSettings.setOrdinal(ordinal++);
mapSettings.setFrom(map);
mapSettings.save();
}
webSettings.save();

View File

@ -0,0 +1,157 @@
/*
* 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 com.flowpowered.math.vector.Vector3f;
import de.bluecolored.bluemap.core.config.MapConfig;
import de.bluecolored.bluemap.core.map.BmMap;
import de.bluecolored.bluemap.core.util.FileUtils;
import de.bluecolored.bluemap.core.util.MathUtils;
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.File;
import java.io.IOException;
public class MapSettings {
private final ConfigurationLoader<? extends ConfigurationNode> configLoader;
private ConfigurationNode rootNode;
public MapSettings(File settingsFile) throws IOException {
FileUtils.createFile(settingsFile);
configLoader = GsonConfigurationLoader.builder()
.file(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 String getMapId() {
return getString("world");
}
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(), "hires", "tileSize", "x");
set(hiresTileSize.getY(), "hires", "tileSize", "z");
set(1, "hires", "scale", "x");
set(1, "hires", "scale", "z");
set(gridOrigin.getX(), "hires", "translate", "x");
set(gridOrigin.getY(), "hires", "translate", "z");
Vector2i pointSize = hiresTileSize.div(lowresPointsPerHiresTile);
Vector2i tileSize = pointSize.mul(lowresTileSize);
set(tileSize.getX(), "lowres", "tileSize", "x");
set(tileSize.getY(), "lowres", "tileSize", "z");
set(pointSize.getX(),"lowres", "scale", "x");
set(pointSize.getY(),"lowres", "scale", "z");
set(pointSize.getX() / 2, "lowres", "translate", "x");
set(pointSize.getY() / 2, "lowres", "translate", "z");
set(map.getWorld().getSpawnPoint().getX(), "startPos", "x");
set(map.getWorld().getSpawnPoint().getZ(), "startPos", "z");
set(map.getWorld().getUUID().toString(), "world");
}
public void setFrom(MapConfig mapConfig) throws SerializationException {
Vector2i startPos = mapConfig.getStartPos();
if (startPos != null) {
set(startPos.getX(), "startPos", "x");
set(startPos.getY(), "startPos", "z");
}
Vector3f skyColor = MathUtils.color3FromInt(mapConfig.getSkyColor());
set(skyColor.getX(), "skyColor", "r");
set(skyColor.getY(), "skyColor", "g");
set(skyColor.getZ(), "skyColor", "b");
set(mapConfig.getAmbientLight(), "ambientLight");
setName(mapConfig.getName(), mapConfig.getId());
}
public void setOrdinal(int ordinal) throws SerializationException {
set(ordinal, "ordinal");
}
public int getOrdinal(String mapId) {
return getInt("ordinal");
}
public void setName(String name, String mapId) throws SerializationException {
set(name, "name");
}
public String getName(String mapId) {
return getString("name");
}
}

View File

@ -38,6 +38,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.UUID;
import java.util.stream.Collectors;
public class WebSettings {
@ -104,7 +105,12 @@ public void setAllMapsEnabled(boolean enabled) throws SerializationException {
public void setMapEnabled(boolean enabled, String mapId) throws SerializationException {
set(enabled, "maps", mapId, "enabled");
}
public void setMap(String mapId, UUID worldUUID) throws SerializationException {
set(worldUUID.toString(), "maps", mapId, "world");
}
@Deprecated
public void setFrom(BmMap map) throws SerializationException {
Vector2i hiresTileSize = map.getHiresModelManager().getTileGrid().getGridSize();
Vector2i gridOrigin = map.getHiresModelManager().getTileGrid().getOffset();
@ -132,7 +138,8 @@ public void setFrom(BmMap map) throws SerializationException {
set(map.getWorld().getSpawnPoint().getZ(), "maps", map.getId(), "startPos", "z");
set(map.getWorld().getUUID().toString(), "maps", map.getId(), "world");
}
@Deprecated
public void setFrom(MapConfig mapConfig) throws SerializationException {
Vector2i startPos = mapConfig.getStartPos();
if (startPos != null) {
@ -165,5 +172,4 @@ public void setName(String name, String mapId) throws SerializationException {
public String getName(String mapId) {
return getString("maps", mapId, "name");
}
}