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

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.spell.BaseSpell;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import com.elmakers.mine.bukkit.utility.NMSUtils;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.InventoryHolder;

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

public class BreakBlockAction extends ModifyBlockAction {
    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) {
        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.clearBreaking(block);
                BlockState blockState = block.getState();
                if (blockState != null && (blockState instanceof InventoryHolder || blockState.getType() == Material.FLOWER_POT)) {
                    NMSUtils.clearItems(blockState.getLocation());
                }
                MaterialBrush brush = context.getBrush();
                if (brush == null) {
                    brush = new com.elmakers.mine.bukkit.block.MaterialBrush(context.getMage(), Material.AIR, (byte)0);
                    context.setBrush(brush);
                } else {
                    brush.setMaterial(Material.AIR);
                }
                super.perform(context);
                context.unregisterBreaking(block);
                context.playEffects("break");
            }
        } else {
            CompatibilityUtils.setBreaking(block, breakAmount);
        }
        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 usesBrush() {
        return false;
    }
}
@@ -760,16 +760,55 @@
 yAmount;
+%0A    private double maxDistanceSquared;
 %0A%0A    @O
@@ -1008,16 +1008,150 @@
 y%22, 1);%0A
+        double maxDistance = parameters.getDouble(%22durability_max_distance%22);%0A        maxDistanceSquared = maxDistance * maxDistance;%0A
     %7D%0A%0A 
@@ -1430,32 +1430,690 @@
 ForUndo(block);%0A
+        double scaledAmount = durabilityAmount;%0A        if (maxDistanceSquared %3E 0) %7B%0A            double distanceSquared = context.getTargetCenterLocation().distanceSquared(block.getLocation());%0A            org.bukkit.Bukkit.getLogger().info(%22Distance%5E2: %22 + distanceSquared);%0A            if (distanceSquared %3E maxDistanceSquared) %7B%0A                return SpellResult.NO_TARGET;%0A            %7D%0A            if (distanceSquared %3E 0) %7B%0A                scaledAmount = scaledAmount * (1 - distanceSquared / maxDistanceSquared);%0A            %7D%0A            org.bukkit.Bukkit.getLogger().info(%22Break amount: %22 + scaledAmount + %22 from %22 + durabilityAmount);%0A        %7D%0A%0A
         double b
@@ -2274,26 +2274,22 @@
 ntage = 
-durability
+scaled
 Amount /
@@ -3622,24 +3622,75 @@
 rability%22);%0A
+        parameters.add(%22durability_max_distance%22);%0A
     %7D%0A%0A    @
@@ -3795,24 +3795,24 @@
 examples) %7B%0A
-
         if (
@@ -3844,16 +3844,66 @@
 bility%22)
+ %7C%7C parameterKey.equals(%22durability_max_distance%22)
 ) %7B%0A    

Add break power based on distance to BreakBlock action