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, item stack, level or the server itself 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, block entities, levels and the server: 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.
A level's values (get(Level)) are saved in its data folder and synced to the players in it; the server's
(get(MinecraftServer)) in the server's, synced to every player, whom getGlobal(net.minecraft.world.level.Level) lets read them. For
both, SyncPolicy.ALL sends them and SyncPolicy.NONE keeps them on the server. A player's values can
be read and written while they're offline too (getOffline(net.minecraft.server.MinecraftServer, java.util.UUID)).
TEAM_STAGE.set(server, 3); // the whole server's, saved with it
int stage = TEAM_STAGE.getGlobal(level); // on either side: the server's, or the client's synced copy
MADNESS.updateOffline(server, uuid, m -> m + 1); // a player who isn't online: written to their data file
-
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.server.MinecraftServer server) get(net.minecraft.world.entity.Entity entity) get(net.minecraft.world.item.ItemStack stack) get(net.minecraft.world.level.block.entity.BlockEntity blockEntity) get(net.minecraft.world.level.Level level) getGlobal(net.minecraft.world.level.Level level) The server's value as this side knows it: on the server, its own (get(MinecraftServer)); on a client, the copy it was sent (the default if this attachment isn't synced).getOffline(net.minecraft.server.MinecraftServer server, UUID player) A player's value whether or not they're online: an online player's from their entity, an offline one's from their data file, or the default for a player who has never played here.booleanhas(net.minecraft.server.MinecraftServer server) booleanhas(net.minecraft.world.entity.Entity entity) booleanhas(net.minecraft.world.item.ItemStack stack) booleanhas(net.minecraft.world.level.block.entity.BlockEntity blockEntity) booleanhas(net.minecraft.world.level.Level level) 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.voidvoidChanges a mutable value in place, then saves and syncs it.voidvoidname()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.booleanWhether 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.registry()The registry of the mod it belongs to; its values are stored under that mod's id.voidreset(net.minecraft.server.MinecraftServer server) 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) voidreset(net.minecraft.world.level.Level level) booleanresetOffline(net.minecraft.server.MinecraftServer server, UUID player) Removes a player's value whether or not they're online (seesetOffline(net.minecraft.server.MinecraftServer, java.util.UUID, T)).voidvoidvoidvoidvoidbooleansetOffline(net.minecraft.server.MinecraftServer server, UUID player, T value) Sets a player's value whether or not they're online; an offline player's is written to their data file at once (noAttachmentEventsfire for it, and it reaches them when they next join).@Nullable net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf, T> The codec values are synced with, ornullif they never leave the server.toString()update(net.minecraft.server.MinecraftServer server, UnaryOperator<T> function) 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) update(net.minecraft.world.level.Level level, UnaryOperator<T> function) booleanupdateOffline(net.minecraft.server.MinecraftServer server, UUID player, UnaryOperator<T> function) Sets a player's value tofunction's result, whether or not they're online (seesetOffline(net.minecraft.server.MinecraftServer, java.util.UUID, T)).
-
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. -
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 (seeHeldItemSwaps). -
createDefault
A fresh default value. -
codec
The codec values are saved with, ornullfor 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, 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) -
get
-
set
-
update
-
modify
-
has
public boolean has(net.minecraft.world.level.Level level) -
reset
public void reset(net.minecraft.world.level.Level level) -
get
-
set
-
update
-
modify
-
has
public boolean has(net.minecraft.server.MinecraftServer server) -
reset
public void reset(net.minecraft.server.MinecraftServer server) -
getGlobal
The server's value as this side knows it: on the server, its own (get(MinecraftServer)); on a client, the copy it was sent (the default if this attachment isn't synced).- Parameters:
level- any level of the side asking
-
getOffline
A player's value whether or not they're online: an online player's from their entity, an offline one's from their data file, or the default for a player who has never played here. Only for saved attachments. On the server thread. -
setOffline
Sets a player's value whether or not they're online; an offline player's is written to their data file at once (noAttachmentEventsfire for it, and it reaches them when they next join). Only for saved attachments. On the server thread.- Returns:
- false if the player has never played here, or the file couldn't be written
-
updateOffline
public boolean updateOffline(net.minecraft.server.MinecraftServer server, UUID player, UnaryOperator<T> function) Sets a player's value tofunction's result, whether or not they're online (seesetOffline(net.minecraft.server.MinecraftServer, java.util.UUID, T)). -
resetOffline
Removes a player's value whether or not they're online (seesetOffline(net.minecraft.server.MinecraftServer, java.util.UUID, T)). -
toString
-