Class AttachmentHolder
java.lang.Object
net.neoforged.neoforge.attachment.AttachmentHolder
- All Implemented Interfaces:
IAttachmentHolder
- Direct Known Subclasses:
AttachmentHolder.AsField
,BlockEntity
,Entity
,Level
Implementation class for objects that can hold data attachments.
For the user-facing methods, see
IAttachmentHolder
.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Version of theAttachmentHolder
that is suitable for storing in a field. -
Field Summary
Modifier and TypeFieldDescription(package private) @Nullable Map
<AttachmentType<?>, Object> static final String
private static final boolean
private static final org.slf4j.Logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected final void
deserializeAttachments
(HolderLookup.Provider provider, 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> Optional
<T> getExistingData
(AttachmentType<T> type) Returns an optional possibly containing a data attachment value of the given 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 CompoundTag
serializeAttachments
(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, 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
-
getExistingData
Description copied from interface:IAttachmentHolder
Returns an optional possibly containing a data attachment value of the given type.If there is no data attachment of the given type, an empty optional is returned.
- Specified by:
getExistingData
in interfaceIAttachmentHolder
- Returns:
- an optional possibly containing a data attachment value 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
Writes the serializable attachments to a tag. Returnsnull
if there are no serializable attachments. -
deserializeAttachments
Reads serializable attachments from a tag previously created viaserializeAttachments(HolderLookup.Provider)
.
-