package com.forgeessentials.commands.world;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.permission.PermissionLevel;

import com.forgeessentials.commands.util.FEcmdModuleCommands;
import com.forgeessentials.commands.util.TickTaskBlockFinder;
import com.forgeessentials.core.misc.FECommandManager.ConfigurableCommand;
import com.forgeessentials.core.misc.TranslatedCommandException;

import cpw.mods.fml.common.registry.GameData;

public class CommandFindblock extends FEcmdModuleCommands implements ConfigurableCommand
{

    public static final int defaultCount = 1;
    public static int defaultRange = 20 * 16;
    public static int defaultSpeed = 16 * 16;

    @Override
    public void loadConfig(Configuration config, String category)
    {
        defaultRange = config.get(category, "defaultRange", defaultRange, "Default max distance used.").getInt();
        defaultSpeed = config.get(category, "defaultSpeed", defaultSpeed, "Default speed used.").getInt();
    }

    @Override
    public String[] getDefaultAliases()
    {
        return new String[] { "fb" };
    }

    @Override
    public String getCommandName()
    {
        return "findblock";
    }

    /*
     * syntax: /fb <block> [max distance, def = 20 * 16] [amount of blocks, def = 1] [speed, def = 10]
     */
    @Override
    public void processCommandPlayer(EntityPlayerMP sender, String[] args)
    {
        if (args.length < 2)
        {
            throw new TranslatedCommandException(getCommandUsage(sender));
        }
        String id = args[0];
        int meta = parseInt(sender, args[1]);
        int range = (args.length < 2) ? defaultRange : parseIntWithMin(sender, args[2], 1);
        int amount = (args.length < 3) ? defaultCount : parseIntWithMin(sender, args[3], 1);
        int speed = (args.length < 4) ? defaultSpeed : parseIntWithMin(sender, args[4], 1);

        new TickTaskBlockFinder(sender, id, meta, range, amount, speed);
    }

    @Override
    public boolean canConsoleUseCommand()
    {
        return false;
    }

    @Override
    public List<String> addTabCompletionOptions(ICommandSender sender, String[] args)
    {
        if (args.length == 1)
        {
            List<String> names = new ArrayList<String>();
            for (Item i : GameData.getItemRegistry().typeSafeIterable())
            {
                names.add(i.getUnlocalizedName());
            }
            return getListOfStringsMatchingLastWord(args, names);
        }
        else if (args.length == 2)
        {
            return getListOfStringsMatchingLastWord(args, defaultRange + "");
        }
        else if (args.length == 3)
        {
            return getListOfStringsMatchingLastWord(args, defaultCount + "");
        }
        else if (args.length == 4)
        {
            return getListOfStringsMatchingLastWord(args, defaultSpeed + "");
        }
        else
        {
            throw new TranslatedCommandException(getCommandUsage(sender));
        }
    }

    @Override
    public PermissionLevel getPermissionLevel()
    {
        return PermissionLevel.OP;
    }

    @Override
    public String getCommandUsage(ICommandSender sender)
    {

        return "/fb <block> [max distance] [amount of blocks] [speed] Finds a block.";
    }
}
@@ -1619,17 +1619,17 @@
 ength %3C 
-2
+1
 )%0A      
@@ -1750,24 +1750,26 @@
 0%5D;%0A        
+//
 int meta = p
@@ -1794,16 +1794,96 @@
 gs%5B1%5D);%0A
+        int meta = (args.length %3C 2) ? 0 : parseIntWithMin(sender, args%5B1%5D, 0);%0A
         
@@ -1905,25 +1905,25 @@
 gs.length %3C 
-2
+3
 ) ? defaultR
@@ -1994,33 +1994,33 @@
  (args.length %3C 
-3
+4
 ) ? defaultCount
@@ -2086,33 +2086,33 @@
  (args.length %3C 
-4
+5
 ) ? defaultSpeed
@@ -3476,32 +3476,39 @@
 urn %22/fb %3Cblock%3E
+ %5Bmeta%5D
  %5Bmax distance%5D 

Fix #1702