Class Attachment<T>
java.lang.Object
net.ixdarklord.coolcatcore.api.attachment.Attachment<T>
- Type Parameters:
T- the value's type
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(net.minecraft.world.entity.Entity, T) again. Mutable values
are allowed on entities and block entities: change them with modify(net.minecraft.world.entity.Entity, java.util.function.Consumer<T>), or make them Trackable.
Item stacks only take immutable, persistent values.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic interface -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable Attachment<?>byId(net.minecraft.resources.ResourceLocation id) The attachment with this id, if one was built.@Nullable com.mojang.serialization.Codec<T>codec()The codec values are saved with, ornullfor a transient attachment.booleanWhether a player keeps the value when they die and respawn.A fresh default value.voidfireChanged(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) booleanhas(net.minecraft.world.entity.Entity entity) booleanhas(net.minecraft.world.item.ItemStack stack) booleanhas(net.minecraft.world.level.block.entity.BlockEntity blockEntity) net.minecraft.resources.ResourceLocationid()modid:name.booleanbooleanisSynced()booleanWhether a block entity's value travels with its dropped item and comes back when it's placed.voidChanges a mutable value in place, then saves and syncs it.voidname()The name within its mod.onChange(Attachment.Listener<T> listener) Callslistenerwhenever a holder's value changes, on the side it changed: on the server when it's set, on clients when a synced value arrives.registry()The registry of the mod it belongs to; its values are stored under that mod's id.voidreset(net.minecraft.world.entity.Entity entity) Removes the value; the entity reads the default again.voidreset(net.minecraft.world.item.ItemStack stack) voidreset(net.minecraft.world.level.block.entity.BlockEntity blockEntity) voidvoidvoid@Nullable StreamCodec<? super net.minecraft.network.FriendlyByteBuf,T> The codec values are synced with, ornullif they never leave the server.toString()update(net.minecraft.world.entity.Entity entity, UnaryOperator<T> function) Sets the value tofunction'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)
-
Method Details
-
byId
The attachment with this id, if one was built. -
id
public net.minecraft.resources.ResourceLocation id()modid:name. -
name
The name within its mod. -
registry
The registry of the mod it belongs to; its values are stored under that mod's id. -
createDefault
A fresh default value. -
codec
The codec values are saved with, ornullfor a transient attachment. -
streamCodec
@Nullable public @Nullable StreamCodec<? super net.minecraft.network.FriendlyByteBuf,T> streamCodec()The codec values are synced with, ornullif they never leave the server. -
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
Callslistenerwhenever a holder's value changes, on the side it changed: on the server when it's set, on clients when a synced value arrives. Formodify(net.minecraft.world.entity.Entity, java.util.function.Consumer<T>)andTrackablechanges, the old and new values are the same object. -
fireChanged
For holders: tells the listeners about a change. -
get
-
set
-
update
Sets the value tofunction's result and returns it. -
modify
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
-
set
-
update
public T update(net.minecraft.world.level.block.entity.BlockEntity blockEntity, UnaryOperator<T> function) -
modify
-
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
-
set
-
update
-
has
public boolean has(net.minecraft.world.item.ItemStack stack) -
reset
public void reset(net.minecraft.world.item.ItemStack stack) -
toString
-