| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Lifecycle;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.concurrent.CompletableFuture;
|
| 8 | import java.util.concurrent.Executor;
|
| 9 | import java.util.stream.Stream;
|
| 10 | import net.minecraft.core.HolderLookup;
|
| 11 | import net.minecraft.core.LayeredRegistryAccess;
|
| 12 | import net.minecraft.core.RegistrationInfo;
|
| 13 | import net.minecraft.core.Registry;
|
| 14 | import net.minecraft.core.RegistryAccess;
|
| 15 | import net.minecraft.core.registries.Registries;
|
| 16 | import net.minecraft.resources.RegistryDataLoader;
|
| 17 | import net.minecraft.resources.ResourceKey;
|
| 18 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 19 | import net.minecraft.tags.TagLoader;
|
| 20 | import net.minecraft.util.ProblemReporter;
|
| 21 | import net.minecraft.world.level.storage.loot.LootDataType;
|
| 22 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 23 | import net.minecraft.world.level.storage.loot.ValidationContextSource;
|
| 24 | import org.slf4j.Logger;
|
| 25 |
|
| 26 | public class ReloadableServerRegistries {
|
| 27 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 28 | private static final RegistrationInfo DEFAULT_REGISTRATION_INFO = new RegistrationInfo(Optional.empty(), Lifecycle.experimental());
|
| 29 |
|
| 30 | public static CompletableFuture<ReloadableServerRegistries.LoadResult> reload(
|
| 31 | final LayeredRegistryAccess<RegistryLayer> context,
|
| 32 | final List<Registry.PendingTags<?>> updatedContextTags,
|
| 33 | final ResourceManager manager,
|
| 34 | final Executor executor
|
| 35 | ) {
|
| 36 | List<HolderLookup.RegistryLookup<?>> contextRegistriesWithTags = TagLoader.buildUpdatedLookups(
|
| 37 | context.getAccessForLoading(RegistryLayer.RELOADABLE), updatedContextTags
|
| 38 | );
|
| 39 | HolderLookup.Provider loadingContextWithTags = HolderLookup.Provider.create(contextRegistriesWithTags.stream());
|
| 40 | return RegistryDataLoader.load(manager, contextRegistriesWithTags, RegistryDataLoader.RELOADABLE_REGISTRIES, executor)
|
| 41 | .thenApplyAsync(newlyLoadedRegistries -> createAndValidateFullContext(context, loadingContextWithTags, newlyLoadedRegistries), executor);
|
| 42 | }
|
| 43 |
|
| 44 | private static ReloadableServerRegistries.LoadResult createAndValidateFullContext(
|
| 45 | final LayeredRegistryAccess<RegistryLayer> contextLayers,
|
| 46 | final HolderLookup.Provider contextLookupWithUpdatedTags,
|
| 47 | final RegistryAccess.Frozen reloadableRegistries
|
| 48 | ) {
|
| 49 | LayeredRegistryAccess<RegistryLayer> fullLayers = contextLayers.replaceFrom(RegistryLayer.RELOADABLE, reloadableRegistries);
|
| 50 | HolderLookup.Provider fullLookupWithUpdatedTags = concatenateLookups(contextLookupWithUpdatedTags, reloadableRegistries);
|
| 51 | validateLootRegistries(fullLookupWithUpdatedTags);
|
| 52 | return new ReloadableServerRegistries.LoadResult(fullLayers, fullLookupWithUpdatedTags);
|
| 53 | }
|
| 54 |
|
| 55 | private static HolderLookup.Provider concatenateLookups(final HolderLookup.Provider first, final HolderLookup.Provider second) {
|
| 56 | return HolderLookup.Provider.create(Stream.concat(first.listRegistries(), second.listRegistries()));
|
| 57 | }
|
| 58 |
|
| 59 | private static void validateLootRegistries(final HolderLookup.Provider fullContextWithNewTags) {
|
| 60 | ProblemReporter.Collector problems = new ProblemReporter.Collector();
|
| 61 | ValidationContextSource contextSource = new ValidationContextSource(problems, fullContextWithNewTags);
|
| 62 | LootDataType.values().forEach(lootDataType -> lootDataType.runValidation(contextSource, fullContextWithNewTags));
|
| 63 | problems.forEach((id, problem) -> LOGGER.warn("Found loot table element validation problem in {}: {}", id, problem.description()));
|
| 64 | if (problems.hasFatalProblems()) {
|
| 65 | throw new IllegalStateException("Found loot table validation problems");
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | public static class Holder {
|
| 70 | private final HolderLookup.Provider registries;
|
| 71 |
|
| 72 | public Holder(final HolderLookup.Provider registries) {
|
| 73 | this.registries = registries;
|
| 74 | }
|
| 75 |
|
| 76 | public HolderLookup.Provider lookup() {
|
| 77 | return this.registries;
|
| 78 | }
|
| 79 |
|
| 80 | public LootTable getLootTable(final ResourceKey<LootTable> id) {
|
| 81 | return this.registries.lookup(Registries.LOOT_TABLE).flatMap(r -> r.get(id)).map(net.minecraft.core.Holder::value).orElse(LootTable.EMPTY);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public record LoadResult(LayeredRegistryAccess<RegistryLayer> layers, HolderLookup.Provider lookupWithUpdatedTags) {
|
| 86 | }
|
| 87 | }
|
| 88 |
|