/*
 * Copyright  2014 - 2017 | Wurst-Imperium | All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package tk.wurst_client.features.mods;

import net.minecraft.block.Block;
import net.minecraft.block.BlockCocoa;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockGrass;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.BlockStem;
import net.minecraft.block.IGrowable;
import net.minecraft.item.ItemDye;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import tk.wurst_client.events.listeners.UpdateListener;
import tk.wurst_client.features.special_features.YesCheatSpf.BypassLevel;
import tk.wurst_client.settings.CheckboxSetting;
import tk.wurst_client.settings.SliderSetting;
import tk.wurst_client.settings.SliderSetting.ValueDisplay;
import tk.wurst_client.utils.BlockUtils;
import tk.wurst_client.utils.InventoryUtils;

@Mod.Info(
	description = "Automatically uses bone meal on specific types of plants.\n"
		+ "Use the checkboxes to specify the types of plants.",
	name = "BonemealAura",
	tags = "bonemeal aura, bone meal aura, AutoBone, auto bone",
	help = "Mods/BonemealAura")
@Mod.Bypasses(ghostMode = false)
public class BonemealAuraMod extends Mod implements UpdateListener
{
	public final SliderSetting range =
		new SliderSetting("Range", 4.25, 1, 6, 0.05, ValueDisplay.DECIMAL);
	
	private final CheckboxSetting saplings =
		new CheckboxSetting("Saplings", true);
	private final CheckboxSetting crops = new CheckboxSetting("Crops", true);
	private final CheckboxSetting stems = new CheckboxSetting("Stems", true);
	private final CheckboxSetting cocoa = new CheckboxSetting("Cocoa", true);
	private final CheckboxSetting other = new CheckboxSetting("Other", false);
	
	@Override
	public void initSettings()
	{
		settings.add(range);
		settings.add(saplings);
		settings.add(crops);
		settings.add(stems);
		settings.add(cocoa);
		settings.add(other);
	}
	
	@Override
	public void onEnable()
	{
		wurst.events.add(UpdateListener.class, this);
	}
	
	@Override
	public void onDisable()
	{
		wurst.events.remove(UpdateListener.class, this);
	}
	
	@Override
	public void onUpdate()
	{
		// wait for right click timer
		if(mc.rightClickDelayTimer > 0)
			return;
		
		// check held item
		ItemStack stack = mc.player.inventory.getCurrentItem();
		if(InventoryUtils.isEmptySlot(stack)
			|| !(stack.getItem() instanceof ItemDye)
			|| stack.getMetadata() != 15)
			return;
		
		// use bone meal
		BlockPos playerPos = new BlockPos(mc.player);
		for(int y = -range.getValueI() + 1; y < range.getValueI() + 2; y++)
			for(int x = -range.getValueI(); x < range.getValueI() + 1; x++)
				for(int z = -range.getValueI(); z < range.getValueI() + 1; z++)
				{
					BlockPos pos = playerPos.add(x, y, z);
					
					// check block type
					if(!isCorrectBlock(pos))
						continue;
					
					// right click block
					if(!BlockUtils.rightClickBlockLegit(pos))
						continue;
					
					return;
				}
	}
	
	@Override
	public void onYesCheatUpdate(BypassLevel bypassLevel)
	{
		switch(bypassLevel)
		{
			default:
			case OFF:
			case MINEPLEX:
				range.unlock();
				break;
			case ANTICHEAT:
			case OLDER_NCP:
			case LATEST_NCP:
			case GHOST_MODE:
				range.lockToMax(4.25);
				break;
		}
	}
	
	private boolean isCorrectBlock(BlockPos pos)
	{
		Block block = BlockUtils.getBlock(pos);
		
		if(!(block instanceof IGrowable) || block instanceof BlockGrass
			|| !((IGrowable)block).canGrow(mc.world, pos,
				BlockUtils.getState(pos), false))
			return false;
		
		if(block instanceof BlockSapling)
			return saplings.isChecked();
		else if(block instanceof BlockCrops)
			return crops.isChecked();
		else if(block instanceof BlockStem)
			return stems.isChecked();
		else if(block instanceof BlockCocoa)
			return cocoa.isChecked();
		else
			return other.isChecked();
	}
}

@@ -2629,250 +2629,198 @@
 %09%0A%09%09
-// use bone meal%0A%09%09BlockPos playerPos = new BlockPos(mc.player);%0A%09%09for(int y = -range.getValueI() + 1; y %3C range.getValueI() + 2; y++
+boolean legit = wurst.special.yesCheatSpf.getBypassLevel(
 )%0A%09%09%09
-for(int x = -range.getValueI(); x %3C range.getValueI() + 1; x++)
+.ordinal() %3E BypassLevel.MINEPLEX.ordinal();
 %0A%09%09
+%0A
 %09%09
-for(int z = -range.getValueI(); z %3C 
+// find closest valid block%0A%09%09BlockPos pos = BlockUtils.findClosestValidBlock(
 rang
@@ -2833,81 +2833,53 @@
 alue
-I
 ()
- + 1; z++)%0A%09%09%09%09%7B%0A%09%09%09%09%09BlockPos pos = playerPos.add(x, y, z);%0A%09%09%09
+,%0A%09%09%09!legit, (p) -%3E isCorrectBlock(p));%0A
 %09%09%0A
-%09%09%09
 %09%09//
@@ -2889,184 +2889,148 @@
 eck 
-block type%0A%09%09%09%09%09if(!isCorrectBlock(pos))%0A%09%09%09%09%09%09continue
+if any block was found%0A%09%09if(pos == null)%0A%09%09%09return
 ;%0A%09%09%09
+%0A
 %09%09
-%0A%09%09%09%09%09// right click block%0A%09%09%09%09%09if(!BlockUtils.rightClickBlockLegit(pos))%0A%09%09%09%09%09%09continue;%0A%09%09%09%09%09%0A%09%09%09%09%09return;%0A%09%09%09%09%7D
+// use bone meal%0A%09%09// TODO: non-legit mode%0A%09%09BlockUtils.rightClickBlockLegit(pos);
 %0A%09%7D%0A

Use BlockUtils.findClosestValidBlock() in BonemealAura