CustomMobs/src/main/java/com/alttd/custommobs/abilities/modifiers/EntityAttributeModifier.java

49 lines
1.6 KiB
Java
Raw Normal View History

package com.alttd.custommobs.abilities.modifiers;
import com.alttd.custommobs.Main;
import com.alttd.custommobs.abilities.EntityModifier;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.entity.LivingEntity;
@Slf4j
public class EntityAttributeModifier implements EntityModifier {
private final double value;
private final Attribute attribute;
private final Main main;
public EntityAttributeModifier(Main main, Attribute attribute, double value) {
this.main = main;
this.value = value;
this.attribute = attribute;
}
@Override
public void apply(LivingEntity entity) {
AttributeInstance attributeInstance = entity.getAttribute(attribute);
if (attributeInstance == null) {
log.warn("Entity {} has no {} attribute", entity.getName(), attribute.name());
return;
}
log.info("DEBUG: Setting attribute {} to {}", attribute.name(), value);
NamespacedKey namespacedKey = NamespacedKey.fromString("cm", main);
if (namespacedKey == null) {
log.error("Namespaced key was null when applying attribute modifier");
return;
}
AttributeModifier attributeModifier = new AttributeModifier(namespacedKey,
attributeInstance.getValue() - value,
AttributeModifier.Operation.ADD_NUMBER);
attributeInstance.addModifier(attributeModifier);
}
@Override
public String getName() {
return attribute.name();
}
}