package com.elmakers.mine.bukkit.action.builtin;

import org.bukkit.configuration.ConfigurationSection;

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;

public class ModifyVariableAction extends BaseSpellAction {
    private String key;
    private double value;
    private boolean clear;

    private void checkDefaults(ConfigurationSection variables, ConfigurationSection parameters) {
        if (!variables.contains(key)) {
            double defaultValue = parameters.getDouble("default", 0);
            variables.set(key, defaultValue);
        }
    }

    @Override
    public void initialize(Spell spell, ConfigurationSection parameters) {
        key = parameters.getString("variable", "");
        checkDefaults(spell.getVariables(), parameters);
    }

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters) {
        key = parameters.getString("variable", "");
        checkDefaults(context.getVariables(), parameters);
        clear = parameters.getBoolean("clear");
        value = parameters.getDouble("value", 0);
    }

    @Override
    public SpellResult perform(CastContext context) {
        ConfigurationSection variables = context.getVariables();
        if (clear) {
            if (!variables.contains(key)) {
                return SpellResult.NO_TARGET;
            }
            variables.set(key, null);
        } else {
            if (variables.contains(key) && variables.getDouble(key) == value) {
                return SpellResult.NO_TARGET;
            }
            variables.set(key, value);
        }
        context.getSpell().reloadParameters();
        return SpellResult.CAST;
    }
}

@@ -399,16 +399,46 @@
 ng key;%0A
+    private boolean hasValue;%0A
     priv
@@ -1208,24 +1208,172 @@
 n(%22clear%22);%0A
+        String testValue = parameters.getString(%22value%22);%0A        hasValue = testValue != null && !testValue.isEmpty();%0A        if (hasValue) %7B%0A    
         valu
@@ -1406,24 +1406,34 @@
 value%22, 0);%0A
+        %7D%0A
     %7D%0A%0A    @
@@ -1491,24 +1491,101 @@
  context) %7B%0A
+        if (!hasValue) %7B%0A            return SpellResult.NO_ACTION;%0A        %7D%0A
         Conf

Allow using ModifyVariable just for creating a variable without modifying it