Package valorless.rarespawns.utils
Class AttributeUtils
java.lang.Object
valorless.rarespawns.utils.AttributeUtils
Utility helpers for safely replacing/removing attribute modifiers on ItemStacks.
Behavior:
- removeSameKindModifiers(...) removes existing modifiers for the same Attribute
that match the same Operation and EquipmentSlot as the new modifier.
- addOrReplaceAttributeModifier(...) removes same-kind modifiers then adds the
new modifier to the item.
Change from previous version:
- When replacing an existing modifier, this version preserves the UUID of the
first matching modifier instead of always generating a new random UUID.
This keeps the "id of the one replaced" as requested.
New behavior:
- If the new modifier's amount is lower than the first existing matching modifier's amount,
the replace/add is skipped entirely.
-
Method Summary
Modifier and TypeMethodDescriptionstatic org.bukkit.inventory.meta.ItemMetaaddOrReplaceAttributeModifier(org.bukkit.inventory.meta.ItemMeta meta, org.bukkit.attribute.Attribute attribute, String name, double amount, org.bukkit.attribute.AttributeModifier.Operation operation, org.bukkit.inventory.EquipmentSlot slot) Removes any existing modifiers of the "same kind" and then adds the provided modifier to the item, reusing the UUID of the first removed modifier if any.static org.bukkit.inventory.meta.ItemMetaaddOrReplaceAttributeModifier(org.bukkit.inventory.meta.ItemMeta meta, org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier newModifier) Convenience overload: add/replace using a prepared AttributeModifier instance.static voidremoveSameKindModifiers(org.bukkit.inventory.meta.ItemMeta meta, org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier.Operation operation, org.bukkit.inventory.EquipmentSlot slot) Remove all attribute modifiers on the provided ItemMeta that match the given attribute, operation and slot.
-
Method Details
-
removeSameKindModifiers
public static void removeSameKindModifiers(org.bukkit.inventory.meta.ItemMeta meta, org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier.Operation operation, org.bukkit.inventory.EquipmentSlot slot) Remove all attribute modifiers on the provided ItemMeta that match the given attribute, operation and slot.- Parameters:
meta- the ItemMeta to modifyattribute- attribute to look foroperation- operation to match (ADD_NUMBER, ADD_SCALAR, MULTIPLY_SCALAR_1)slot- equipment slot to match (may be null)
-
addOrReplaceAttributeModifier
public static org.bukkit.inventory.meta.ItemMeta addOrReplaceAttributeModifier(org.bukkit.inventory.meta.ItemMeta meta, org.bukkit.attribute.Attribute attribute, String name, double amount, org.bukkit.attribute.AttributeModifier.Operation operation, org.bukkit.inventory.EquipmentSlot slot) Removes any existing modifiers of the "same kind" and then adds the provided modifier to the item, reusing the UUID of the first removed modifier if any. If a first existing matching modifier exists and the new amount is lower than that existing modifier's amount, the operation is skipped entirely.- Parameters:
attribute- attribute to addname- human-readable name for the modifier (used for identification)amount- amount to apply (depends on operation)operation- operation type for the modifierslot- equipment slot the modifier applies to (may be null)item- target ItemStack (item meta will be updated). If item has no meta or is null, nothing happens.
-
addOrReplaceAttributeModifier
public static org.bukkit.inventory.meta.ItemMeta addOrReplaceAttributeModifier(org.bukkit.inventory.meta.ItemMeta meta, org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier newModifier) Convenience overload: add/replace using a prepared AttributeModifier instance. Removes other modifiers of the same kind (matching operation invalid input: '&' slot) for the same attribute, and reuses the UUID of the first removed modifier if present. The resulting modifier will carry the new modifier's name/amount/operation/slot but keep the reused UUID. If a first existing matching modifier exists and the new modifier's amount is lower than that existing modifier's amount, the operation is skipped entirely.- Parameters:
attribute- attribute the modifier applies tonewModifier- the new modifier to add (its UUID will be replaced to preserve the old one if present)item- target ItemStack
-