| 1 | package net.minecraft.util.filefix.access;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.IOException;
|
| 5 | import java.nio.file.Files;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.HashSet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import org.slf4j.Logger;
|
| 12 |
|
| 13 | @FunctionalInterface
|
| 14 | public interface FileRelation {
|
| 15 | Logger LOGGER = LogUtils.getLogger();
|
| 16 | FileRelation ORIGIN = List::of;
|
| 17 | FileRelation REGION = basePath -> List.of(basePath.resolve("region"));
|
| 18 | FileRelation DATA = basePath -> List.of(basePath.resolve("data"));
|
| 19 | FileRelation PLAYER_DATA = basePath -> List.of(basePath.resolve("players/data"));
|
| 20 | FileRelation DIMENSIONS = FileRelation::discoverDimensions;
|
| 21 | FileRelation DIMENSIONS_DATA = DIMENSIONS.resolve(DATA);
|
| 22 | FileRelation GENERATED_NAMESPACES = ORIGIN.resolve(basePath -> directoriesInPath(basePath.resolve("generated")));
|
| 23 | FileRelation OLD_OVERWORLD = ORIGIN;
|
| 24 | FileRelation OLD_NETHER = ORIGIN.resolve(basePath -> List.of(basePath.resolve("DIM-1")));
|
| 25 | FileRelation OLD_END = ORIGIN.resolve(basePath -> List.of(basePath.resolve("DIM1")));
|
| 26 |
|
| 27 | List<Path> getPaths(final Path basePath);
|
| 28 |
|
| 29 | static FileRelation forDataFileInDimension(final String dimension, final String fileName) {
|
| 30 | return basePath -> List.of(basePath.resolve("dimensions/minecraft/" + dimension + "/data/" + fileName));
|
| 31 | }
|
| 32 |
|
| 33 | default FileRelation forFile(final String fileName) {
|
| 34 | return this.resolve(basePath -> List.of(basePath.resolve(fileName)));
|
| 35 | }
|
| 36 |
|
| 37 | default FileRelation resolve(final FileRelation other) {
|
| 38 | return basePath -> this.getPaths(basePath).stream().flatMap(path -> other.getPaths(path).stream()).toList();
|
| 39 | }
|
| 40 |
|
| 41 | default FileRelation join(final FileRelation... relations) {
|
| 42 | return basePath -> {
|
| 43 | Set<Path> paths = new HashSet<>();
|
| 44 |
|
| 45 | for (FileRelation relation : relations) {
|
| 46 | paths.addAll(relation.getPaths(basePath));
|
| 47 | }
|
| 48 |
|
| 49 | return List.copyOf(paths);
|
| 50 | };
|
| 51 | }
|
| 52 |
|
| 53 | private static List<Path> discoverDimensions(final Path basePath) {
|
| 54 | Path dimensionsRoot = basePath.resolve("dimensions");
|
| 55 | if (!Files.exists(dimensionsRoot)) {
|
| 56 | return getDefaultDimensions(basePath);
|
| 57 | }
|
| 58 |
|
| 59 | try (Stream<Path> namespacePaths = Files.list(dimensionsRoot)) {
|
| 60 | List<Path> discoveredDimensions = namespacePaths.filter(x$0 -> Files.isDirectory(x$0)).flatMap(path -> directoriesInPath(path).stream()).toList();
|
| 61 | return discoveredDimensions.isEmpty() ? getDefaultDimensions(basePath) : discoveredDimensions;
|
| 62 | } catch (IOException e) {
|
| 63 | LOGGER.warn("Failed to discover dimensions, assuming default: {}", e.toString());
|
| 64 | return getDefaultDimensions(basePath);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | static List<Path> directoriesInPath(final Path path) {
|
| 69 | try (Stream<Path> dimensionPaths = Files.list(path)) {
|
| 70 | return dimensionPaths.filter(x$0 -> Files.isDirectory(x$0)).toList();
|
| 71 | } catch (IOException e) {
|
| 72 | return List.of();
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private static List<Path> getDefaultDimensions(final Path basePath) {
|
| 77 | return List.of(
|
| 78 | basePath.resolve("dimensions/minecraft/overworld"), basePath.resolve("dimensions/minecraft/the_nether"), basePath.resolve("dimensions/minecraft/the_end")
|
| 79 | );
|
| 80 | }
|
| 81 | }
|
| 82 |
|