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

import de.craften.plugins.educraft.luaapi.EduCraftApiFunction;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;

/**
 * Lua API function that checks the block ahead of the entity.
 * <p/>
 * The function takes two arguments: a string for the block
 * to check for ("", "lava" or "water") and a callback that is invoked if the check is positive.
 */
public class IfBlockAheadFunction extends EduCraftApiFunction {
    @Override
    public Varargs execute(Varargs varargs) {
        Block blockAhead = getApi().getBlockAhead();
        boolean isAhead;

        switch (varargs.checkjstring(1)) {
            case "":
                isAhead = blockAhead.isEmpty();
                break;
            case "lava":
                blockAhead = blockAhead.getRelative(BlockFace.DOWN);
                isAhead = blockAhead.getType() == Material.LAVA || blockAhead.getType() == Material.STATIONARY_LAVA;
                break;
            case "water":
                blockAhead = blockAhead.getRelative(BlockFace.DOWN);
                isAhead = blockAhead.getType() == Material.WATER || blockAhead.getType() == Material.STATIONARY_WATER;
                break;
            default:
                throw new LuaError("Unsupported block type, must be '', 'lava' or 'water'.");
        }

        if (isAhead) {
            varargs.checkfunction(2).invoke();
        }

        return LuaValue.NIL;
    }
}

@@ -734,16 +734,126 @@
 Ahead;%0A%0A
+        if (varargs.narg() == 0) %7B%0A            isAhead = blockAhead.getType().isSolid();%0A        %7D else %7B%0A    
         
@@ -903,17 +903,53 @@
     
+    
 case %22
+air
 %22:%0A
+                case %22%22:%0A    
     
@@ -1000,32 +1000,36 @@
                 
+    
 break;%0A         
@@ -1027,24 +1027,28 @@
             
+   
+ 
 case %22lava%22:
@@ -1048,16 +1048,20 @@
 %22lava%22:%0A
+    
         
@@ -1113,32 +1113,36 @@
 lockFace.DOWN);%0A
+    
                 
@@ -1242,16 +1242,20 @@
 Y_LAVA;%0A
+    
         
@@ -1277,24 +1277,28 @@
             
+    
 case %22water%22
@@ -1295,24 +1295,28 @@
 se %22water%22:%0A
+    
             
@@ -1364,32 +1364,36 @@
 lockFace.DOWN);%0A
+    
                 
@@ -1503,39 +1503,47 @@
                 
+ 
+   
 break;%0A
+    
             defa
@@ -1547,16 +1547,20 @@
 efault:%0A
+    
         
@@ -1645,16 +1645,30 @@
 er'.%22);%0A
+            %7D%0A
         
@@ -1736,24 +1736,24 @@
 ).invoke();%0A
+
         %7D%0A%0A 
@@ -1742,33 +1742,32 @@
 ke();%0A        %7D%0A
-%0A
         return L

Check for non-solid blocks when no arguments are given to ifBlockAhead. Resolves #6.