<commit_before>package net.glowstone.util;

import net.glowstone.EventFactory;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

/**
 * Utility methods for dealing with entities.
 */
public class EntityUtils {
    /**
     * Heals an entity by a specific amount.
     *
     * @param target the entity to heal
     * @param amount the amount of health to regain
     * @param reason the reason supplied to the {@link EntityRegainHealthEvent}
     */
    public static void heal(LivingEntity target, double amount,
            EntityRegainHealthEvent.RegainReason reason) {
        if (target.isDead()) {
            return; // too late!
        }
        final double maxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
        final double currentHealth = target.getHealth();
        if (currentHealth >= maxHealth) {
            return;
        }
        EntityRegainHealthEvent event = new EntityRegainHealthEvent(target, amount, reason);
        EventFactory.getInstance().callEvent(event);
        if (!event.isCancelled()) {
            target.setHealth(Math.min(maxHealth, currentHealth + event.getAmount()));
        }
    }

    /**
     * Applies a potion effect with an intensity ranging from 0.0 for no effect to 1.0 for full
     * effect. Intensity above 1.0 have no additional effect, with the exceptions of
     * {@link PotionEffectType#HEAL} and {@link PotionEffectType#HARM}; the same applies to
     * negative intensities.
     *
     * @param effect the effect
     * @param target the target to apply the effect to
     * @param instantIntensity the intensity multiplier if the effect is instantaneous
     * @param durationIntensity the duration multiplier if the effect has a duration
     */
    public static void applyPotionEffectWithIntensity(
            PotionEffect effect, LivingEntity target, double instantIntensity,
            double durationIntensity) {
        PotionEffectType type = effect.getType();
        final int baseAmplifier = effect.getAmplifier();
        if (type.equals(PotionEffectType.HEAL)) {
            heal(target, (2 << baseAmplifier) * instantIntensity,
                    EntityRegainHealthEvent.RegainReason.MAGIC);
        } else if (type.equals(PotionEffectType.HARM)) {
            target.damage((3 << baseAmplifier) * instantIntensity,
                    EntityDamageEvent.DamageCause.MAGIC);
        } else if (type.isInstant()) {
            if (instantIntensity <= 0.0) {<commit_msg>Short circuit zero/neg intensity for HEAL & HARM<commit_after>package net.glowstone.util;

import net.glowstone.EventFactory;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

/**
 * Utility methods for dealing with entities.
 */
public class EntityUtils {
    /**
     * Heals an entity by a specific amount.
     *
     * @param target the entity to heal
     * @param amount the amount of health to regain
     * @param reason the reason supplied to the {@link EntityRegainHealthEvent}
     */
    public static void heal(LivingEntity target, double amount,
            EntityRegainHealthEvent.RegainReason reason) {
        if (target.isDead()) {
            return; // too late!
        }
        final double maxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
        final double currentHealth = target.getHealth();
        if (currentHealth >= maxHealth) {
            return;
        }
        EntityRegainHealthEvent event = new EntityRegainHealthEvent(target, amount, reason);
        EventFactory.getInstance().callEvent(event);
        if (!event.isCancelled()) {
            target.setHealth(Math.min(maxHealth, currentHealth + event.getAmount()));
        }
    }

    /**
     * Applies a potion effect with an intensity ranging from 0.0 for no effect to 1.0 for full
     * effect. Intensity above 1.0 has no additional effect, with the exceptions of
     * {@link PotionEffectType#HEAL} and {@link PotionEffectType#HARM}, and negative intensity
     * has no effect.
     *
     * @param effect the effect
     * @param target the target to apply the effect to
     * @param instantIntensity the intensity multiplier if the effect is instantaneous
     * @param durationIntensity the duration multiplier if the effect has a duration
     */
    public static void applyPotionEffectWithIntensity(
            PotionEffect effect, LivingEntity target, double instantIntensity,
            double durationIntensity) {
        PotionEffectType type = effect.getType();
        final int baseAmplifier = effect.getAmplifier();
        if (type.equals(PotionEffectType.HEAL)) {
            if (instantIntensity > 0.0) {
                heal(target, (2 << baseAmplifier) * instantIntensity,
                        EntityRegainHealthEvent.RegainReason.MAGIC);
            }
        } else if (type.equals(PotionEffectType.HARM)) {
            if (instantIntensity > 0.0) {
                target.damage((3 << baseAmplifier) * instantIntensity,
                        EntityDamageEvent.DamageCause.MAGIC);
            }
        } else if (type.isInstant()) {
            if (instantIntensity <= 0.0) {
{'fraction_non_alphanumeric': 0.0865488640461594, 'fraction_numerical': 0.00432744320230797, 'mean_word_length': 2.7709041468388853, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '7583201', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1467, 'n_words': 424}