Class DeferredHolder<R, T extends R>

java.lang.Object
net.minecraft.core.Holder.Reference<R>
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>, IHolderExtension<R>, IWithData<R>
Direct Known Subclasses:
DeferredBlock, DeferredItem

public class DeferredHolder<R, T extends R> extends Holder.Reference<R> implements 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

  • Constructor Details

  • Method Details

    • create

      public static <R, T extends R> DeferredHolder<R,T> create(ResourceKey<? extends Registry<R>> registryKey, Identifier valueName)
      Creates a new DeferredHolder targeting the value with the specified name in the specified registry.
      Type Parameters:
      R - The registry type.
      T - The type of the target value.
      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(Identifier registryName, Identifier 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>
      Overrides:
      value in class Holder.Reference<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
    • unwrapLookup

      public @Nullable HolderLookup.RegistryLookup<R> unwrapLookup()
      Returns the registry that this DeferredHolder is pointing at, or null if it doesn't exist.
      Specified by:
      unwrapLookup in interface IHolderExtension<R>
      Overrides:
      unwrapLookup in class Holder.Reference<R>
      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 Identifier getId()
      Returns:
      The ID of the object pointed to by this DeferredHolder.
    • toString

      public String toString()
      Overrides:
      toString in class Holder.Reference<R>
    • 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>
      Overrides:
      isBound in class Holder.Reference<R>
      Returns:
      true if the underlying object is available
    • areComponentsBound

      public boolean areComponentsBound()
      Specified by:
      areComponentsBound in interface Holder<R>
      Overrides:
      areComponentsBound in class Holder.Reference<R>
    • components

      public DataComponentMap components()
      Specified by:
      components in interface Holder<R>
      Overrides:
      components in class Holder.Reference<R>
    • 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>
      Overrides:
      is in class Holder.Reference<R>
      Returns:
      true if this holder is a member of the passed tag
    • is

      @Deprecated public boolean is(Holder<R> holder)
      Deprecated.
      Returns true if the holder is the same as this holder.
      Specified by:
      is in interface Holder<R>
      Overrides:
      is in class Holder.Reference<R>
      Returns:
      true if the holder is the same as this holder
    • 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>
      Overrides:
      tags in class Holder.Reference<R>
      Returns:
      all tags present on the underlying object
    • canSerializeIn

      public boolean canSerializeIn(HolderOwner<R> owner)
      Specified by:
      canSerializeIn in interface Holder<R>
      Overrides:
      canSerializeIn in class Holder.Reference<R>
    • getDelegate

      public Holder<R> getDelegate()
      Description copied from interface: IHolderExtension
      Returns the holder that this holder wraps. Used by Registry.safeCastToReference(Holder) to resolve the underlying Holder.Reference for delegating holders.
      Specified by:
      getDelegate in interface IHolderExtension<R>
      Returns:
      the holder that this holder wraps