<commit_before>package com.elmakers.mine.bukkit.action.builtin;

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.BaseSpell;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.Collection;

public class DamageAction extends BaseSpellAction
{
    private double entityDamage;
    private double playerDamage;
    private double elementalDamage;
    private boolean magicDamage;
	private boolean magicEntityDamage;
	private Double percentage;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        double damage = parameters.getDouble("damage", 1);
        entityDamage = parameters.getDouble("entity_damage", damage);
        playerDamage = parameters.getDouble("player_damage", damage);
        elementalDamage = parameters.getDouble("elemental_damage", damage);
		if (parameters.contains("percentage")) {
			percentage = parameters.getDouble("percentage");
		} else {
			percentage = null;
		}
        magicDamage = parameters.getBoolean("magic_damage", true);
		magicEntityDamage = parameters.getBoolean("magic_entity_damage", false);
    }

	@Override
	public SpellResult perform(CastContext context)
	{
        Entity entity = context.getTargetEntity();
		if (entity == null || !(entity instanceof LivingEntity))
		{
			return SpellResult.NO_TARGET;
		}

		double damage = 1;

        LivingEntity targetEntity = (LivingEntity)entity;
        context.registerModified(targetEntity);
		Mage mage = context.getMage();
		MageController controller = context.getController();

		if (controller.isElemental(entity)) {
			damage = elementalDamage;
			controller.damageElemental(entity, damage * mage.getDamageMultiplier(), 0, mage.getCommandSender());
		} else {
			if (percentage != null) {
				damage = percentage * targetEntity.getMaxHealth();
			} else if (targetEntity instanceof Player) {
				damage = playerDamage;
			} else {
				damage = entityDamage;
			}
            damage *= mage.getDamageMultiplier();
            if (magicDamage && (magicEntityDamage || targetEntity instanceof Player)) {
                CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
            } else {
                targetEntity.damage(damage, mage.getEntity());
            }
		}

		return SpellResult.CAST;
	}

	@Override
	public boolean isUndoable()
	{<commit_msg>Damage action routes through CompatibilityUtils.damage for non-magic damage<commit_after>package com.elmakers.mine.bukkit.action.builtin;

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.BaseSpell;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.Collection;

public class DamageAction extends BaseSpellAction
{
    private double entityDamage;
    private double playerDamage;
    private double elementalDamage;
    private boolean magicDamage;
	private boolean magicEntityDamage;
	private Double percentage;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        double damage = parameters.getDouble("damage", 1);
        entityDamage = parameters.getDouble("entity_damage", damage);
        playerDamage = parameters.getDouble("player_damage", damage);
        elementalDamage = parameters.getDouble("elemental_damage", damage);
		if (parameters.contains("percentage")) {
			percentage = parameters.getDouble("percentage");
		} else {
			percentage = null;
		}
        magicDamage = parameters.getBoolean("magic_damage", true);
		magicEntityDamage = parameters.getBoolean("magic_entity_damage", false);
    }

	@Override
	public SpellResult perform(CastContext context)
	{
        Entity entity = context.getTargetEntity();
		if (entity == null || !(entity instanceof LivingEntity))
		{
			return SpellResult.NO_TARGET;
		}

		double damage = 1;

        LivingEntity targetEntity = (LivingEntity)entity;
        context.registerModified(targetEntity);
		Mage mage = context.getMage();
		MageController controller = context.getController();

		if (controller.isElemental(entity)) {
			damage = elementalDamage;
			controller.damageElemental(entity, damage * mage.getDamageMultiplier(), 0, mage.getCommandSender());
		} else {
			if (percentage != null) {
				damage = percentage * targetEntity.getMaxHealth();
			} else if (targetEntity instanceof Player) {
				damage = playerDamage;
			} else {
				damage = entityDamage;
			}
            damage *= mage.getDamageMultiplier();
            if (magicDamage && (magicEntityDamage || targetEntity instanceof Player)) {
                CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
            } else {
				CompatibilityUtils.damage(targetEntity, damage, mage.getEntity());
            }
		}

		return SpellResult.CAST;
	}

	@Override
	public boolean isUndoable()
	{
{'fraction_non_alphanumeric': 0.10327756913622396, 'fraction_numerical': 0.0010242403550699897, 'mean_word_length': 4.108108108108108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836744', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1697, 'n_words': 334}