Skip to content

Commit

Permalink
Retain client-only particle registry entries during sync
Browse files Browse the repository at this point in the history
Fixes #1012
  • Loading branch information
Su5eD committed Apr 17, 2024
1 parent 95fc5f4 commit a69fdaf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dev.su5ed.sinytra.connector.mod.compat;

import com.google.common.collect.BiMap;
import com.mojang.datafixers.util.Pair;
import com.mojang.logging.LogUtils;
import dev.su5ed.sinytra.connector.loader.ConnectorEarlyLoader;
import net.fabricmc.fabric.impl.registry.sync.DynamicRegistriesImpl;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.IForgeRegistry;
import org.slf4j.Logger;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static cpw.mods.modlauncher.api.LamdbaExceptionUtils.uncheck;

public class RegistryUtil {
private static final VarHandle REGISTRY_NAMES = uncheck(() -> MethodHandles.privateLookupIn(ForgeRegistry.class, MethodHandles.lookup()).findVarHandle(ForgeRegistry.class, "names", BiMap.class));
private static final ResourceLocation PARTICLE_TYPE_REGISTRY = ForgeRegistries.Keys.PARTICLE_TYPES.location();
private static final Logger LOGGER = LogUtils.getLogger();

public static boolean isRegisteredFabricDynamicRegistry(ResourceKey<?> key) {
return DynamicRegistriesImpl.FABRIC_DYNAMIC_REGISTRY_KEYS.stream().anyMatch(key::equals);
}

public static <V> void retainFabricClientEntries(ResourceLocation name, ForgeRegistry<V> from, IForgeRegistry<V> to) {
if (FMLLoader.getDist().isClient() && name.equals(PARTICLE_TYPE_REGISTRY)) {
List<Pair<ResourceLocation, V>> list = new ArrayList<>();

for (Map.Entry<ResourceKey<V>, V> entry : to.getEntries()) {
ResourceLocation location = entry.getKey().location();
if (!from.containsKey(location) && ConnectorEarlyLoader.isConnectorMod(location.getNamespace())) {
list.add(Pair.of(location, entry.getValue()));
}
}

if (!list.isEmpty()) {
LOGGER.info("Connector found {} items to retain in registry {}", list.size(), name);
}

for (Pair<ResourceLocation, V> pair : list) {
RegistryUtil.getNames(from).put(pair.getFirst(), pair.getSecond());
}
}
}

private static <V> BiMap<ResourceLocation, V> getNames(ForgeRegistry<V> registry) {
return (BiMap<ResourceLocation, V>) uncheck(() -> REGISTRY_NAMES.get(registry));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.su5ed.sinytra.connector.mod.mixin.registries;

import dev.su5ed.sinytra.connector.mod.compat.RegistryUtil;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.IForgeRegistry;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ForgeRegistry.class)
public abstract class ClientForgeRegistryMixin<V> implements IForgeRegistry<V> {

@Inject(method = "sync", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/BiMap;clear()V", ordinal = 0), remap = false)
private void retainFabricClientEntries(ResourceLocation name, ForgeRegistry<V> from, CallbackInfo ci) {
RegistryUtil.retainFabricClientEntries(name, from, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.su5ed.sinytra.connector.mod.mixin.registries;

import dev.su5ed.sinytra.connector.loader.ConnectorEarlyLoader;
import dev.su5ed.sinytra.connector.mod.compat.DynamicRegistryPrefixes;
import dev.su5ed.sinytra.connector.mod.compat.RegistryUtil;
import net.minecraft.core.Registry;
import net.minecraft.resources.FileToIdConverter;
import net.minecraft.resources.RegistryDataLoader;
Expand Down Expand Up @@ -73,7 +73,7 @@ private static String modifyRegistryDirPath(ResourceLocation location, RegistryO
private static boolean connector$shouldOmitPrefix(ResourceLocation location, ResourceKey<? extends Registry<?>> registryKey, ResourceManager manager) {
String modid = location.getNamespace();
// Fabric mod registries added directly to RegistryDataLoader.WORLDGEN_REGISTRIES should not be prefixed
if (ConnectorEarlyLoader.isConnectorMod(modid) && ModList.get().isLoaded("fabric_registry_sync_v0") && !DynamicRegistryPrefixes.isRegisteredFabricDynamicRegistry(registryKey)) {
if (ConnectorEarlyLoader.isConnectorMod(modid) && ModList.get().isLoaded("fabric_registry_sync_v0") && !RegistryUtil.isRegisteredFabricDynamicRegistry(registryKey)) {
return true;
}
// Check if the registry has been registered
Expand Down
3 changes: 2 additions & 1 deletion src/mod/resources/connectormod.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"recipebook.RecipeBookCategoriesAccessor",
"recipebook.RecipeBookCategoriesMixin",
"recipebook.RecipeBookManagerMixin",
"registries.MinecraftMixin"
"registries.MinecraftMixin",
"registries.ClientForgeRegistryMixin"
],
"server": [
"boot.ServerMainMixin",
Expand Down

0 comments on commit a69fdaf

Please sign in to comment.