[1.18.2] Fix tags for custom forge registries. (#8495)

Tag-enabled registries must now be registered to vanilla's root registry. See RegistryBuilder#hasTags.
Modded tag-enabled registries have to use the format `data/<namespace>/tags/<registrynamespace>/<registryname>/<tag>.json`
This format is to prevent conflicts for registries with the same path but different namespaces
EX: Registry name `examplemod:shoe`, tag name `blue_shoes` would be `data/<namespace>/tags/examplemod/shoe/blue_shoes.json`
RegistryEvent.NewRegistry has been moved and renamed to NewRegistryEvent.
RegistryBuilder#create has been made private. See NewRegistryEvent#create
Created new ITagManager system for looking up Forge tags. See IForgeRegistry#tags.
Add lookup methods for Holders from forge registries. See IForgeRegistry#getHolder.
This commit is contained in:
SizableShrimp authored and GitHub committed 2022-03-16 13:36:00 -07:00
1 parent 30f3a7dbba
commit ad75cb8644
51 files changed
+1621 -531

No files matched your search

@@ -7,9 +7,13 @@ package net.minecraftforge.registries;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.google.common.collect.BiMap;
@@ -18,9 +22,13 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
import com.mojang.serialization.Lifecycle;
import net.minecraft.core.WritableRegistry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraftforge.fml.IModStateTransition;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.network.HandshakeMessages;
import net.minecraftforge.registries.ForgeRegistry.Snapshot;
import org.apache.commons.lang3.tuple.Pair;
@@ -33,6 +41,7 @@ public class RegistryManager
public static final RegistryManager ACTIVE = new RegistryManager("ACTIVE");
public static final RegistryManager VANILLA = new RegistryManager("VANILLA");
public static final RegistryManager FROZEN = new RegistryManager("FROZEN");
private static Set<ResourceLocation> vanillaRegistryKeys = Set.of();
BiMap<ResourceLocation, ForgeRegistry<? extends IForgeRegistryEntry<?>>> registries = HashBiMap.create();
private BiMap<Class<? extends IForgeRegistryEntry<?>>, ResourceLocation> superTypes = HashBiMap.create();
@@ -68,6 +77,12 @@ public class RegistryManager
return getRegistry(key.location());
}
/**
* @see #getRegistry(ResourceLocation)
* @see #getRegistry(ResourceKey)
* @deprecated The uniqueness of registry super types will not be guaranteed starting in 1.19.
*/
@Deprecated(forRemoval = true, since = "1.18.2")
public <V extends IForgeRegistryEntry<V>> IForgeRegistry<V> getRegistry(Class<? super V> cls)
{
return getRegistry(superTypes.get(cls));
@@ -124,6 +139,9 @@ public class RegistryManager
foundType, superTypes.get(foundType), name, builder.getType(), builder.getType());
throw new IllegalArgumentException("Duplicate registry parent type found - you can only have one registry for a particular super type");
}
if (registries.containsKey(name))
throw new IllegalArgumentException("Attempted to register a registry for " + name + " with type "
+ builder.getType().getName() + " but it already exists as " + registries.get(name).getRegistrySuperType().getName());
ForgeRegistry<V> reg = new ForgeRegistry<V>(this, name, builder);
registries.put(name, reg);
superTypes.put(builder.getType(), name);
@@ -136,6 +154,36 @@ public class RegistryManager
return getRegistry(name);
}
@SuppressWarnings("unchecked")
static <V extends IForgeRegistryEntry<V>> void registerToRootRegistry(ForgeRegistry<V> forgeReg)
{
WritableRegistry<Registry<V>> registry = (WritableRegistry<Registry<V>>) Registry.REGISTRY;
Registry<V> wrapper = forgeReg.getWrapper();
if (wrapper != null)
registry.register(forgeReg.getRegistryKey(), wrapper, Lifecycle.experimental());
}
public static IModStateTransition.EventGenerator<NewRegistryEvent> newRegistryEventGenerator()
{
NewRegistryEvent event = new NewRegistryEvent();
return mc -> event;
}
public static CompletableFuture<List<Throwable>> preNewRegistryEvent(final Executor executor,
final IModStateTransition.EventGenerator<? extends NewRegistryEvent> eventGenerator)
{
return CompletableFuture.runAsync(() -> vanillaRegistryKeys = Set.copyOf(Registry.REGISTRY.keySet()), executor)
.handle((v, t) -> t != null ? Collections.singletonList(t) : Collections.emptyList());
}
public static CompletableFuture<List<Throwable>> postNewRegistryEvent(final Executor executor,
final IModStateTransition.EventGenerator<? extends NewRegistryEvent> eventGenerator)
{
return CompletableFuture.runAsync(() -> eventGenerator.apply(null).fill(), executor)
.handle((v, t) -> t != null ? Collections.singletonList(t) : Collections.emptyList());
}
private void addLegacyName(ResourceLocation legacyName, ResourceLocation name)
{
if (this.legacyNames.containsKey(legacyName))
@@ -189,4 +237,9 @@ public class RegistryManager
filter(resloc -> ACTIVE.synced.contains(resloc)).
collect(Collectors.toList());
}
public static Set<ResourceLocation> getVanillaRegistryKeys()
{
return vanillaRegistryKeys;
}
}