| 1 | package net.minecraft.village;
|
| 2 |
|
| 3 | import java.io.IOException;
|
| 4 | import java.util.ArrayList;
|
| 5 | import net.minecraft.item.ItemStack;
|
| 6 | import net.minecraft.nbt.NBTTagCompound;
|
| 7 | import net.minecraft.nbt.NBTTagList;
|
| 8 | import net.minecraft.nbt.NBTUtil;
|
| 9 | import net.minecraft.network.PacketBuffer;
|
| 10 |
|
| 11 | public class MerchantRecipeList extends ArrayList<MerchantRecipe>
|
| 12 | {
|
| 13 | public MerchantRecipeList()
|
| 14 | {
|
| 15 | }
|
| 16 |
|
| 17 | public MerchantRecipeList(NBTTagCompound compound)
|
| 18 | {
|
| 19 | this.readRecipiesFromTags(compound);
|
| 20 | }
|
| 21 |
|
| 22 | /**
|
| 23 | * can par1,par2 be used to in crafting recipe par3
|
| 24 | */
|
| 25 | public MerchantRecipe canRecipeBeUsed(ItemStack p_77203_1_, ItemStack p_77203_2_, int p_77203_3_)
|
| 26 | {
|
| 27 | if (p_77203_3_ > 0 && p_77203_3_ < this.size())
|
| 28 | {
|
| 29 | MerchantRecipe merchantrecipe1 = (MerchantRecipe)this.get(p_77203_3_);
|
| 30 | return !this.func_181078_a(p_77203_1_, merchantrecipe1.getItemToBuy()) || (p_77203_2_ != null || merchantrecipe1.hasSecondItemToBuy()) && (!merchantrecipe1.hasSecondItemToBuy() || !this.func_181078_a(p_77203_2_, merchantrecipe1.getSecondItemToBuy())) || p_77203_1_.stackSize < merchantrecipe1.getItemToBuy().stackSize || merchantrecipe1.hasSecondItemToBuy() && p_77203_2_.stackSize < merchantrecipe1.getSecondItemToBuy().stackSize ? null : merchantrecipe1;
|
| 31 | }
|
| 32 | else
|
| 33 | {
|
| 34 | for (int i = 0; i < this.size(); ++i)
|
| 35 | {
|
| 36 | MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
|
| 37 |
|
| 38 | if (this.func_181078_a(p_77203_1_, merchantrecipe.getItemToBuy()) && p_77203_1_.stackSize >= merchantrecipe.getItemToBuy().stackSize && (!merchantrecipe.hasSecondItemToBuy() && p_77203_2_ == null || merchantrecipe.hasSecondItemToBuy() && this.func_181078_a(p_77203_2_, merchantrecipe.getSecondItemToBuy()) && p_77203_2_.stackSize >= merchantrecipe.getSecondItemToBuy().stackSize))
|
| 39 | {
|
| 40 | return merchantrecipe;
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return null;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | private boolean func_181078_a(ItemStack p_181078_1_, ItemStack p_181078_2_)
|
| 49 | {
|
| 50 | return ItemStack.areItemsEqual(p_181078_1_, p_181078_2_) && (!p_181078_2_.hasTagCompound() || p_181078_1_.hasTagCompound() && NBTUtil.func_181123_a(p_181078_2_.getTagCompound(), p_181078_1_.getTagCompound(), false));
|
| 51 | }
|
| 52 |
|
| 53 | public void writeToBuf(PacketBuffer buffer)
|
| 54 | {
|
| 55 | buffer.writeByte((byte)(this.size() & 255));
|
| 56 |
|
| 57 | for (int i = 0; i < this.size(); ++i)
|
| 58 | {
|
| 59 | MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
|
| 60 | buffer.writeItemStackToBuffer(merchantrecipe.getItemToBuy());
|
| 61 | buffer.writeItemStackToBuffer(merchantrecipe.getItemToSell());
|
| 62 | ItemStack itemstack = merchantrecipe.getSecondItemToBuy();
|
| 63 | buffer.writeBoolean(itemstack != null);
|
| 64 |
|
| 65 | if (itemstack != null)
|
| 66 | {
|
| 67 | buffer.writeItemStackToBuffer(itemstack);
|
| 68 | }
|
| 69 |
|
| 70 | buffer.writeBoolean(merchantrecipe.isRecipeDisabled());
|
| 71 | buffer.writeInt(merchantrecipe.getToolUses());
|
| 72 | buffer.writeInt(merchantrecipe.getMaxTradeUses());
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public static MerchantRecipeList readFromBuf(PacketBuffer buffer) throws IOException
|
| 77 | {
|
| 78 | MerchantRecipeList merchantrecipelist = new MerchantRecipeList();
|
| 79 | int i = buffer.readByte() & 255;
|
| 80 |
|
| 81 | for (int j = 0; j < i; ++j)
|
| 82 | {
|
| 83 | ItemStack itemstack = buffer.readItemStackFromBuffer();
|
| 84 | ItemStack itemstack1 = buffer.readItemStackFromBuffer();
|
| 85 | ItemStack itemstack2 = null;
|
| 86 |
|
| 87 | if (buffer.readBoolean())
|
| 88 | {
|
| 89 | itemstack2 = buffer.readItemStackFromBuffer();
|
| 90 | }
|
| 91 |
|
| 92 | boolean flag = buffer.readBoolean();
|
| 93 | int k = buffer.readInt();
|
| 94 | int l = buffer.readInt();
|
| 95 | MerchantRecipe merchantrecipe = new MerchantRecipe(itemstack, itemstack2, itemstack1, k, l);
|
| 96 |
|
| 97 | if (flag)
|
| 98 | {
|
| 99 | merchantrecipe.compensateToolUses();
|
| 100 | }
|
| 101 |
|
| 102 | merchantrecipelist.add(merchantrecipe);
|
| 103 | }
|
| 104 |
|
| 105 | return merchantrecipelist;
|
| 106 | }
|
| 107 |
|
| 108 | public void readRecipiesFromTags(NBTTagCompound compound)
|
| 109 | {
|
| 110 | NBTTagList nbttaglist = compound.getTagList("Recipes", 10);
|
| 111 |
|
| 112 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 113 | {
|
| 114 | NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
| 115 | this.add(new MerchantRecipe(nbttagcompound));
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | public NBTTagCompound getRecipiesAsTags()
|
| 120 | {
|
| 121 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 122 | NBTTagList nbttaglist = new NBTTagList();
|
| 123 |
|
| 124 | for (int i = 0; i < this.size(); ++i)
|
| 125 | {
|
| 126 | MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
|
| 127 | nbttaglist.appendTag(merchantrecipe.writeToTags());
|
| 128 | }
|
| 129 |
|
| 130 | nbttagcompound.setTag("Recipes", nbttaglist);
|
| 131 | return nbttagcompound;
|
| 132 | }
|
| 133 | }
|
| 134 |
|