Class AttachmentHolder

java.lang.Object
net.neoforged.neoforge.attachment.AttachmentHolder
All Implemented Interfaces:
IAttachmentHolder
Direct Known Subclasses:
AttachmentHolder.AsField, BlockEntity, Entity, Level

public abstract class AttachmentHolder extends Object implements IAttachmentHolder
Implementation class for objects that can hold data attachments. For the user-facing methods, see IAttachmentHolder.
  • Field Details

    • ATTACHMENTS_NBT_KEY

      public static final String ATTACHMENTS_NBT_KEY
      See Also:
    • IN_DEV

      private static final boolean IN_DEV
    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • attachments

      @Nullable @Nullable Map<AttachmentType<?>,Object> attachments
  • Constructor Details

    • AttachmentHolder

      public AttachmentHolder()
  • Method Details

    • validateAttachmentType

      private void validateAttachmentType(AttachmentType<?> type)
    • getAttachmentMap

      final Map<AttachmentType<?>,Object> getAttachmentMap()
      Create the attachment map if it does not yet exist, or return the current map.
    • getExposedHolder

      IAttachmentHolder getExposedHolder()
      Returns the attachment holder that is exposed to the user. This is the same as this for most cases, but when using AttachmentHolder.AsField it is the field holder.
    • hasAttachments

      public final boolean hasAttachments()
      Description copied from interface: IAttachmentHolder
      Returns true if there is any data attachments, false otherwise.
      Specified by:
      hasAttachments in interface IAttachmentHolder
    • hasData

      public final boolean hasData(AttachmentType<?> type)
      Description copied from interface: IAttachmentHolder
      Returns true if there is a data attachment of the give type, false otherwise.
      Specified by:
      hasData in interface IAttachmentHolder
    • getData

      public final <T> T getData(AttachmentType<T> type)
      Description copied from interface: IAttachmentHolder
      Returns the data attachment of the given type.

      If there is no data attachment of the given type, the default value is stored in this holder and returned.

      Specified by:
      getData in interface IAttachmentHolder
      Returns:
      the data attachment of the given type
    • getExistingData

      public <T> Optional<T> getExistingData(AttachmentType<T> type)
      Description copied from interface: IAttachmentHolder
      Returns an optional possibly containing a data attachment value of the given type.

      If there is no data attachment of the given type, an empty optional is returned.

      Specified by:
      getExistingData in interface IAttachmentHolder
      Returns:
      an optional possibly containing a data attachment value of the given type
    • setData

      @MustBeInvokedByOverriders @Nullable public <T> T setData(AttachmentType<T> type, T data)
      Description copied from interface: IAttachmentHolder
      Sets the data attachment of the given type.
      Specified by:
      setData in interface IAttachmentHolder
      Returns:
      the previous value for that attachment type, if any, or null if there was none
    • removeData

      @MustBeInvokedByOverriders @Nullable public <T> T removeData(AttachmentType<T> type)
      Description copied from interface: IAttachmentHolder
      Removes the data attachment of the given type.
      Specified by:
      removeData in interface IAttachmentHolder
      Returns:
      the previous value for that attachment type, if any, or null if there was none
    • serializeAttachments

      @Nullable public final @Nullable CompoundTag serializeAttachments(HolderLookup.Provider provider)
      Writes the serializable attachments to a tag. Returns null if there are no serializable attachments.
    • deserializeAttachments

      protected final void deserializeAttachments(HolderLookup.Provider provider, CompoundTag tag)
      Reads serializable attachments from a tag previously created via serializeAttachments(HolderLookup.Provider).