From 9c21829daac750fe95f66e884e9c112c35e52437 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Fri, 27 Sep 2024 16:33:39 +0300 Subject: [PATCH 1/2] Change server root registry to reloadable --- .../org/spongepowered/vanilla/generator/GeneratorMain.java | 3 +-- .../mixin/api/minecraft/server/MinecraftServerMixin_API.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/generator/src/main/java/org/spongepowered/vanilla/generator/GeneratorMain.java b/generator/src/main/java/org/spongepowered/vanilla/generator/GeneratorMain.java index 2b19277298e..bf0569b1d98 100644 --- a/generator/src/main/java/org/spongepowered/vanilla/generator/GeneratorMain.java +++ b/generator/src/main/java/org/spongepowered/vanilla/generator/GeneratorMain.java @@ -175,7 +175,6 @@ private static Pair loadVanill ); - final RegistryAccess.Frozen compositeRegistries = withDimensions.getAccessForLoading(RegistryLayer.RELOADABLE); final var resourcesFuture = ReloadableServerResources.loadResources( resourceManager, withDimensions, @@ -207,7 +206,7 @@ private static Pair loadVanill Logger.info("Datapack load complete"); return Pair.of( - compositeRegistries, + resources.fullRegistries().get(), resources ); } diff --git a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/server/MinecraftServerMixin_API.java b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/server/MinecraftServerMixin_API.java index f6a97235086..bcbf516f734 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/server/MinecraftServerMixin_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/server/MinecraftServerMixin_API.java @@ -170,7 +170,7 @@ public abstract class MinecraftServerMixin_API implements SpongeServer, SpongeRe this.api$playerDataHandler = new SpongePlayerDataManager(this); this.api$teleportHelper = new SpongeTeleportHelper(); this.api$mapStorage = new SpongeMapStorage(); - this.api$registryHolder = new RegistryHolderLogic($$3.registries().compositeAccess()); + this.api$registryHolder = new RegistryHolderLogic($$3.dataPackResources().fullRegistries().get()); this.api$userManager = new SpongeUserManager((MinecraftServer) (Object) this); this.api$dataPackManager = new SpongeDataPackManager((MinecraftServer) (Object) this, this.storageSource.getLevelPath(LevelResource.DATAPACK_DIR)); From 187cf6ca0c4d2a528fa6ac5936b12428c91c3bd8 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Fri, 27 Sep 2024 21:01:29 +0300 Subject: [PATCH 2/2] Handle registries reload --- .../common/registry/RegistryHolderLogic.java | 11 +++++++++++ .../mixin/core/server/MinecraftServerMixin.java | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/org/spongepowered/common/registry/RegistryHolderLogic.java b/src/main/java/org/spongepowered/common/registry/RegistryHolderLogic.java index 5b1d397126f..b88a0509cad 100644 --- a/src/main/java/org/spongepowered/common/registry/RegistryHolderLogic.java +++ b/src/main/java/org/spongepowered/common/registry/RegistryHolderLogic.java @@ -220,4 +220,15 @@ public void freezeSpongeRootRegistry() { public void freezeSpongeDynamicRegistries() { this.roots.get(RegistryRoots.SPONGE).forEach(net.minecraft.core.Registry::freeze); } + + public void reload(final RegistryAccess access) { + final WritableRegistry root = new MappedRegistry<>( + net.minecraft.resources.ResourceKey.createRegistryKey((ResourceLocation) (Object) RegistryRoots.MINECRAFT), + Lifecycle.experimental() + ); + + access.registries().forEach(entry -> root.register(entry.key(), entry.value(), RegistrationInfo.BUILT_IN)); + root.freeze(); + this.roots.put((ResourceKey) (Object) ResourceLocation.withDefaultNamespace("root"), root); + } } diff --git a/src/mixins/java/org/spongepowered/common/mixin/core/server/MinecraftServerMixin.java b/src/mixins/java/org/spongepowered/common/mixin/core/server/MinecraftServerMixin.java index fdc015ccfdc..38cc4be52d9 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/core/server/MinecraftServerMixin.java +++ b/src/mixins/java/org/spongepowered/common/mixin/core/server/MinecraftServerMixin.java @@ -33,6 +33,7 @@ import net.minecraft.obfuscate.DontObfuscate; import net.minecraft.resources.ResourceKey; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.ReloadableServerRegistries; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.packs.repository.PackRepository; import net.minecraft.server.players.GameProfileCache; @@ -61,6 +62,7 @@ import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Coerce; import org.spongepowered.asm.mixin.injection.Constant; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyConstant; @@ -81,6 +83,7 @@ import org.spongepowered.common.config.inheritable.WorldConfig; import org.spongepowered.common.datapack.SpongeDataPackManager; import org.spongepowered.common.event.tracking.PhaseTracker; +import org.spongepowered.common.registry.SpongeRegistryHolder; import org.spongepowered.common.service.server.SpongeServerScopedServiceProvider; import java.io.IOException; @@ -114,6 +117,7 @@ public abstract class MinecraftServerMixin implements SpongeServer, MinecraftSer @Shadow public abstract WorldData shadow$getWorldData(); @Shadow protected abstract void loadLevel(); // has overrides! @Shadow public abstract boolean shadow$haveTime(); + @Shadow public abstract ReloadableServerRegistries.Holder shadow$reloadableRegistries(); @Shadow private volatile boolean isSaving; // @formatter:on @@ -387,6 +391,13 @@ public void setDifficulty(final Difficulty difficulty, final boolean forceDiffic this.shadow$getPackRepository().reload(); } + // Targets lambda where ReloadableResources is set to server because at + // this point reloaded resources become "valid" in terms of registries + @Inject(method = "lambda$reloadResources$29", at = @At(value = "TAIL")) + private void impl$handleRegistriesReload(final Collection $$0x, final @Coerce Object reloadableResources, final CallbackInfo ci) { + ((SpongeRegistryHolder) this).registryHolder().reload(this.shadow$reloadableRegistries().get()); + } + @Override public String toString() { return this.getClass().getSimpleName();