Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reloadable registries #4132

Open
wants to merge 2 commits into
base: api-12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ private static Pair<RegistryAccess.Frozen, ReloadableServerResources> loadVanill
);


final RegistryAccess.Frozen compositeRegistries = withDimensions.getAccessForLoading(RegistryLayer.RELOADABLE);
final var resourcesFuture = ReloadableServerResources.loadResources(
resourceManager,
withDimensions,
Expand Down Expand Up @@ -207,7 +206,7 @@ private static Pair<RegistryAccess.Frozen, ReloadableServerResources> loadVanill
Logger.info("Datapack load complete");

return Pair.of(
compositeRegistries,
resources.fullRegistries().get(),
resources
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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<String> $$0x, final @Coerce Object reloadableResources, final CallbackInfo ci) {
((SpongeRegistryHolder) this).registryHolder().reload(this.shadow$reloadableRegistries().get());
}

@Override
public String toString() {
return this.getClass().getSimpleName();
Expand Down