Class Attribute<T>

java.lang.Object
alexiil.mc.lib.attributes.Attribute<T>
Direct Known Subclasses:
DefaultedAttribute

public class Attribute<T> extends Object
The central holding class for all attribute instances.

An Attribute can be of a single Class that should be accessible from blocks or items. Instances can be created from the various static factory methods in Attributes. Due to the different subclasses no registry is provided, so instances should be stored in a public static final field somewhere near the target class.

Usage

All attributes offer "getAll" and "getFirstOrNull" methods.

Blocks

Instances can be obtained from blocks with the getAll(World, BlockPos, SearchOption) or getFirstOrNull(World, BlockPos, SearchOption) methods, although the SearchOption can be omitted (or passed as null) if you don't wish to restrict what attributes are returned to you.
For convenience there is also a "getAllFromNeighbour" (and getFirstOrNullFromNeighbour) which takes a BlockEntity to search from, and a Direction to search in.

Items

Instances can be obtained from items with the getAll(Reference, LimitedConsumer, Predicate) or getFirstOrNull(Reference, LimitedConsumer, Predicate) methods, however the predicate may be omitted (or passed as null) if you f you don't wish to restrict what attributes are returned to you.

ItemStacks don't inherently have any information about what they are stored in (unlike blocks) so instead of a world and block position we use a Reference for the current stack, and a LimitedConsumer for the excess. The Reference may contain an item with any count, although generally only the uppermost item on the stack will be used by attributes. Attribute instances which modify the ItemStack are highly encouraged to extend AbstractItemBasedAttribute to help manage returning the modified ItemStack to the reference and limited consumer.

Entities

Currently LBA doesn't offer support for entities, although it is planned.

Subclasses

There are 2 provided subclasses of Attribute: DefaultedAttribute and CombinableAttribute.

Custom Adders

