public class AttributeBonus implements PassiveBonus { private final Attribute attribute; private final GemBonusType type; public AttributeBonus(Attribute attribute, GemBonusType type) { this.attribute = attribute; this.type = type; } @Override public void apply(Player player, float value) { System.out.println("Applying " + attribute.name() + " passive attribute value " + value + " to player " + player.getName()); if(type == GemBonusType.PERCENTAGE) value /= 100; player.registerAttribute(this.attribute); AttributeInstance attribute = player.getAttribute(this.attribute); AttributeModifier modifier = getModifier(attribute); if (modifier != null) attribute.removeModifier(modifier); attribute.addModifier(new AttributeModifier("Gem-" + attribute, value, type.asModifierOperation())); } @Override public void stop(Player player, float value) { AttributeInstance attribute = player.getAttribute(this.attribute); AttributeModifier modifier = getModifier(attribute); if (modifier == null) return; attribute.removeModifier(modifier); } private AttributeModifier getModifier(AttributeInstance instance) { for (AttributeModifier modifier : instance.getModifiers()) if (modifier.getName().equalsIgnoreCase("Gem-" + attribute)) return modifier; return null; } }