-
Notifications
You must be signed in to change notification settings - Fork 755
/
PotionLauncher.java
186 lines (160 loc) · 5.87 KB
/
PotionLauncher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package tconstruct.items.tools;
import java.util.List;
import tconstruct.entity.projectile.LaunchedPotion;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class PotionLauncher extends Item
{
@SideOnly(Side.CLIENT)
private IIcon[] icons;
public static final String[] textureNames = new String[] { "potionlauncher" };
public PotionLauncher()
{
super();
this.maxStackSize = 1;
this.setCreativeTab(CreativeTabs.tabCombat);
this.setMaxDamage(3);
}
public ItemStack onEaten (ItemStack stack, World world, EntityPlayer player)
{
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
boolean loaded = tags.getBoolean("Loaded");
if (!loaded)
{
int slotID = getInventorySlotContainItem(Items.potionitem, player.inventory);
ItemStack potion = player.inventory.getStackInSlot(slotID);
NBTTagCompound potionTag = new NBTTagCompound();
potion.writeToNBT(potionTag);
tags.setTag("LoadedPotion", potionTag);
tags.setBoolean("Loaded", true);
if (!player.capabilities.isCreativeMode)
{
potion.stackSize--;
if (potion.stackSize < 1)
player.inventory.setInventorySlotContents(slotID, null);
}
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;
}
public void onPlayerStoppedUsing (ItemStack stack, World world, EntityPlayer player, int time)
{
}
public ItemStack onItemRightClick (ItemStack stack, World world, EntityPlayer player)
{
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
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
*/
public int getMaxItemUseDuration (ItemStack stack)
{
return 30;
}
/**
* returns the action that specifies what animation to play when the items is being used
*/
public EnumAction getItemUseAction (ItemStack stack)
{
if (!stack.getTagCompound().getCompoundTag("InfiTool").getBoolean("Loaded"))
return EnumAction.bow;
else
return EnumAction.none;
}
@SideOnly(Side.CLIENT)
public void registerIcons (IIconRegister par1IconRegister)
{
this.icons = new IIcon[textureNames.length];
for (int i = 0; i < this.icons.length; ++i)
{
this.icons[i] = par1IconRegister.registerIcon("tinker:" + textureNames[i]);
}
}
@SideOnly(Side.CLIENT)
public IIcon getIconFromDamage (int meta)
{
return icons[0];
}
@Override
public void getSubItems (Item b, CreativeTabs tabs, List list)
{
ItemStack stack = new ItemStack(b, 1, 0);
NBTTagCompound compound = new NBTTagCompound();
NBTTagCompound tags = new NBTTagCompound();
compound.setTag("InfiTool", tags);
tags.setBoolean("Loaded", false);
stack.setTagCompound(compound);
list.add(stack);
}
@Override
public boolean onEntitySwing (EntityLivingBase player, ItemStack stack)
{
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
if (tags.getBoolean("Loaded"))
{
NBTTagCompound potionTag = tags.getCompoundTag("LoadedPotion");
ItemStack potion = ItemStack.loadItemStackFromNBT(potionTag);//findPotion(player);InventoryLogic
if (potion != null)
{
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);
}
return true;
}
return false;
}
ItemStack findPotion (EntityLivingBase living)
{
if (living instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) living;
int potionSlot = getInventorySlotContainItem(Items.potionitem, player.inventory);
if (potionSlot >= 0)
{
return player.inventory.getStackInSlot(potionSlot);
}
}
return null;
}
int getInventorySlotContainItem (Item item, InventoryPlayer inventory)
{
for (int j = 0; j < inventory.mainInventory.length; ++j)
{
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"));
}
}