| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import java.util.List;
|
| 5 | import java.util.concurrent.CompletableFuture;
|
| 6 | import java.util.concurrent.Executor;
|
| 7 | import java.util.stream.Stream;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.core.HolderLookup;
|
| 10 | import net.minecraft.core.LayeredRegistryAccess;
|
| 11 | import net.minecraft.core.Registry;
|
| 12 | import net.minecraft.core.RegistryAccess;
|
| 13 | import net.minecraft.resources.RegistryDataLoader;
|
| 14 | import net.minecraft.server.packs.PackResources;
|
| 15 | import net.minecraft.server.packs.PackType;
|
| 16 | import net.minecraft.server.packs.repository.PackRepository;
|
| 17 | import net.minecraft.server.packs.resources.CloseableResourceManager;
|
| 18 | import net.minecraft.server.packs.resources.MultiPackResourceManager;
|
| 19 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 20 | import net.minecraft.server.permissions.PermissionSet;
|
| 21 | import net.minecraft.tags.TagLoader;
|
| 22 | import net.minecraft.world.level.WorldDataConfiguration;
|
| 23 |
|
| 24 | public class WorldLoader {
|
| 25 | public static <D, R> CompletableFuture<R> load(
|
| 26 | final WorldLoader.InitConfig config,
|
| 27 | final WorldLoader.WorldDataSupplier<D> worldDataSupplier,
|
| 28 | final WorldLoader.ResultFactory<D, R> resultFactory,
|
| 29 | final Executor backgroundExecutor,
|
| 30 | final Executor mainThreadExecutor
|
| 31 | ) {
|
| 32 | return CompletableFuture.supplyAsync(config.packConfig::createResourceManager, mainThreadExecutor)
|
| 33 | .thenComposeAsync(
|
| 34 | packsAndResourceManager -> {
|
| 35 | CloseableResourceManager resources = packsAndResourceManager.getSecond();
|
| 36 | LayeredRegistryAccess<RegistryLayer> initialLayers = RegistryLayer.createRegistryAccess();
|
| 37 | List<Registry.PendingTags<?>> staticLayerTags = TagLoader.loadTagsForExistingRegistries(resources, initialLayers.getLayer(RegistryLayer.STATIC));
|
| 38 | RegistryAccess.Frozen worldLoadContext = initialLayers.getAccessForLoading(RegistryLayer.WORLD);
|
| 39 | List<HolderLookup.RegistryLookup<?>> worldContextRegistries = TagLoader.buildUpdatedLookups(worldLoadContext, staticLayerTags);
|
| 40 | return RegistryDataLoader.load(resources, worldContextRegistries, RegistryDataLoader.WORLD_REGISTRIES, backgroundExecutor)
|
| 41 | .thenComposeAsync(
|
| 42 | loadedWorldRegistries -> {
|
| 43 | List<HolderLookup.RegistryLookup<?>> dimensionContextRegistries = Stream.concat(worldContextRegistries.stream(), loadedWorldRegistries.listRegistries())
|
| 44 | .toList();
|
| 45 | return RegistryDataLoader.load(resources, dimensionContextRegistries, RegistryDataLoader.DIMENSION_REGISTRIES, backgroundExecutor)
|
| 46 | .thenComposeAsync(
|
| 47 | initialWorldgenDimensions -> {
|
| 48 | WorldDataConfiguration worldDataConfiguration = (WorldDataConfiguration)packsAndResourceManager.getFirst();
|
| 49 | HolderLookup.Provider dimensionContextProvider = HolderLookup.Provider.create(dimensionContextRegistries.stream());
|
| 50 | WorldLoader.DataLoadOutput<D> worldDataAndRegistries = worldDataSupplier.get(
|
| 51 | new WorldLoader.DataLoadContext(resources, worldDataConfiguration, dimensionContextProvider, initialWorldgenDimensions)
|
| 52 | );
|
| 53 | LayeredRegistryAccess<RegistryLayer> resourcesLoadContext = initialLayers.replaceFrom(
|
| 54 | RegistryLayer.WORLD, loadedWorldRegistries, worldDataAndRegistries.finalDimensions
|
| 55 | );
|
| 56 | return ReloadableServerResources.loadResources(
|
| 57 | resources,
|
| 58 | resourcesLoadContext,
|
| 59 | staticLayerTags,
|
| 60 | worldDataConfiguration.enabledFeatures(),
|
| 61 | config.commandSelection(),
|
| 62 | config.functionCompilationPermissions(),
|
| 63 | backgroundExecutor,
|
| 64 | mainThreadExecutor
|
| 65 | )
|
| 66 | .whenComplete((managers, throwable) -> {
|
| 67 | if (throwable != null) {
|
| 68 | resources.close();
|
| 69 | }
|
| 70 | })
|
| 71 | .thenApplyAsync(managers -> {
|
| 72 | managers.updateComponentsAndStaticRegistryTags();
|
| 73 | return resultFactory.create(resources, managers, resourcesLoadContext, worldDataAndRegistries.cookie);
|
| 74 | }, mainThreadExecutor);
|
| 75 | },
|
| 76 | backgroundExecutor
|
| 77 | );
|
| 78 | },
|
| 79 | backgroundExecutor
|
| 80 | );
|
| 81 | },
|
| 82 | backgroundExecutor
|
| 83 | );
|
| 84 | }
|
| 85 |
|
| 86 | public record DataLoadContext(
|
| 87 | ResourceManager resources, WorldDataConfiguration dataConfiguration, HolderLookup.Provider datapackWorldRegistries, RegistryAccess.Frozen datapackDimensions
|
| 88 | ) {
|
| 89 | }
|
| 90 |
|
| 91 | public record DataLoadOutput<D>(D cookie, RegistryAccess.Frozen finalDimensions) {
|
| 92 | }
|
| 93 |
|
| 94 | public record InitConfig(WorldLoader.PackConfig packConfig, Commands.CommandSelection commandSelection, PermissionSet functionCompilationPermissions) {
|
| 95 | }
|
| 96 |
|
| 97 | public record PackConfig(PackRepository packRepository, WorldDataConfiguration initialDataConfig, boolean safeMode, boolean initMode) {
|
| 98 | public Pair<WorldDataConfiguration, CloseableResourceManager> createResourceManager() {
|
| 99 | WorldDataConfiguration newPackConfig = MinecraftServer.configurePackRepository(this.packRepository, this.initialDataConfig, this.initMode, this.safeMode);
|
| 100 | List<PackResources> openedPacks = this.packRepository.openAllSelected();
|
| 101 | CloseableResourceManager resources = new MultiPackResourceManager(PackType.SERVER_DATA, openedPacks);
|
| 102 | return Pair.of(newPackConfig, resources);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | @FunctionalInterface
|
| 107 | public interface ResultFactory<D, R> {
|
| 108 | R create(CloseableResourceManager resources, ReloadableServerResources managers, LayeredRegistryAccess<RegistryLayer> registries, D cookie);
|
| 109 | }
|
| 110 |
|
| 111 | @FunctionalInterface
|
| 112 | public interface WorldDataSupplier<D> {
|
| 113 | WorldLoader.DataLoadOutput<D> get(WorldLoader.DataLoadContext context);
|
| 114 | }
|
| 115 | }
|
| 116 |
|