All Downloads are FREE. Search and download functionalities are using the official Maven repository.

personthecat.catlib.registry.RegistryUtils Maven / Gradle / Ivy

package personthecat.catlib.registry;

import com.mojang.serialization.Lifecycle;
import dev.architectury.injectables.annotations.ExpectPlatform;
import lombok.experimental.UtilityClass;
import net.minecraft.class_2370;
import net.minecraft.class_2378;
import net.minecraft.class_5321;
import net.minecraft.class_5458;
import org.jetbrains.annotations.NotNull;
import personthecat.catlib.exception.MissingElementException;
import personthecat.catlib.exception.MissingOverrideException;
import personthecat.catlib.exception.RegistryLookupException;

import java.util.Optional;

@UtilityClass
public class RegistryUtils {

    /**
     * Acquires a handle on the registry corresponding to this key. In particular, this
     * is useful for two reason:
     *
     * 
    *
  • To dynamically acquire a registry from either {@link class_2378} or {@link class_5458}
  • *
  • On Forge, to acquire either of the above or the preferred Forge registry.
  • *
* * @throws RegistryLookupException If the expected registry does not exist. * @param key The key of the registry being returned. * @param The type of object contained within the registry. * @return A platform-agnostic representation of this registry. */ public static RegistryHandle getHandle(final class_5321> key) { return tryGetHandle(key).orElseThrow(() -> new RegistryLookupException(key)); } /** * Attempts to acquire a handle on the corresponding registry for this key. Or else, * returns {@link Optional#empty}. * * @param key The key of the registry being returned. * @param The type of object contained within the registry. * @return A platform-agnostic representation of this registry, or else {@link Optional#empty}. */ @ExpectPlatform public static Optional> tryGetHandle(final class_5321> key) { throw new MissingOverrideException(); } /** * Inserts a registry of this type into {@link class_5458#field_25926}. * * @param key The key of the registry being added. * @param The type of object contained within the registry. */ @SuppressWarnings({"unchecked", "rawtypes"}) public static void createBuiltinRegistry(final class_5321> key) { if (!class_5458.field_25926.method_10250(key.method_29177())) { final class_2378 dummyRegistry = new class_2370<>(key, Lifecycle.experimental(), null); class_2378.method_10230((class_2378) class_5458.field_25926, key.method_29177(), dummyRegistry); } } /** * Acquires a handle on a registry when given the element type. For example, when * given Biome.class, will return {@link class_5458#field_25933}. * On the Forge platform, this method will return the equivalent Forge registry. * * @throws MissingElementException if the expected registry is not found. * @param clazz The element type contained within the registry. * @param The type token of this element. * @return A handle on the expected registry, guaranteed. */ @NotNull @ExpectPlatform public static RegistryHandle getByType(final Class clazz) { throw new MissingOverrideException(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy