Skip to content

Commit

Permalink
Fix #3215 - Only add namespace to registries added using the Fabric API.
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Jul 24, 2023
1 parent e3d1b5f commit b574cf1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

public final class DynamicRegistriesImpl {
private static final List<RegistryLoader.Entry<?>> DYNAMIC_REGISTRIES = new ArrayList<>(RegistryLoader.DYNAMIC_REGISTRIES);
public static final Set<RegistryKey<?>> FABRIC_DYNAMIC_REGISTRY_KEYS = new HashSet<>();
public static final Set<RegistryKey<? extends Registry<?>>> DYNAMIC_REGISTRY_KEYS = new HashSet<>();
public static final Set<RegistryKey<? extends Registry<?>>> SKIP_EMPTY_SYNC_REGISTRIES = new HashSet<>();

Expand All @@ -61,6 +62,7 @@ public static <T> void register(RegistryKey<? extends Registry<T>> key, Codec<T>

var entry = new RegistryLoader.Entry<>(key, codec);
DYNAMIC_REGISTRIES.add(entry);
FABRIC_DYNAMIC_REGISTRY_KEYS.add(key);
}

public static <T> void addSyncedRegistry(RegistryKey<? extends Registry<T>> registryKey, Codec<T> networkCodec, DynamicRegistries.SyncOption... options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import java.util.Map;

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Decoder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

Expand All @@ -37,10 +40,14 @@
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.event.registry.DynamicRegistrySetupCallback;
import net.fabricmc.fabric.impl.registry.sync.DynamicRegistriesImpl;
import net.fabricmc.fabric.impl.registry.sync.DynamicRegistryViewImpl;

@Mixin(RegistryLoader.class)
public class RegistryLoaderMixin {
@Unique
private static final ThreadLocal<RegistryKey<?>> REGISTRY_KEY_THREAD_LOCAL = new ThreadLocal<>();

@Inject(
method = "load(Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/registry/DynamicRegistryManager;Ljava/util/List;)Lnet/minecraft/registry/DynamicRegistryManager$Immutable;",
at = @At(
Expand All @@ -60,13 +67,24 @@ private static void beforeLoad(ResourceManager resourceManager, DynamicRegistryM
DynamicRegistrySetupCallback.EVENT.invoker().onRegistrySetup(new DynamicRegistryViewImpl(registries));
}

@Inject(at = @At("HEAD"), method = "load(Lnet/minecraft/registry/RegistryOps$RegistryInfoGetter;Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/registry/RegistryKey;Lnet/minecraft/registry/MutableRegistry;Lcom/mojang/serialization/Decoder;Ljava/util/Map;)V")
private static <E> void captureRegistryKey(RegistryOps.RegistryInfoGetter registryInfoGetter, ResourceManager resourceManager, RegistryKey<? extends Registry<E>> registryKey, MutableRegistry<E> newRegistry, Decoder<E> decoder, Map<RegistryKey<?>, Exception> exceptions, CallbackInfo ci) {
REGISTRY_KEY_THREAD_LOCAL.set(registryKey);
}

// Vanilla doesn't mark namespaces in the directories of dynamic registries at all,
// so we prepend the directories with the namespace if it's a modded registry id.
// so we prepend the directories with the namespace if it's a modded registry registered using the Fabric API.
@Inject(method = "getPath", at = @At("RETURN"), cancellable = true)
private static void prependDirectoryWithNamespace(Identifier id, CallbackInfoReturnable<String> info) {
if (!id.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)) {
String newPath = id.getNamespace() + "/" + info.getReturnValue();
if (!id.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)
&& DynamicRegistriesImpl.FABRIC_DYNAMIC_REGISTRY_KEYS.contains(REGISTRY_KEY_THREAD_LOCAL.get())) {
final String newPath = id.getNamespace() + "/" + info.getReturnValue();
info.setReturnValue(newPath);
}
}

@Inject(at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/registry/RegistryLoader;getPath(Lnet/minecraft/util/Identifier;)Ljava/lang/String;"), method = "load(Lnet/minecraft/registry/RegistryOps$RegistryInfoGetter;Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/registry/RegistryKey;Lnet/minecraft/registry/MutableRegistry;Lcom/mojang/serialization/Decoder;Ljava/util/Map;)V")
private static <E> void clearRegistryKey(RegistryOps.RegistryInfoGetter registryInfoGetter, ResourceManager resourceManager, RegistryKey<? extends Registry<E>> registryKey, MutableRegistry<E> newRegistry, Decoder<E> decoder, Map<RegistryKey<?>, Exception> exceptions, CallbackInfo ci) {
REGISTRY_KEY_THREAD_LOCAL.set(null);
}
}

0 comments on commit b574cf1

Please sign in to comment.