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

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

/**
 * Lua API function to move one block forward.
 */
public class MoveForwardFunction extends EduCraftApiFunction {
    @Override
    public Varargs execute(Varargs varargs) {
        Location targetLocation = getApi().getStationaryBehavior().getLocation().clone().add(getApi().getDirection());

        if (!targetLocation.getBlock().getType().isSolid()
                && !targetLocation.getBlock().getRelative(BlockFace.UP).getType().isSolid()) {
            getApi().getStationaryBehavior().setLocation(targetLocation);
        }

        return LuaValue.NIL;
    }
}

@@ -174,16 +174,49 @@
 ckFace;%0A
+import org.bukkit.entity.Entity;%0A
 import o
@@ -267,16 +267,46 @@
 rargs;%0A%0A
+import java.util.Collection;%0A%0A
 /**%0A * L
@@ -593,16 +593,174 @@
 tion());
+%0A        Collection%3CEntity%3E entities = targetLocation.getWorld().getNearbyEntities(targetLocation.getBlock().getLocation().add(0.5, 0.5, 0.5), 0.5, 0.5, 0.5);
 %0A%0A      
@@ -903,16 +903,54 @@
 sSolid()
+%0A                && entities.isEmpty()
 ) %7B%0A    

Check for entities ahead before moving forward.