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

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.block.MaterialBrush;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.block.UndoList;
import com.elmakers.mine.bukkit.spell.BaseSpell;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;

import java.util.Arrays;
import java.util.Collection;

public class BreakBlockAction extends BaseSpellAction {
    private int durabilityAmount;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters) {
        super.prepare(context, parameters);
        durabilityAmount = parameters.getInt("break_durability", 1);
    }

    @Override
    public SpellResult perform(CastContext context) {
        MaterialBrush brush = context.getBrush();
        if (brush == null) {
            return SpellResult.FAIL;
        }
        Block block = context.getTargetBlock();
        if (block.getType() == Material.AIR || !context.isDestructible(block)) {
            return SpellResult.NO_TARGET;
        }
        context.registerForUndo(block);
        double breakAmount = 1;
        double durability = CompatibilityUtils.getDurability(block.getType());
        if (durability > 0) {
            double breakPercentage = durabilityAmount / durability;
            breakAmount = context.registerBreaking(block, breakPercentage);
        }

        if (breakAmount > 1) {
            if (context.hasBreakPermission(block)) {
                CompatibilityUtils.setBreaking(block, 10, UndoList.BLOCK_BREAK_RANGE);
                block.setType(Material.AIR);
                context.unregisterBreaking(block);
                context.playEffects("break");
            }
        } else {
            int breakState = (int)Math.floor(9 * breakAmount);
            CompatibilityUtils.setBreaking(block, breakState, UndoList.BLOCK_BREAK_RANGE);
        }
        return SpellResult.CAST;
    }

    @Override
    public void getParameterNames(Spell spell, Collection<String> parameters) {
        super.getParameterNames(spell, parameters);
        parameters.add("durability");
    }

    @Override
    public void getParameterOptions(Spell spell, String parameterKey, Collection<String> examples) {
        if (parameterKey.equals("durability")) {
            examples.addAll(Arrays.asList((BaseSpell.EXAMPLE_SIZES)));
        } else {
            super.getParameterOptions(spell, parameterKey, examples);
        }
    }

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

    @Override
    public boolean isUndoable() {
        return true;
    }
}
@@ -473,16 +473,66 @@
 yUtils;%0A
+import com.elmakers.mine.bukkit.utility.NMSUtils;%0A
 import o
@@ -604,42 +604,123 @@
 kit.
-configuration.ConfigurationSection
+block.BlockState;%0Aimport org.bukkit.configuration.ConfigurationSection;%0Aimport org.bukkit.inventory.InventoryHolder
 ;%0A%0Ai
@@ -1952,32 +1952,307 @@
 K_BREAK_RANGE);%0A
+                BlockState blockState = block.getState();%0A                if (blockState != null && (blockState instanceof InventoryHolder %7C%7C blockState.getType() == Material.FLOWER_POT)) %7B%0A                    NMSUtils.clearItems(blockState.getLocation());%0A                %7D%0A
                 

Fix BreakBlock action not clearing chest items