|
1 | 1 | package com.faboslav.structurify.common.registry; |
2 | 2 |
|
3 | 3 | import com.faboslav.structurify.common.Structurify; |
4 | | -import com.mojang.serialization.Lifecycle; |
5 | | -import net.minecraft.util.Util; |
6 | | -import net.minecraft.commands.Commands; |
7 | | -import net.minecraft.core.HolderLookup; |
8 | | -import net.minecraft.core.MappedRegistry; |
| 4 | +import com.mojang.serialization.Codec; |
| 5 | +import net.minecraft.core.*; |
| 6 | +import net.minecraft.resources.RegistryDataLoader; |
| 7 | +import net.minecraft.resources.ResourceKey; |
| 8 | +import net.minecraft.server.packs.PackType; |
| 9 | +import net.minecraft.server.packs.resources.MultiPackResourceManager; |
9 | 10 | import net.minecraft.core.registries.Registries; |
10 | | -import net.minecraft.server.WorldLoader; |
11 | | -import net.minecraft.server.WorldStem; |
12 | 11 | import net.minecraft.server.packs.repository.PackRepository; |
13 | 12 | import net.minecraft.server.packs.repository.RepositorySource; |
14 | | -import net.minecraft.world.level.WorldDataConfiguration; |
15 | 13 | import net.minecraft.world.level.biome.Biome; |
16 | | -import net.minecraft.world.level.levelgen.presets.WorldPresets; |
| 14 | +import net.minecraft.world.level.levelgen.carver.ConfiguredWorldCarver; |
| 15 | +import net.minecraft.world.level.levelgen.feature.ConfiguredFeature; |
| 16 | +import net.minecraft.world.level.levelgen.placement.PlacedFeature; |
17 | 17 | import net.minecraft.world.level.levelgen.structure.Structure; |
| 18 | +import net.minecraft.world.level.levelgen.structure.StructureSet; |
| 19 | +import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool; |
| 20 | +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType; |
18 | 21 | import org.jetbrains.annotations.Nullable; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +//? if >= 1.21.3 { |
| 25 | +import net.minecraft.server.RegistryLayer; |
| 26 | +import net.minecraft.tags.TagLoader; |
| 27 | +//?} else { |
| 28 | +/*import net.minecraft.core.registries.BuiltInRegistries; |
| 29 | +*///?} |
19 | 30 |
|
20 | | -//? if >= 1.21.11 { |
21 | | -import net.minecraft.server.permissions.PermissionSet; |
| 31 | +//? if >= 26.1 { |
| 32 | +import net.minecraft.resources.RegistryValidator; |
| 33 | +import net.minecraft.util.Util; |
22 | 34 | //?} |
23 | 35 |
|
24 | 36 | public final class StructurifyRegistryManagerProvider |
@@ -77,46 +89,71 @@ public static void loadRegistryManager() { |
77 | 89 | } |
78 | 90 |
|
79 | 91 | var resourcePackManager = new PackRepository(StructurifyResourcePackProvider.getResourcePackProviders().toArray(new RepositorySource[0])); |
80 | | - var dataPacks = new WorldLoader.PackConfig(resourcePackManager, WorldDataConfiguration.DEFAULT, false, false); |
81 | | - var serverConfig = new WorldLoader.InitConfig(dataPacks, Commands.CommandSelection.INTEGRATED, /*? if >= 1.21.11 {*/PermissionSet.ALL_PERMISSIONS/*?} else {*//*2*//*?}*/); |
82 | | - |
83 | | - var saveLoader = Util.blockUntilDone(executor -> |
84 | | - WorldLoader.load(serverConfig, loadContextSupplierContext -> { |
85 | | - var registry = new MappedRegistry<>(Registries.LEVEL_STEM, Lifecycle.stable()).freeze(); |
86 | | - |
87 | | - //? if >=1.21.3 { |
88 | | - var dimensionsConfig = loadContextSupplierContext |
89 | | - .datapackWorldgen() |
90 | | - .lookupOrThrow(Registries.WORLD_PRESET) |
91 | | - .getOrThrow(WorldPresets.FLAT) |
92 | | - .value() |
93 | | - .createWorldDimensions() |
94 | | - .bake(registry); |
95 | | - //?} else { |
96 | | - /*var dimensionsConfig = loadContextSupplierContext |
97 | | - .datapackWorldgen() |
98 | | - .registryOrThrow(Registries.WORLD_PRESET) |
99 | | - .getHolderOrThrow(WorldPresets.FLAT) |
100 | | - .value() |
101 | | - .createWorldDimensions() |
102 | | - .bake(registry); |
103 | | - *///?} |
104 | | - |
105 | | - return new WorldLoader.DataLoadOutput<>(null, dimensionsConfig.dimensionsRegistryAccess()); |
106 | | - }, WorldStem::new, Util.backgroundExecutor(), executor) |
107 | | - ).get(); |
108 | | - |
109 | | - if (saveLoader == null || saveLoader.registries() == null) { |
110 | | - Structurify.getLogger().error("SaveLoader or CombinedDynamicRegistries is null."); |
111 | | - return; |
| 92 | + resourcePackManager.reload(); |
| 93 | + resourcePackManager.setSelected(resourcePackManager.getAvailableIds()); |
| 94 | + |
| 95 | + try (var resourceManager = new MultiPackResourceManager( |
| 96 | + PackType.SERVER_DATA, |
| 97 | + resourcePackManager.openAllSelected() |
| 98 | + )) { |
| 99 | + List<RegistryDataLoader.RegistryData<?>> registries = List.of( |
| 100 | + getRegistryDataLoader(Registries.BIOME, Biome.DIRECT_CODEC), |
| 101 | + getRegistryDataLoader(Registries.CONFIGURED_CARVER, ConfiguredWorldCarver.DIRECT_CODEC), |
| 102 | + getRegistryDataLoader(Registries.PROCESSOR_LIST, StructureProcessorType.DIRECT_CODEC), |
| 103 | + getRegistryDataLoader(Registries.TEMPLATE_POOL, StructureTemplatePool.DIRECT_CODEC), |
| 104 | + getRegistryDataLoader(Registries.CONFIGURED_FEATURE, ConfiguredFeature.DIRECT_CODEC), |
| 105 | + getRegistryDataLoader(Registries.PLACED_FEATURE, PlacedFeature.DIRECT_CODEC), |
| 106 | + getRegistryDataLoader(Registries.STRUCTURE, Structure.DIRECT_CODEC), |
| 107 | + getRegistryDataLoader(Registries.STRUCTURE_SET, StructureSet.DIRECT_CODEC) |
| 108 | + ); |
| 109 | + //? if >= 1.21.3 { |
| 110 | + LayeredRegistryAccess<RegistryLayer> initialLayers = RegistryLayer.createRegistryAccess(); |
| 111 | + List<Registry.PendingTags<?>> staticLayerTags = TagLoader.loadTagsForExistingRegistries( |
| 112 | + resourceManager, |
| 113 | + initialLayers.getLayer(RegistryLayer.STATIC) |
| 114 | + ); |
| 115 | + var baseRegistryAccess = TagLoader.buildUpdatedLookups( |
| 116 | + initialLayers.getAccessForLoading(RegistryLayer.WORLDGEN), |
| 117 | + staticLayerTags |
| 118 | + ); |
| 119 | + //?} else { |
| 120 | + /*var baseRegistryAccess = RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY); |
| 121 | + *///?} |
| 122 | + //? if >= 26.1 { |
| 123 | + var registryAccess = Util.blockUntilDone(executor -> |
| 124 | + RegistryDataLoader.load( |
| 125 | + resourceManager, |
| 126 | + baseRegistryAccess, |
| 127 | + registries, |
| 128 | + executor |
| 129 | + ) |
| 130 | + ).get(); |
| 131 | + //?} else { |
| 132 | + /*var registryAccess = RegistryDataLoader.load( |
| 133 | + resourceManager, |
| 134 | + baseRegistryAccess, |
| 135 | + registries |
| 136 | + ); |
| 137 | + *///?} |
| 138 | + |
| 139 | + setRegistryManager(registryAccess); |
112 | 140 | } |
113 | 141 |
|
114 | | - setRegistryManager(saveLoader.registries().compositeAccess()); |
115 | 142 | Structurify.getLogger().info("Finished loading registry manager"); |
116 | 143 | } catch (Exception exception) { |
117 | 144 | Structurify.getLogger().error("Failed to load registry manager.", exception); |
118 | 145 | } finally { |
119 | 146 | isLoading = false; |
120 | 147 | } |
121 | 148 | } |
| 149 | + |
| 150 | + private static <T> RegistryDataLoader.RegistryData<T> getRegistryDataLoader(ResourceKey<? extends Registry<T>> key, Codec<T> codec) { |
| 151 | + //? if >= 26.1 { |
| 152 | + return new RegistryDataLoader.RegistryData<>(key, codec, RegistryValidator.none()); |
| 153 | + //?} else if >= 1.21.1 { |
| 154 | + /*return new RegistryDataLoader.RegistryData<>(key, codec, false); |
| 155 | + *///?} else { |
| 156 | + /*return new RegistryDataLoader.RegistryData<>(key, codec); |
| 157 | + *///?} |
| 158 | + } |
122 | 159 | } |
0 commit comments