2020-08-24 23:58:11 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2022-07-30 13:24:07 +02:00
|
|
|
import com.google.gson.FieldNamingPolicy;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonParseException;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import de.bluecolored.bluemap.api.debug.DebugDump;
|
2022-08-01 12:53:58 +02:00
|
|
|
import de.bluecolored.bluemap.api.gson.MarkerGson;
|
2022-07-30 13:24:07 +02:00
|
|
|
import de.bluecolored.bluemap.api.markers.MarkerSet;
|
2022-04-20 22:54:27 +02:00
|
|
|
import de.bluecolored.bluemap.common.config.ConfigurationException;
|
|
|
|
import de.bluecolored.bluemap.common.config.MapConfig;
|
|
|
|
import de.bluecolored.bluemap.common.config.storage.StorageConfig;
|
2020-08-24 23:58:11 +02:00
|
|
|
import de.bluecolored.bluemap.common.plugin.Plugin;
|
2020-08-25 15:07:42 +02:00
|
|
|
import de.bluecolored.bluemap.core.MinecraftVersion;
|
2022-05-28 21:55:41 +02:00
|
|
|
import de.bluecolored.bluemap.core.debug.StateDumper;
|
2020-08-24 23:58:11 +02:00
|
|
|
import de.bluecolored.bluemap.core.logger.Logger;
|
2021-04-24 22:11:26 +02:00
|
|
|
import de.bluecolored.bluemap.core.map.BmMap;
|
2024-02-02 16:49:32 +01:00
|
|
|
import de.bluecolored.bluemap.core.resources.datapack.DataPack;
|
2022-05-28 21:55:41 +02:00
|
|
|
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
|
2021-11-06 16:14:14 +01:00
|
|
|
import de.bluecolored.bluemap.core.storage.Storage;
|
2022-10-14 10:24:19 +02:00
|
|
|
import de.bluecolored.bluemap.core.util.FileHelper;
|
2023-11-08 13:27:51 +01:00
|
|
|
import de.bluecolored.bluemap.core.util.Key;
|
2020-08-24 23:58:11 +02:00
|
|
|
import de.bluecolored.bluemap.core.world.World;
|
2023-11-08 13:27:51 +01:00
|
|
|
import de.bluecolored.bluemap.core.world.mca.MCAWorld;
|
2021-03-28 18:56:26 +02:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2023-01-03 16:03:07 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2022-07-30 13:24:07 +02:00
|
|
|
import org.spongepowered.configurate.ConfigurateException;
|
2022-08-09 22:35:21 +02:00
|
|
|
import org.spongepowered.configurate.ConfigurationNode;
|
2022-07-30 13:24:07 +02:00
|
|
|
import org.spongepowered.configurate.gson.GsonConfigurationLoader;
|
|
|
|
import org.spongepowered.configurate.loader.HeaderMode;
|
2021-03-28 18:56:26 +02:00
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
import java.io.Closeable;
|
2021-03-28 18:56:26 +02:00
|
|
|
import java.io.IOException;
|
2022-07-30 13:24:07 +02:00
|
|
|
import java.lang.reflect.Type;
|
2021-03-28 18:56:26 +02:00
|
|
|
import java.net.URL;
|
2021-11-14 14:18:31 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2021-03-28 18:56:26 +02:00
|
|
|
import java.util.*;
|
2022-08-09 22:35:21 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2024-01-23 19:35:25 +01:00
|
|
|
import java.util.function.Predicate;
|
2022-05-28 21:55:41 +02:00
|
|
|
import java.util.stream.Stream;
|
2020-08-24 23:58:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the attempt to generalize as many actions as possible to have CLI and Plugins run on the same general setup-code.
|
|
|
|
*/
|
2021-06-16 20:09:45 +02:00
|
|
|
@DebugDump
|
2022-08-27 15:08:17 +02:00
|
|
|
public class BlueMapService implements Closeable {
|
2023-03-13 19:30:31 +01:00
|
|
|
|
2023-11-18 13:50:13 +01:00
|
|
|
private final BlueMapConfiguration config;
|
|
|
|
private final WebFilesManager webFilesManager;
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
private ResourcePack resourcePack;
|
|
|
|
private final Map<String, World> worlds;
|
2023-11-18 13:50:13 +01:00
|
|
|
private final Map<String, BmMap> maps;
|
2021-11-14 14:18:31 +01:00
|
|
|
private final Map<String, Storage> storages;
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
|
|
|
|
public BlueMapService(BlueMapConfiguration configuration, @Nullable ResourcePack preloadedResourcePack) {
|
|
|
|
this(configuration);
|
|
|
|
this.resourcePack = preloadedResourcePack;
|
|
|
|
}
|
|
|
|
|
2023-11-18 13:50:13 +01:00
|
|
|
public BlueMapService(BlueMapConfiguration configuration) {
|
|
|
|
this.config = configuration;
|
|
|
|
this.webFilesManager = new WebFilesManager(config.getWebappConfig().getWebroot());
|
2022-12-13 16:54:31 +01:00
|
|
|
|
2023-11-18 13:50:13 +01:00
|
|
|
this.worlds = new ConcurrentHashMap<>();
|
|
|
|
this.maps = new ConcurrentHashMap<>();
|
|
|
|
this.storages = new ConcurrentHashMap<>();
|
2022-05-28 21:55:41 +02:00
|
|
|
|
|
|
|
StateDumper.global().register(this);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 16:54:31 +01:00
|
|
|
public WebFilesManager getWebFilesManager() {
|
|
|
|
return webFilesManager;
|
|
|
|
}
|
|
|
|
|
2021-11-14 14:18:31 +01:00
|
|
|
public synchronized void createOrUpdateWebApp(boolean force) throws ConfigurationException {
|
2022-06-06 22:51:26 +02:00
|
|
|
try {
|
2022-12-13 16:54:31 +01:00
|
|
|
WebFilesManager webFilesManager = getWebFilesManager();
|
2022-06-06 22:51:26 +02:00
|
|
|
|
|
|
|
// update web-app files
|
|
|
|
if (force || webFilesManager.filesNeedUpdate()) {
|
2021-11-14 14:18:31 +01:00
|
|
|
webFilesManager.updateFiles();
|
|
|
|
}
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2022-06-06 22:51:26 +02:00
|
|
|
// update settings.json
|
2023-11-18 13:50:13 +01:00
|
|
|
if (!config.getWebappConfig().isUpdateSettingsFile()) {
|
2022-06-06 22:51:26 +02:00
|
|
|
webFilesManager.loadSettings();
|
2023-11-18 13:50:13 +01:00
|
|
|
webFilesManager.addFrom(config.getWebappConfig());
|
2022-06-06 22:51:26 +02:00
|
|
|
} else {
|
2023-11-18 13:50:13 +01:00
|
|
|
webFilesManager.setFrom(config.getWebappConfig());
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
2023-11-18 13:50:13 +01:00
|
|
|
for (String mapId : config.getMapConfigs().keySet()) {
|
2022-06-06 22:51:26 +02:00
|
|
|
webFilesManager.addMap(mapId);
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
2022-06-06 22:51:26 +02:00
|
|
|
webFilesManager.saveSettings();
|
|
|
|
|
2021-11-14 14:18:31 +01:00
|
|
|
} catch (IOException ex) {
|
2022-06-06 22:51:26 +02:00
|
|
|
throw new ConfigurationException("Failed to update web-app files!", ex);
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
/**
|
|
|
|
* Gets all loaded maps.
|
|
|
|
* @return A map of loaded maps
|
|
|
|
*/
|
|
|
|
public Map<String, BmMap> getMaps() {
|
|
|
|
return Collections.unmodifiableMap(maps);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
/**
|
|
|
|
* Gets all loaded worlds.
|
|
|
|
* @return A map of loaded worlds
|
|
|
|
*/
|
|
|
|
public Map<String, World> getWorlds() {
|
|
|
|
return Collections.unmodifiableMap(worlds);
|
2023-11-18 13:50:13 +01:00
|
|
|
}
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
/**
|
|
|
|
* Gets or loads configured maps.
|
|
|
|
* @return A map of loaded maps
|
|
|
|
*/
|
|
|
|
public Map<String, BmMap> getOrLoadMaps() throws InterruptedException {
|
|
|
|
return getOrLoadMaps(mapId -> true);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
/**
|
|
|
|
* Gets or loads configured maps.
|
|
|
|
* @param filter A predicate filtering map-ids that should be loaded
|
|
|
|
* (if maps are already loaded, they will be returned as well)
|
|
|
|
* @return A map of all loaded maps
|
|
|
|
*/
|
|
|
|
public synchronized Map<String, BmMap> getOrLoadMaps(Predicate<String> filter) throws InterruptedException {
|
2023-11-18 13:50:13 +01:00
|
|
|
for (var entry : config.getMapConfigs().entrySet()) {
|
2024-02-01 23:01:42 +01:00
|
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
|
|
|
|
|
|
|
if (!filter.test(entry.getKey())) continue;
|
|
|
|
if (maps.containsKey(entry.getKey())) continue;
|
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
try {
|
2024-02-01 23:01:42 +01:00
|
|
|
loadMap(entry.getKey(), entry.getValue());
|
2022-08-27 15:08:17 +02:00
|
|
|
} catch (ConfigurationException ex) {
|
2022-09-12 12:02:36 +02:00
|
|
|
Logger.global.logWarning(ex.getFormattedExplanation());
|
|
|
|
Throwable cause = ex.getRootCause();
|
|
|
|
if (cause != null) {
|
|
|
|
Logger.global.logError("Detailed error:", ex);
|
|
|
|
}
|
2022-08-27 15:08:17 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-01 23:01:42 +01:00
|
|
|
return Collections.unmodifiableMap(maps);
|
2022-08-27 15:08:17 +02:00
|
|
|
}
|
2022-04-20 22:54:27 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
private synchronized void loadMap(String id, MapConfig mapConfig) throws ConfigurationException, InterruptedException {
|
2022-08-27 15:08:17 +02:00
|
|
|
String name = mapConfig.getName();
|
|
|
|
if (name == null) name = id;
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
Path worldFolder = mapConfig.getWorld();
|
2023-11-18 13:50:13 +01:00
|
|
|
Key dimension = mapConfig.getDimension();
|
2022-08-03 12:54:03 +02:00
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
// if there is no world configured, we assume the map is static, or supplied from a different server
|
|
|
|
if (worldFolder == null) {
|
|
|
|
Logger.global.logInfo("The map '" + name + "' has no world configured. The map will be displayed, but not updated!");
|
|
|
|
return;
|
|
|
|
}
|
2022-08-03 12:54:03 +02:00
|
|
|
|
2024-02-02 16:49:32 +01:00
|
|
|
// if there is no dimension configured, we assume world-folder is actually the dimension-folder and convert (backwards compatibility)
|
|
|
|
if (dimension == null) {
|
|
|
|
worldFolder = worldFolder.normalize();
|
|
|
|
if (worldFolder.endsWith("DIM-1")) {
|
|
|
|
worldFolder = worldFolder.getParent();
|
|
|
|
dimension = DataPack.DIMENSION_THE_NETHER;
|
|
|
|
} else if (worldFolder.endsWith("DIM1")) {
|
|
|
|
worldFolder = worldFolder.getParent();
|
|
|
|
dimension = DataPack.DIMENSION_THE_END;
|
|
|
|
} else if (
|
|
|
|
worldFolder.getNameCount() > 3 &&
|
|
|
|
worldFolder.getName(worldFolder.getNameCount() - 3).equals(Path.of("dimensions"))
|
|
|
|
) {
|
|
|
|
String namespace = worldFolder.getName(worldFolder.getNameCount() - 2).toString();
|
|
|
|
String value = worldFolder.getName(worldFolder.getNameCount() - 1).toString();
|
|
|
|
worldFolder = worldFolder.subpath(0, worldFolder.getNameCount() - 3);
|
|
|
|
dimension = new Key(namespace, value);
|
|
|
|
} else {
|
|
|
|
dimension = DataPack.DIMENSION_OVERWORLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.global.logInfo("The map '" + name + "' has no dimension configured.\n" +
|
|
|
|
"Assuming world: '" + worldFolder + "' and dimension: '" + dimension + "'.");
|
|
|
|
}
|
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
if (!Files.isDirectory(worldFolder)) {
|
|
|
|
throw new ConfigurationException(
|
|
|
|
"'" + worldFolder.toAbsolutePath().normalize() + "' does not exist or is no directory!\n" +
|
|
|
|
"Check if the 'world' setting in the config-file for that map is correct, or remove the entire config-file if you don't want that map.");
|
|
|
|
}
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
String worldId = MCAWorld.id(worldFolder, dimension);
|
|
|
|
World world = worlds.get(worldId);
|
2022-08-27 15:08:17 +02:00
|
|
|
if (world == null) {
|
2021-09-19 22:15:50 +02:00
|
|
|
try {
|
2024-02-01 23:01:42 +01:00
|
|
|
Logger.global.logDebug("Loading world " + worldId + " ...");
|
2023-11-18 13:50:13 +01:00
|
|
|
world = MCAWorld.load(worldFolder, dimension);
|
2024-02-01 23:01:42 +01:00
|
|
|
worlds.put(worldId, world);
|
2022-04-20 22:54:27 +02:00
|
|
|
} catch (IOException ex) {
|
2022-08-27 15:08:17 +02:00
|
|
|
throw new ConfigurationException(
|
2024-02-01 23:01:42 +01:00
|
|
|
"Failed to load world " + worldId + "!\n" +
|
2022-08-27 15:08:17 +02:00
|
|
|
"Is the level.dat of that world present and not corrupted?",
|
2022-04-20 22:54:27 +02:00
|
|
|
ex);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
2022-08-27 15:08:17 +02:00
|
|
|
}
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
Storage storage = getOrLoadStorage(mapConfig.getStorage());
|
2022-04-20 22:54:27 +02:00
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
try {
|
2022-07-30 13:24:07 +02:00
|
|
|
|
2024-02-05 16:38:33 +01:00
|
|
|
Logger.global.logInfo("Loading map '" + id + "'...");
|
2022-08-27 15:08:17 +02:00
|
|
|
BmMap map = new BmMap(
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
world,
|
|
|
|
storage,
|
2024-02-01 23:01:42 +01:00
|
|
|
getOrLoadResourcePack(),
|
2022-08-27 15:08:17 +02:00
|
|
|
mapConfig
|
|
|
|
);
|
|
|
|
maps.put(id, map);
|
|
|
|
|
|
|
|
// load marker-config by converting it first from hocon to json and then loading it with MarkerGson
|
|
|
|
ConfigurationNode markerSetNode = mapConfig.getMarkerSets();
|
|
|
|
if (markerSetNode != null && !markerSetNode.empty()) {
|
|
|
|
String markerJson = GsonConfigurationLoader.builder()
|
|
|
|
.headerMode(HeaderMode.NONE)
|
|
|
|
.lenient(false)
|
|
|
|
.indent(0)
|
|
|
|
.buildAndSaveString(markerSetNode);
|
|
|
|
Gson gson = MarkerGson.addAdapters(new GsonBuilder())
|
|
|
|
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
|
|
|
|
.create();
|
|
|
|
Type markerSetType = new TypeToken<Map<String, MarkerSet>>() {}.getType();
|
|
|
|
Map<String, MarkerSet> markerSets = gson.fromJson(markerJson, markerSetType);
|
|
|
|
map.getMarkerSets().putAll(markerSets);
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
2022-07-30 13:24:07 +02:00
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
} catch (ConfigurateException | JsonParseException ex) {
|
|
|
|
throw new ConfigurationException(
|
|
|
|
"Failed to create the markers for map '" + id + "'!\n" +
|
|
|
|
"Make sure your marker-configuration for this map is valid.",
|
|
|
|
ex);
|
|
|
|
} catch (IOException | ConfigurationException ex) {
|
|
|
|
throw new ConfigurationException("Failed to load map '" + id + "'!", ex);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
public synchronized Storage getOrLoadStorage(String storageId) throws ConfigurationException, InterruptedException {
|
2022-04-20 22:54:27 +02:00
|
|
|
Storage storage = storages.get(storageId);
|
2021-11-14 14:18:31 +01:00
|
|
|
|
|
|
|
if (storage == null) {
|
|
|
|
try {
|
2023-11-18 13:50:13 +01:00
|
|
|
StorageConfig storageConfig = getConfig().getStorageConfigs().get(storageId);
|
2022-04-20 22:54:27 +02:00
|
|
|
if (storageConfig == null) {
|
|
|
|
throw new ConfigurationException("There is no storage-configuration for '" + storageId + "'!\n" +
|
|
|
|
"You will either need to define that storage, or change the map-config to use a storage-config that exists.");
|
|
|
|
}
|
2021-11-14 14:18:31 +01:00
|
|
|
|
2022-08-09 22:35:21 +02:00
|
|
|
Logger.global.logInfo("Initializing Storage: '" + storageId + "' (Type: " + storageConfig.getStorageType() + ")");
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
storage = storageConfig.createStorage();
|
|
|
|
storage.initialize();
|
|
|
|
} catch (Exception ex) {
|
2022-08-27 15:08:17 +02:00
|
|
|
ConfigurationException confEx = new ConfigurationException(
|
|
|
|
"Failed to load and initialize the storage '" + storageId + "'!",
|
|
|
|
ex
|
|
|
|
);
|
|
|
|
|
|
|
|
if (storage != null) {
|
|
|
|
try {
|
|
|
|
storage.close();
|
|
|
|
} catch (Exception closeEx) {
|
|
|
|
confEx.addSuppressed(closeEx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw confEx;
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:54:27 +02:00
|
|
|
storages.put(storageId, storage);
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
2022-04-20 22:54:27 +02:00
|
|
|
|
|
|
|
return storage;
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
public @Nullable ResourcePack getResourcePack() {
|
|
|
|
return resourcePack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized ResourcePack getOrLoadResourcePack() throws ConfigurationException, InterruptedException {
|
2021-09-19 22:15:50 +02:00
|
|
|
if (resourcePack == null) {
|
2024-02-01 23:01:42 +01:00
|
|
|
MinecraftVersion minecraftVersion = config.getMinecraftVersion();
|
|
|
|
@Nullable Path resourcePackFolder = config.getResourcePacksFolder();
|
|
|
|
@Nullable Path modsFolder = config.getModsFolder();
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2023-11-18 13:50:13 +01:00
|
|
|
Path defaultResourceFile = config.getCoreConfig().getData().resolve("minecraft-client-" + minecraftVersion.getResource().getVersion().getVersionString() + ".jar");
|
|
|
|
Path resourceExtensionsFile = config.getCoreConfig().getData().resolve("resourceExtensions.zip");
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
try {
|
|
|
|
FileHelper.createDirectories(resourcePackFolder);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new ConfigurationException(
|
|
|
|
"BlueMap failed to create this folder:\n" +
|
|
|
|
resourcePackFolder + "\n" +
|
|
|
|
"Does BlueMap have sufficient permissions?",
|
|
|
|
ex);
|
|
|
|
}
|
2022-04-20 22:54:27 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2022-05-28 21:55:41 +02:00
|
|
|
if (!Files.exists(defaultResourceFile)) {
|
2023-11-18 13:50:13 +01:00
|
|
|
if (config.getCoreConfig().isAcceptDownload()) {
|
2021-09-19 22:15:50 +02:00
|
|
|
//download file
|
|
|
|
try {
|
|
|
|
Logger.global.logInfo("Downloading " + minecraftVersion.getResource().getClientUrl() + " to " + defaultResourceFile + " ...");
|
2022-04-20 22:54:27 +02:00
|
|
|
|
2022-10-14 10:24:19 +02:00
|
|
|
FileHelper.createDirectories(defaultResourceFile.getParent());
|
2022-05-28 21:55:41 +02:00
|
|
|
Path tempResourceFile = defaultResourceFile.getParent().resolve(defaultResourceFile.getFileName() + ".filepart");
|
|
|
|
Files.deleteIfExists(tempResourceFile);
|
|
|
|
FileUtils.copyURLToFile(new URL(minecraftVersion.getResource().getClientUrl()), tempResourceFile.toFile(), 10000, 10000);
|
2022-10-14 10:24:19 +02:00
|
|
|
FileHelper.move(tempResourceFile, defaultResourceFile);
|
2021-11-14 14:18:31 +01:00
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new ConfigurationException("Failed to download resources!", ex);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new MissingResourcesException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
|
|
|
|
2021-11-14 14:18:31 +01:00
|
|
|
try {
|
2022-05-28 21:55:41 +02:00
|
|
|
Files.deleteIfExists(resourceExtensionsFile);
|
2022-10-14 10:24:19 +02:00
|
|
|
FileHelper.createDirectories(resourceExtensionsFile.getParent());
|
2021-11-14 14:18:31 +01:00
|
|
|
URL resourceExtensionsUrl = Objects.requireNonNull(
|
|
|
|
Plugin.class.getResource(
|
|
|
|
"/de/bluecolored/bluemap/" + minecraftVersion.getResource().getResourcePrefix() +
|
2024-02-01 23:01:42 +01:00
|
|
|
"/resourceExtensions.zip")
|
2021-11-14 14:18:31 +01:00
|
|
|
);
|
2022-05-28 21:55:41 +02:00
|
|
|
FileUtils.copyURLToFile(resourceExtensionsUrl, resourceExtensionsFile.toFile(), 10000, 10000);
|
2021-11-14 14:18:31 +01:00
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new ConfigurationException(
|
|
|
|
"Failed to create resourceExtensions.zip!\n" +
|
2024-02-01 23:01:42 +01:00
|
|
|
"Does BlueMap has sufficient write permissions?",
|
2021-11-14 14:18:31 +01:00
|
|
|
ex);
|
|
|
|
}
|
2021-09-19 22:15:50 +02:00
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
if (Thread.interrupted()) throw new InterruptedException();
|
|
|
|
|
2021-09-19 22:15:50 +02:00
|
|
|
try {
|
2024-02-01 23:01:42 +01:00
|
|
|
ResourcePack resourcePack = new ResourcePack();
|
2022-05-28 21:55:41 +02:00
|
|
|
|
2022-08-01 00:15:39 +02:00
|
|
|
List<Path> resourcePackRoots = new ArrayList<>();
|
2024-02-01 23:01:42 +01:00
|
|
|
|
|
|
|
if (resourcePackFolder != null) {
|
|
|
|
// load from resourcepack folder
|
|
|
|
try (Stream<Path> resourcepackFiles = Files.list(resourcePackFolder)) {
|
|
|
|
resourcepackFiles
|
|
|
|
.sorted(Comparator.reverseOrder())
|
|
|
|
.forEach(resourcePackRoots::add);
|
|
|
|
}
|
2022-05-28 21:55:41 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 13:50:13 +01:00
|
|
|
if (config.getCoreConfig().isScanForModResources()) {
|
2022-06-01 00:03:35 +02:00
|
|
|
|
|
|
|
// load from mods folder
|
|
|
|
if (modsFolder != null && Files.isDirectory(modsFolder)) {
|
|
|
|
try (Stream<Path> resourcepackFiles = Files.list(modsFolder)) {
|
|
|
|
resourcepackFiles
|
|
|
|
.filter(Files::isRegularFile)
|
|
|
|
.filter(file -> file.getFileName().toString().endsWith(".jar"))
|
2022-08-01 00:15:39 +02:00
|
|
|
.forEach(resourcePackRoots::add);
|
2022-06-01 00:03:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load from datapacks
|
|
|
|
for (Path worldFolder : getWorldFolders()) {
|
|
|
|
Path datapacksFolder = worldFolder.resolve("datapacks");
|
|
|
|
if (!Files.isDirectory(datapacksFolder)) continue;
|
|
|
|
|
|
|
|
try (Stream<Path> resourcepackFiles = Files.list(worldFolder.resolve("datapacks"))) {
|
2022-08-01 00:15:39 +02:00
|
|
|
resourcepackFiles.forEach(resourcePackRoots::add);
|
2022-06-01 00:03:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-01 00:15:39 +02:00
|
|
|
resourcePackRoots.add(resourceExtensionsFile);
|
|
|
|
resourcePackRoots.add(defaultResourceFile);
|
|
|
|
|
|
|
|
resourcePack.loadResources(resourcePackRoots);
|
2024-02-01 23:01:42 +01:00
|
|
|
|
|
|
|
this.resourcePack = resourcePack;
|
2022-05-28 21:55:41 +02:00
|
|
|
} catch (IOException | RuntimeException e) {
|
2022-04-20 22:54:27 +02:00
|
|
|
throw new ConfigurationException("Failed to parse resources!\n" +
|
|
|
|
"Is one of your resource-packs corrupted?", e);
|
2021-09-19 22:15:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 23:01:42 +01:00
|
|
|
return this.resourcePack;
|
2023-01-03 16:03:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 00:03:35 +02:00
|
|
|
private Collection<Path> getWorldFolders() {
|
|
|
|
Set<Path> folders = new HashSet<>();
|
2023-11-18 13:50:13 +01:00
|
|
|
for (MapConfig mapConfig : config.getMapConfigs().values()) {
|
2022-08-03 12:54:03 +02:00
|
|
|
Path folder = mapConfig.getWorld();
|
|
|
|
if (folder == null) continue;
|
|
|
|
folder = folder.toAbsolutePath().normalize();
|
2022-06-01 00:03:35 +02:00
|
|
|
if (Files.isDirectory(folder)) {
|
|
|
|
folders.add(folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
2023-11-18 13:50:13 +01:00
|
|
|
public BlueMapConfiguration getConfig() {
|
|
|
|
return config;
|
2021-11-14 14:18:31 +01:00
|
|
|
}
|
|
|
|
|
2022-08-27 15:08:17 +02:00
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
IOException exception = null;
|
|
|
|
|
|
|
|
for (Storage storage : storages.values()) {
|
|
|
|
try {
|
|
|
|
if (storage != null) {
|
|
|
|
storage.close();
|
|
|
|
}
|
|
|
|
} catch (IOException ex) {
|
|
|
|
if (exception == null) exception = ex;
|
|
|
|
else exception.addSuppressed(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exception != null)
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:58:11 +02:00
|
|
|
}
|