Search in sources :

Example 21 with AttributeInstance

use of org.bukkit.attribute.AttributeInstance in project MagicPlugin by elBukkit.

the class DamageAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null || !(entity instanceof Damageable) || entity.isDead()) {
        return SpellResult.NO_TARGET;
    }
    double damage = 1;
    Damageable targetEntity = (Damageable) entity;
    LivingEntity livingTarget = (entity instanceof LivingEntity) ? (LivingEntity) entity : null;
    context.registerDamaged(targetEntity);
    Mage mage = context.getMage();
    MageController controller = context.getController();
    double previousKnockbackResistance = 0D;
    try {
        if (knockbackResistance != null && livingTarget != null) {
            AttributeInstance knockBackAttribute = livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
            previousKnockbackResistance = knockBackAttribute.getBaseValue();
            knockBackAttribute.setBaseValue(knockbackResistance);
        }
        if (controller.isElemental(entity)) {
            damage = elementalDamage;
            controller.damageElemental(entity, damage * mage.getDamageMultiplier(), 0, mage.getCommandSender());
        } else {
            if (targetEntity instanceof LivingEntity && noDamageTicks >= 0) {
                LivingEntity li = (LivingEntity) targetEntity;
                li.setNoDamageTicks(noDamageTicks);
            }
            if (percentage != null) {
                damage = percentage * CompatibilityLib.getCompatibilityUtils().getMaxHealth(targetEntity);
            } else if (targetEntity instanceof Player) {
                damage = playerDamage;
            } else {
                damage = entityDamage;
            }
            String multiplierType = damageType;
            if (multiplierType == null) {
                multiplierType = magicDamage ? "magic" : "physical";
            }
            double mageMultiplier = mage.getDamageMultiplier(multiplierType);
            damage *= mageMultiplier;
            if (maxDistanceSquared > 0) {
                Location entityLocation = damageSourceLocation.getLocation(context);
                double distanceSquared = context.getLocation().distanceSquared(entityLocation);
                if (distanceSquared > maxDistanceSquared) {
                    if (invertDistance) {
                        distanceSquared = maxDistanceSquared;
                    } else {
                        return SpellResult.NO_TARGET;
                    }
                }
                double distanceRange = maxDistanceSquared - minDistanceSquared;
                double distanceScale = Math.min(1, Math.max(0, (distanceSquared - minDistanceSquared)) / distanceRange);
                if (!invertDistance) {
                    distanceScale = 1 - distanceScale;
                }
                damage = damage * distanceScale;
            }
            if (damageMultiplier != null) {
                damage *= damageMultiplier;
                mageMultiplier *= damageMultiplier;
            }
            if (criticalProbability > 0 && criticalMultiplier > 0 && context.getRandom().nextDouble() <= criticalProbability) {
                damage *= criticalMultiplier;
                context.playEffects(criticalEffectsKey);
            }
            damage = Math.max(minDamage, damage);
            if (damageType != null) {
                mage.setLastDamageDealtType(damageType);
                Mage targetMage = controller.getRegisteredMage(targetEntity);
                String targetAnnotation = "";
                if (targetMage != null) {
                    targetMage.setLastDamageType(damageType);
                } else {
                    targetAnnotation = "*";
                }
                mage.sendDebugMessage(ChatColor.RED + "Damaging (" + ChatColor.DARK_RED + damageType + ChatColor.RED + ") x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + targetAnnotation + ": " + ChatColor.RED + damage, 5);
                // mages since other plugins may be tracking kills.
                if (mage.isPlayer() && controller.getDamageTypes().contains(damageType)) {
                    CompatibilityLib.getCompatibilityUtils().magicDamage(targetEntity, damage, mage.getEntity());
                } else {
                    CompatibilityLib.getCompatibilityUtils().damage(targetEntity, damage, mage.getEntity(), damageType);
                }
            } else if (magicDamage && (magicEntityDamage || targetEntity instanceof Player)) {
                mage.sendDebugMessage(ChatColor.RED + "Damaging (Magic) x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + ": " + damage, 5);
                CompatibilityLib.getCompatibilityUtils().magicDamage(targetEntity, damage, mage.getEntity());
            } else {
                mage.sendDebugMessage(ChatColor.RED + "Damaging x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + ": " + damage, 5);
                CompatibilityLib.getCompatibilityUtils().damage(targetEntity, damage, mage.getEntity());
            }
            if (damageType != null && !damageType.isEmpty()) {
                String typeDescription = context.getController().getMessages().get("damage_types." + damageType, damageType);
                context.addMessageParameter("damage_type", typeDescription);
            }
            if (setLastDamaged) {
                CompatibilityLib.getCompatibilityUtils().setLastDamaged(targetEntity, mage.getEntity());
            }
            if (setLastDamagedBy) {
                CompatibilityLib.getCompatibilityUtils().setLastDamagedBy(targetEntity, mage.getEntity());
            }
        }
    } finally {
        if (knockbackResistance != null && livingTarget != null) {
            AttributeInstance knockBackAttribute = livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
            knockBackAttribute.setBaseValue(previousKnockbackResistance);
        }
    }
    if (cancelOnKillTarget && targetEntity.isDead()) {
        return SpellResult.STOP;
    }
    return SpellResult.CAST;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) Damageable(org.bukkit.entity.Damageable) Mage(com.elmakers.mine.bukkit.api.magic.Mage) AttributeInstance(org.bukkit.attribute.AttributeInstance) Location(org.bukkit.Location) SourceLocation(com.elmakers.mine.bukkit.magic.SourceLocation)

Example 22 with AttributeInstance

use of org.bukkit.attribute.AttributeInstance in project MagicPlugin by elBukkit.

the class BaseMageModifier method activateAttributes.

protected void activateAttributes() {
    Collection<EntityAttributeModifier> modifiers = getAttributeModifiers();
    if (modifiers == null)
        return;
    LivingEntity entity = mage.getLivingEntity();
    if (entity == null)
        return;
    for (EntityAttributeModifier modifier : modifiers) {
        AttributeInstance attribute = entity.getAttribute(modifier.attribute);
        if (attribute == null)
            continue;
        if (modifier.modifier != null) {
            if (!checkedAttributes) {
                // Only do this once, it's really here to clean up attributes that may have gotten stuck on server crash
                Collection<AttributeModifier> existingModifiers = attribute.getModifiers();
                for (AttributeModifier existing : existingModifiers) {
                    if (existing.getName().equalsIgnoreCase(modifier.modifier.getName())) {
                        mage.getController().getLogger().warning("Removed duplicate attribute modifier " + modifier.modifier.getName() + ", was this leftover from a server crash?");
                        attribute.removeModifier(existing);
                        break;
                    }
                }
            }
            try {
                attribute.addModifier(modifier.modifier);
            } catch (Exception ex) {
                controller.getLogger().log(Level.WARNING, "Error adding vanilla attribute modifier: " + modifier.modifier.getName() + " from class/modifier " + getKey(), ex);
            }
        }
        if (modifier.base != null) {
            modifier.previous = attribute.getBaseValue();
            attribute.setBaseValue(modifier.base);
        }
    }
    checkedAttributes = true;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) AttributeInstance(org.bukkit.attribute.AttributeInstance) AttributeModifier(org.bukkit.attribute.AttributeModifier)

Example 23 with AttributeInstance

use of org.bukkit.attribute.AttributeInstance in project MagicPlugin by elBukkit.

the class Mage method getVanillaAttribute.

@Nullable
private Double getVanillaAttribute(Attribute attribute) {
    LivingEntity living = getLivingEntity();
    if (living == null) {
        return null;
    }
    AttributeInstance instance = living.getAttribute(attribute);
    return instance == null ? null : instance.getValue();
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) AttributeInstance(org.bukkit.attribute.AttributeInstance) Nullable(javax.annotation.Nullable)

Aggregations

AttributeInstance (org.bukkit.attribute.AttributeInstance)23 LivingEntity (org.bukkit.entity.LivingEntity)8 Attribute (org.bukkit.attribute.Attribute)5 AttributeModifier (org.bukkit.attribute.AttributeModifier)5 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 MapTag (com.denizenscript.denizencore.objects.core.MapTag)3 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)3 Attributable (org.bukkit.attribute.Attributable)3 Horse (org.bukkit.entity.Horse)3 Player (org.bukkit.entity.Player)3 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)2 ISoliniaLivingEntity (com.solinia.solinia.Interfaces.ISoliniaLivingEntity)2 ISoliniaPlayer (com.solinia.solinia.Interfaces.ISoliniaPlayer)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 DamageSource (net.minecraft.world.damagesource.DamageSource)2 net.minecraft.world.entity (net.minecraft.world.entity)2