/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Apr 13, 2014, 7:15:39 PM (GMT)]
*/
package vazkii.botania.common.item.equipment.tool.manasteel;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.item.ISortableTool;
import vazkii.botania.api.mana.IManaUsingItem;
import vazkii.botania.api.mana.ManaItemHandler;
import vazkii.botania.client.core.handler.ItemsRemainingRenderHandler;
import vazkii.botania.client.lib.LibResources;
import vazkii.botania.client.render.IModelRegister;
import vazkii.botania.common.core.BotaniaCreativeTab;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.item.equipment.tool.ToolCommons;
import vazkii.botania.common.lib.LibItemNames;
import vazkii.botania.common.lib.LibMisc;
public class ItemManasteelAxe extends ItemAxe implements IManaUsingItem, ISortableTool, IModelRegister {
private static final Pattern SAPLING_PATTERN = Pattern.compile("(?:(?:(?:[A-Z-_.:]|^)sapling)|(?:(?:[a-z-_.:]|^)Sapling))(?:[A-Z-_.:]|$)");
private static final int MANA_PER_DAMAGE = 60;
public ItemManasteelAxe() {
this(BotaniaAPI.manasteelToolMaterial, LibItemNames.MANASTEEL_AXE);
}
public ItemManasteelAxe(ToolMaterial mat, String name) {
super(mat, 8F, -3.1F);
setCreativeTab(BotaniaCreativeTab.INSTANCE);
GameRegistry.register(this, new ResourceLocation(LibMisc.MOD_ID, name));
setUnlocalizedName(name);
}
@Nonnull
@Override
public String getUnlocalizedNameInefficiently(@Nonnull ItemStack par1ItemStack) {
return super.getUnlocalizedNameInefficiently(par1ItemStack).replaceAll("item.", "item." + LibResources.PREFIX_MOD);
}
@Override
public boolean hitEntity(ItemStack par1ItemStack, EntityLivingBase par2EntityLivingBase, @Nonnull EntityLivingBase par3EntityLivingBase) {
ToolCommons.damageItem(par1ItemStack, 1, par3EntityLivingBase, getManaPerDamage());
return true;
}
@Override
public boolean onBlockDestroyed(@Nonnull ItemStack stack, @Nonnull World world, @Nonnull IBlockState state, @Nonnull BlockPos pos, @Nonnull EntityLivingBase entity) {
if (state.getBlockHardness(world, pos) != 0F)
ToolCommons.damageItem(stack, 1, entity, getManaPerDamage());
return true;
}
public int getManaPerDamage() {
return MANA_PER_DAMAGE;
}
@Nonnull
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float sx, float sy, float sz) {
for(int i = 0; i < player.inventory.getSizeInventory(); i++) {
ItemStack stackAt = player.inventory.getStackInSlot(i);
if(!stackAt.isEmpty() && SAPLING_PATTERN.matcher(stackAt.getItem().getUnlocalizedName()).find()) {
ItemStack saveHeldStack = player.getHeldItem(hand);
player.setHeldItem(hand, stackAt);
EnumActionResult did = stackAt.getItem().onItemUse(player, world, pos, hand, side, sx, sy, sz);
player.setHeldItem(hand, saveHeldStack);
ItemsRemainingRenderHandler.set(player, new ItemStack(Blocks.SAPLING), SAPLING_PATTERN);
return did;
}
}
return EnumActionResult.PASS;
}
@Override
public void onUpdate(ItemStack stack, World world, Entity player, int par4, boolean par5) {
if(!world.isRemote && player instanceof EntityPlayer && stack.getItemDamage() > 0 && ManaItemHandler.requestManaExactForTool(stack, (EntityPlayer) player, getManaPerDamage() * 2, true))
stack.setItemDamage(stack.getItemDamage() - 1);
}
@Override
public boolean getIsRepairable(ItemStack par1ItemStack, @Nonnull ItemStack par2ItemStack) {
return par2ItemStack.getItem() == ModItems.manaResource && par2ItemStack.getItemDamage() == 0 ? true : super.getIsRepairable(par1ItemStack, par2ItemStack);
}
@Override
public boolean usesMana(ItemStack stack) {
return true;
}
@Override
public ToolType getSortingType(ItemStack stack) {
return ToolType.AXE;
}
@Override
public int getSortingPriority(ItemStack stack) {
return ToolCommons.getToolPriority(stack);
}
@SideOnly(Side.CLIENT)
@Override
public void registerModels() {
ModelLoader.setCustomModelResourceLocation(this, 0, new ModelResourceLocation(getRegistryName(), "inventory"));
}
}