Package vazkii.botania.common.item.equipment.tool.manasteel

Source Code of vazkii.botania.common.item.equipment.tool.manasteel.ItemManasteelShears

/**
* 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 a
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
*
* File Created @ [Apr 13, 2014, 7:28:35 PM (GMT)]
*/
package vazkii.botania.common.item.equipment.tool.manasteel;

import java.util.ArrayList;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemShears;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.world.World;
import net.minecraftforge.common.IShearable;
import vazkii.botania.api.mana.IManaUsingItem;
import vazkii.botania.api.mana.ManaItemHandler;
import vazkii.botania.client.core.helper.IconHelper;
import vazkii.botania.client.lib.LibResources;
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 cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ItemManasteelShears extends ItemShears implements IManaUsingItem {

  public static final int MANA_PER_DAMAGE = 30;

  public ItemManasteelShears() {
    this(LibItemNames.MANASTEEL_SHEARS);
  }

  public ItemManasteelShears(String name) {
    setCreativeTab(BotaniaCreativeTab.INSTANCE);
    setUnlocalizedName(name);
  }

  @Override
  public Item setUnlocalizedName(String par1Str) {
    GameRegistry.registerItem(this, par1Str);
    return super.setUnlocalizedName(par1Str);
  }

  @Override
  public String getUnlocalizedNameInefficiently(ItemStack par1ItemStack) {
    return super.getUnlocalizedNameInefficiently(par1ItemStack).replaceAll("item.", "item." + LibResources.PREFIX_MOD);
  }

  @Override
  @SideOnly(Side.CLIENT)
  public void registerIcons(IIconRegister par1IconRegister) {
    itemIcon = IconHelper.forItem(par1IconRegister, this);
  }

  @Override
  public boolean itemInteractionForEntity(ItemStack itemstack, EntityPlayer player, EntityLivingBase entity) {
    if(entity.worldObj.isRemote)
      return false;

    if(entity instanceof IShearable) {
      IShearable target = (IShearable)entity;
      if(target.isShearable(itemstack, entity.worldObj, (int) entity.posX, (int) entity.posY, (int) entity.posZ)) {
        ArrayList<ItemStack> drops = target.onSheared(itemstack, entity.worldObj, (int) entity.posX, (int) entity.posY, (int) entity.posZ, EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, itemstack));

        Random rand = new Random();
        for(ItemStack stack : drops) {
          EntityItem ent = entity.entityDropItem(stack, 1.0F);
          ent.motionY += rand.nextFloat() * 0.05F;
          ent.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
          ent.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
        }

        ToolCommons.damageItem(itemstack, 1, player, MANA_PER_DAMAGE);
      }

      return true;
    }

    return false;
  }

  @Override
  public boolean onBlockStartBreak(ItemStack itemstack, int x, int y, int z, EntityPlayer player) {
    if (player.worldObj.isRemote)
      return false;

    Block block = player.worldObj.getBlock(x, y, z);
    if(block instanceof IShearable) {
      IShearable target = (IShearable)block;
      if(target.isShearable(itemstack, player.worldObj, x, y, z)) {
        ArrayList<ItemStack> drops = target.onSheared(itemstack, player.worldObj, x, y, z, EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, itemstack));
        Random rand = new Random();

        for(ItemStack stack : drops) {
          float f = 0.7F;
          double d  = rand.nextFloat() * f + (1D - f) * 0.5;
          double d1 = rand.nextFloat() * f + (1D - f) * 0.5;
          double d2 = rand.nextFloat() * f + (1D - f) * 0.5;

          EntityItem entityitem = new EntityItem(player.worldObj, x + d, y + d1, z + d2, stack);
          entityitem.delayBeforeCanPickup = 10;
          player.worldObj.spawnEntityInWorld(entityitem);
        }

        ToolCommons.damageItem(itemstack, 1, player, MANA_PER_DAMAGE);
        player.addStat(StatList.mineBlockStatArray[Block.getIdFromBlock(block)], 1);
      }
    }

    return false;
  }

  @Override
  public void onUpdate(ItemStack stack, World world, Entity player, int par4, boolean par5) {
    if(!world.isRemote && player instanceof EntityPlayer && stack.getItemDamage() > 0 && ManaItemHandler.requestManaExact(stack, (EntityPlayer) player, MANA_PER_DAMAGE * 2, true))
      stack.setItemDamage(stack.getItemDamage() - 1);
  }
 
  @Override
  public boolean getIsRepairable(ItemStack par1ItemStack, ItemStack par2ItemStack) {
    return par2ItemStack.getItem() == ModItems.manaResource && par2ItemStack.getItemDamage() == 0 ? true : super.getIsRepairable(par1ItemStack, par2ItemStack);
  }

  @Override
  public boolean usesMana(ItemStack stack) {
    return true;
  }
}
TOP

Related Classes of vazkii.botania.common.item.equipment.tool.manasteel.ItemManasteelShears

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.