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