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

import com.elmakers.mine.bukkit.api.action.CastContext;
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.action.BaseSpellAction;
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.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

public class PotionEffectAction extends BaseSpellAction
{
    private Set<PotionEffectType> removeEffects;
    private Collection<PotionEffect> addEffects;
    private Integer duration;

    @Override
    public void initialize(Spell spell, ConfigurationSection parameters)
    {
        super.initialize(spell, parameters);
        if (parameters.contains("remove_effects"))
        {
            removeEffects = new HashSet<PotionEffectType>();
            Collection<String> removeEffectKeys = parameters.getStringList("remove_effects");
            for (String removeKey : removeEffectKeys)
            {
                try {
                    PotionEffectType removeType = PotionEffectType.getByName(removeKey);
                    if (removeType != null) {
                        removeEffects.add(removeType);
                    }
                } catch (Exception ex) {
                    spell.getController().getLogger().log(Level.WARNING, "Invalid potion effect type: " + removeKey, ex);
                }
            }
        }
        else
        {
            removeEffects = null;
        }

        if (parameters.contains("duration"))
        {
            duration = parameters.getInt("duration");
        }
        else
        {
            duration = null;
        }
        addEffects = BaseSpell.getPotionEffects(parameters, duration);
    }

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        if (parameters.contains("duration"))
        {
            int durationOverride = parameters.getInt("duration");
            if (duration == null || durationOverride != duration)
            {
                addEffects = BaseSpell.getPotionEffects(parameters, durationOverride);
            }
        }
    }

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

        LivingEntity targetEntity = (LivingEntity)entity;
        context.registerPotionEffects(targetEntity);
        if (addEffects != null)
        {
            CompatibilityUtils.applyPotionEffects(targetEntity, addEffects);
        }

        if (removeEffects != null)
        {
            Collection<PotionEffect> currentEffects = targetEntity.getActivePotionEffects();
            for (PotionEffect effect : currentEffects)
            {
                PotionEffectType removeType = effect.getType();
                if (removeEffects.contains(removeType) && effect.getDuration() < Integer.MAX_VALUE / 4)
                {
                    targetEntity.removePotionEffect(removeType);
                }
            }
        }

		return SpellResult.CAST;
	}

    @Override
    public boolean isUndoable()
    {
        return true;
    }

    @Override
    public boolean requiresTargetEntity()
    {
        return true;
    }

    @Override
    public void getParameterNames(Spell spell, Collection<String> parameters) {
        super.getParameterNames(spell, parameters);
        parameters.add("duration");
        PotionEffectType[] effectTypes = PotionEffectType.values();
        for (PotionEffectType effectType : effectTypes) {
            if (effectType == null) continue;
            parameters.add("effect_" + effectType.getName().toLowerCase());<commit_msg>Cure only levels up if it cures something<commit_after>package com.elmakers.mine.bukkit.action.builtin;

import com.elmakers.mine.bukkit.api.action.CastContext;
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.action.BaseSpellAction;
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.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

public class PotionEffectAction extends BaseSpellAction
{
    private Set<PotionEffectType> removeEffects;
    private Collection<PotionEffect> addEffects;
    private Integer duration;

    @Override
    public void initialize(Spell spell, ConfigurationSection parameters)
    {
        super.initialize(spell, parameters);
        if (parameters.contains("remove_effects"))
        {
            removeEffects = new HashSet<PotionEffectType>();
            Collection<String> removeEffectKeys = parameters.getStringList("remove_effects");
            for (String removeKey : removeEffectKeys)
            {
                try {
                    PotionEffectType removeType = PotionEffectType.getByName(removeKey);
                    if (removeType != null) {
                        removeEffects.add(removeType);
                    }
                } catch (Exception ex) {
                    spell.getController().getLogger().log(Level.WARNING, "Invalid potion effect type: " + removeKey, ex);
                }
            }
        }
        else
        {
            removeEffects = null;
        }

        if (parameters.contains("duration"))
        {
            duration = parameters.getInt("duration");
        }
        else
        {
            duration = null;
        }
        addEffects = BaseSpell.getPotionEffects(parameters, duration);
    }

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        if (parameters.contains("duration"))
        {
            int durationOverride = parameters.getInt("duration");
            if (duration == null || durationOverride != duration)
            {
                addEffects = BaseSpell.getPotionEffects(parameters, durationOverride);
            }
        }
    }

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

        LivingEntity targetEntity = (LivingEntity)entity;
        context.registerPotionEffects(targetEntity);
        boolean effected = false;
        if (addEffects != null && addEffects.size() > 0)
        {
            effected = true;
            CompatibilityUtils.applyPotionEffects(targetEntity, addEffects);
        }

        if (removeEffects != null)
        {
            Collection<PotionEffect> currentEffects = targetEntity.getActivePotionEffects();
            for (PotionEffect effect : currentEffects)
            {
                PotionEffectType removeType = effect.getType();
                if (removeEffects.contains(removeType) && effect.getDuration() < Integer.MAX_VALUE / 4)
                {
                    targetEntity.removePotionEffect(removeType);
                    effected = true;
                }
            }
        }

		return effected ? SpellResult.CAST : SpellResult.NO_TARGET;
	}

    @Override
    public boolean isUndoable()
    {
        return true;
    }

    @Override
    public boolean requiresTargetEntity()
    {
        return true;
    }

    @Override
    public void getParameterNames(Spell spell, Collection<String> parameters) {
        super.getParameterNames(spell, parameters);
        parameters.add("duration");
        PotionEffectType[] effectTypes = PotionEffectType.values();
        for (PotionEffectType effectType : effectTypes) {
            if (effectType == null) continue;
            parameters.add("effect_" + effectType.getName().toLowerCase());
{'fraction_non_alphanumeric': 0.09243205248359887, 'fraction_numerical': 0.0003514526710402999, 'mean_word_length': 2.3743083003952568, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, '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': '3959388', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2182, 'n_words': 423}