001 package net.minecraft.src;
002
003 import cpw.mods.fml.common.Side;
004 import cpw.mods.fml.common.asm.SideOnly;
005
006 public class MerchantRecipe
007 {
008 /** Item the Villager buys. */
009 private ItemStack itemToBuy;
010
011 /** Second Item the Villager buys. */
012 private ItemStack secondItemToBuy;
013
014 /** Item the Villager sells. */
015 private ItemStack itemToSell;
016
017 /**
018 * Saves how much has been tool used when put into to slot to be enchanted.
019 */
020 private int toolUses;
021
022 /** Maximum times this trade can be used. */
023 private int maxTradeUses;
024
025 public MerchantRecipe(NBTTagCompound par1NBTTagCompound)
026 {
027 this.readFromTags(par1NBTTagCompound);
028 }
029
030 public MerchantRecipe(ItemStack par1ItemStack, ItemStack par2ItemStack, ItemStack par3ItemStack)
031 {
032 this.itemToBuy = par1ItemStack;
033 this.secondItemToBuy = par2ItemStack;
034 this.itemToSell = par3ItemStack;
035 this.maxTradeUses = 7;
036 }
037
038 public MerchantRecipe(ItemStack par1ItemStack, ItemStack par2ItemStack)
039 {
040 this(par1ItemStack, (ItemStack)null, par2ItemStack);
041 }
042
043 public MerchantRecipe(ItemStack par1ItemStack, Item par2Item)
044 {
045 this(par1ItemStack, new ItemStack(par2Item));
046 }
047
048 /**
049 * Gets the itemToBuy.
050 */
051 public ItemStack getItemToBuy()
052 {
053 return this.itemToBuy;
054 }
055
056 /**
057 * Gets secondItemToBuy.
058 */
059 public ItemStack getSecondItemToBuy()
060 {
061 return this.secondItemToBuy;
062 }
063
064 /**
065 * Gets if Villager has secondItemToBuy.
066 */
067 public boolean hasSecondItemToBuy()
068 {
069 return this.secondItemToBuy != null;
070 }
071
072 /**
073 * Gets itemToSell.
074 */
075 public ItemStack getItemToSell()
076 {
077 return this.itemToSell;
078 }
079
080 /**
081 * checks if both the first and second ItemToBuy IDs are the same
082 */
083 public boolean hasSameIDsAs(MerchantRecipe par1MerchantRecipe)
084 {
085 return this.itemToBuy.itemID == par1MerchantRecipe.itemToBuy.itemID && this.itemToSell.itemID == par1MerchantRecipe.itemToSell.itemID ? this.secondItemToBuy == null && par1MerchantRecipe.secondItemToBuy == null || this.secondItemToBuy != null && par1MerchantRecipe.secondItemToBuy != null && this.secondItemToBuy.itemID == par1MerchantRecipe.secondItemToBuy.itemID : false;
086 }
087
088 /**
089 * checks first and second ItemToBuy ID's and count. Calls hasSameIDs
090 */
091 public boolean hasSameItemsAs(MerchantRecipe par1MerchantRecipe)
092 {
093 return this.hasSameIDsAs(par1MerchantRecipe) && (this.itemToBuy.stackSize < par1MerchantRecipe.itemToBuy.stackSize || this.secondItemToBuy != null && this.secondItemToBuy.stackSize < par1MerchantRecipe.secondItemToBuy.stackSize);
094 }
095
096 public void incrementToolUses()
097 {
098 ++this.toolUses;
099 }
100
101 public void func_82783_a(int par1)
102 {
103 this.maxTradeUses += par1;
104 }
105
106 public boolean func_82784_g()
107 {
108 return this.toolUses >= this.maxTradeUses;
109 }
110
111 @SideOnly(Side.CLIENT)
112 public void func_82785_h()
113 {
114 this.toolUses = this.maxTradeUses;
115 }
116
117 public void readFromTags(NBTTagCompound par1NBTTagCompound)
118 {
119 NBTTagCompound var2 = par1NBTTagCompound.getCompoundTag("buy");
120 this.itemToBuy = ItemStack.loadItemStackFromNBT(var2);
121 NBTTagCompound var3 = par1NBTTagCompound.getCompoundTag("sell");
122 this.itemToSell = ItemStack.loadItemStackFromNBT(var3);
123
124 if (par1NBTTagCompound.hasKey("buyB"))
125 {
126 this.secondItemToBuy = ItemStack.loadItemStackFromNBT(par1NBTTagCompound.getCompoundTag("buyB"));
127 }
128
129 if (par1NBTTagCompound.hasKey("uses"))
130 {
131 this.toolUses = par1NBTTagCompound.getInteger("uses");
132 }
133
134 if (par1NBTTagCompound.hasKey("maxUses"))
135 {
136 this.maxTradeUses = par1NBTTagCompound.getInteger("maxUses");
137 }
138 else
139 {
140 this.maxTradeUses = 7;
141 }
142 }
143
144 public NBTTagCompound writeToTags()
145 {
146 NBTTagCompound var1 = new NBTTagCompound();
147 var1.setCompoundTag("buy", this.itemToBuy.writeToNBT(new NBTTagCompound("buy")));
148 var1.setCompoundTag("sell", this.itemToSell.writeToNBT(new NBTTagCompound("sell")));
149
150 if (this.secondItemToBuy != null)
151 {
152 var1.setCompoundTag("buyB", this.secondItemToBuy.writeToNBT(new NBTTagCompound("buyB")));
153 }
154
155 var1.setInteger("uses", this.toolUses);
156 var1.setInteger("maxUses", this.maxTradeUses);
157 return var1;
158 }
159 }