|
| 1 | +package net.theevilreaper.aves.map; |
| 2 | + |
| 3 | +import net.theevilreaper.aves.file.FileHandler; |
| 4 | +import net.theevilreaper.aves.util.functional.PathFilter; |
| 5 | +import net.minestom.server.MinecraftServer; |
| 6 | +import net.minestom.server.coordinate.Pos; |
| 7 | +import net.minestom.server.entity.Player; |
| 8 | +import net.minestom.server.instance.Instance; |
| 9 | +import net.minestom.server.instance.InstanceContainer; |
| 10 | +import net.minestom.server.instance.anvil.AnvilLoader; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | +import org.jetbrains.annotations.UnmodifiableView; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.function.Supplier; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +/** |
| 27 | + * Abstract base implementation of {@link MapProvider} for managing map lifecycles |
| 28 | + * within a server session. Handles loading map entries from disk, registering map |
| 29 | + * instances, and providing access to the currently active map and instance. |
| 30 | + * <p> |
| 31 | + * Subclasses should implement logic for switching maps and managing map-specific data. |
| 32 | + * </p> |
| 33 | + * |
| 34 | + * @author theEvilReaper |
| 35 | + * @version 1.0.0 |
| 36 | + * @since 1.6.0 |
| 37 | + */ |
| 38 | +public abstract class AbstractMapProvider implements MapProvider { |
| 39 | + |
| 40 | + private static final Logger MAP_LOGGER = LoggerFactory.getLogger(AbstractMapProvider.class); |
| 41 | + |
| 42 | + private final PathFilter<MapEntry> mapFilter; |
| 43 | + |
| 44 | + protected FileHandler fileHandler; |
| 45 | + protected List<MapEntry> mapEntries; |
| 46 | + |
| 47 | + protected BaseMap activeMap; |
| 48 | + protected InstanceContainer activeInstance; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructs a BaseMapProvider with the specified FileHandler. |
| 52 | + * |
| 53 | + * @param fileHandler the {@link FileHandler} used to load and save maps |
| 54 | + * @param mapFilter the filtering logic for the map entries |
| 55 | + */ |
| 56 | + protected AbstractMapProvider(@NotNull FileHandler fileHandler, @NotNull PathFilter<MapEntry> mapFilter) { |
| 57 | + this.fileHandler = fileHandler; |
| 58 | + this.mapFilter = mapFilter; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Registers the specified map entry as an active instance in the server. |
| 63 | + * Sets up chunk loading and time rate, and registers the instance with the server manager. |
| 64 | + * |
| 65 | + * @param instance the instance to be registered; must not be null |
| 66 | + * @param mapEntry the map entry representing the world data; must not be null |
| 67 | + */ |
| 68 | + protected void registerInstance(@NotNull InstanceContainer instance, @NotNull MapEntry mapEntry) { |
| 69 | + instance.setChunkLoader(new AnvilLoader(mapEntry.getDirectoryRoot())); |
| 70 | + instance.enableAutoChunkLoad(true); |
| 71 | + instance.setTimeRate(0); |
| 72 | + MinecraftServer.getInstanceManager().registerInstance(instance); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Loads all available map entries from the specified directory. |
| 77 | + * Only directories passing the configured map filter are included. |
| 78 | + * Handles IO exceptions gracefully by logging and reporting to the server exception manager. |
| 79 | + * |
| 80 | + * @param path the root directory containing map folders; must not be null |
| 81 | + * @return a list of map entries found in the directory, possibly empty but never null |
| 82 | + */ |
| 83 | + protected @NotNull List<MapEntry> loadMapEntries(@NotNull Path path) { |
| 84 | + List<MapEntry> givenMaps = new ArrayList<>(); |
| 85 | + try (Stream<Path> stream = Files.list(path)) { |
| 86 | + givenMaps.addAll(this.mapFilter.filter(stream.filter(Files::isDirectory))); |
| 87 | + } catch (IOException exception) { |
| 88 | + MinecraftServer.getExceptionManager().handleException(exception); |
| 89 | + MAP_LOGGER.error("Unable to load maps from path {}", path, exception); |
| 90 | + } |
| 91 | + return givenMaps; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void teleportToSpawn(@NotNull Player player, boolean instanceSet) { |
| 96 | + Pos pos = this.activeMap.getSpawnOrDefault(FALLBACK_POS); |
| 97 | + if (!instanceSet) { |
| 98 | + player.teleport(pos); |
| 99 | + return; |
| 100 | + } |
| 101 | + player.setInstance(this.activeInstance, pos); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public @UnmodifiableView @NotNull List<MapEntry> getEntries() { |
| 106 | + if (this.mapEntries == null) return Collections.emptyList(); |
| 107 | + return Collections.unmodifiableList(this.mapEntries); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public @NotNull Supplier<@Nullable Instance> getActiveInstance() { |
| 112 | + return () -> this.activeInstance; |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments