Class AttachmentHolder
java.lang.Object
net.neoforged.neoforge.attachment.AttachmentHolder
- All Implemented Interfaces:
IAttachmentHolder
- Direct Known Subclasses:
AttachmentHolder.AsField
,net.minecraft.world.level.block.entity.BlockEntity
,net.minecraft.world.entity.Entity
Implementation class for objects that can hold data attachments.
For the user-facing methods, see
IAttachmentHolder
.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Version of theAttachmentHolder
that is suitable for storing in a field. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) @Nullable Map
<AttachmentType<?>, Object> static final String
private static final boolean
private static final org.slf4j.Logger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
deserializeAttachments
(net.minecraft.core.HolderLookup.Provider provider, net.minecraft.nbt.CompoundTag tag) Reads serializable attachments from a tag previously created viaserializeAttachments(HolderLookup.Provider)
.(package private) final Map
<AttachmentType<?>, Object> Create the attachment map if it does not yet exist, or return the current map.final <T> T
getData
(AttachmentType<T> type) Returns the data attachment of the given type.<T> T
getExistingDataOrNull
(AttachmentType<T> type) (package private) IAttachmentHolder
Returns the attachment holder that is exposed to the user.final boolean
Returnstrue
if there is any data attachments,false
otherwise.final boolean
hasData
(AttachmentType<?> type) Returnstrue
if there is a data attachment of the give type,false
otherwise.<T> T
removeData
(AttachmentType<T> type) Removes the data attachment of the given type.final @Nullable net.minecraft.nbt.CompoundTag
serializeAttachments
(net.minecraft.core.HolderLookup.Provider provider) Writes the serializable attachments to a tag.<T> T
setData
(AttachmentType<T> type, T data) Sets the data attachment of the given type.private void
validateAttachmentType
(AttachmentType<?> type) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.neoforged.neoforge.attachment.IAttachmentHolder
getData, getExistingData, getExistingData, getExistingDataOrNull, hasData, removeData, setData
-
Field Details
-
ATTACHMENTS_NBT_KEY
- See Also:
-
IN_DEV
private static final boolean IN_DEV -
LOGGER
private static final org.slf4j.Logger LOGGER -
attachments
-
-
Constructor Details
-
AttachmentHolder
public AttachmentHolder()
-
-
Method Details
-
validateAttachmentType
-
getAttachmentMap
Create the attachment map if it does not yet exist, or return the current map. -
getExposedHolder
IAttachmentHolder getExposedHolder()Returns the attachment holder that is exposed to the user. This is the same asthis
for most cases, but when usingAttachmentHolder.AsField
it is the field holder. -
hasAttachments
public final boolean hasAttachments()Description copied from interface:IAttachmentHolder
Returnstrue
if there is any data attachments,false
otherwise.- Specified by:
hasAttachments
in interfaceIAttachmentHolder
-
hasData
Description copied from interface:IAttachmentHolder
Returnstrue
if there is a data attachment of the give type,false
otherwise.- Specified by:
hasData
in interfaceIAttachmentHolder
-
getData
Description copied from interface:IAttachmentHolder
Returns the data attachment of the given type.If there is no data attachment of the given type, the default value is stored in this holder and returned.
- Specified by:
getData
in interfaceIAttachmentHolder
- Returns:
- the data attachment of the given type
-
getExistingDataOrNull
- Specified by:
getExistingDataOrNull
in interfaceIAttachmentHolder
- Returns:
- an existing data attachment value of the given type, or null if there is no data attachment of the given type
-
setData
Description copied from interface:IAttachmentHolder
Sets the data attachment of the given type.- Specified by:
setData
in interfaceIAttachmentHolder
- Returns:
- the previous value for that attachment type, if any, or
null
if there was none
-
removeData
Description copied from interface:IAttachmentHolder
Removes the data attachment of the given type.- Specified by:
removeData
in interfaceIAttachmentHolder
- Returns:
- the previous value for that attachment type, if any, or
null
if there was none
-
serializeAttachments
@Nullable public final @Nullable net.minecraft.nbt.CompoundTag serializeAttachments(net.minecraft.core.HolderLookup.Provider provider) Writes the serializable attachments to a tag. Returnsnull
if there are no serializable attachments. -
deserializeAttachments
protected final void deserializeAttachments(net.minecraft.core.HolderLookup.Provider provider, net.minecraft.nbt.CompoundTag tag) Reads serializable attachments from a tag previously created viaserializeAttachments(HolderLookup.Provider)
.
-