Improve autogenerated config names

This commit is contained in:
Lukas Rieger (Blue) 2024-02-06 00:31:41 +01:00
parent fab523fe05
commit e68aacfd5d
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2

View File

@ -239,20 +239,33 @@ private Map<String, MapConfig> loadMapConfigs(Collection<ServerWorld> autoConfig
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING
);
} else {
for (var world : autoConfigWorlds) {
Path worldFolder = world.getWorldFolder();
// make sure overworld-dimensions come first, so they are the ones where the
// dimension-key is omitted in the generated map-id
List<ServerWorld> overworldFirstAutoConfigWorlds = new ArrayList<>(autoConfigWorlds.size());
overworldFirstAutoConfigWorlds.addAll(autoConfigWorlds);
overworldFirstAutoConfigWorlds.sort(Comparator.comparingInt(w ->
DataPack.DIMENSION_OVERWORLD.equals(w.getDimension()) ? 0 : 1
));
Set<String> mapIds = new HashSet<>();
for (var world : overworldFirstAutoConfigWorlds) {
Path worldFolder = world.getWorldFolder().normalize();
Key dimension = world.getDimension();
String dimensionName = dimension.getNamespace().equals("minecraft") ?
dimension.getValue() : dimension.getFormatted();
String id = sanitiseMapId(worldFolder.getFileName() + "_" + dimensionName).toLowerCase(Locale.ROOT);
Path configFile = mapConfigFolder.resolve(id + ".conf");
// find unique map id
String id = sanitiseMapId(worldFolder.getFileName().toString()).toLowerCase(Locale.ROOT);
if (mapIds.contains(id))
id = sanitiseMapId(worldFolder.getFileName() + "_" + dimensionName).toLowerCase(Locale.ROOT);
int i = 1;
while (Files.exists(configFile)) {
configFile = mapConfigFolder.resolve(id + '_' + (++i) + ".conf");
}
String uniqueId = id;
while (mapIds.contains(uniqueId))
uniqueId = id + "_" + (++i);
mapIds.add(uniqueId);
Path configFile = mapConfigFolder.resolve(uniqueId + ".conf");
String name = worldFolder.getFileName() + " (" + dimensionName + ")";
if (i > 1) name = name + " (" + i + ")";