| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.List;
|
| 5 | import java.util.concurrent.CompletableFuture;
|
| 6 | import java.util.concurrent.Executor;
|
| 7 | import net.minecraft.commands.CommandBuildContext;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.core.LayeredRegistryAccess;
|
| 10 | import net.minecraft.core.Registry;
|
| 11 | import net.minecraft.core.component.DataComponentInitializers;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.server.packs.resources.PreparableReloadListener;
|
| 14 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 15 | import net.minecraft.server.packs.resources.SimpleReloadInstance;
|
| 16 | import net.minecraft.server.permissions.PermissionSet;
|
| 17 | import net.minecraft.util.Unit;
|
| 18 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 19 | import net.minecraft.world.item.crafting.RecipeManager;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public class ReloadableServerResources {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final CompletableFuture<Unit> DATA_RELOAD_INITIAL_TASK = CompletableFuture.completedFuture(Unit.INSTANCE);
|
| 25 | private final ReloadableServerRegistries.Holder fullRegistryHolder;
|
| 26 | private final Commands commands;
|
| 27 | private final RecipeManager recipes;
|
| 28 | private final ServerAdvancementManager advancements;
|
| 29 | private final ServerFunctionLibrary functionLibrary;
|
| 30 | private final List<Registry.PendingTags<?>> postponedTags;
|
| 31 | private final List<DataComponentInitializers.PendingComponents<?>> newComponents;
|
| 32 |
|
| 33 | private ReloadableServerResources(
|
| 34 | final ReloadableServerRegistries.LoadResult loadingContext,
|
| 35 | final FeatureFlagSet enabledFeatures,
|
| 36 | final Commands.CommandSelection commandSelection,
|
| 37 | final List<Registry.PendingTags<?>> postponedTags,
|
| 38 | final PermissionSet functionCompilationPermissions,
|
| 39 | final List<DataComponentInitializers.PendingComponents<?>> newComponents
|
| 40 | ) {
|
| 41 | this.fullRegistryHolder = new ReloadableServerRegistries.Holder(loadingContext.layers().compositeAccess());
|
| 42 | this.postponedTags = postponedTags;
|
| 43 | this.newComponents = newComponents;
|
| 44 | this.recipes = new RecipeManager(loadingContext.lookupWithUpdatedTags());
|
| 45 | this.commands = new Commands(commandSelection, CommandBuildContext.simple(loadingContext.lookupWithUpdatedTags(), enabledFeatures));
|
| 46 | this.advancements = new ServerAdvancementManager(loadingContext.lookupWithUpdatedTags());
|
| 47 | this.functionLibrary = new ServerFunctionLibrary(functionCompilationPermissions, this.commands.getDispatcher());
|
| 48 | }
|
| 49 |
|
| 50 | public ServerFunctionLibrary getFunctionLibrary() {
|
| 51 | return this.functionLibrary;
|
| 52 | }
|
| 53 |
|
| 54 | public ReloadableServerRegistries.Holder fullRegistries() {
|
| 55 | return this.fullRegistryHolder;
|
| 56 | }
|
| 57 |
|
| 58 | public RecipeManager getRecipeManager() {
|
| 59 | return this.recipes;
|
| 60 | }
|
| 61 |
|
| 62 | public Commands getCommands() {
|
| 63 | return this.commands;
|
| 64 | }
|
| 65 |
|
| 66 | public ServerAdvancementManager getAdvancements() {
|
| 67 | return this.advancements;
|
| 68 | }
|
| 69 |
|
| 70 | public List<PreparableReloadListener> listeners() {
|
| 71 | return List.of(this.functionLibrary);
|
| 72 | }
|
| 73 |
|
| 74 | public static CompletableFuture<ReloadableServerResources> loadResources(
|
| 75 | final ResourceManager resourceManager,
|
| 76 | final LayeredRegistryAccess<RegistryLayer> contextLayers,
|
| 77 | final List<Registry.PendingTags<?>> updatedContextTags,
|
| 78 | final FeatureFlagSet enabledFeatures,
|
| 79 | final Commands.CommandSelection commandSelection,
|
| 80 | final PermissionSet functionCompilationPermissions,
|
| 81 | final Executor backgroundExecutor,
|
| 82 | final Executor mainThreadExecutor
|
| 83 | ) {
|
| 84 | return ReloadableServerRegistries.reload(contextLayers, updatedContextTags, resourceManager, backgroundExecutor)
|
| 85 | .thenCompose(
|
| 86 | fullRegistries -> CompletableFuture.<List<DataComponentInitializers.PendingComponents<?>>>supplyAsync(
|
| 87 | () -> BuiltInRegistries.DATA_COMPONENT_INITIALIZERS.build(fullRegistries.lookupWithUpdatedTags()), backgroundExecutor
|
| 88 | )
|
| 89 | .thenCompose(
|
| 90 | pendingComponents -> {
|
| 91 | ReloadableServerResources result = new ReloadableServerResources(
|
| 92 | fullRegistries,
|
| 93 | enabledFeatures,
|
| 94 | commandSelection,
|
| 95 | updatedContextTags,
|
| 96 | functionCompilationPermissions,
|
| 97 | (List<DataComponentInitializers.PendingComponents<?>>)pendingComponents
|
| 98 | );
|
| 99 | return SimpleReloadInstance.create(
|
| 100 | resourceManager, result.listeners(), backgroundExecutor, mainThreadExecutor, DATA_RELOAD_INITIAL_TASK, LOGGER.isDebugEnabled()
|
| 101 | )
|
| 102 | .done()
|
| 103 | .thenApply(ignore -> result);
|
| 104 | }
|
| 105 | )
|
| 106 | );
|
| 107 | }
|
| 108 |
|
| 109 | public void updateComponentsAndStaticRegistryTags() {
|
| 110 | this.postponedTags.forEach(Registry.PendingTags::apply);
|
| 111 | this.newComponents.forEach(DataComponentInitializers.PendingComponents::apply);
|
| 112 | }
|
| 113 | }
|
| 114 |
|