Class ItemAttributeModifierEvent

java.lang.Object
net.neoforged.bus.api.Event
net.neoforged.neoforge.event.ItemAttributeModifierEvent

public class ItemAttributeModifierEvent extends net.neoforged.bus.api.Event
This event is fired when the attributes for an item stack are queried (for any reason) through IItemStackExtension.getAttributeModifiers().
This event is fired regardless of if the stack has DataComponents.ATTRIBUTE_MODIFIERS or not. If your attribute should be ignored when attributes are overridden, you can check for the presence of the component.

This event may be fired on both the logical server and logical client.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    Advanced version of ItemAttributeModifiers.Builder which supports removal and better sanity-checking.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    private final net.minecraft.world.item.component.ItemAttributeModifiers
     
    private final net.minecraft.world.item.ItemStack
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    ItemAttributeModifierEvent(net.minecraft.world.item.ItemStack stack, net.minecraft.world.item.component.ItemAttributeModifiers defaultModifiers)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    addModifier(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute, net.minecraft.world.entity.ai.attributes.AttributeModifier modifier, net.minecraft.world.entity.EquipmentSlotGroup slot)
    Adds a new attribute modifier to the given stack.
    net.minecraft.world.item.component.ItemAttributeModifiers
    Builds a new ItemAttributeModifiers with the results of this event, returning the default modifiers if no changes were made.
    void
    Removes all modifiers for all attributes.
    Returns the builder used for adjusting the attribute modifiers, creating it if it does not already exist.
    net.minecraft.world.item.component.ItemAttributeModifiers
    Returns the default attribute modifiers before changes made by the event.
    net.minecraft.world.item.ItemStack
    Returns the item stack whose attribute modifiers are being computed.
    List<net.minecraft.world.item.component.ItemAttributeModifiers.Entry>
    Returns an unmodifiable view of the attribute modifier entries.
    boolean
    removeAllModifiersFor(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute)
    Removes all modifiers for the given attribute.
    boolean
    removeIf(Predicate<net.minecraft.world.item.component.ItemAttributeModifiers.Entry> condition)
    Removes modifiers based on a condition.
    boolean
    removeModifier(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute, net.minecraft.resources.ResourceLocation id)
    Removes an attribute modifier for the target attribute by id
    void
    replaceModifier(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute, net.minecraft.world.entity.ai.attributes.AttributeModifier modifier, net.minecraft.world.entity.EquipmentSlotGroup slot)
    Adds a new attribute modifier to the given stack, optionally replacing any existing modifiers with the same id.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • ItemAttributeModifierEvent

      @Internal public ItemAttributeModifierEvent(net.minecraft.world.item.ItemStack stack, net.minecraft.world.item.component.ItemAttributeModifiers defaultModifiers)
  • Method Details

    • getItemStack

      public net.minecraft.world.item.ItemStack getItemStack()
      Returns the item stack whose attribute modifiers are being computed.
      Returns:
      the item stack whose attribute modifiers are being computed
    • getDefaultModifiers

      public net.minecraft.world.item.component.ItemAttributeModifiers getDefaultModifiers()
      Returns the default attribute modifiers before changes made by the event.
      Returns:
      the default attribute modifiers before changes made by the event
    • getModifiers

      public List<net.minecraft.world.item.component.ItemAttributeModifiers.Entry> getModifiers()
      Returns an unmodifiable view of the attribute modifier entries. Do not use the returned value to create an ItemAttributeModifiers since the underlying list is not immutable.

      If you need an ItemAttributeModifiers, you may need to call build()

    • addModifier

      public boolean addModifier(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute, net.minecraft.world.entity.ai.attributes.AttributeModifier modifier, net.minecraft.world.entity.EquipmentSlotGroup slot)
      Adds a new attribute modifier to the given stack. Two modifiers with the same id may not exist for the same attribute, and this method will fail if one exists.
      Parameters:
      attribute - The attribute the modifier is for
      modifier - The new attribute modifier
      slot - The equipment slots for which the modifier should apply
      Returns:
      True if the modifier was added, false if it was already present
    • removeModifier

      public boolean removeModifier(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute, net.minecraft.resources.ResourceLocation id)
      Removes an attribute modifier for the target attribute by id
      Returns:
      True if an attribute modifier was removed, false otherwise
    • replaceModifier

      public void replaceModifier(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute, net.minecraft.world.entity.ai.attributes.AttributeModifier modifier, net.minecraft.world.entity.EquipmentSlotGroup slot)
      Adds a new attribute modifier to the given stack, optionally replacing any existing modifiers with the same id.
      Parameters:
      attribute - The attribute the modifier is for
      modifier - The new attribute modifier
      slot - The equipment slots for which the modifier should apply
    • removeIf

      public boolean removeIf(Predicate<net.minecraft.world.item.component.ItemAttributeModifiers.Entry> condition)
      Removes modifiers based on a condition.
      Returns:
      true if any modifiers were removed
    • removeAllModifiersFor

      public boolean removeAllModifiersFor(net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute)
      Removes all modifiers for the given attribute.
      Returns:
      true if any modifiers were removed
    • clearModifiers

      public void clearModifiers()
      Removes all modifiers for all attributes.
    • build

      public net.minecraft.world.item.component.ItemAttributeModifiers build()
      Builds a new ItemAttributeModifiers with the results of this event, returning the default modifiers if no changes were made.
    • getBuilder

      Returns the builder used for adjusting the attribute modifiers, creating it if it does not already exist.