Interface AttachmentHolder

All Known Implementing Classes:
AttachmentStore, ItemStackAttachments

public interface AttachmentHolder
The Attachment values of one entity, block entity or item stack. Attachment's own methods (MADNESS.get(player)) are shortcuts for these.

Entities and block entities keep their values in memory, save them with their other data and sync them by each attachment's SyncPolicy. Values set on a client stay on that client until the server's next sync. Item stacks keep theirs in their mod's <modid>:attachments component, which travels with the stack like any other component.

  • Method Summary

    Modifier and Type
    Method
    Description
    The attachments with a stored value.
    <T> T
    get(Attachment<T> attachment)
    The value, or the attachment's default if none was set.
    boolean
    has(Attachment<?> attachment)
    Whether a value is stored, rather than read from the default.
    boolean
    Whether the holder is on a client.
    boolean
    Whether the holder still exists: the entity or block entity isn't removed, the stack isn't empty.
    void
    markDirty(Attachment<?> attachment)
    Saves and syncs a value that was changed in place, without modify(Attachment, Consumer).
    <T> void
    modify(Attachment<T> attachment, Consumer<T> mutator)
    Changes a mutable value in place, then saves and syncs it.
    of(net.minecraft.world.entity.Entity entity)
     
    of(net.minecraft.world.item.ItemStack stack)
    A view of the stack's data; it reads and writes the stack directly, so it's cheap and never out of date.
    of(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
     
    The entity, block entity or item stack holding the values.
    void
    reset(Attachment<?> attachment)
    Removes the value, so the default is read again.
    <T> void
    set(Attachment<T> attachment, T value)
    Sets the value.
    default <T> T
    update(Attachment<T> attachment, UnaryOperator<T> function)
    Sets the value to function's result and returns it.
  • Method Details

    • of

      static AttachmentHolder of(net.minecraft.world.entity.Entity entity)
    • of

      static AttachmentHolder of(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
    • of

      static AttachmentHolder of(net.minecraft.world.item.ItemStack stack)
      A view of the stack's data; it reads and writes the stack directly, so it's cheap and never out of date.
    • owner

      Object owner()
      The entity, block entity or item stack holding the values.
    • isClientSide

      boolean isClientSide()
      Whether the holder is on a client. Always false for item stacks, which have no side.
    • isValid

      boolean isValid()
      Whether the holder still exists: the entity or block entity isn't removed, the stack isn't empty.
    • get

      <T> T get(Attachment<T> attachment)
      The value, or the attachment's default if none was set.
    • set

      <T> void set(Attachment<T> attachment, T value)
      Sets the value. Setting an equal value does nothing.
    • update

      default <T> T update(Attachment<T> attachment, UnaryOperator<T> function)
      Sets the value to function's result and returns it.
    • modify

      <T> void modify(Attachment<T> attachment, Consumer<T> mutator)
      Changes a mutable value in place, then saves and syncs it. Not for item stacks, whose values are immutable: use update(Attachment, UnaryOperator) there.
    • markDirty

      void markDirty(Attachment<?> attachment)
      Saves and syncs a value that was changed in place, without modify(Attachment, Consumer).
    • has

      boolean has(Attachment<?> attachment)
      Whether a value is stored, rather than read from the default.
    • reset

      void reset(Attachment<?> attachment)
      Removes the value, so the default is read again.
    • attachments

      Set<Attachment<?>> attachments()
      The attachments with a stored value.