Class Attachment<T>

java.lang.Object
net.ixdarklord.coolcatcore.api.attachment.Attachment<T>
Type Parameters:
T - the value's type

public final class Attachment<T> extends Object
A typed piece of data any entity (players included), block entity or item stack can carry. Attachments come from a mod's AttachmentRegistry, declared once, statically:
public static final AttachmentRegistry ATTACHMENTS = AttachmentRegistry.create(MOD_ID);

// Saved with the player, sent only to that player, kept when they die.
public static final Attachment<Integer> MADNESS = ATTACHMENTS.builder("madness", Codec.INT, 0)
        .sync(SyncPolicy.SELF)
        .copyOnDeath()
        .build();

int madness = MADNESS.get(player);
MADNESS.set(player, madness + 1);
MADNESS.update(player, m -> Math.max(0, m - 1));
Values should be immutable (numbers, records, immutable lists); a changed value is set(Entity, T) again. Mutable values are allowed on entities and block entities: change them with modify(Entity, Consumer), or make them Trackable. Item stacks only take immutable, persistent values.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
     
    static interface 
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static @Nullable Attachment<?>
    byId(net.minecraft.resources.Identifier id)
    The attachment with this id, if one was built.
    @Nullable com.mojang.serialization.Codec<T>
    The codec values are saved with, or null for a transient attachment.
    boolean
    Whether a player keeps the value when they die and respawn.
    A fresh default value.
    void
    fireChanged(AttachmentHolder holder, T oldValue, T newValue)
    For holders: tells the listeners about a change.
    get(net.minecraft.world.entity.Entity entity)
     
    get(net.minecraft.world.item.ItemStack stack)
     
    get(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
     
    boolean
    has(net.minecraft.world.entity.Entity entity)
     
    boolean
    has(net.minecraft.world.item.ItemStack stack)
     
    boolean
    has(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
     
    net.minecraft.resources.Identifier
    id()
    modid:name.
    boolean
     
    boolean
     
    boolean
    Whether a block entity's value travels with its dropped item and comes back when it's placed.
    void
    modify(net.minecraft.world.entity.Entity entity, Consumer<T> mutator)
    Changes a mutable value in place, then saves and syncs it.
    void
    modify(net.minecraft.world.level.block.entity.BlockEntity blockEntity, Consumer<T> mutator)
     
    The name within its mod.
    Calls listener whenever a holder's value changes, on the side it changed: on the server when it's set, on clients when a synced value arrives.
    boolean
    Whether a change to its value on a held item shows the item being put away and taken out again, as vanilla does for most changes to a held stack.
    The registry of the mod it belongs to; its values are stored under that mod's id.
    void
    reset(net.minecraft.world.entity.Entity entity)
    Removes the value; the entity reads the default again.
    void
    reset(net.minecraft.world.item.ItemStack stack)
     
    void
    reset(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
     
    void
    set(net.minecraft.world.entity.Entity entity, T value)
     
    void
    set(net.minecraft.world.item.ItemStack stack, T value)
     
    void
    set(net.minecraft.world.level.block.entity.BlockEntity blockEntity, T value)
     
    @Nullable net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf, T>
    The codec values are synced with, or null if they never leave the server.
     
     
    update(net.minecraft.world.entity.Entity entity, UnaryOperator<T> function)
    Sets the value to function's result and returns it.
    update(net.minecraft.world.item.ItemStack stack, UnaryOperator<T> function)
     
    update(net.minecraft.world.level.block.entity.BlockEntity blockEntity, UnaryOperator<T> function)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • byId

      @Nullable public static @Nullable Attachment<?> byId(net.minecraft.resources.Identifier id)
      The attachment with this id, if one was built.
    • id

      public net.minecraft.resources.Identifier id()
      modid:name.
    • name

      public String name()
      The name within its mod.
    • registry

      public AttachmentRegistry registry()
      The registry of the mod it belongs to; its values are stored under that mod's id.
    • playsSwapAnimation

      public boolean playsSwapAnimation()
      Whether a change to its value on a held item shows the item being put away and taken out again, as vanilla does for most changes to a held stack. Off unless the builder asked for it (Attachment.Builder.swapAnimation()): an attachment's value changing leaves the item where it is in the hand (see HeldItemSwaps).
    • createDefault

      public T createDefault()
      A fresh default value.
    • codec

      @Nullable public @Nullable com.mojang.serialization.Codec<T> codec()
      The codec values are saved with, or null for a transient attachment.
    • streamCodec

      @Nullable public @Nullable net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf, T> streamCodec()
      The codec values are synced with, or null if they never leave the server.
    • syncPolicy

      public SyncPolicy syncPolicy()
    • isPersistent

      public boolean isPersistent()
    • isSynced

      public boolean isSynced()
    • copyOnDeath

      public boolean copyOnDeath()
      Whether a player keeps the value when they die and respawn.
    • keepOnDrop

      public boolean keepOnDrop()
      Whether a block entity's value travels with its dropped item and comes back when it's placed.
    • onChange

      public Attachment<T> onChange(Attachment.Listener<T> listener)
      Calls listener whenever a holder's value changes, on the side it changed: on the server when it's set, on clients when a synced value arrives. For modify(Entity, Consumer) and Trackable changes, the old and new values are the same object.
    • fireChanged

      public void fireChanged(AttachmentHolder holder, T oldValue, T newValue)
      For holders: tells the listeners about a change.
    • get

      public T get(net.minecraft.world.entity.Entity entity)
    • set

      public void set(net.minecraft.world.entity.Entity entity, T value)
    • update

      public T update(net.minecraft.world.entity.Entity entity, UnaryOperator<T> function)
      Sets the value to function's result and returns it.
    • modify

      public void modify(net.minecraft.world.entity.Entity entity, Consumer<T> mutator)
      Changes a mutable value in place, then saves and syncs it.
    • has

      public boolean has(net.minecraft.world.entity.Entity entity)
    • reset

      public void reset(net.minecraft.world.entity.Entity entity)
      Removes the value; the entity reads the default again.
    • get

      public T get(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
    • set

      public void set(net.minecraft.world.level.block.entity.BlockEntity blockEntity, T value)
    • update

      public T update(net.minecraft.world.level.block.entity.BlockEntity blockEntity, UnaryOperator<T> function)
    • modify

      public void modify(net.minecraft.world.level.block.entity.BlockEntity blockEntity, Consumer<T> mutator)
    • has

      public boolean has(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
    • reset

      public void reset(net.minecraft.world.level.block.entity.BlockEntity blockEntity)
    • get

      public T get(net.minecraft.world.item.ItemStack stack)
    • set

      public void set(net.minecraft.world.item.ItemStack stack, T value)
    • update

      public T update(net.minecraft.world.item.ItemStack stack, UnaryOperator<T> function)
    • has

      public boolean has(net.minecraft.world.item.ItemStack stack)
    • reset

      public void reset(net.minecraft.world.item.ItemStack stack)
    • toString

      public String toString()
      Overrides:
      toString in class Object