    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 var4 = (MerchantRecipe)this.get(par3);
    027
    028                if (par1ItemStack.itemID == var4.getItemToBuy().itemID && (par2ItemStack == null && !var4.hasSecondItemToBuy() || var4.hasSecondItemToBuy() && par2ItemStack != null && var4.getSecondItemToBuy().itemID == par2ItemStack.itemID))
    029                {
    030                    if (par1ItemStack.stackSize >= var4.getItemToBuy().stackSize && (!var4.hasSecondItemToBuy() || par2ItemStack.stackSize >= var4.getSecondItemToBuy().stackSize))
    031                    {
    032                        return var4;
    033                    }
    034
    035                    return null;
    036                }
    037            }
    038
    039            for (int var6 = 0; var6 < this.size(); ++var6)
    040            {
    041                MerchantRecipe var5 = (MerchantRecipe)this.get(var6);
    042
    043                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))
    044                {
    045                    return var5;
    046                }
    047            }
    048
    049            return null;
    050        }
    051
    052        /**
    053         * checks if there is a recipie for the same ingredients already on the list, and replaces it. otherwise, adds it
    054         */
    055        public void addToListWithCheck(MerchantRecipe par1MerchantRecipe)
    056        {
    057            for (int var2 = 0; var2 < this.size(); ++var2)
    058            {
    059                MerchantRecipe var3 = (MerchantRecipe)this.get(var2);
    060
    061                if (par1MerchantRecipe.hasSameIDsAs(var3))
    062                {
    063                    if (par1MerchantRecipe.hasSameItemsAs(var3))
    064                    {
    065                        this.set(var2, par1MerchantRecipe);
    066                    }
    067
    068                    return;
    069                }
    070            }
    071
    072            this.add(par1MerchantRecipe);
    073        }
    074
    075        public void writeRecipiesToStream(DataOutputStream par1DataOutputStream) throws IOException
    076        {
    077            par1DataOutputStream.writeByte((byte)(this.size() & 255));
    078
    079            for (int var2 = 0; var2 < this.size(); ++var2)
    080            {
    081                MerchantRecipe var3 = (MerchantRecipe)this.get(var2);
    082                Packet.writeItemStack(var3.getItemToBuy(), par1DataOutputStream);
    083                Packet.writeItemStack(var3.getItemToSell(), par1DataOutputStream);
    084                ItemStack var4 = var3.getSecondItemToBuy();
    085                par1DataOutputStream.writeBoolean(var4 != null);
    086
    087                if (var4 != null)
    088                {
    089                    Packet.writeItemStack(var4, par1DataOutputStream);
    090                }
    091            }
    092        }
    093
    094        @SideOnly(Side.CLIENT)
    095        public static MerchantRecipeList readRecipiesFromStream(DataInputStream par0DataInputStream) throws IOException
    096        {
    097            MerchantRecipeList var1 = new MerchantRecipeList();
    098            int var2 = par0DataInputStream.readByte() & 255;
    099
    100            for (int var3 = 0; var3 < var2; ++var3)
    101            {
    102                ItemStack var4 = Packet.readItemStack(par0DataInputStream);
    103                ItemStack var5 = Packet.readItemStack(par0DataInputStream);
    104                ItemStack var6 = null;
    105
    106                if (par0DataInputStream.readBoolean())
    107                {
    108                    var6 = Packet.readItemStack(par0DataInputStream);
    109                }
    110
    111                var1.add(new MerchantRecipe(var4, var6, var5));
    112            }
    113
    114            return var1;
    115        }
    116
    117        public void readRecipiesFromTags(NBTTagCompound par1NBTTagCompound)
    118        {
    119            NBTTagList var2 = par1NBTTagCompound.getTagList("Recipes");
    120
    121            for (int var3 = 0; var3 < var2.tagCount(); ++var3)
    122            {
    123                NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
    124                this.add(new MerchantRecipe(var4));
    125            }
    126        }
    127
    128        public NBTTagCompound getRecipiesAsTags()
    129        {
    130            NBTTagCompound var1 = new NBTTagCompound();
    131            NBTTagList var2 = new NBTTagList("Recipes");
    132
    133            for (int var3 = 0; var3 < this.size(); ++var3)
    134            {
    135                MerchantRecipe var4 = (MerchantRecipe)this.get(var3);
    136                var2.appendTag(var4.writeToTags());
    137            }
    138
    139            var1.setTag("Recipes", var2);
    140            return var1;
    141        }
    142    }