/* * Copyright (c) Forge Development LLC and contributors * SPDX-License-Identifier: LGPL-2.1-only */ package net.minecraftforge.registries; import com.google.common.collect.Multimaps; import com.google.common.collect.SetMultimap; import net.minecraft.core.Registry; import net.minecraft.data.BuiltinRegistries; 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; import java.util.Objects; import java.util.Set; import java.util.function.Supplier; /** * Utility class to help with managing registry entries. * Maintains a list of all suppliers for entries and registers them during the proper Register event. * Suppliers should return NEW instances every time. * *Example Usage: *
{@code
* private static final DeferredRegister- ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
* private static final DeferredRegister
BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
*
* public static final RegistryObject ROCK_BLOCK = BLOCKS.register("rock", () -> new Block(Block.Properties.create(Material.ROCK)));
* public static final RegistryObject- ROCK_ITEM = ITEMS.register("rock", () -> new BlockItem(ROCK_BLOCK.get(), new Item.Properties().group(ItemGroup.MISC)));
*
* public ExampleMod() {
* ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
* BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus());
* }
*}
*
* @param * If you have a supplier, do not use this method. * Instead, use one of the other factories that takes in a registry key or registry name. * * @param reg the forge registry to wrap * @param modid the namespace for all objects registered to this DeferredRegister * @see #create(ResourceKey, String) * @see #create(ResourceLocation, String) */ public static > DeferredRegister create(IForgeRegistry reg, String modid) { return new DeferredRegister<>(reg, modid); } /** * 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 > DeferredRegister create(Class base, String modid) { return new DeferredRegister<>(null, base, modid, false); } /** * DeferredRegister factory for custom forge registries, {@link Registry vanilla registries}, * or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry key. * Supports both registries that already exist or do not exist yet. *
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will throw an exception. * To allow the optional existence of a registry without error, use {@link #createOptional(ResourceKey, String)}. * * @param key the key of the registry to reference * @param modid the namespace for all objects registered to this DeferredRegister * @see #createOptional(ResourceKey, String) * @see #create(IForgeRegistry, String) * @see #create(ResourceLocation, String) */ public static DeferredRegister create(ResourceKey extends Registry> key, String modid) { return new DeferredRegister<>(key, null, modid, false); } /** * DeferredRegister factory for the optional existence of custom forge registries, {@link Registry vanilla registries}, * or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry key. * Supports both registries that already exist or do not exist yet. *
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will never be filled but will not throw an exception. * * @param key the key of the registry to reference * @param modid the namespace for all objects registered to this DeferredRegister * @see #create(ResourceKey, String) * @see #create(IForgeRegistry, String) * @see #create(ResourceLocation, String) */ public static DeferredRegister createOptional(ResourceKey extends Registry> key, String modid) { return new DeferredRegister<>(key, null, modid, true); } /** * DeferredRegister factory for custom forge registries, {@link Registry vanilla registries}, * or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry name. * Supports both registries that already exist or do not exist yet. *
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will throw an exception. * To allow the optional existence of a registry without error, use {@link #createOptional(ResourceLocation, String)}. * * @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 * @see #createOptional(ResourceLocation, String) * @see #create(IForgeRegistry, String) * @see #create(ResourceKey, String) */ public static DeferredRegister create(ResourceLocation registryName, String modid) { return new DeferredRegister<>(ResourceKey.createRegistryKey(registryName), null, modid, false); } /** * DeferredRegister factory for the optional existence of custom forge registries, {@link Registry vanilla registries}, * or {@link BuiltinRegistries built-in registries} to lookup based on the provided registry name. *
* The registry may not exist at the time this DeferredRegister is created.
* If the registry is never created, any {@link RegistryObject}s made from this DeferredRegister will never be filled but will not throw an exception.
*
* @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
* @see #create(ResourceLocation, String)
* @see #create(IForgeRegistry, String)
* @see #create(ResourceKey, String)
*/
public static DeferredRegister createOptional(ResourceLocation registryName, String modid)
{
return new DeferredRegister<>(ResourceKey.createRegistryKey(registryName), null, modid, true);
}
@Nullable // Nullable when superType is not null
private final ResourceKey extends Registry