Attribute bonuses: AttributeBonusHelper¶
An idempotent pair for applying and removing an attribute modifier:
AttributeBonusHelper.add(entity, Attributes.MOVEMENT_SPEED, SPEED_UUID, "My Speed",
0.2, AttributeModifier.Operation.MULTIPLY_BASE);
AttributeBonusHelper.remove(entity, Attributes.MOVEMENT_SPEED, SPEED_UUID);
adddoes nothing if a modifier with that UUID is already present;removedoes nothing if it is absent.
So both can be called every tick at no cost, which is the safe way to hold a conditional bonus.
Apply and remove every tick, never on transitions
add uses a permanent modifier, which is saved with the entity. A bonus applied when a condition starts and removed when it ends is kept for good if the entity logs out in between: on reconnect, your "was active" flag is gone and the modifier is not. Checking the condition every tick and calling add or remove accordingly cannot drift.
For a passive, ConditionalStatPassiveAbility already does this, with tiers and disable handling. For a morph, see MorphStats.
One UUID per attribute per bonus
Two modifiers with the same UUID on one attribute cannot coexist: the second is refused while the first is present. Give every bonus its own UUID.