[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

@@ -5,13 +5,23 @@
package net.minecraftforge.registries;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.registries.tags.ITagManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -47,35 +57,66 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
*/
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(IForgeRegistry<B> reg, String modid)
{
return new DeferredRegister<B>(reg, modid);
return new DeferredRegister<>(reg, modid);
}
/**
* Use for custom registries that are made during the NewRegistry event.
* Use for custom registries that are made during the NewRegistry event.
*
* @deprecated Use {@link #create(ResourceLocation, String)} and {@link #makeRegistry(Class, Supplier)} instead
*/
@Deprecated(forRemoval = true, since = "1.18.2")
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(Class<B> base, String modid)
{
return new DeferredRegister<B>(base, modid);
return new DeferredRegister<>(null, base, modid);
}
/**
* DeferredRegister factory for custom registries that are made during the {@link NewRegistryEvent} event
* or with {@link #makeRegistry(Class, Supplier)}.
*
* @param key The key of the registry to reference
* @param modid The namespace for all objects registered to this DeferredRegister
*/
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(ResourceKey<? extends Registry<B>> key, String modid)
{
return new DeferredRegister<>(key.location(), null, modid);
}
/**
* DeferredRegister factory for custom registries that are made during the {@link NewRegistryEvent} event
* or with {@link #makeRegistry(Class, Supplier)}.
*
* @param registryName The name of the registry, should include namespace. May come from another DeferredRegister through {@link #getRegistryName()}.
* @param modid The namespace for all objects registered to this DeferredRegister
*/
public static <B extends IForgeRegistryEntry<B>> DeferredRegister<B> create(ResourceLocation registryName, String modid)
{
return new DeferredRegister<>(registryName, null, modid);
}
private final ResourceLocation registryName;
private final Class<T> superType;
private final String modid;
private final Map<RegistryObject<T>, Supplier<? extends T>> entries = new LinkedHashMap<>();
private final Set<RegistryObject<T>> entriesView = Collections.unmodifiableSet(entries.keySet());
private IForgeRegistry<T> type;
private Supplier<IForgeRegistry<T>> typeSupplier;
private Supplier<RegistryBuilder<T>> registryFactory;
private SetMultimap<TagKey<T>, Supplier<T>> optionalTags;
private boolean seenRegisterEvent = false;
private DeferredRegister(Class<T> base, String modid)
private DeferredRegister(@Nullable ResourceLocation registryName, @Nullable Class<T> base, String modid)
{
this.registryName = registryName;
this.superType = base;
this.modid = modid;
}
private DeferredRegister(IForgeRegistry<T> reg, String modid)
{
this(reg.getRegistrySuperType(), modid);
this(reg.getRegistryName(), reg.getRegistrySuperType(), modid);
this.type = reg;
}
@@ -96,8 +137,11 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
final ResourceLocation key = new ResourceLocation(modid, name);
RegistryObject<I> ret;
if (this.type != null)
ret = RegistryObject.of(key, this.type);
IForgeRegistry<T> reg = getForgeRegistry();
if (reg != null)
ret = RegistryObject.of(key, reg);
else if (this.registryName != null)
ret = RegistryObject.of(key, this.registryName, this.modid);
else if (this.superType != null)
ret = RegistryObject.of(key, this.superType, this.modid);
else
@@ -118,15 +162,122 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
* @param name Path of the registry's {@link ResourceLocation}
* @param sup Supplier of the RegistryBuilder that is called to fill {@link #type} during the NewRegistry event
* @return A supplier of the {@link IForgeRegistry} created by the builder.
*
* @deprecated Use {@link #create(ResourceLocation, String)} and {@link #makeRegistry(Class, Supplier)} instead
*/
public Supplier<IForgeRegistry<T>> makeRegistry(final String name, final Supplier<RegistryBuilder<T>> sup) {
if (this.superType == null)
throw new IllegalStateException("Cannot create a registry without specifying a base type");
if (this.type != null || this.registryFactory != null)
throw new IllegalStateException("Cannot create a registry for a type that already exists");
@Deprecated(forRemoval = true, since = "1.18.2")
public Supplier<IForgeRegistry<T>> makeRegistry(final String name, final Supplier<RegistryBuilder<T>> sup)
{
return makeRegistry(new ResourceLocation(modid, name), this.superType, sup);
}
this.registryFactory = () -> sup.get().setName(new ResourceLocation(modid, name)).setType(this.superType);
return () -> this.type;
/**
* Only used for custom registries to fill the forge registry held in this DeferredRegister.
*
* Calls {@link RegistryBuilder#setName} and {@link RegistryBuilder#setType} automatically.
*
* @param base The base type to use in the created {@link IForgeRegistry}
* @param sup Supplier of a RegistryBuilder that initializes a {@link IForgeRegistry} during the {@link NewRegistryEvent} event
* @return A supplier of the {@link IForgeRegistry} created by the builder.
* Will always return null until after the {@link NewRegistryEvent} event fires.
*/
public Supplier<IForgeRegistry<T>> makeRegistry(final Class<T> base, final Supplier<RegistryBuilder<T>> sup)
{
return makeRegistry(this.registryName, base, sup);
}
/**
* Creates a tag key based on the current modid and provided path as the location and the registry name linked to this DeferredRegister.
* To control the namespace, use {@link #createTagKey(ResourceLocation)}.
*
* @throws IllegalStateException If the registry name was not set.
* Use the factories that take {@link #create(ResourceLocation, String) a registry name} or {@link #create(IForgeRegistry, String) forge registry}.
* @see #createTagKey(ResourceLocation)
* @see #createOptionalTagKey(String, Set)
*/
@NotNull
public TagKey<T> createTagKey(@NotNull String path)
{
Objects.requireNonNull(path);
return createTagKey(new ResourceLocation(this.modid, path));
}
/**
* Creates a tag key based on the provided resource location and the registry name linked to this DeferredRegister.
* To use the current modid as the namespace, use {@link #createTagKey(String)}.
*
* @throws IllegalStateException If the registry name was not set.
* Use the factories that take {@link #create(ResourceLocation, String) a registry name} or {@link #create(IForgeRegistry, String) forge registry}.
* @see #createTagKey(String)
* @see #createOptionalTagKey(ResourceLocation, Set)
*/
@NotNull
public TagKey<T> createTagKey(@NotNull ResourceLocation location)
{
if (this.registryName == null)
throw new IllegalStateException("The registry name was not set, cannot create a tag key");
Objects.requireNonNull(location);
return TagKey.create(ResourceKey.createRegistryKey(this.registryName), location);
}
/**
* Creates a tag key with the current modid and provided path that will use the set of defaults if the tag is not loaded from any datapacks.
* Useful on the client side when a server may not provide a specific tag.
* To control the namespace, use {@link #createOptionalTagKey(ResourceLocation, Set)}.
*
* @throws IllegalStateException If the registry name was not set.
* Use the factories that take {@link #create(ResourceLocation, String) a registry name} or {@link #create(IForgeRegistry, String) forge registry}.
* @see #createTagKey(String)
* @see #createTagKey(ResourceLocation)
* @see #createOptionalTagKey(ResourceLocation, Set)
* @see #addOptionalTagDefaults(TagKey, Set)
*/
@NotNull
public TagKey<T> createOptionalTagKey(@NotNull String path, @NotNull Set<? extends Supplier<T>> defaults)
{
Objects.requireNonNull(path);
return createOptionalTagKey(new ResourceLocation(this.modid, path), defaults);
}
/**
* Creates a tag key with the provided location that will use the set of defaults if the tag is not loaded from any datapacks.
* Useful on the client side when a server may not provide a specific tag.
* To use the current modid as the namespace, use {@link #createOptionalTagKey(String, Set)}.
*
* @throws IllegalStateException If the registry name was not set.
* Use the factories that take {@link #create(ResourceLocation, String) a registry name} or {@link #create(IForgeRegistry, String) forge registry}.
* @see #createTagKey(String)
* @see #createTagKey(ResourceLocation)
* @see #createOptionalTagKey(String, Set)
* @see #addOptionalTagDefaults(TagKey, Set)
*/
@NotNull
public TagKey<T> createOptionalTagKey(@NotNull ResourceLocation location, @NotNull Set<? extends Supplier<T>> defaults)
{
TagKey<T> tagKey = createTagKey(location);
addOptionalTagDefaults(tagKey, defaults);
return tagKey;
}
/**
* Adds defaults to an existing tag key.
* The set of defaults will be bound to the tag if the tag is not loaded from any datapacks.
* Useful on the client side when a server may not provide a specific tag.
*
* @throws IllegalStateException If the registry name was not set.
* Use the factories that take {@link #create(ResourceLocation, String) a registry name} or {@link #create(IForgeRegistry, String) forge registry}.
* @see #createOptionalTagKey(String, Set)
* @see #createOptionalTagKey(ResourceLocation, Set)
*/
public void addOptionalTagDefaults(@NotNull TagKey<T> name, @NotNull Set<? extends Supplier<T>> defaults)
{
Objects.requireNonNull(defaults);
if (optionalTags == null)
optionalTags = Multimaps.newSetMultimap(new IdentityHashMap<>(), HashSet::new);
optionalTags.putAll(name, defaults);
}
/**
@@ -162,17 +313,60 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
return entriesView;
}
/**
* @return The registry name stored in this deferred register. Useful for creating new deferred registers based on an existing one.
*/
@Nullable
public ResourceLocation getRegistryName()
{
return this.registryName;
}
private Supplier<IForgeRegistry<T>> makeRegistry(final ResourceLocation registryName, final Class<T> superType, final Supplier<RegistryBuilder<T>> sup) {
if (registryName == null)
throw new IllegalStateException("Cannot create a registry without specifying a registry name");
if (superType == null)
throw new IllegalStateException("Cannot create a registry without specifying a base type");
if (this.type != null || this.registryFactory != null)
throw new IllegalStateException("Cannot create a registry for a type that already exists");
this.registryFactory = () -> sup.get().setName(registryName).setType(superType);
return this::getForgeRegistry;
}
private IForgeRegistry<T> getForgeRegistry()
{
if (this.type == null && this.typeSupplier != null)
this.type = this.typeSupplier.get();
return this.type;
}
private void onFill(IForgeRegistry<T> registry)
{
if (this.optionalTags == null)
return;
ITagManager<T> tagManager = registry.tags();
if (tagManager == null)
throw new IllegalStateException("The forge registry " + registry.getRegistryName() + " does not support tags, but optional tags were registered!");
Multimaps.asMap(this.optionalTags).forEach(tagManager::addOptionalTagDefaults);
}
private void addEntries(RegistryEvent.Register<?> event)
{
if (this.type == null && this.registryFactory == null)
IForgeRegistry<T> storedType = this.getForgeRegistry();
if (storedType == null && this.registryFactory == null)
{
//If there is no type yet and we don't have a registry factory, attempt to capture the registry
//Note: This will only ever get run on the first registry event, as after that time,
// the type will no longer be null. This is needed here rather than during the NewRegistry event
// to ensure that mods can properly use deferred registers for custom registries added by other mods
captureRegistry();
storedType = this.type;
}
if (this.type != null && event.getGenericType() == this.type.getRegistrySuperType())
if (storedType != null && event.getGenericType() == storedType.getRegistrySuperType())
{
this.seenRegisterEvent = true;
@SuppressWarnings("unchecked")
@@ -185,14 +379,20 @@ public class DeferredRegister<T extends IForgeRegistryEntry<T>>
}
}
private void createRegistry(RegistryEvent.NewRegistry event)
private void createRegistry(NewRegistryEvent event)
{
this.type = this.registryFactory.get().create();
this.typeSupplier = event.create(this.registryFactory.get(), this::onFill);
}
private void captureRegistry()
{
if (this.superType != null)
if (this.registryName != null)
{
this.type = RegistryManager.ACTIVE.getRegistry(this.registryName);
if (this.type == null)
throw new IllegalStateException("Unable to find registry with key " + this.registryName + " for modid \"" + modid + "\" after NewRegistry event");
}
else if (this.superType != null)
{
this.type = RegistryManager.ACTIVE.getRegistry(this.superType);
if (this.type == null)