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;
}
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;
}
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();
}
Aggregations