<commit_before>package com.elmakers.mine.bukkit.configuration;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.configuration.ConfigurationSection;

import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.VariableScope;
import com.elmakers.mine.bukkit.api.spell.MageSpell;

public class SpellParameters extends MageParameters {
    private @Nullable ConfigurationSection castVariables;
    private @Nonnull ConfigurationSection spellVariables;
    private @Nullable ConfigurationSection mageVariables;
    private final @Nonnull Set<String> allParameters = new HashSet<>();
    private final @Nonnull MageSpell spell;
    private @Nullable CastContext context;

    public SpellParameters(@Nonnull MageSpell spell, @Nullable ConfigurationSection mageVariables, @Nullable ConfigurationSection variables) {
        super(spell.getMage(), "Spell: " + spell.getKey());
        this.spell = spell;
        this.spellVariables = spell.getVariables();
        this.mageVariables = mageVariables;
        Set<String> superParameters = super.getParameters();
        if (superParameters != null) {
            this.allParameters.addAll(superParameters);
        }
        // Only do this one, parameters don't change
        ConfigurationSection spellParameters = spell.getWorkingParameters();
        if (spellParameters != null) {
            this.allParameters.addAll(spellParameters.getKeys(false));
        }
        this.castVariables = variables;
    }

    public SpellParameters(@Nonnull MageSpell spell, @Nullable ConfigurationSection variables) {
        this(spell, spell.getMage() != null ? spell.getMage().getVariables() : null, variables);
    }

    public SpellParameters(@Nonnull MageSpell spell, @Nonnull CastContext context) {
        this(spell, context.getVariables());
        this.context = context;
    }

    public SpellParameters(@Nonnull MageSpell spell, @Nonnull CastContext context, ConfigurationSection config) {
        this(spell, context);
        wrap(config);
    }

    public SpellParameters(SpellParameters copy) {
        super(copy);
        this.spell = copy.spell;
        this.castVariables = copy.castVariables;
        this.spellVariables = copy.spellVariables;
        this.mageVariables = copy.mageVariables;
        this.allParameters.addAll(copy.allParameters);
        this.context = copy.context;
    }

    @Override
    public double getParameter(String parameter) {
        if (castVariables != null && castVariables.contains(parameter)) {
            return castVariables.getDouble(parameter);
        }
        if (spellVariables.contains(parameter)) {
            return spellVariables.getDouble(parameter);
        }
        if (mageVariables != null && mageVariables.contains(parameter)) {
            return mageVariables.getDouble(parameter);
        }
        Double value = context != null ? context.getAttribute(parameter) : spell.getAttribute(parameter);
        if (value == null) {
            ConfigurationSection spellParameters = spell.getWorkingParameters();
            if (spellParameters != null && spellParameters.contains(parameter)) {
                return spellParameters.getDouble(parameter);
            }
        }
        return value == null || Double.isNaN(value) || Double.isInfinite(value) ? 0 : value;
    }

    @Override
    public Set<String> getParameters() {
        if (castVariables != null) {
            this.allParameters.addAll(castVariables.getKeys(false));
        }
        this.allParameters.addAll(spellVariables.getKeys(false));
        if (mageVariables != null) {
            this.allParameters.addAll(mageVariables.getKeys(false));
        }
        return allParameters;
    }

    public void setMageVariables(@Nonnull ConfigurationSection variables) {
        this.mageVariables = checkNotNull(variables, "variables");
    }

    public void setSpellVariables(@Nonnull ConfigurationSection variables) {
        this.spellVariables = checkNotNull(variables, "variables");
    }

    @Nullable
    public ConfigurationSection getVariables(VariableScope scope) {
        switch (scope) {
            case CAST:
                return castVariables;
            case SPELL:<commit_msg>Fix validation errors from parameters on BaseSpell not getting updated after parameters have loaded<commit_after>package com.elmakers.mine.bukkit.configuration;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.configuration.ConfigurationSection;

import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.VariableScope;
import com.elmakers.mine.bukkit.api.spell.MageSpell;

public class SpellParameters extends MageParameters {
    private @Nullable ConfigurationSection castVariables;
    private @Nonnull ConfigurationSection spellVariables;
    private @Nullable ConfigurationSection mageVariables;
    private final @Nonnull Set<String> allParameters = new HashSet<>();
    private final @Nonnull MageSpell spell;
    private @Nullable CastContext context;

    public SpellParameters(@Nonnull MageSpell spell, @Nullable ConfigurationSection mageVariables, @Nullable ConfigurationSection variables) {
        super(spell.getMage(), "Spell: " + spell.getKey());
        this.spell = spell;
        this.spellVariables = spell.getVariables();
        this.mageVariables = mageVariables;
        Set<String> superParameters = super.getParameters();
        if (superParameters != null) {
            this.allParameters.addAll(superParameters);
        }
        this.castVariables = variables;
    }

    public SpellParameters(@Nonnull MageSpell spell, @Nullable ConfigurationSection variables) {
        this(spell, spell.getMage() != null ? spell.getMage().getVariables() : null, variables);
    }

    public SpellParameters(@Nonnull MageSpell spell, @Nonnull CastContext context) {
        this(spell, context.getVariables());
        this.context = context;
    }

    public SpellParameters(@Nonnull MageSpell spell, @Nonnull CastContext context, ConfigurationSection config) {
        this(spell, context);
        wrap(config);
    }

    public SpellParameters(SpellParameters copy) {
        super(copy);
        this.spell = copy.spell;
        this.castVariables = copy.castVariables;
        this.spellVariables = copy.spellVariables;
        this.mageVariables = copy.mageVariables;
        this.allParameters.addAll(copy.allParameters);
        this.context = copy.context;
    }

    @Override
    public double getParameter(String parameter) {
        if (castVariables != null && castVariables.contains(parameter)) {
            return castVariables.getDouble(parameter);
        }
        if (spellVariables.contains(parameter)) {
            return spellVariables.getDouble(parameter);
        }
        if (mageVariables != null && mageVariables.contains(parameter)) {
            return mageVariables.getDouble(parameter);
        }
        Double value = context != null ? context.getAttribute(parameter) : spell.getAttribute(parameter);
        if (value == null) {
            ConfigurationSection spellParameters = spell.getWorkingParameters();
            if (spellParameters != null && spellParameters.contains(parameter)) {
                return spellParameters.getDouble(parameter);
            }
        }
        return value == null || Double.isNaN(value) || Double.isInfinite(value) ? 0 : value;
    }

    @Override
    public Set<String> getParameters() {
        if (castVariables != null) {
            this.allParameters.addAll(castVariables.getKeys(false));
        }
        this.allParameters.addAll(spellVariables.getKeys(false));
        if (mageVariables != null) {
            this.allParameters.addAll(mageVariables.getKeys(false));
        }
        ConfigurationSection spellParameters = spell.getWorkingParameters();
        if (spellParameters != null) {
            this.allParameters.addAll(spellParameters.getKeys(false));
        }
        return allParameters;
    }

    public void setMageVariables(@Nonnull ConfigurationSection variables) {
        this.mageVariables = checkNotNull(variables, "variables");
    }

    public void setSpellVariables(@Nonnull ConfigurationSection variables) {
        this.spellVariables = checkNotNull(variables, "variables");
    }

    @Nullable
    public ConfigurationSection getVariables(VariableScope scope) {
        switch (scope) {
            case CAST:
                return castVariables;
            case SPELL:
{'fraction_non_alphanumeric': 0.09486975315663747, 'fraction_numerical': 0.00022750540325332727, 'mean_word_length': 3.1550094517958414, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7153660', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2199, 'n_words': 479}