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