    001package net.minecraft.village;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.DataInputStream;
    006import java.io.DataOutputStream;
    007import java.io.IOException;
    008import java.util.ArrayList;
    009import net.minecraft.item.ItemStack;
    010import net.minecraft.nbt.NBTTagCompound;
    011import net.minecraft.nbt.NBTTagList;
    012import net.minecraft.network.packet.Packet;
    013
    014public 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 merchantrecipe = (MerchantRecipe)this.get(par3);
    031            return par1ItemStack.itemID == merchantrecipe.getItemToBuy().itemID && (par2ItemStack == null && !merchantrecipe.hasSecondItemToBuy() || merchantrecipe.hasSecondItemToBuy() && par2ItemStack != null && merchantrecipe.getSecondItemToBuy().itemID == par2ItemStack.itemID) && par1ItemStack.stackSize >= merchantrecipe.getItemToBuy().stackSize && (!merchantrecipe.hasSecondItemToBuy() || par2ItemStack.stackSize >= merchantrecipe.getSecondItemToBuy().stackSize) ? merchantrecipe : null;
    032        }
    033        else
    034        {
    035            for (int j = 0; j < this.size(); ++j)
    036            {
    037                MerchantRecipe merchantrecipe1 = (MerchantRecipe)this.get(j);
    038
    039                if (par1ItemStack.itemID == merchantrecipe1.getItemToBuy().itemID && par1ItemStack.stackSize >= merchantrecipe1.getItemToBuy().stackSize && (!merchantrecipe1.hasSecondItemToBuy() && par2ItemStack == null || merchantrecipe1.hasSecondItemToBuy() && par2ItemStack != null && merchantrecipe1.getSecondItemToBuy().itemID == par2ItemStack.itemID && par2ItemStack.stackSize >= merchantrecipe1.getSecondItemToBuy().stackSize))
    040                {
    041                    return merchantrecipe1;
    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 i = 0; i < this.size(); ++i)
    055        {
    056            MerchantRecipe merchantrecipe1 = (MerchantRecipe)this.get(i);
    057
    058            if (par1MerchantRecipe.hasSameIDsAs(merchantrecipe1))
    059            {
    060                if (par1MerchantRecipe.hasSameItemsAs(merchantrecipe1))
    061                {
    062                    this.set(i, 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 i = 0; i < this.size(); ++i)
    077        {
    078            MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
    079            Packet.writeItemStack(merchantrecipe.getItemToBuy(), par1DataOutputStream);
    080            Packet.writeItemStack(merchantrecipe.getItemToSell(), par1DataOutputStream);
    081            ItemStack itemstack = merchantrecipe.getSecondItemToBuy();
    082            par1DataOutputStream.writeBoolean(itemstack != null);
    083
    084            if (itemstack != null)
    085            {
    086                Packet.writeItemStack(itemstack, par1DataOutputStream);
    087            }
    088
    089            par1DataOutputStream.writeBoolean(merchantrecipe.func_82784_g());
    090        }
    091    }
    092
    093    @SideOnly(Side.CLIENT)
    094    public static MerchantRecipeList readRecipiesFromStream(DataInputStream par0DataInputStream) throws IOException
    095    {
    096        MerchantRecipeList merchantrecipelist = new MerchantRecipeList();
    097        int i = par0DataInputStream.readByte() & 255;
    098
    099        for (int j = 0; j < i; ++j)
    100        {
    101            ItemStack itemstack = Packet.readItemStack(par0DataInputStream);
    102            ItemStack itemstack1 = Packet.readItemStack(par0DataInputStream);
    103            ItemStack itemstack2 = null;
    104
    105            if (par0DataInputStream.readBoolean())
    106            {
    107                itemstack2 = Packet.readItemStack(par0DataInputStream);
    108            }
    109
    110            boolean flag = par0DataInputStream.readBoolean();
    111            MerchantRecipe merchantrecipe = new MerchantRecipe(itemstack, itemstack2, itemstack1);
    112
    113            if (flag)
    114            {
    115                merchantrecipe.func_82785_h();
    116            }
    117
    118            merchantrecipelist.add(merchantrecipe);
    119        }
    120
    121        return merchantrecipelist;
    122    }
    123
    124    public void readRecipiesFromTags(NBTTagCompound par1NBTTagCompound)
    125    {
    126        NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Recipes");
    127
    128        for (int i = 0; i < nbttaglist.tagCount(); ++i)
    129        {
    130            NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
    131            this.add(new MerchantRecipe(nbttagcompound1));
    132        }
    133    }
    134
    135    public NBTTagCompound getRecipiesAsTags()
    136    {
    137        NBTTagCompound nbttagcompound = new NBTTagCompound();
    138        NBTTagList nbttaglist = new NBTTagList("Recipes");
    139
    140        for (int i = 0; i < this.size(); ++i)
    141        {
    142            MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
    143            nbttaglist.appendTag(merchantrecipe.writeToTags());
    144        }
    145
    146        nbttagcompound.setTag("Recipes", nbttaglist);
    147        return nbttagcompound;
    148    }
    149}