Package net.neoforged.neoforge.event
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
This event is fired regardless of if the stack has
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
Modifier and TypeClassDescriptionprivate static class
Advanced version ofItemAttributeModifiers.Builder
which supports removal and better sanity-checking. -
Field Summary
-
Constructor Summary
ConstructorDescriptionItemAttributeModifierEvent
(ItemStack stack, ItemAttributeModifiers defaultModifiers) -
Method Summary
Modifier and TypeMethodDescriptionboolean
addModifier
(Holder<Attribute> attribute, AttributeModifier modifier, EquipmentSlotGroup slot) Adds a new attribute modifier to the given stack.build()
Builds a newItemAttributeModifiers
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.Returns the default attribute modifiers before changes made by the event.Returns the item stack whose attribute modifiers are being computed.Returns an unmodifiable view of the attribute modifier entries.boolean
removeAllModifiersFor
(Holder<Attribute> attribute) Removes all modifiers for the given attribute.boolean
removeIf
(Predicate<ItemAttributeModifiers.Entry> condition) Removes modifiers based on a condition.boolean
removeModifier
(Holder<Attribute> attribute, ResourceLocation id) Removes an attribute modifier for the target attribute by idvoid
replaceModifier
(Holder<Attribute> attribute, AttributeModifier modifier, EquipmentSlotGroup slot) Adds a new attribute modifier to the given stack, optionally replacing any existing modifiers with the same id.
-
Field Details
-
stack
-
defaultModifiers
-
builder
-
-
Constructor Details
-
ItemAttributeModifierEvent
@Internal public ItemAttributeModifierEvent(ItemStack stack, ItemAttributeModifiers defaultModifiers)
-
-
Method Details
-
getItemStack
Returns the item stack whose attribute modifiers are being computed.- Returns:
- the item stack whose attribute modifiers are being computed
-
getDefaultModifiers
Returns the default attribute modifiers before changes made by the event.- Returns:
- the default attribute modifiers before changes made by the event
-
getModifiers
Returns an unmodifiable view of the attribute modifier entries. Do not use the returned value to create anItemAttributeModifiers
since the underlying list is not immutable.If you need an
ItemAttributeModifiers
, you may need to callbuild()
-
addModifier
public boolean addModifier(Holder<Attribute> attribute, AttributeModifier modifier, 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 formodifier
- The new attribute modifierslot
- The equipment slots for which the modifier should apply- Returns:
- True if the modifier was added, false if it was already present
-
removeModifier
Removes an attribute modifier for the target attribute by id- Returns:
- True if an attribute modifier was removed, false otherwise
-
replaceModifier
public void replaceModifier(Holder<Attribute> attribute, AttributeModifier modifier, 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 formodifier
- The new attribute modifierslot
- The equipment slots for which the modifier should apply
-
removeIf
Removes modifiers based on a condition.- Returns:
- true if any modifiers were removed
-
removeAllModifiersFor
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
Builds a newItemAttributeModifiers
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.
-