Interface AttachmentTarget
AttachmentType
s.
Fabric implements this on Entity
, BlockEntity
, ServerWorld
and Chunk
via mixin.
Note about BlockEntity
and Chunk
targets: these objects need to be notified of changes to their
state (using BlockEntity.markDirty()
and Chunk.setNeedsSaving(boolean)
respectively), otherwise the modifications will not take effect properly.
The setAttached(AttachmentType, Object)
method handles this automatically, but this needs to be done manually
when attached data is mutable, for example:
AttachmentType<MutableType> MUTABLE_ATTACHMENT_TYPE = ...;
BlockEntity be = ...;
MutableType data = be.getAttachedOrCreate(MUTABLE_ATTACHMENT_TYPE);
data.mutate();
be.markDirty(); // Required because we are not using setAttached
Note about BlockEntity
targets: by default, many block entities use their NBT to synchronize with the client.
That would mean persistent attachments are automatically synced with the client for those block entities. As this is
undesirable behavior, the API completely removes attachments from the result of BlockEntity.toInitialChunkDataNbt(net.minecraft.registry.RegistryWrapper.WrapperLookup)
,
which takes care of all vanilla types. However, modded block entities may be coded differently, so be wary of this
when attaching data to modded block entities.
Note about Chunk
targets with ChunkStatus.EMPTY
: These chunks are not saved unless the generation
progresses to at least ChunkStatus.STRUCTURE_STARTS
. Therefore, persistent attachments to those chunks may not
be saved. The setAttached(AttachmentType, Object)
method will log a warning when this is attempted.
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptiondefault <A> A
getAttached
(AttachmentType<A> type) Gets the data associated with the givenAttachmentType
, ornull
if it doesn't yet exist.default <A> A
getAttachedOrCreate
(AttachmentType<A> type) Specialization ofgetAttachedOrCreate(AttachmentType, Supplier)
, but only for attachment types withinitializers
. It will throw an exception if one is not present.default <A> A
getAttachedOrCreate
(AttachmentType<A> type, Supplier<A> initializer) Gets the data associated with the givenAttachmentType
, or initializes it using the non-null
result of the providedSupplier
.default <A> A
getAttachedOrElse
(AttachmentType<A> type, A defaultValue) Gets the data associated with the givenAttachmentType
, or returns the provided default value if it doesn't exist.default <A> A
getAttachedOrGet
(AttachmentType<A> type, Supplier<A> defaultValue) Gets the data associated with the givenAttachmentType
, or gets the provided default value from the provided non-null
supplier if it doesn't exist.default <A> A
getAttachedOrSet
(AttachmentType<A> type, A defaultValue) Gets the data associated with the givenAttachmentType
, or initializes it using the provided non-null
default value.default <A> A
getAttachedOrThrow
(AttachmentType<A> type) Gets the data associated with the givenAttachmentType
, throwing aNullPointerException
if it doesn't yet exist.default boolean
hasAttached
(AttachmentType<?> type) Tests whether the givenAttachmentType
has any associated data.default <A> A
modifyAttached
(AttachmentType<A> type, UnaryOperator<A> modifier) Modifies the data associated with the givenAttachmentType
.default <A> A
removeAttached
(AttachmentType<A> type) Removes any data associated with the givenAttachmentType
.default <A> A
setAttached
(AttachmentType<A> type, A value) Sets the data associated with the givenAttachmentType
.
-
Field Details
-
NBT_ATTACHMENT_KEY
- See Also:
-
-
Method Details
-
getAttached
Gets the data associated with the givenAttachmentType
, ornull
if it doesn't yet exist.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment type- Returns:
- the attached data
-
getAttachedOrThrow
Gets the data associated with the givenAttachmentType
, throwing aNullPointerException
if it doesn't yet exist.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment type- Returns:
- the attached data
-
getAttachedOrSet
Gets the data associated with the givenAttachmentType
, or initializes it using the provided non-null
default value.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment typedefaultValue
- the fallback default value- Returns:
- the attached data, initialized if originally absent
-
getAttachedOrCreate
Gets the data associated with the givenAttachmentType
, or initializes it using the non-null
result of the providedSupplier
.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment typeinitializer
- the fallback initializer- Returns:
- the attached data, initialized if originally absent
-
getAttachedOrCreate
Specialization ofgetAttachedOrCreate(AttachmentType, Supplier)
, but only for attachment types withinitializers
. It will throw an exception if one is not present.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment type- Returns:
- the attached data, initialized if originally absent
-
getAttachedOrElse
@Contract("_, !null -> !null") default <A> A getAttachedOrElse(AttachmentType<A> type, @Nullable A defaultValue) Gets the data associated with the givenAttachmentType
, or returns the provided default value if it doesn't exist. UnlikegetAttachedOrCreate(AttachmentType, Supplier)
, this doesn't initialize the attachment with the default value.- Type Parameters:
A
- the type of the attached data- Parameters:
type
- the attachment typedefaultValue
- the default value to use as fallback- Returns:
- the attached data, or the default value
-
getAttachedOrGet
Gets the data associated with the givenAttachmentType
, or gets the provided default value from the provided non-null
supplier if it doesn't exist. The supplier may returnnull
. UnlikegetAttachedOrCreate(AttachmentType, Supplier)
, this doesn't initialize the attachment with the default value.- Type Parameters:
A
- the type of the attached data- Parameters:
type
- the attachment typedefaultValue
- the default value supplier to use as fallback- Returns:
- the attached data, or the default value
-
setAttached
Sets the data associated with the givenAttachmentType
. Passingnull
removes the data.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment typevalue
- the new value- Returns:
- the previous data
-
hasAttached
Tests whether the givenAttachmentType
has any associated data. This doesn't create any data, and may returnfalse
even for attachment types with an automatic initializer.- Parameters:
type
- the attachment type- Returns:
- whether there is associated data
-
removeAttached
Removes any data associated with the givenAttachmentType
. Equivalent to callingsetAttached(AttachmentType, Object)
withnull
.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment type- Returns:
- the previous data
-
modifyAttached
Modifies the data associated with the givenAttachmentType
. Functionally the same as callinggetAttached(AttachmentType)
, applying the modifier, then callingsetAttached(AttachmentType, Object)
with the result. The modifier takes in the currently attached value, ornull
if no attachment is present.- Type Parameters:
A
- the type of the data- Parameters:
type
- the attachment typemodifier
- the operation to apply to the current data, or tonull
if it doesn't exist yet- Returns:
- the previous data
-