Class ComponentMap

java.lang.Object
com.github.darksoulq.abyssallib.world.item.component.ComponentMap

public class ComponentMap extends Object
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 Details

    • ComponentMap

      public ComponentMap(org.bukkit.inventory.ItemStack stack)
      Constructs a ComponentMap by wrapping a Bukkit ItemStack.
      Parameters:
      stack - The ItemStack to load components from.
    • ComponentMap

      public ComponentMap(Item item)
      Constructs a ComponentMap for a specific custom Item.
      Parameters:
      item - The Item instance.
    • ComponentMap

      public ComponentMap(CustomEntity<? extends org.bukkit.entity.LivingEntity> entity)
      Constructs a ComponentMap for a specific custom Entity.
      Parameters:
      entity - The CustomEntity instance.
  • 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 using DataComponentType.createFromValue(Object).

    • loadEntity

      public void loadEntity()
      Synchronizes internal storage with the persistent tags of the associated entity.
    • setData

      public void setData(DataComponent<?> component)
      Assigns a component to this map and immediately triggers an application to the source.
      Parameters:
      component - The DataComponent to add or update.
    • removeData

      public void removeData(DataComponentType<?> type)
      Removes a component by its type and updates the underlying item/entity state.
      Parameters:
      type - The DataComponentType to remove.
    • getData

      public <C extends DataComponent<?>> C getData(DataComponentType<C> type)
      Retrieves a component instance for the given type.
      Type Parameters:
      C - The specific DataComponent class.
      Parameters:
      type - The DataComponentType to retrieve.
      Returns:
      The component instance, or null if not present.
    • hasData

      public boolean hasData(DataComponentType<?> type)
      Checks if a component type exists in this map.
      Parameters:
      type - The DataComponentType to check.
      Returns:
      true if 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 CustomComponents compound tag. Vanilla components are applied directly via Vanilla.apply(ItemStack).

    • getAllComponents

      public List<DataComponent<?>> getAllComponents()
      Returns:
      A list of all active DataComponent instances in this map.
    • encodeComponent

      public static <T,D> D encodeComponent(DataComponent<T> component, DynamicOps<D> ops)
      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 - The DataComponent to encode.
      ops - The DynamicOps logic to use for encoding.
      Returns:
      The encoded data object.