Class DefaultedAttribute<T>

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

public class DefaultedAttribute<T> extends Attribute<T>
An Attribute which offers "getFirst" which returns a non-null default value rather than the null default that Attribute provides.
  • Field Details

    • defaultValue

      @Nonnull public final T defaultValue
      A non-null default value that can be used as a no-op value if searching failed to find any normal instances.
  • Constructor Details

  • Method Details

    • appendBlockAdder

      public DefaultedAttribute<T> appendBlockAdder(CustomAttributeAdder<T> blockAdder)
      Description copied from class: Attribute
      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.
      Overrides:
      appendBlockAdder in class Attribute<T>
      Returns:
      This.
    • appendItemAdder

      public DefaultedAttribute<T> appendItemAdder(ItemAttributeAdder<T> itemAdder)
      Description copied from class: Attribute
      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.
      Overrides:
      appendItemAdder in class Attribute<T>
      Returns:
      This.
    • getFirst

      @Nonnull public final T getFirst(net.minecraft.world.World world, net.minecraft.util.math.BlockPos pos)
      Returns:
      The first attribute instance (as obtained by Attribute.getAll(World, BlockPos, SearchOption)), or the defaultValue if none were found.
    • getFirst

      @Nonnull public final T getFirst(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:
      The first attribute instance (as obtained by Attribute.getAll(World, BlockPos, SearchOption)), or the defaultValue if the search didn't find any attribute instances at the specified position.
    • getFirstFromNeighbour

      @Nonnull public final T getFirstFromNeighbour(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));
    • getFirst

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

      This method is just a quicker way of calling getFirst(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 Attribute.getAll(ItemStack), or the defaultValue if none were found in the given ItemStack.
    • getFirst

      @Nonnull public final T getFirst(Reference<net.minecraft.item.ItemStack> stackRef)
      Obtains the first instance of this attribute in the given ItemStack Reference, or the defaultValue 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 Attribute.getAll(Reference), or tge defaultValue if none were found in the given ItemStack.
    • getFirst

      @Nonnull public final T getFirst(Reference<net.minecraft.item.ItemStack> stackRef, @Nullable Predicate<T> filter)
      Obtains the first instance of this attribute in the given ItemStack Reference, or the defaultValue 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 Attribute.getAll(Reference, Predicate), or the defaultValue if none were found in the given ItemStack.
    • getFirst

      @Nonnull public final T getFirst(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 the defaultValue 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 Attribute.getAll(Reference, LimitedConsumer), or the defaultValue if none were found in the given ItemStack.
    • getFirst

      @Nonnull public final T getFirst(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 the defaultValue 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 Attribute.getAll(Reference, LimitedConsumer, Predicate), or the defaultValue if none were found in the given ItemStack.