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

import com.elmakers.mine.bukkit.action.CompoundAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.block.MaterialBrush;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;

import java.util.ArrayList;
import java.util.List;

public class BrushAction extends CompoundAction {
    private List<MaterialBrush> brushes = new ArrayList<MaterialBrush>();
    private boolean sample = false;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters) {
        super.prepare(context, parameters);
        brushes.clear();

        Mage mage = context.getMage();
        Location location = context.getLocation();
        String materialKey = parameters.getString("brush", null);
        if (materialKey != null) {
            addBrush(mage, location, materialKey);
        }
        List<String> brushList = parameters.getStringList("brushes");
        if (brushList != null) {
            for (String brushKey : brushList) {
                addBrush(mage, location, brushKey);
            }
        }
        sample = parameters.getBoolean("sample");
    }

    protected void addBrush(Mage mage, Location location, String brushKey) {
        brushes.add(new MaterialBrush(mage, location, brushKey));
    }

    @Override
    public SpellResult perform(CastContext context) {
        if (brushes.size() == 0 && !sample) {
            return super.perform(context);
        }
        createActionContext(context);
        if (sample)
        {
            Block targetBlock = context.getTargetBlock();
            if (targetBlock != null)
            {
                Mage mage = context.getMage();
                MaterialBrush brush = new MaterialBrush(mage, targetBlock);
                actionContext.setBrush(brush);
            }
        }
        else
        {
            MaterialBrush brush = brushes.get(context.getRandom().nextInt(brushes.size()));
            actionContext.setBrush(brush);
        }
        return super.perform(actionContext);
    }

    @Override
    public boolean usesBrush() {
        return false;
    }
}

@@ -635,16 +635,55 @@
 = false;
+%0A    private boolean performed = false;
 %0A%0A    @O
@@ -1518,32 +1518,155 @@
 shKey));%0A    %7D%0A%0A
+    @Override%0A    public void reset(CastContext context) %7B%0A        super.reset(context);%0A        performed = false;%0A    %7D%0A%0A
     @Override%0A  
@@ -1808,32 +1808,186 @@
 ext);%0A        %7D%0A
+%0A        // Don't re-assign the brush if we are re-running pending actions%0A        if (performed) %7B%0A            return super.perform(context);%0A        %7D%0A%0A
         createAc
@@ -2356,32 +2356,32 @@
  else%0A        %7B%0A
-
             Mate
@@ -2452,24 +2452,62 @@
 s.size()));%0A
+            brush.clearCloneTarget();%0A
             

Fix bugs on pending BrushAction repeats, and clear clone target