| 1 | package net.minecraft.inventory;
|
| 2 |
|
| 3 | import net.minecraft.entity.IMerchant;
|
| 4 | import net.minecraft.entity.player.EntityPlayer;
|
| 5 | import net.minecraft.item.ItemStack;
|
| 6 | import net.minecraft.util.ChatComponentText;
|
| 7 | import net.minecraft.util.ChatComponentTranslation;
|
| 8 | import net.minecraft.util.IChatComponent;
|
| 9 | import net.minecraft.village.MerchantRecipe;
|
| 10 | import net.minecraft.village.MerchantRecipeList;
|
| 11 |
|
| 12 | public class InventoryMerchant implements IInventory
|
| 13 | {
|
| 14 | private final IMerchant theMerchant;
|
| 15 | private ItemStack[] theInventory = new ItemStack[3];
|
| 16 | private final EntityPlayer thePlayer;
|
| 17 | private MerchantRecipe currentRecipe;
|
| 18 | private int currentRecipeIndex;
|
| 19 |
|
| 20 | public InventoryMerchant(EntityPlayer thePlayerIn, IMerchant theMerchantIn)
|
| 21 | {
|
| 22 | this.thePlayer = thePlayerIn;
|
| 23 | this.theMerchant = theMerchantIn;
|
| 24 | }
|
| 25 |
|
| 26 | /**
|
| 27 | * Returns the number of slots in the inventory.
|
| 28 | */
|
| 29 | public int getSizeInventory()
|
| 30 | {
|
| 31 | return this.theInventory.length;
|
| 32 | }
|
| 33 |
|
| 34 | /**
|
| 35 | * Returns the stack in the given slot.
|
| 36 | */
|
| 37 | public ItemStack getStackInSlot(int index)
|
| 38 | {
|
| 39 | return this.theInventory[index];
|
| 40 | }
|
| 41 |
|
| 42 | /**
|
| 43 | * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
|
| 44 | */
|
| 45 | public ItemStack decrStackSize(int index, int count)
|
| 46 | {
|
| 47 | if (this.theInventory[index] != null)
|
| 48 | {
|
| 49 | if (index == 2)
|
| 50 | {
|
| 51 | ItemStack itemstack2 = this.theInventory[index];
|
| 52 | this.theInventory[index] = null;
|
| 53 | return itemstack2;
|
| 54 | }
|
| 55 | else if (this.theInventory[index].stackSize <= count)
|
| 56 | {
|
| 57 | ItemStack itemstack1 = this.theInventory[index];
|
| 58 | this.theInventory[index] = null;
|
| 59 |
|
| 60 | if (this.inventoryResetNeededOnSlotChange(index))
|
| 61 | {
|
| 62 | this.resetRecipeAndSlots();
|
| 63 | }
|
| 64 |
|
| 65 | return itemstack1;
|
| 66 | }
|
| 67 | else
|
| 68 | {
|
| 69 | ItemStack itemstack = this.theInventory[index].splitStack(count);
|
| 70 |
|
| 71 | if (this.theInventory[index].stackSize == 0)
|
| 72 | {
|
| 73 | this.theInventory[index] = null;
|
| 74 | }
|
| 75 |
|
| 76 | if (this.inventoryResetNeededOnSlotChange(index))
|
| 77 | {
|
| 78 | this.resetRecipeAndSlots();
|
| 79 | }
|
| 80 |
|
| 81 | return itemstack;
|
| 82 | }
|
| 83 | }
|
| 84 | else
|
| 85 | {
|
| 86 | return null;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | /**
|
| 91 | * if par1 slot has changed, does resetRecipeAndSlots need to be called?
|
| 92 | */
|
| 93 | private boolean inventoryResetNeededOnSlotChange(int p_70469_1_)
|
| 94 | {
|
| 95 | return p_70469_1_ == 0 || p_70469_1_ == 1;
|
| 96 | }
|
| 97 |
|
| 98 | /**
|
| 99 | * Removes a stack from the given slot and returns it.
|
| 100 | */
|
| 101 | public ItemStack removeStackFromSlot(int index)
|
| 102 | {
|
| 103 | if (this.theInventory[index] != null)
|
| 104 | {
|
| 105 | ItemStack itemstack = this.theInventory[index];
|
| 106 | this.theInventory[index] = null;
|
| 107 | return itemstack;
|
| 108 | }
|
| 109 | else
|
| 110 | {
|
| 111 | return null;
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
|
| 117 | */
|
| 118 | public void setInventorySlotContents(int index, ItemStack stack)
|
| 119 | {
|
| 120 | this.theInventory[index] = stack;
|
| 121 |
|
| 122 | if (stack != null && stack.stackSize > this.getInventoryStackLimit())
|
| 123 | {
|
| 124 | stack.stackSize = this.getInventoryStackLimit();
|
| 125 | }
|
| 126 |
|
| 127 | if (this.inventoryResetNeededOnSlotChange(index))
|
| 128 | {
|
| 129 | this.resetRecipeAndSlots();
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | /**
|
| 134 | * Get the name of this object. For players this returns their username
|
| 135 | */
|
| 136 | public String getName()
|
| 137 | {
|
| 138 | return "mob.villager";
|
| 139 | }
|
| 140 |
|
| 141 | /**
|
| 142 | * Returns true if this thing is named
|
| 143 | */
|
| 144 | public boolean hasCustomName()
|
| 145 | {
|
| 146 | return false;
|
| 147 | }
|
| 148 |
|
| 149 | /**
|
| 150 | * Get the formatted ChatComponent that will be used for the sender's username in chat
|
| 151 | */
|
| 152 | public IChatComponent getDisplayName()
|
| 153 | {
|
| 154 | return (IChatComponent)(this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName(), new Object[0]));
|
| 155 | }
|
| 156 |
|
| 157 | /**
|
| 158 | * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
|
| 159 | */
|
| 160 | public int getInventoryStackLimit()
|
| 161 | {
|
| 162 | return 64;
|
| 163 | }
|
| 164 |
|
| 165 | /**
|
| 166 | * Do not make give this method the name canInteractWith because it clashes with Container
|
| 167 | */
|
| 168 | public boolean isUseableByPlayer(EntityPlayer player)
|
| 169 | {
|
| 170 | return this.theMerchant.getCustomer() == player;
|
| 171 | }
|
| 172 |
|
| 173 | public void openInventory(EntityPlayer player)
|
| 174 | {
|
| 175 | }
|
| 176 |
|
| 177 | public void closeInventory(EntityPlayer player)
|
| 178 | {
|
| 179 | }
|
| 180 |
|
| 181 | /**
|
| 182 | * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
|
| 183 | */
|
| 184 | public boolean isItemValidForSlot(int index, ItemStack stack)
|
| 185 | {
|
| 186 | return true;
|
| 187 | }
|
| 188 |
|
| 189 | /**
|
| 190 | * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
|
| 191 | * hasn't changed and skip it.
|
| 192 | */
|
| 193 | public void markDirty()
|
| 194 | {
|
| 195 | this.resetRecipeAndSlots();
|
| 196 | }
|
| 197 |
|
| 198 | public void resetRecipeAndSlots()
|
| 199 | {
|
| 200 | this.currentRecipe = null;
|
| 201 | ItemStack itemstack = this.theInventory[0];
|
| 202 | ItemStack itemstack1 = this.theInventory[1];
|
| 203 |
|
| 204 | if (itemstack == null)
|
| 205 | {
|
| 206 | itemstack = itemstack1;
|
| 207 | itemstack1 = null;
|
| 208 | }
|
| 209 |
|
| 210 | if (itemstack == null)
|
| 211 | {
|
| 212 | this.setInventorySlotContents(2, (ItemStack)null);
|
| 213 | }
|
| 214 | else
|
| 215 | {
|
| 216 | MerchantRecipeList merchantrecipelist = this.theMerchant.getRecipes(this.thePlayer);
|
| 217 |
|
| 218 | if (merchantrecipelist != null)
|
| 219 | {
|
| 220 | MerchantRecipe merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack, itemstack1, this.currentRecipeIndex);
|
| 221 |
|
| 222 | if (merchantrecipe != null && !merchantrecipe.isRecipeDisabled())
|
| 223 | {
|
| 224 | this.currentRecipe = merchantrecipe;
|
| 225 | this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy());
|
| 226 | }
|
| 227 | else if (itemstack1 != null)
|
| 228 | {
|
| 229 | merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack1, itemstack, this.currentRecipeIndex);
|
| 230 |
|
| 231 | if (merchantrecipe != null && !merchantrecipe.isRecipeDisabled())
|
| 232 | {
|
| 233 | this.currentRecipe = merchantrecipe;
|
| 234 | this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy());
|
| 235 | }
|
| 236 | else
|
| 237 | {
|
| 238 | this.setInventorySlotContents(2, (ItemStack)null);
|
| 239 | }
|
| 240 | }
|
| 241 | else
|
| 242 | {
|
| 243 | this.setInventorySlotContents(2, (ItemStack)null);
|
| 244 | }
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | this.theMerchant.verifySellingItem(this.getStackInSlot(2));
|
| 249 | }
|
| 250 |
|
| 251 | public MerchantRecipe getCurrentRecipe()
|
| 252 | {
|
| 253 | return this.currentRecipe;
|
| 254 | }
|
| 255 |
|
| 256 | public void setCurrentRecipeIndex(int currentRecipeIndexIn)
|
| 257 | {
|
| 258 | this.currentRecipeIndex = currentRecipeIndexIn;
|
| 259 | this.resetRecipeAndSlots();
|
| 260 | }
|
| 261 |
|
| 262 | public int getField(int id)
|
| 263 | {
|
| 264 | return 0;
|
| 265 | }
|
| 266 |
|
| 267 | public void setField(int id, int value)
|
| 268 | {
|
| 269 | }
|
| 270 |
|
| 271 | public int getFieldCount()
|
| 272 | {
|
| 273 | return 0;
|
| 274 | }
|
| 275 |
|
| 276 | public void clear()
|
| 277 | {
|
| 278 | for (int i = 0; i < this.theInventory.length; ++i)
|
| 279 | {
|
| 280 | this.theInventory[i] = null;
|
| 281 | }
|
| 282 | }
|
| 283 | }
|
| 284 |
|