If the target block or item doesn't implement AttributeProvider or AttributeProviderItem (or those methods don't offer the attribute instance that you need) then you can create a custom adder for each block or item that you need. The old (deprecated) method of adding custom attribute adders called every single one in the order that they were added. The new method however only matches a single one (per attribute), and has the following order:
  1. If the block or item implements AttributeProvider or AttributeProviderItem directly then is is used first. If that adder didn't add anything then the next steps aren't skipped (so it will exit early if the block/item provided any implementations itself, otherwise it will continue to try to find one).
  2. If the block state is meant to have a BlockEntity (AbstractBlock.AbstractBlockState.hasBlockEntity()), and a BlockEntity is present in the world, and it implements AttributeProviderBlockEntity then it is checked second. If that adder didn't add anything then the next steps aren't skipped (so it will exit early if the block entity provided any implementations itself, otherwise it will continue to try to find one).
  3. AttributeSourceType.INSTANCE implementations are considered:
    1. If the block/item has an exact mapping registered in setBlockAdder(AttributeSourceType, Block, CustomAttributeAdder) then it is used.
    2. Next, any predicate adders (addBlockPredicateAdder(AttributeSourceType, boolean, Predicate, CustomAttributeAdder)) are considered (if they passed "true" for "specific").
    3. The exact class mapped by (putBlockClassAdder(AttributeSourceType, Class, boolean, CustomAttributeAdder)) is considered (if they passed "false" for "matchSubclasses").
    4. Any super-classes or interfaces mapped by (putBlockClassAdder(AttributeSourceType, Class, boolean, CustomAttributeAdder)) is considered (if they passed "true" for "matchSubclasses").
    5. Finally, any predicate adders (addBlockPredicateAdder(AttributeSourceType, boolean, Predicate, CustomAttributeAdder)) are considered (if they passed "false" for "specific").
  4. AttributeSourceType.COMPAT_WRAPPER implementations are considered, in the same order as AttributeSourceType.INSTANCE above.
  5. Finally everything registered to appendBlockAdder(CustomAttributeAdder) is called. (Unlike every other adder above, every single one is called).
  • Field Details

    • clazz

      public final Class<T> clazz
  • Constructor Details

  • Method Details

    • isInstance

      public final boolean isInstance(Object obj)
      Checks to see if the given object is an Class.isInstance(Object) of this attribute.
    • cast

      public final T cast(Object obj)
      Casts The given object to type of this attribute.
    • equals

      public final boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • setBlockAdder

      public final void setBlockAdder(AttributeSourceType sourceType, net.minecraft.block.Block block, CustomAttributeAdder<T> adder)
      Sets the CustomAttributeAdder for the given block, which is only used if the block in question doesn't implement AttributeProvider. Only one CustomAttributeAdder may respond to a singular block.
    • setBlockEntityAdder

      public final <BE extends net.minecraft.block.entity.BlockEntity> void setBlockEntityAdder(AttributeSourceType sourceType, net.minecraft.block.entity.BlockEntityType<BE> type, BlockEntityAttributeAdder<T,BE> adder)
      Sets the BlockEntityAttributeAdder for the given block entity type, which is only used if the block entity in question doesn't implement AttributeProviderBlockEntity. Only one BlockEntityAttributeAdder may respond to a singular block.
    • setBlockEntityAdder

      public final <BE extends net.minecraft.block.entity.BlockEntity> void setBlockEntityAdder(AttributeSourceType sourceType, net.minecraft.block.entity.BlockEntityType<BE> type, Class<BE> clazz, BlockEntityAttributeAdder.BlockEntityAttributeAdderFN<T,BE> adder)
      Sets the BlockEntityAttributeAdder for the given block entity type, which is only used if the block entity in question doesn't implement AttributeProviderBlockEntity. Only one BlockEntityAttributeAdder may respond to a singular block.
    • setBlockEntityAdderFN

      public final void setBlockEntityAdderFN(AttributeSourceType sourceType, net.minecraft.block.entity.BlockEntityType<?> type, BlockEntityAttributeAdder.BlockEntityAttributeAdderFN<T,net.minecraft.block.entity.BlockEntity> adder)
      Sets the BlockEntityAttributeAdder for the given block entity type, which is only used if the block entity in question doesn't implement AttributeProviderBlockEntity. Only one BlockEntityAttributeAdder may respond to a singular block.
    • setItemAdder

      public final void setItemAdder(AttributeSourceType sourceType, net.minecraft.item.Item item, ItemAttributeAdder<T> adder)
      Sets the ItemAttributeAdder for the given item, which is only used if the item in question doesn't implement AttributeProviderItem. Only one CustomAttributeAdder may respond to a singular item.
    • addBlockPredicateAdder

      public final void addBlockPredicateAdder(AttributeSourceType sourceType, boolean specific, Predicate<net.minecraft.block.Block> filter, CustomAttributeAdder<T> adder)
      Predicate-based block attribute adder. If "specific" is true then these are called directly after setBlockAdder(AttributeSourceType, Block, CustomAttributeAdder), otherwise they are called after the class-based mappings have been called.
    • addBlockEntityPredicateAdder

      public final void addBlockEntityPredicateAdder(AttributeSourceType sourceType, boolean specific, Predicate<net.minecraft.block.entity.BlockEntityType<?>> filter, BlockEntityAttributeAdder.BlockEntityAttributeAdderFN<T,net.minecraft.block.entity.BlockEntity> adder)
      Predicate-based block entity attribute adder. If "specific" is true then these are called directly after setBlockEntityAdder(AttributeSourceType, BlockEntityType, BlockEntityAttributeAdder), otherwise they are called after the class-based mappings have been called.
    • addItemPredicateAdder

      public final void addItemPredicateAdder(AttributeSourceType sourceType, boolean specific, Predicate<net.minecraft.item.Item> filter, ItemAttributeAdder<T> adder)
      Predicate-based item attribute adder. If "specific" is true then these are called directly after setItemAdder(AttributeSourceType, Item, ItemAttributeAdder), otherwise they are called after the class-based mappings have been called.
    • putBlockClassAdder

      public final void putBlockClassAdder(AttributeSourceType sourceType, Class<?> clazz, boolean matchSubclasses, CustomAttributeAdder<T> adder)
      Class-based block attribute adder. If no specific predicate adder has been registered then this checks for an exact class match, and then for a super-type match. Only one adder may be present for any given class.
    • putBlockEntityClassAdder

      public final <BE> void putBlockEntityClassAdder(AttributeSourceType sourceType, Class<BE> clazz, boolean matchSubclasses, BlockEntityAttributeAdder.BlockEntityAttributeAdderFN<T,BE> adder)
      Class-based block entity attribute adder. If no specific predicate adder has been registered then this checks for an exact class match, and then for a super-type match. Only one adder may be present for any given class.
    • putItemClassAdder

      public final void putItemClassAdder(AttributeSourceType sourceType, Class<?> clazz, boolean matchSubclasses, ItemAttributeAdder<T> adder)
      Class-based item attribute adder. If no specific predicate adder has been registered then this checks for an exact class match, and then for a super-type match.
    • appendCustomAdder

      @Deprecated(since="0.5.0", forRemoval=true) public final void appendCustomAdder(CustomAttributeAdder<T> customAdder)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Provided for backwards compatibility - instead you should use appendBlockAdder(CustomAttributeAdder).
    • appendBlockAdder

      public Attribute<T> appendBlockAdder(CustomAttributeAdder<T> blockAdder)
      Appends a single CustomAttributeAdder to the list of custom block adders. These are called only for blocks that don't implement AttributeProvider, or have an existing registration in one of the more specific methods above.
      Returns:
      This.
    • appendItemAdder

      public Attribute<T> appendItemAdder(ItemAttributeAdder<T> itemAdder)
      Appends a single ItemAttributeAdder to the list of custom item adders. These are called only for items that don't implement AttributeProviderItem, or have an existing registration in one of the more specific methods above.
      Returns:
      This.
    • getAll

      public final AttributeList<T> getAll(net.minecraft.world.World world, net.minecraft.util.math.BlockPos pos)
      Returns:
      A complete AttributeList of every attribute instance that can be found.
    • getAll

      public final AttributeList<T> getAll(net.minecraft.world.World world, net.minecraft.util.math.BlockPos pos, SearchOption<? super T> searchParam)
      Parameters:
      searchParam - The search parameters to use for accessing instances. Many blocks only offer attributes from a certain direction, which should be provided as a SearchOptionDirectional. A full list of possible SearchOption's is in SearchOptions.
      Returns:
      A complete AttributeList of every attribute instance that can be found with the supplied search parameters.
    • getAllFromNeighbour

      public final AttributeList<T> getAllFromNeighbour(net.minecraft.block.entity.BlockEntity be, net.minecraft.util.math.Direction dir)
      Shorter method call for the common case of:
      BlockEntity be = ...;
      Direction dir = ...;
      Attribute<T> attr = ...;
      AttributeList<T> list = attr.getAll(be.getWorld(), be.getPos().offset(dir), SearchOptions.inDirection(dir));
    • getFirstOrNull

      @Nullable public final T getFirstOrNull(net.minecraft.world.World world, net.minecraft.util.math.BlockPos pos)
      Returns:
      The first attribute instance (as obtained by getAll(World, BlockPos)), or null if this didn't find any instances.
    • getFirstOrNull

      @Nullable public final T getFirstOrNull(net.minecraft.world.World world, net.minecraft.util.math.BlockPos pos, @Nullable SearchOption<? super T> searchParam)
      Parameters:
      searchParam - The search parameters to use for accessing instances. Many blocks only offer attributes from a certain direction, which should be provided as a SearchOptionDirectional. A full list of possible SearchOption's is in SearchOptions.
      Returns:
      The first attribute instance (as obtained by getAll(World, BlockPos, SearchOption)), or null if the search didn't find any attribute instances at the specified position.
    • getFirstOrNullFromNeighbour

      @Nullable public final T getFirstOrNullFromNeighbour(net.minecraft.block.entity.BlockEntity be, net.minecraft.util.math.Direction dir)
      Shorter method call for the common case of:
      BlockEntity be = ...;
      Direction dir = ...;
      Attribute<T> attr = ...;
      AttributeList<T> list = attr.getAll(be.getWorld(), be.getPos().offset(dir), SearchOptions.inDirection(dir));
    • getAll

      public final ItemAttributeList<T> getAll(net.minecraft.item.ItemStack unmodifiableStack)
      Obtains all instances of this attribute in the given ItemStack Reference.

      This method is just a quicker way of calling getAll(Reference) of a single ItemStack which cannot be modified. Internally this creates a new UnmodifiableRef for the reference.

      Parameters:
      unmodifiableStack - An ItemStack that may not be modified by any of the attribute instances returned.
      Returns:
      A complete AttributeList of every attribute instance that can be found in the given ItemStack.
    • getAll

      public final ItemAttributeList<T> getAll(Reference<net.minecraft.item.ItemStack> stackRef)
      Obtains all instances of this attribute in the given ItemStack Reference.
      Parameters:
      stackRef - A Reference to the ItemStack to be searched. This is a full reference, which may allow any of the returned attribute instances to modify it. (For example if it was in an inventory then changes would be correctly reflected in the backing inventory).
      Returns:
      A complete AttributeList of every attribute instance that can be found in the given ItemStack.
    • getAll

      public final ItemAttributeList<T> getAll(Reference<net.minecraft.item.ItemStack> stackRef, @Nullable Predicate<T> filter)
      Obtains all instances of this attribute in the given ItemStack Reference.
      Parameters:
      filter - A Predicate to test all offered objects before accepting them into the list. A null value equals no filter, which will not block any values.
      Returns:
      A complete AttributeList of every attribute instance that can be found in the given ItemStack.
    • getAll

      public final ItemAttributeList<T> getAll(Reference<net.minecraft.item.ItemStack> stackRef, LimitedConsumer<net.minecraft.item.ItemStack> excess)
      Obtains all instances of this attribute in the given ItemStack Reference.
      Parameters:
      stackRef - A Reference to the ItemStack to be searched. This is a full reference, which may allow any of the returned attribute instances to modify it. (For example if it was in an inventory then changes would be correctly reflected in the backing inventory).
      excess - A LimitedConsumer which allows any of the returned attribute instances to spit out excess items in addition to changing the main stack. (As this is a LimitedConsumer rather than a normal consumer it is important to note that excess items items are not guaranteed to be accepted). A null value will default to LimitedConsumer.rejecting().
      Returns:
      A complete AttributeList of every attribute instance that can be found in the given ItemStack.
    • getAll

      public final ItemAttributeList<T> getAll(Reference<net.minecraft.item.ItemStack> stackRef, LimitedConsumer<net.minecraft.item.ItemStack> excess, @Nullable Predicate<T> filter)
      Obtains all instances of this attribute in the given ItemStack Reference.
      Parameters:
      stackRef - A Reference to the ItemStack to be searched. This is a full reference, which may allow any of the returned attribute instances to modify it. (For example if it was in an inventory then changes would be correctly reflected in the backing inventory).
      excess - A LimitedConsumer which allows any of the returned attribute instances to spit out excess items in addition to changing the main stack. (As this is a LimitedConsumer rather than a normal consumer it is important to note that excess items items are not guaranteed to be accepted). A null value will default to LimitedConsumer.rejecting().
      filter - A Predicate to test all offered objects before accepting them into the list. A null value equals no filter, which will not block any values.
      Returns:
      A complete AttributeList of every attribute instance that can be found in the given ItemStack.
    • getFirstOrNull

      @Nullable public final T getFirstOrNull(net.minecraft.item.ItemStack unmodifiableStack)
      Obtains the first instance of this attribute in the given ItemStack Reference, or null if none were found.

      This method is just a quicker way of calling getAll(Reference) of a single ItemStack which cannot be modified. Internally this creates a new UnmodifiableRef for the reference.

      Parameters:
      unmodifiableStack - An ItemStack that may not be modified by any of the attribute instances returned.
      Returns:
      The first attribute instance found by getAll(ItemStack), or null if none were found in the given ItemStack.
    • getFirstOrNull

      @Nullable public final T getFirstOrNull(Reference<net.minecraft.item.ItemStack> stackRef)
      Obtains the first instance of this attribute in the given ItemStack Reference, or null if none were found.
      Parameters:
      stackRef - A Reference to the ItemStack to be searched. This is a full reference, which may allow any of the returned attribute instances to modify it. (For example if it was in an inventory then changes would be correctly reflected in the backing inventory).
      Returns:
      The first attribute instance found by getAll(Reference), or null if none were found in the given ItemStack.
    • getFirstOrNull

      @Nullable public final T getFirstOrNull(Reference<net.minecraft.item.ItemStack> stackRef, @Nullable Predicate<T> filter)
      Obtains the first instance of this attribute in the given ItemStack Reference, or null if none were found.
      Parameters:
      filter - A Predicate to test all offered objects before accepting them into the list. A null value equals no filter, which will not block any values.
      Returns:
      The first attribute instance found by getAll(Reference, Predicate), or null if none were found in the given ItemStack.
    • getFirstOrNull

      @Nullable public final T getFirstOrNull(Reference<net.minecraft.item.ItemStack> stackRef, LimitedConsumer<net.minecraft.item.ItemStack> excess)
      Obtains the first instance of this attribute in the given ItemStack Reference, or null if none were found.
      Parameters:
      stackRef - A Reference to the ItemStack to be searched. This is a full reference, which may allow any of the returned attribute instances to modify it. (For example if it was in an inventory then changes would be correctly reflected in the backing inventory).
      excess - A LimitedConsumer which allows any of the returned attribute instances to spit out excess items in addition to changing the main stack. (As this is a LimitedConsumer rather than a normal consumer it is important to note that excess items items are not guaranteed to be accepted). A null value will default to LimitedConsumer.rejecting().
      Returns:
      The first attribute instance found by getAll(Reference, LimitedConsumer), or null if none were found in the given ItemStack.
    • getFirstOrNull

      @Nullable public final T getFirstOrNull(Reference<net.minecraft.item.ItemStack> stackRef, LimitedConsumer<net.minecraft.item.ItemStack> excess, @Nullable Predicate<T> filter)
      Obtains the first instance of this attribute in the given ItemStack Reference, or null if none were found.
      Parameters:
      stackRef - A Reference to the ItemStack to be searched. This is a full reference, which may allow any of the returned attribute instances to modify it. (For example if it was in an inventory then changes would be correctly reflected in the backing inventory).
      excess - A LimitedConsumer which allows any of the returned attribute instances to spit out excess items in addition to changing the main stack. (As this is a LimitedConsumer rather than a normal consumer it is important to note that excess items items are not guaranteed to be accepted). A null value will default to LimitedConsumer.rejecting().
      filter - A Predicate to test all offered objects before accepting them into the list. A null value equals no filter, which will not block any values.
      Returns:
      The first attribute instance found by getAll(Reference, LimitedConsumer, Predicate), or null if none were found in the given ItemStack.