Files
TinkersConstruct/src/main/java/tconstruct/items/tools/PotionLauncher.java
T

190 lines
5.9 KiB
Java
Raw Normal View History

package tconstruct.items.tools;
2014-09-09 18:30:15 +10:00
import cpw.mods.fml.relauncher.*;
import java.util.List;
2013-12-31 11:26:08 -05:00
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
2014-09-09 18:30:15 +10:00
import net.minecraft.entity.player.*;
2014-01-10 21:14:54 -05:00
import net.minecraft.init.Items;
2014-09-09 18:30:15 +10:00
import net.minecraft.item.*;
import net.minecraft.nbt.NBTTagCompound;
2014-09-09 18:30:15 +10:00
import net.minecraft.util.*;
import net.minecraft.world.World;
2014-06-04 21:44:48 -08:00
import tconstruct.tools.entity.LaunchedPotion;
public class PotionLauncher extends Item
{
@SideOnly(Side.CLIENT)
2013-12-31 11:26:08 -05:00
private IIcon[] icons;
public static final String[] textureNames = new String[] { "potionlauncher" };
2013-12-31 11:26:08 -05:00
public PotionLauncher()
{
2013-12-31 11:26:08 -05:00
super();
this.maxStackSize = 1;
this.setCreativeTab(CreativeTabs.tabCombat);
this.setMaxDamage(3);
}
2014-02-17 00:31:43 -05:00
@Override
public ItemStack onEaten (ItemStack stack, World world, EntityPlayer player)
{
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
boolean loaded = tags.getBoolean("Loaded");
if (!loaded)
{
2014-01-10 21:14:54 -05:00
int slotID = getInventorySlotContainItem(Items.potionitem, player.inventory);
ItemStack potion = player.inventory.getStackInSlot(slotID);
NBTTagCompound potionTag = new NBTTagCompound();
potion.writeToNBT(potionTag);
2013-12-31 16:37:46 -05:00
tags.setTag("LoadedPotion", potionTag);
tags.setBoolean("Loaded", true);
if (!player.capabilities.isCreativeMode)
{
potion.stackSize--;
if (potion.stackSize < 1)
player.inventory.setInventorySlotContents(slotID, null);
}
2013-07-28 19:56:01 -07:00
world.playSoundEffect(player.posX, player.posY, player.posZ, "tinker:launcher_clank", 1.0F, (world.rand.nextFloat() - world.rand.nextFloat()) * 0.15F + 1.0F);
}
return stack;
}
2014-02-17 00:31:43 -05:00
@Override
public void onPlayerStoppedUsing (ItemStack stack, World world, EntityPlayer player, int time)
{
}
2014-02-17 00:31:43 -05:00
@Override
public ItemStack onItemRightClick (ItemStack stack, World world, EntityPlayer player)
{
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
2014-01-10 21:14:54 -05:00
int slotID = getInventorySlotContainItem(Items.potionitem, player.inventory);
if (!tags.getBoolean("Loaded") && slotID >= 0)
player.setItemInUse(stack, this.getMaxItemUseDuration(stack));
return stack;
}
/**
* How long it takes to use or consume an item
*/
2014-02-17 00:31:43 -05:00
@Override
public int getMaxItemUseDuration (ItemStack stack)
{
return 30;
}
/**
2014-02-16 14:23:50 -05:00
* returns the action that specifies what animation to play when the items
* is being used
*/
2014-02-17 00:31:43 -05:00
@Override
public EnumAction getItemUseAction (ItemStack stack)
{
if (stack != null && stack.hasTagCompound() && !stack.getTagCompound().getCompoundTag("InfiTool").getBoolean("Loaded"))
return EnumAction.bow;
else
return EnumAction.none;
}
2014-02-17 00:31:43 -05:00
@Override
@SideOnly(Side.CLIENT)
2013-12-31 11:26:08 -05:00
public void registerIcons (IIconRegister par1IconRegister)
{
2013-12-31 11:26:08 -05:00
this.icons = new IIcon[textureNames.length];
for (int i = 0; i < this.icons.length; ++i)
{
this.icons[i] = par1IconRegister.registerIcon("tinker:" + textureNames[i]);
}
}
2014-02-17 00:31:43 -05:00
@Override
@SideOnly(Side.CLIENT)
2013-12-31 14:11:05 -05:00
public IIcon getIconFromDamage (int meta)
{
return icons[0];
}
@Override
2014-02-05 08:28:53 -05:00
public void getSubItems (Item b, CreativeTabs tabs, List list)
{
2013-12-31 11:26:08 -05:00
ItemStack stack = new ItemStack(b, 1, 0);
NBTTagCompound compound = new NBTTagCompound();
NBTTagCompound tags = new NBTTagCompound();
2013-12-31 16:37:46 -05:00
compound.setTag("InfiTool", tags);
tags.setBoolean("Loaded", false);
stack.setTagCompound(compound);
list.add(stack);
}
@Override
public boolean onEntitySwing (EntityLivingBase player, ItemStack stack)
{
2014-03-28 01:37:13 -04:00
if (stack != null && stack.hasTagCompound())
{
2014-03-28 01:37:13 -04:00
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
if (tags.getBoolean("Loaded"))
{
2014-03-28 01:37:13 -04:00
NBTTagCompound potionTag = tags.getCompoundTag("LoadedPotion");
ItemStack potion = ItemStack.loadItemStackFromNBT(potionTag);// findPotion(player);InventoryLogic
if (potion != null)
{
2014-03-28 01:37:13 -04:00
World world = player.worldObj;
world.playSoundAtEntity(player, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
if (!world.isRemote)
{
world.spawnEntityInWorld(new LaunchedPotion(world, player, potion));
}
tags.removeTag("LoadedPotion");
tags.setBoolean("Loaded", false);
}
2014-03-28 01:37:13 -04:00
return true;
}
}
return false;
}
ItemStack findPotion (EntityLivingBase living)
{
if (living instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) living;
2014-01-10 21:14:54 -05:00
int potionSlot = getInventorySlotContainItem(Items.potionitem, player.inventory);
if (potionSlot >= 0)
{
return player.inventory.getStackInSlot(potionSlot);
}
}
return null;
}
2014-01-10 21:14:54 -05:00
int getInventorySlotContainItem (Item item, InventoryPlayer inventory)
{
for (int j = 0; j < inventory.mainInventory.length; ++j)
{
2014-01-10 21:14:54 -05:00
if (inventory.mainInventory[j] != null && inventory.mainInventory[j].getItem() == item)
{
return j;
}
}
return -1;
}
@Override
@SideOnly(Side.CLIENT)
public void addInformation (ItemStack stack, EntityPlayer player, List list, boolean par4)
{
list.add(StatCollector.translateToLocal("potionlauncher.tooltip"));
}
}