Class AttributeUtils

java.lang.Object
valorless.rarespawns.utils.AttributeUtils

public final class AttributeUtils extends Object
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 Type
    Method
    Description
    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.
    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.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 modify
      attribute - attribute to look for
      operation - 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 add
      name - human-readable name for the modifier (used for identification)
      amount - amount to apply (depends on operation)
      operation - operation type for the modifier
      slot - 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 to
      newModifier - the new modifier to add (its UUID will be replaced to preserve the old one if present)
      item - target ItemStack