Class DeferredHolder<R,T extends R>

java.lang.Object
net.neoforged.neoforge.registries.DeferredHolder<R,T>
Type Parameters:
T - The type of object being held by this DeferredHolder.
All Implemented Interfaces:
Supplier<T>, Holder<R>, IWithData<R>
Direct Known Subclasses:
DeferredBlock, DeferredItem

public class DeferredHolder<R,T extends R> extends Object implements Holder<R>, Supplier<T>
A Deferred Holder is a Holder that is constructed with only a ResourceKey.

It will be populated with the underlying Holder from the registry when available.

  • Field Details

    • key

      protected final ResourceKey<R> key
      The resource key of the target object.
    • holder

      @Nullable private @Nullable Holder<R> holder
      The currently cached value.
  • Constructor Details

  • Method Details

    • create

      public static <R, T extends R> DeferredHolder<R,T> create(ResourceKey<? extends Registry<R>> registryKey, ResourceLocation valueName)
      Creates a new DeferredHolder targeting the value with the specified name in the specified registry.
      Type Parameters:
      T - The type of the target value.
      R - The registry type.
      Parameters:
      registryKey - The name of the registry the target value is a member of.
      valueName - The name of the target value.
    • create

      public static <R, T extends R> DeferredHolder<R,T> create(ResourceLocation registryName, ResourceLocation valueName)
      Creates a new DeferredHolder targeting the value with the specified name in the specified registry.
      Type Parameters:
      T - The registry type.
      Parameters:
      registryName - The name of the registry the target value is a member of.
      valueName - The name of the target value.
    • create

      public static <R, T extends R> DeferredHolder<R,T> create(ResourceKey<R> key)
      Creates a new DeferredHolder targeting the specified value.
      Type Parameters:
      T - The type of the target value.
      Parameters:
      key - The resource key of the target value.
    • value

      public T value()
      Gets the object stored by this DeferredHolder, if this holder is bound.
      Specified by:
      value in interface Holder<R>
      Throws:
      IllegalStateException - If the backing registry is unavailable.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • get

      public T get()
      Gets the object stored by this DeferredHolder, if this holder is bound.
      Specified by:
      get in interface Supplier<R>
      Throws:
      IllegalStateException - If the backing registry is unavailable.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • asOptional

      public Optional<T> asOptional()
      Returns an optional containing the target object, if bound; otherwise an empty optional.
      Returns:
      an optional containing the target object, if bound; otherwise an empty optional
    • getRegistry

      @Nullable protected @Nullable Registry<R> getRegistry()
      Returns the registry that this DeferredHolder is pointing at, or null if it doesn't exist.
      Returns:
      the registry that this DeferredHolder is pointing at, or null if it doesn't exist
    • bind

      protected final void bind(boolean throwOnMissingRegistry)
      Binds this DeferredHolder to the underlying registry and target object.

      Has no effect if already bound.

      Parameters:
      throwOnMissingRegistry - If true, an exception will be thrown if the registry is absent.
      Throws:
      IllegalStateException - If throwOnMissingRegistry is true and the backing registry is unavailable.
    • getId

      public ResourceLocation getId()
      Returns:
      The ID of the object pointed to by this DeferredHolder.
    • getKey

      public ResourceKey<R> getKey()
      Returns:
      The ResourceKey of the object pointed to by this DeferredHolder.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isBound

      public boolean isBound()
      Returns true if the underlying object is available.

      If true, the underlying object was added to the registry, and value() or get() can be called.

      Specified by:
      isBound in interface Holder<R>
      Returns:
      true if the underlying object is available
    • is

      public boolean is(ResourceLocation id)
      Returns true if the passed ResourceLocation is the same as the ID of the target object.
      Specified by:
      is in interface Holder<R>
      Returns:
      true if the passed ResourceLocation is the same as the ID of the target object
    • is

      public boolean is(ResourceKey<R> key)
      Returns true if the passed ResourceKey is the same as this holder's resource key.
      Specified by:
      is in interface Holder<R>
      Returns:
      true if the passed ResourceKey is the same as this holder's resource key
    • is

      public boolean is(Predicate<ResourceKey<R>> filter)
      Evaluates the passed predicate against this holder's resource key.
      Specified by:
      is in interface Holder<R>
      Returns:
      true if the filter matches this DH's resource key
    • is

      public boolean is(TagKey<R> tag)
      Returns true if this holder is a member of the passed tag.
      Specified by:
      is in interface Holder<R>
      Returns:
      true if this holder is a member of the passed tag
    • tags

      public Stream<TagKey<R>> tags()
      Returns all tags present on the underlying object.

      If the underlying object is not bound yet, and empty stream is returned.

      Specified by:
      tags in interface Holder<R>
      Returns:
      all tags present on the underlying object
    • unwrap

      public com.mojang.datafixers.util.Either<ResourceKey<R>,R> unwrap()
      Returns an Either.left() containing the resource key of this holder.
      Specified by:
      unwrap in interface Holder<R>
      API Note:
      This method is implemented for Holder compatibility, but getKey() should be preferred.
    • unwrapKey

      public Optional<ResourceKey<R>> unwrapKey()
      Returns the resource key of this holder.
      Specified by:
      unwrapKey in interface Holder<R>
      Returns:
      a present optional containing the resource key of this holder
      API Note:
      This method is implemented for Holder compatibility, but getKey() should be preferred.
    • kind

      public Holder.Kind kind()
      Specified by:
      kind in interface Holder<R>
    • canSerializeIn

      public boolean canSerializeIn(HolderOwner<R> owner)
      Specified by:
      canSerializeIn in interface Holder<R>