    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.io.DataInputStream;
    006    import java.io.DataOutputStream;
    007    import java.io.IOException;
    008    import java.util.ArrayList;
    009
    010    public class MerchantRecipeList extends ArrayList
    011    {
    012        public MerchantRecipeList() {}
    013
    014        public MerchantRecipeList(NBTTagCompound par1NBTTagCompound)
    015        {
    016            this.readRecipiesFromTags(par1NBTTagCompound);
    017        }
    018
    019        /**
    020         * can par1,par2 be used to in crafting recipe par3
    021         */
    022        public MerchantRecipe canRecipeBeUsed(ItemStack par1ItemStack, ItemStack par2ItemStack, int par3)
    023        {
    024            if (par3 > 0 && par3 < this.size())
    025            {
    026                MerchantRecipe var6 = (MerchantRecipe)this.get(par3);
    027                return par1ItemStack.itemID == var6.getItemToBuy().itemID && (par2ItemStack == null && !var6.hasSecondItemToBuy() || var6.hasSecondItemToBuy() && par2ItemStack != null && var6.getSecondItemToBuy().itemID == par2ItemStack.itemID) && par1ItemStack.stackSize >= var6.getItemToBuy().stackSize && (!var6.hasSecondItemToBuy() || par2ItemStack.stackSize >= var6.getSecondItemToBuy().stackSize) ? var6 : null;
    028            }
    029            else
    030            {
    031                for (int var4 = 0; var4 < this.size(); ++var4)
    032                {
    033                    MerchantRecipe var5 = (MerchantRecipe)this.get(var4);
    034
    035                    if (par1ItemStack.itemID == var5.getItemToBuy().itemID && par1ItemStack.stackSize >= var5.getItemToBuy().stackSize && (!var5.hasSecondItemToBuy() && par2ItemStack == null || var5.hasSecondItemToBuy() && par2ItemStack != null && var5.getSecondItemToBuy().itemID == par2ItemStack.itemID && par2ItemStack.stackSize >= var5.getSecondItemToBuy().stackSize))
    036                    {
    037                        return var5;
    038                    }
    039                }
    040
    041                return null;
    042            }
    043        }
    044
    045        /**
    046         * checks if there is a recipie for the same ingredients already on the list, and replaces it. otherwise, adds it
    047         */
    048        public void addToListWithCheck(MerchantRecipe par1MerchantRecipe)
    049        {
    050            for (int var2 = 0; var2 < this.size(); ++var2)
    051            {
    052                MerchantRecipe var3 = (MerchantRecipe)this.get(var2);
    053
    054                if (par1MerchantRecipe.hasSameIDsAs(var3))
    055                {
    056                    if (par1MerchantRecipe.hasSameItemsAs(var3))
    057                    {
    058                        this.set(var2, par1MerchantRecipe);
    059                    }
    060
    061                    return;
    062                }
    063            }
    064
    065            this.add(par1MerchantRecipe);
    066        }
    067
    068        public void writeRecipiesToStream(DataOutputStream par1DataOutputStream) throws IOException
    069        {
    070            par1DataOutputStream.writeByte((byte)(this.size() & 255));
    071
    072            for (int var2 = 0; var2 < this.size(); ++var2)
    073            {
    074                MerchantRecipe var3 = (MerchantRecipe)this.get(var2);
    075                Packet.writeItemStack(var3.getItemToBuy(), par1DataOutputStream);
    076                Packet.writeItemStack(var3.getItemToSell(), par1DataOutputStream);
    077                ItemStack var4 = var3.getSecondItemToBuy();
    078                par1DataOutputStream.writeBoolean(var4 != null);
    079
    080                if (var4 != null)
    081                {
    082                    Packet.writeItemStack(var4, par1DataOutputStream);
    083                }
    084
    085                par1DataOutputStream.writeBoolean(var3.func_82784_g());
    086            }
    087        }
    088
    089        @SideOnly(Side.CLIENT)
    090        public static MerchantRecipeList readRecipiesFromStream(DataInputStream par0DataInputStream) throws IOException
    091        {
    092            MerchantRecipeList var1 = new MerchantRecipeList();
    093            int var2 = par0DataInputStream.readByte() & 255;
    094
    095            for (int var3 = 0; var3 < var2; ++var3)
    096            {
    097                ItemStack var4 = Packet.readItemStack(par0DataInputStream);
    098                ItemStack var5 = Packet.readItemStack(par0DataInputStream);
    099                ItemStack var6 = null;
    100
    101                if (par0DataInputStream.readBoolean())
    102                {
    103                    var6 = Packet.readItemStack(par0DataInputStream);
    104                }
    105
    106                boolean var7 = par0DataInputStream.readBoolean();
    107                MerchantRecipe var8 = new MerchantRecipe(var4, var6, var5);
    108
    109                if (var7)
    110                {
    111                    var8.func_82785_h();
    112                }
    113
    114                var1.add(var8);
    115            }
    116
    117            return var1;
    118        }
    119
    120        public void readRecipiesFromTags(NBTTagCompound par1NBTTagCompound)
    121        {
    122            NBTTagList var2 = par1NBTTagCompound.getTagList("Recipes");
    123
    124            for (int var3 = 0; var3 < var2.tagCount(); ++var3)
    125            {
    126                NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
    127                this.add(new MerchantRecipe(var4));
    128            }
    129        }
    130
    131        public NBTTagCompound getRecipiesAsTags()
    132        {
    133            NBTTagCompound var1 = new NBTTagCompound();
    134            NBTTagList var2 = new NBTTagList("Recipes");
    135
    136            for (int var3 = 0; var3 < this.size(); ++var3)
    137            {
    138                MerchantRecipe var4 = (MerchantRecipe)this.get(var3);
    139                var2.appendTag(var4.writeToTags());
    140            }
    141
    142            var1.setTag("Recipes", var2);
    143            return var1;
    144        }
    145    }