Skip to content

Commit

Permalink
feat: allow copying of pregen MCA files for overworld/nether/end at w…
Browse files Browse the repository at this point in the history
…orld creation
  • Loading branch information
desht committed Oct 23, 2023
1 parent b12879e commit c922bdf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import dev.ftb.mods.ftbteamdimensions.client.DimensionsClient;
import dev.ftb.mods.ftbteamdimensions.commands.FTBDimensionsCommands;
import dev.ftb.mods.ftbteamdimensions.dimensions.BiomeReplacementUtils;
import dev.ftb.mods.ftbteamdimensions.dimensions.DimensionUtils;
import dev.ftb.mods.ftbteamdimensions.dimensions.DimensionsMain;
import dev.ftb.mods.ftbteamdimensions.dimensions.DimensionsManager;
import dev.ftb.mods.ftbteamdimensions.dimensions.*;
import dev.ftb.mods.ftbteamdimensions.dimensions.level.DimensionStorage;
import dev.ftb.mods.ftbteamdimensions.dimensions.level.DynamicDimensionManager;
import dev.ftb.mods.ftbteamdimensions.dimensions.prebuilt.PrebuiltStructureManager;
Expand Down Expand Up @@ -33,6 +30,7 @@
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.event.entity.EntityTravelToDimensionEvent;
import net.minecraftforge.event.level.ChunkEvent;
import net.minecraftforge.event.level.LevelEvent;
import net.minecraftforge.event.level.SleepFinishedTimeEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.IEventBus;
Expand Down Expand Up @@ -70,6 +68,7 @@ public FTBTeamDimensions() {
MinecraftForge.EVENT_BUS.addListener(this::reloadListener);
MinecraftForge.EVENT_BUS.addListener(this::dimensionChanged);
MinecraftForge.EVENT_BUS.addListener(this::entityJoinLevel);
MinecraftForge.EVENT_BUS.addListener(this::onLevelLoad);
MinecraftForge.EVENT_BUS.addListener(this::onChunkLoad);
MinecraftForge.EVENT_BUS.addListener(EventPriority.LOWEST, this::onSleepFinished);

Expand Down Expand Up @@ -131,6 +130,12 @@ private void entityJoinLevel(EntityJoinLevelEvent event) {
}
}

private void onLevelLoad(LevelEvent.Load event) {
if (event.getLevel() instanceof Level level && !level.isClientSide && level.dimension() == Level.OVERWORLD && level.getServer() != null) {
Pregen.maybeDoStartupPregen(level.getServer());
}
}

private void onChunkLoad(ChunkEvent.Load event) {
if (event.getLevel() instanceof ServerLevel level && DimensionUtils.isTeamDimension(level) && FTBDimensionsConfig.COMMON_GENERAL.replaceColdBiomesNearSpawn.get() > 0) {
BlockPos spawnPos = DimensionStorage.get(level.getServer()).getDimensionSpawnLocation(level.dimension().location());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
import org.apache.commons.io.FileUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class Pregen {
private static final Path PREGEN_PATH = Path.of(FTBTeamDimensions.MOD_ID, "pregen");
private static final Path PREGEN_STARTUP_PATH = Path.of(FTBTeamDimensions.MOD_ID, "pregen_startup");

private static final List<String> STARTUP_SUBDIRS = List.of("region", "entities", "poi", "DIM1", "DIM-1");

public static void copyIfExists(MinecraftServer server, ResourceLocation prebuiltId, ResourceKey<Level> levelKey) {
Path rootDir = server.getServerDirectory().toPath();
Expand All @@ -36,4 +41,24 @@ public static void copyIfExists(MinecraftServer server, ResourceLocation prebuil
e.printStackTrace();
}
}

public static void maybeDoStartupPregen(MinecraftServer server) {
Path startupPath = server.getServerDirectory().toPath().resolve(PREGEN_STARTUP_PATH);
Path worldPath = server.getWorldPath(LevelResource.ROOT);
if (Files.isDirectory(startupPath) && !Files.isDirectory(worldPath.resolve("region"))) {
// looks like a brand-new world, just created - copy over any pregen MCA files for overworld/nether/end if they exist
for (String subDir : STARTUP_SUBDIRS) {
Path srcDir = startupPath.resolve(subDir);
Path destDir = worldPath.resolve(subDir);
if (Files.isDirectory(srcDir) && !Files.isDirectory(destDir)) {
try {
FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
FTBTeamDimensions.LOGGER.info("Copied startup pregen MCA files from {} to {}", srcDir, destDir);
} catch (IOException e) {
FTBTeamDimensions.LOGGER.error("Failed to copy startup pregen MCA files from {} to {}: {}", srcDir, destDir, e.getMessage());
}
}
}
}
}
}

0 comments on commit c922bdf

Please sign in to comment.