Class ComponentMap
java.lang.Object
com.github.darksoulq.abyssallib.world.item.component.ComponentMap
A specialized map used to manage and persist
DataComponents for library objects.
This class bridge the gap between volatile in-memory component states and persistent storage (NBT for items/entities). It handles both standard Minecraft Vanilla components via Paper's API and custom AbyssalLib components via direct NBT serialization.
-
Constructor Summary
ConstructorsConstructorDescriptionComponentMap(CustomEntity<? extends org.bukkit.entity.LivingEntity> entity) Constructs a ComponentMap for a specific custom Entity.ComponentMap(Item item) Constructs a ComponentMap for a specific custom Item.ComponentMap(org.bukkit.inventory.ItemStack stack) Constructs a ComponentMap by wrapping a Bukkit ItemStack. -
Method Summary
Modifier and TypeMethodDescriptionvoidSerializes all current components back into the source's persistent data (PDC/NBT).static <T,D> D encodeComponent(DataComponent<T> component, DynamicOps<D> ops) Encodes a component into a serialized format using the provided DynamicOps.List<DataComponent<?>> <C extends DataComponent<?>>
CgetData(DataComponentType<C> type) Retrieves a component instance for the given type.booleanhasData(DataComponentType<?> type) Checks if a component type exists in this map.voidload()voidSynchronizes internal storage with the persistent tags of the associated entity.voidloadItem()Synchronizes internal storage with the NMS/Paper data components of the associated item.voidremoveData(DataComponentType<?> type) Removes a component by its type and updates the underlying item/entity state.voidsetData(DataComponent<?> component) Assigns a component to this map and immediately triggers an application to the source.
-
Constructor Details
-
ComponentMap
public ComponentMap(org.bukkit.inventory.ItemStack stack) Constructs a ComponentMap by wrapping a Bukkit ItemStack.- Parameters:
stack- TheItemStackto load components from.
-
ComponentMap
-
ComponentMap
Constructs a ComponentMap for a specific custom Entity.- Parameters:
entity- TheCustomEntityinstance.
-
-
Method Details
-
load
public void load() -
loadItem
public void loadItem()Synchronizes internal storage with the NMS/Paper data components of the associated item.Scans all vanilla data types and maps them to library-compatible
DataComponents usingDataComponentType.createFromValue(Object). -
loadEntity
public void loadEntity()Synchronizes internal storage with the persistent tags of the associated entity. -
setData
Assigns a component to this map and immediately triggers an application to the source.- Parameters:
component- TheDataComponentto add or update.
-
removeData
Removes a component by its type and updates the underlying item/entity state.- Parameters:
type- TheDataComponentTypeto remove.
-
getData
Retrieves a component instance for the given type.- Type Parameters:
C- The specific DataComponent class.- Parameters:
type- TheDataComponentTypeto retrieve.- Returns:
- The component instance, or
nullif not present.
-
hasData
Checks if a component type exists in this map.- Parameters:
type- TheDataComponentTypeto check.- Returns:
trueif present.
-
applyData
public void applyData()Serializes all current components back into the source's persistent data (PDC/NBT).Custom components are encoded directly into NBT tags and stored under the
CustomComponentscompound tag. Vanilla components are applied directly viaVanilla.apply(ItemStack). -
getAllComponents
- Returns:
- A list of all active
DataComponentinstances in this map.
-
encodeComponent
Encodes a component into a serialized format using the provided DynamicOps.- Type Parameters:
T- The component's value type.D- The target serialization format type (e.g., Tag).- Parameters:
component- TheDataComponentto encode.ops- TheDynamicOpslogic to use for encoding.- Returns:
- The encoded data object.
-