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

public class DeferredHolder<R,T extends R> extends Object implements net.minecraft.core.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.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface net.minecraft.core.Holder

    net.minecraft.core.Holder.Direct<T>, net.minecraft.core.Holder.Kind, net.minecraft.core.Holder.Reference<T>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private @Nullable net.minecraft.core.Holder<R>
    The currently cached value.
    protected final net.minecraft.resources.ResourceKey<R>
    The resource key of the target object.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    DeferredHolder(net.minecraft.resources.ResourceKey<R> key)
    Creates a new DeferredHolder with a ResourceKey.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns an optional containing the target object, if bound; otherwise an empty optional.
    protected final void
    bind(boolean throwOnMissingRegistry)
    Binds this DeferredHolder to the underlying registry and target object.
    boolean
    canSerializeIn(net.minecraft.core.HolderOwner<R> owner)
     
    static <R, T extends R>
    DeferredHolder<R,T>
    create(net.minecraft.resources.ResourceKey<? extends net.minecraft.core.Registry<R>> registryKey, net.minecraft.resources.ResourceLocation valueName)
    Creates a new DeferredHolder targeting the value with the specified name in the specified registry.
    static <R, T extends R>
    DeferredHolder<R,T>
    create(net.minecraft.resources.ResourceKey<R> key)
    Creates a new DeferredHolder targeting the specified value.
    static <R, T extends R>
    DeferredHolder<R,T>
    create(net.minecraft.resources.ResourceLocation registryName, net.minecraft.resources.ResourceLocation valueName)
    Creates a new DeferredHolder targeting the value with the specified name in the specified registry.
    boolean
     
    get()
    Gets the object stored by this DeferredHolder, if this holder is bound.
    <Z> Z
    Returns the data of the given type that is attached to this object, or null if one isn't.
    net.minecraft.core.Holder<R>
    Returns the holder that this holder wraps.
    net.minecraft.resources.ResourceLocation
     
    net.minecraft.resources.ResourceKey<R>
    Get the resource key held by this Holder, or null if none is present.
    protected @Nullable net.minecraft.core.Registry<R>
    Returns the registry that this DeferredHolder is pointing at, or null if it doesn't exist.
    int
     
    boolean
    is(Predicate<net.minecraft.resources.ResourceKey<R>> filter)
    Evaluates the passed predicate against this holder's resource key.
    boolean
    is(net.minecraft.core.Holder<R> holder)
    Deprecated.
    boolean
    is(net.minecraft.resources.ResourceKey<R> key)
    Returns true if the passed ResourceKey is the same as this holder's resource key.
    boolean
    is(net.minecraft.resources.ResourceLocation id)
    Returns true if the passed ResourceLocation is the same as the ID of the target object.
    boolean
    is(net.minecraft.tags.TagKey<R> tag)
    Returns true if this holder is a member of the passed tag.
    boolean
    Returns true if the underlying object is available.
    net.minecraft.core.Holder.Kind
     
    Stream<net.minecraft.tags.TagKey<R>>
    Returns all tags present on the underlying object.
     
    com.mojang.datafixers.util.Either<net.minecraft.resources.ResourceKey<R>,R>
    Returns an Either.left() containing the resource key of this holder.
    Optional<net.minecraft.resources.ResourceKey<R>>
    Returns the resource key of this holder.
    Gets the object stored by this DeferredHolder, if this holder is bound.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface net.minecraft.core.Holder

    getRegisteredName

    Methods inherited from interface net.neoforged.neoforge.common.extensions.IHolderExtension

    unwrapLookup
  • Field Details

    • key

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

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

  • Method Details

    • create

      public static <R, T extends R> DeferredHolder<R,T> create(net.minecraft.resources.ResourceKey<? extends net.minecraft.core.Registry<R>> registryKey, net.minecraft.resources.ResourceLocation 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(net.minecraft.resources.ResourceLocation registryName, net.minecraft.resources.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(net.minecraft.resources.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 net.minecraft.core.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 net.minecraft.core.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 net.minecraft.resources.ResourceLocation getId()
      Returns:
      The ID of the object pointed to by this DeferredHolder.
    • getKey

      public net.minecraft.resources.ResourceKey<R> getKey()
      Description copied from interface: IHolderExtension
      Get the resource key held by this Holder, or null if none is present. This method will be overriden by Holder implementations to avoid allocation associated with Holder.unwrapKey()
      Specified by:
      getKey in interface IHolderExtension<R>
      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 net.minecraft.core.Holder<R>
      Returns:
      true if the underlying object is available
    • is

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

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

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

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

      @Deprecated public boolean is(net.minecraft.core.Holder<R> holder)
      Deprecated.
      Returns true if the holder is the same as this holder.
      Specified by:
      is in interface net.minecraft.core.Holder<R>
      Returns:
      true if the holder is the same as this holder
    • getData

      @Nullable public <Z> Z getData(DataMapType<R,Z> type)
      Returns the data of the given type that is attached to this object, or null if one isn't.
      Specified by:
      getData in interface IWithData<R>
      Type Parameters:
      Z - the type of the data
      Parameters:
      type - the data type
      Returns:
      the data of the given type that is attached to this object, or null if one isn't
    • tags

      public Stream<net.minecraft.tags.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 net.minecraft.core.Holder<R>
      Returns:
      all tags present on the underlying object
    • unwrap

      public com.mojang.datafixers.util.Either<net.minecraft.resources.ResourceKey<R>,R> unwrap()
      Returns an Either.left() containing the resource key of this holder.
      Specified by:
      unwrap in interface net.minecraft.core.Holder<R>
    • unwrapKey

      public Optional<net.minecraft.resources.ResourceKey<R>> unwrapKey()
      Returns the resource key of this holder.
      Specified by:
      unwrapKey in interface net.minecraft.core.Holder<R>
      Returns:
      a present optional containing the resource key of this holder
    • kind

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

      public boolean canSerializeIn(net.minecraft.core.HolderOwner<R> owner)
      Specified by:
      canSerializeIn in interface net.minecraft.core.Holder<R>
    • getDelegate

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