Interface AttachmentHolder

All Known Implementing Classes:
AttachmentStore, ItemStackAttachments

public interface AttachmentHolder
The Attachment values of one entity, block entity, item stack, level or the server. 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. Levels and the server keep theirs in saved data, in the level's and the server's data folder.

  • 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.
    global(net.minecraft.world.level.Level level)
    The server's values as a side knows them: the server's own, or on a client the copy it was sent.
    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.server.MinecraftServer server)
    The server's own values, saved in its data folder.
    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)
     
    of(net.minecraft.world.level.Level level)
    A level's values: a server level's saved in its data folder, a client level's as the server sent them.
    The entity, block entity, item stack, level or server 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.level.Level level)
      A level's values: a server level's saved in its data folder, a client level's as the server sent them.
    • of

      static AttachmentHolder of(net.minecraft.server.MinecraftServer server)
      The server's own values, saved in its data folder.
    • global

      static AttachmentHolder global(net.minecraft.world.level.Level level)
      The server's values as a side knows them: the server's own, or on a client the copy it was sent.
    • 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, item stack, level or server 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.