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

public class ComponentMap extends Object
Stores and manages DataComponents for an item or entity.

Components are loaded lazily from Minecraft's data components or custom NBT, then cached in memory. Changes can be written back to the underlying item or entity using applyData().

  • 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()
      Loads all components if they have not already been loaded.
    • loadItem

      public void loadItem()
      Loads all components from the associated item.

      Vanilla components are read from Paper's data component API, while custom components are loaded from the CustomComponents NBT tag.

    • loadEntity

      public void loadEntity()
      Loads all custom components from the associated entity.
    • setData

      public void setData(DataComponent<?> component)
      Adds or replaces a component and applies the change immediately.
      Parameters:
      component - the component to store
    • removeData

      public void removeData(DataComponentType<?> type)
      Removes a component and updates the underlying item or entity.
      Parameters:
      type - the component type
    • getData

      public <C extends DataComponent<?>> C getData(DataComponentType<C> type)
      Returns the component of the given type.
      Parameters:
      type - the component type
      Returns:
      the component, or null if it is not present
    • hasData

      public boolean hasData(DataComponentType<?> type)
      Returns whether this map contains the given component type.
      Parameters:
      type - the component type
      Returns:
      true if the component exists
    • applyData

      public void applyData()
      Writes all components back to the associated item or entity.

      Vanilla components are applied through Paper's API, while custom components are serialized into the CustomComponents NBT tag.

    • getAllComponents

      public List<DataComponent<?>> getAllComponents()
      Returns every loaded component.
      Returns:
      all components in this map
    • encodeComponent

      public static <T, D> D encodeComponent(DataComponent<T> component, DynamicOps<D> ops)
      Encodes a component using the given DynamicOps.
      Type Parameters:
      T - the component value type
      D - the serialized data type
      Parameters:
      component - the component to encode
      ops - the target serialization format
      Returns:
      the encoded value