Skip to content

Commit

Permalink
Freeze registries much sooner after mod init
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed May 23, 2024
1 parent c9b48ce commit c30460c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package net.fabricmc.fabric.mixin.registry.sync.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -35,23 +35,24 @@

@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {
@Unique
private static Logger FABRIC_LOGGER = LoggerFactory.getLogger(MinecraftClientMixin.class);
@Shadow
@Final
private static Logger LOGGER;

// Unmap the registry before loading a new SP/MP setup.
@Inject(at = @At("RETURN"), method = "disconnect(Lnet/minecraft/client/gui/screen/Screen;Z)V")
public void disconnectAfter(Screen disconnectionScreen, boolean bl, CallbackInfo ci) {
try {
RegistrySyncManager.unmap();
} catch (RemapException e) {
FABRIC_LOGGER.warn("Failed to unmap Fabric registries!", e);
LOGGER.warn("Failed to unmap Fabric registries!", e);
}
}

@Inject(at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;thread:Ljava/lang/Thread;", shift = At.Shift.AFTER, ordinal = 0), method = "run")
private void onStart(CallbackInfo ci) {
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Ljava/lang/Thread;currentThread()Ljava/lang/Thread;"))
private void afterModInit(CallbackInfo ci) {
// Freeze the registries on the client
FABRIC_LOGGER.debug("Freezing registries");
LOGGER.debug("Freezing registries");
Registries.bootstrap();
BlockInitTracker.postFreeze();
ItemGroups.collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,32 @@
package net.fabricmc.fabric.mixin.registry.sync;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.item.ItemGroups;
import net.minecraft.registry.Registries;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.Main;

import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.impl.registry.sync.trackers.vanilla.BlockInitTracker;
import net.fabricmc.loader.api.FabricLoader;

@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {
@Unique
private static final Logger FABRIC_LOGGER = LoggerFactory.getLogger(MinecraftServerMixin.class);
@Mixin(Main.class)
public class MainMixin {
@Shadow
@Final
private static Logger LOGGER;

@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;setupServer()Z"), method = "runServer")
private void beforeSetupServer(CallbackInfo info) {
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Util;startTimerHack()V"), method = "main")
private static void afterModInit(CallbackInfo info) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
// Freeze the registries on the server
FABRIC_LOGGER.debug("Freezing registries");
LOGGER.debug("Freezing registries");

Registries.bootstrap();
BlockInitTracker.postFreeze();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"DebugChunkGeneratorAccessor",
"ExperimentalRegistriesValidatorMixin",
"IdListMixin",
"MinecraftServerMixin",
"MainMixin",
"RegistriesAccessor",
"RegistriesMixin",
"RegistryLoaderMixin",
Expand Down

0 comments on commit c30460c

Please sign in to comment.