package de.craften.plugins.educraft.luaapi.functions;

import de.craften.plugins.educraft.environment.LivingArmorStandBehavior;
import de.craften.plugins.educraft.luaapi.EduCraftApiFunction;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;

/**
 * Lua API function to destroy the block in front of the entity.
 */
public class DestroyBlockFunction extends EduCraftApiFunction {
    @Override
    protected void beforeExecute(Varargs varargs) {
        LivingArmorStandBehavior armorStand = (LivingArmorStandBehavior) getApi().getEntity().getBehaviors(LivingArmorStandBehavior.class).iterator().next();
        armorStand.setItemInHand(new ItemStack(Material.DIAMOND_PICKAXE));
    }

    @Override
    public Varargs execute(Varargs varargs) {
        if (getApi().getEnvironment().contains(getApi().getBlockAhead().getLocation())) {
            getApi().getBlockAhead().setType(Material.AIR);
        }
        return LuaValue.NIL;
    }

    @Override
    protected void afterExecute(Varargs varargs) {
        LivingArmorStandBehavior armorStand = (LivingArmorStandBehavior) getApi().getEntity().getBehaviors(LivingArmorStandBehavior.class).iterator().next();
        armorStand.setItemInHand(null);
    }
}

@@ -234,27 +234,123 @@
 kit.
-inventory.ItemStack
+block.Block;%0Aimport org.bukkit.block.BlockFace;%0Aimport org.bukkit.inventory.ItemStack;%0Aimport org.luaj.vm2.LuaError
 ;%0Aim
@@ -923,158 +923,1307 @@
    i
-f (getApi().getEnvironment().contains(getApi().getBlockAhead().getLocation())) %7B%0A            getApi().getBlockAhead().setType(Material.AIR);%0A        %7D
+nt forwardBackward = varargs.optint(1, 1);%0A        if (forwardBackward %3C 0 %7C%7C forwardBackward %3E 1) %7B%0A            throw new LuaError(%22forward/backward offset must be either 0 or 1%22);%0A        %7D%0A        int upDown = varargs.optint(2, 0);%0A        if (upDown %3C -1 %7C%7C upDown %3E 2) %7B%0A            throw new LuaError(%22up/down offset must be between -1 and 2%22);%0A        %7D%0A%0A        Block block = getApi().getLocation().clone()%0A                .add(getApi().getDirection().clone().normalize().multiply(forwardBackward))%0A                .add(0, upDown, 0)%0A                .getBlock();%0A%0A        if (getApi().getEnvironment().contains(block.getLocation())) %7B%0A            block.setType(Material.AIR);%0A%0A            if (forwardBackward == 0 && upDown == -1) %7B%0A                //block below was destroyed =%3E fall on solid block (without falling out of the environment)%0A                Block targetBlock = block.getRelative(BlockFace.DOWN);%0A                while (!targetBlock.getRelative(BlockFace.DOWN).getType().isSolid()%0A                        && getApi().getEnvironment().contains(targetBlock.getLocation().subtract(0, -1, 0))) %7B%0A                    targetBlock = targetBlock.getRelative(BlockFace.DOWN);%0A                %7D%0A                getApi().moveTo(targetBlock.getLocation(), false);%0A            %7D%0A        %7D%0A
 %0A   

Add two optional parameters to destroy blocks ahead of, below or above the bot. Resolves #7.