package com.choonster.testmod3.item;

import com.choonster.testmod3.TestMod3;
import com.google.common.collect.ImmutableSet;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.util.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.ForgeHooks;

import java.util.Collections;
import java.util.Set;

/**
 * A tool that can function as a sword, pickaxe, axe or shovel.
 * <p>
 * Test for this thread:
 * http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/modification-development/2550421-how-to-make-a-tool-e-g-a-sword-have-the-abilities
 */
public class ItemHarvestSword extends ItemTool {
	public ItemHarvestSword(ToolMaterial toolMaterial) {
		super(4.0f, toolMaterial, Collections.EMPTY_SET);
		setHarvestLevel("pickaxe", toolMaterial.getHarvestLevel());
		setHarvestLevel("axe", toolMaterial.getHarvestLevel());
		setHarvestLevel("shovel", toolMaterial.getHarvestLevel());
		setCreativeTab(TestMod3.creativeTab);
	}

	/**
	 * The {@link Material}s that this tool is effective on.
	 */
	public static final Set<Material> EFFECTIVE_MATERIALS = ImmutableSet.of(
			// Pickaxe
			Material.rock, Material.iron, Material.ice, Material.glass, Material.piston, Material.anvil, Material.circuits,

			// Axe
			Material.wood, Material.gourd, Material.plants, Material.vine,

			// Shovel
			Material.grass, Material.ground, Material.sand, Material.snow, Material.craftedSnow, Material.clay
	);

	/**
	 * Can this tool harvest the {@link Block}?
	 * <p>
	 * This should only be used by {@link ForgeHooks#canHarvestBlock(Block, EntityPlayer, IBlockAccess, BlockPos)},
	 * use the tool class/harvest level system where possible.
	 *
	 * @param block     The Block
	 * @param itemStack The tool
	 * @return Is this tool effective on the {@link Block's} {@link Material}?
	 */
	@Override
	public boolean canHarvestBlock(Block block, ItemStack itemStack) {
		return EFFECTIVE_MATERIALS.contains(block.getMaterial());
	}

	@Override
	public float getDigSpeed(ItemStack stack, IBlockState state) {
		// Not all blocks have a harvest tool/level set, so we need to fall back to checking the Material like the vanilla tools do
		if (EFFECTIVE_MATERIALS.contains(state.getBlock().getMaterial())) {
			return efficiencyOnProperMaterial;
		}

		return super.getDigSpeed(stack, state);
	}

	public boolean hitEntity(ItemStack itemStack, EntityLivingBase target, EntityLivingBase attacker) {
		itemStack.damageItem(1, attacker); // Only reduce the durability by 1 point (like swords do) instead of 2 (like tools do)
		return true;
	}
}

@@ -339,16 +339,50 @@
 Player;%0A
+import net.minecraft.init.Blocks;%0A
 import n
@@ -933,16 +933,397 @@
 mTool %7B%0A
+%0A%09/**%0A%09 * The speed at which Cobwebs are harvested%0A%09 */%0A%09public static final float DIG_SPEED_WEB = 15.0f;%0A%0A%09/**%0A%09 * The speed at which Sword-effective %7B@link Material%7Ds are harvested%0A%09 */%0A%09public static final float DIG_SPEED_SWORD = 1.5f;%0A%0A%09/**%0A%09 * The speed at which blocks are harvested if this isn't their correct tool%0A%09 */%0A%09public static final float DIG_SPEED_DEFAULT = 1.0f;%0A%0A
 %09public 
@@ -1597,24 +1597,25 @@
 stLevel());%0A
+%0A
 %09%09setCreativ
@@ -2115,16 +2115,240 @@
 ay%0A%09);%0A%0A
+%09/**%0A%09 * The %7B@link Material%7Ds that Swords are effective on.%0A%09 */%0A%09public static final Set%3CMaterial%3E SWORD_MATERIALS = ImmutableSet.of(%0A%09%09%09Material.plants, Material.vine, Material.coral, Material.leaves, Material.gourd%0A%09);%0A%0A
 %09/**%0A%09 *
@@ -2853,16 +2853,39 @@
 erial())
+ %7C%7C block == Blocks.web
 ;%0A%09%7D%0A%0A%09@
@@ -2957,16 +2957,230 @@
 tate) %7B%0A
+%09%09if (state.getBlock() == Blocks.web) %7B%0A%09%09%09return DIG_SPEED_WEB;%0A%09%09%7D%0A%0A%09%09for (String type : getToolClasses(stack)) %7B%0A%09%09%09if (state.getBlock().isToolEffective(type, state))%0A%09%09%09%09return efficiencyOnProperMaterial;%0A%09%09%7D%0A%0A
 %09%09// Not
@@ -3416,46 +3416,130 @@
 %0A%0A%09%09
-return super.getDigSpeed(stack, state)
+if (SWORD_MATERIALS.contains(state.getBlock().getMaterial())) %7B%0A%09%09%09return DIG_SPEED_SWORD;%0A%09%09%7D%0A%0A%09%09return DIG_SPEED_DEFAULT
 ;%0A%09%7D

Add Sword-style harvesting to Harvest Swords