| 1 | package net.minecraft.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Random;
|
| 5 | import net.minecraft.enchantment.EnchantmentData;
|
| 6 | import net.minecraft.enchantment.EnchantmentHelper;
|
| 7 | import net.minecraft.entity.player.EntityPlayer;
|
| 8 | import net.minecraft.entity.player.InventoryPlayer;
|
| 9 | import net.minecraft.init.Blocks;
|
| 10 | import net.minecraft.init.Items;
|
| 11 | import net.minecraft.item.EnumDyeColor;
|
| 12 | import net.minecraft.item.ItemStack;
|
| 13 | import net.minecraft.stats.StatList;
|
| 14 | import net.minecraft.util.BlockPos;
|
| 15 | import net.minecraft.world.World;
|
| 16 |
|
| 17 | public class ContainerEnchantment extends Container
|
| 18 | {
|
| 19 | /** SlotEnchantmentTable object with ItemStack to be enchanted */
|
| 20 | public IInventory tableInventory;
|
| 21 |
|
| 22 | /** current world (for bookshelf counting) */
|
| 23 | private World worldPointer;
|
| 24 | private BlockPos position;
|
| 25 | private Random rand;
|
| 26 | public int xpSeed;
|
| 27 |
|
| 28 | /** 3-member array storing the enchantment levels of each slot */
|
| 29 | public int[] enchantLevels;
|
| 30 | public int[] enchantmentIds;
|
| 31 |
|
| 32 | public ContainerEnchantment(InventoryPlayer playerInv, World worldIn)
|
| 33 | {
|
| 34 | this(playerInv, worldIn, BlockPos.ORIGIN);
|
| 35 | }
|
| 36 |
|
| 37 | public ContainerEnchantment(InventoryPlayer playerInv, World worldIn, BlockPos pos)
|
| 38 | {
|
| 39 | this.tableInventory = new InventoryBasic("Enchant", true, 2)
|
| 40 | {
|
| 41 | public int getInventoryStackLimit()
|
| 42 | {
|
| 43 | return 64;
|
| 44 | }
|
| 45 | public void markDirty()
|
| 46 | {
|
| 47 | super.markDirty();
|
| 48 | ContainerEnchantment.this.onCraftMatrixChanged(this);
|
| 49 | }
|
| 50 | };
|
| 51 | this.rand = new Random();
|
| 52 | this.enchantLevels = new int[3];
|
| 53 | this.enchantmentIds = new int[] { -1, -1, -1};
|
| 54 | this.worldPointer = worldIn;
|
| 55 | this.position = pos;
|
| 56 | this.xpSeed = playerInv.player.getXPSeed();
|
| 57 | this.addSlotToContainer(new Slot(this.tableInventory, 0, 15, 47)
|
| 58 | {
|
| 59 | public boolean isItemValid(ItemStack stack)
|
| 60 | {
|
| 61 | return true;
|
| 62 | }
|
| 63 | public int getSlotStackLimit()
|
| 64 | {
|
| 65 | return 1;
|
| 66 | }
|
| 67 | });
|
| 68 | this.addSlotToContainer(new Slot(this.tableInventory, 1, 35, 47)
|
| 69 | {
|
| 70 | public boolean isItemValid(ItemStack stack)
|
| 71 | {
|
| 72 | return stack.getItem() == Items.dye && EnumDyeColor.byDyeDamage(stack.getMetadata()) == EnumDyeColor.BLUE;
|
| 73 | }
|
| 74 | });
|
| 75 |
|
| 76 | for (int i = 0; i < 3; ++i)
|
| 77 | {
|
| 78 | for (int j = 0; j < 9; ++j)
|
| 79 | {
|
| 80 | this.addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | for (int k = 0; k < 9; ++k)
|
| 85 | {
|
| 86 | this.addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142));
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | public void onCraftGuiOpened(ICrafting listener)
|
| 91 | {
|
| 92 | super.onCraftGuiOpened(listener);
|
| 93 | listener.sendProgressBarUpdate(this, 0, this.enchantLevels[0]);
|
| 94 | listener.sendProgressBarUpdate(this, 1, this.enchantLevels[1]);
|
| 95 | listener.sendProgressBarUpdate(this, 2, this.enchantLevels[2]);
|
| 96 | listener.sendProgressBarUpdate(this, 3, this.xpSeed & -16);
|
| 97 | listener.sendProgressBarUpdate(this, 4, this.enchantmentIds[0]);
|
| 98 | listener.sendProgressBarUpdate(this, 5, this.enchantmentIds[1]);
|
| 99 | listener.sendProgressBarUpdate(this, 6, this.enchantmentIds[2]);
|
| 100 | }
|
| 101 |
|
| 102 | /**
|
| 103 | * Looks for changes made in the container, sends them to every listener.
|
| 104 | */
|
| 105 | public void detectAndSendChanges()
|
| 106 | {
|
| 107 | super.detectAndSendChanges();
|
| 108 |
|
| 109 | for (int i = 0; i < this.crafters.size(); ++i)
|
| 110 | {
|
| 111 | ICrafting icrafting = (ICrafting)this.crafters.get(i);
|
| 112 | icrafting.sendProgressBarUpdate(this, 0, this.enchantLevels[0]);
|
| 113 | icrafting.sendProgressBarUpdate(this, 1, this.enchantLevels[1]);
|
| 114 | icrafting.sendProgressBarUpdate(this, 2, this.enchantLevels[2]);
|
| 115 | icrafting.sendProgressBarUpdate(this, 3, this.xpSeed & -16);
|
| 116 | icrafting.sendProgressBarUpdate(this, 4, this.enchantmentIds[0]);
|
| 117 | icrafting.sendProgressBarUpdate(this, 5, this.enchantmentIds[1]);
|
| 118 | icrafting.sendProgressBarUpdate(this, 6, this.enchantmentIds[2]);
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | public void updateProgressBar(int id, int data)
|
| 123 | {
|
| 124 | if (id >= 0 && id <= 2)
|
| 125 | {
|
| 126 | this.enchantLevels[id] = data;
|
| 127 | }
|
| 128 | else if (id == 3)
|
| 129 | {
|
| 130 | this.xpSeed = data;
|
| 131 | }
|
| 132 | else if (id >= 4 && id <= 6)
|
| 133 | {
|
| 134 | this.enchantmentIds[id - 4] = data;
|
| 135 | }
|
| 136 | else
|
| 137 | {
|
| 138 | super.updateProgressBar(id, data);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | /**
|
| 143 | * Callback for when the crafting matrix is changed.
|
| 144 | */
|
| 145 | public void onCraftMatrixChanged(IInventory inventoryIn)
|
| 146 | {
|
| 147 | if (inventoryIn == this.tableInventory)
|
| 148 | {
|
| 149 | ItemStack itemstack = inventoryIn.getStackInSlot(0);
|
| 150 |
|
| 151 | if (itemstack != null && itemstack.isItemEnchantable())
|
| 152 | {
|
| 153 | if (!this.worldPointer.isRemote)
|
| 154 | {
|
| 155 | int l = 0;
|
| 156 |
|
| 157 | for (int j = -1; j <= 1; ++j)
|
| 158 | {
|
| 159 | for (int k = -1; k <= 1; ++k)
|
| 160 | {
|
| 161 | if ((j != 0 || k != 0) && this.worldPointer.isAirBlock(this.position.add(k, 0, j)) && this.worldPointer.isAirBlock(this.position.add(k, 1, j)))
|
| 162 | {
|
| 163 | if (this.worldPointer.getBlockState(this.position.add(k * 2, 0, j * 2)).getBlock() == Blocks.bookshelf)
|
| 164 | {
|
| 165 | ++l;
|
| 166 | }
|
| 167 |
|
| 168 | if (this.worldPointer.getBlockState(this.position.add(k * 2, 1, j * 2)).getBlock() == Blocks.bookshelf)
|
| 169 | {
|
| 170 | ++l;
|
| 171 | }
|
| 172 |
|
| 173 | if (k != 0 && j != 0)
|
| 174 | {
|
| 175 | if (this.worldPointer.getBlockState(this.position.add(k * 2, 0, j)).getBlock() == Blocks.bookshelf)
|
| 176 | {
|
| 177 | ++l;
|
| 178 | }
|
| 179 |
|
| 180 | if (this.worldPointer.getBlockState(this.position.add(k * 2, 1, j)).getBlock() == Blocks.bookshelf)
|
| 181 | {
|
| 182 | ++l;
|
| 183 | }
|
| 184 |
|
| 185 | if (this.worldPointer.getBlockState(this.position.add(k, 0, j * 2)).getBlock() == Blocks.bookshelf)
|
| 186 | {
|
| 187 | ++l;
|
| 188 | }
|
| 189 |
|
| 190 | if (this.worldPointer.getBlockState(this.position.add(k, 1, j * 2)).getBlock() == Blocks.bookshelf)
|
| 191 | {
|
| 192 | ++l;
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | this.rand.setSeed((long)this.xpSeed);
|
| 200 |
|
| 201 | for (int i1 = 0; i1 < 3; ++i1)
|
| 202 | {
|
| 203 | this.enchantLevels[i1] = EnchantmentHelper.calcItemStackEnchantability(this.rand, i1, l, itemstack);
|
| 204 | this.enchantmentIds[i1] = -1;
|
| 205 |
|
| 206 | if (this.enchantLevels[i1] < i1 + 1)
|
| 207 | {
|
| 208 | this.enchantLevels[i1] = 0;
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | for (int j1 = 0; j1 < 3; ++j1)
|
| 213 | {
|
| 214 | if (this.enchantLevels[j1] > 0)
|
| 215 | {
|
| 216 | List<EnchantmentData> list = this.func_178148_a(itemstack, j1, this.enchantLevels[j1]);
|
| 217 |
|
| 218 | if (list != null && !list.isEmpty())
|
| 219 | {
|
| 220 | EnchantmentData enchantmentdata = (EnchantmentData)list.get(this.rand.nextInt(list.size()));
|
| 221 | this.enchantmentIds[j1] = enchantmentdata.enchantmentobj.effectId | enchantmentdata.enchantmentLevel << 8;
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | this.detectAndSendChanges();
|
| 227 | }
|
| 228 | }
|
| 229 | else
|
| 230 | {
|
| 231 | for (int i = 0; i < 3; ++i)
|
| 232 | {
|
| 233 | this.enchantLevels[i] = 0;
|
| 234 | this.enchantmentIds[i] = -1;
|
| 235 | }
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | /**
|
| 241 | * Handles the given Button-click on the server, currently only used by enchanting. Name is for legacy.
|
| 242 | */
|
| 243 | public boolean enchantItem(EntityPlayer playerIn, int id)
|
| 244 | {
|
| 245 | ItemStack itemstack = this.tableInventory.getStackInSlot(0);
|
| 246 | ItemStack itemstack1 = this.tableInventory.getStackInSlot(1);
|
| 247 | int i = id + 1;
|
| 248 |
|
| 249 | if ((itemstack1 == null || itemstack1.stackSize < i) && !playerIn.capabilities.isCreativeMode)
|
| 250 | {
|
| 251 | return false;
|
| 252 | }
|
| 253 | else if (this.enchantLevels[id] > 0 && itemstack != null && (playerIn.experienceLevel >= i && playerIn.experienceLevel >= this.enchantLevels[id] || playerIn.capabilities.isCreativeMode))
|
| 254 | {
|
| 255 | if (!this.worldPointer.isRemote)
|
| 256 | {
|
| 257 | List<EnchantmentData> list = this.func_178148_a(itemstack, id, this.enchantLevels[id]);
|
| 258 | boolean flag = itemstack.getItem() == Items.book;
|
| 259 |
|
| 260 | if (list != null)
|
| 261 | {
|
| 262 | playerIn.removeExperienceLevel(i);
|
| 263 |
|
| 264 | if (flag)
|
| 265 | {
|
| 266 | itemstack.setItem(Items.enchanted_book);
|
| 267 | }
|
| 268 |
|
| 269 | for (int j = 0; j < list.size(); ++j)
|
| 270 | {
|
| 271 | EnchantmentData enchantmentdata = (EnchantmentData)list.get(j);
|
| 272 |
|
| 273 | if (flag)
|
| 274 | {
|
| 275 | Items.enchanted_book.addEnchantment(itemstack, enchantmentdata);
|
| 276 | }
|
| 277 | else
|
| 278 | {
|
| 279 | itemstack.addEnchantment(enchantmentdata.enchantmentobj, enchantmentdata.enchantmentLevel);
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | if (!playerIn.capabilities.isCreativeMode)
|
| 284 | {
|
| 285 | itemstack1.stackSize -= i;
|
| 286 |
|
| 287 | if (itemstack1.stackSize <= 0)
|
| 288 | {
|
| 289 | this.tableInventory.setInventorySlotContents(1, (ItemStack)null);
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | playerIn.triggerAchievement(StatList.field_181739_W);
|
| 294 | this.tableInventory.markDirty();
|
| 295 | this.xpSeed = playerIn.getXPSeed();
|
| 296 | this.onCraftMatrixChanged(this.tableInventory);
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | return true;
|
| 301 | }
|
| 302 | else
|
| 303 | {
|
| 304 | return false;
|
| 305 | }
|
| 306 | }
|
| 307 |
|
| 308 | private List<EnchantmentData> func_178148_a(ItemStack stack, int p_178148_2_, int p_178148_3_)
|
| 309 | {
|
| 310 | this.rand.setSeed((long)(this.xpSeed + p_178148_2_));
|
| 311 | List<EnchantmentData> list = EnchantmentHelper.buildEnchantmentList(this.rand, stack, p_178148_3_);
|
| 312 |
|
| 313 | if (stack.getItem() == Items.book && list != null && list.size() > 1)
|
| 314 | {
|
| 315 | list.remove(this.rand.nextInt(list.size()));
|
| 316 | }
|
| 317 |
|
| 318 | return list;
|
| 319 | }
|
| 320 |
|
| 321 | public int getLapisAmount()
|
| 322 | {
|
| 323 | ItemStack itemstack = this.tableInventory.getStackInSlot(1);
|
| 324 | return itemstack == null ? 0 : itemstack.stackSize;
|
| 325 | }
|
| 326 |
|
| 327 | /**
|
| 328 | * Called when the container is closed.
|
| 329 | */
|
| 330 | public void onContainerClosed(EntityPlayer playerIn)
|
| 331 | {
|
| 332 | super.onContainerClosed(playerIn);
|
| 333 |
|
| 334 | if (!this.worldPointer.isRemote)
|
| 335 | {
|
| 336 | for (int i = 0; i < this.tableInventory.getSizeInventory(); ++i)
|
| 337 | {
|
| 338 | ItemStack itemstack = this.tableInventory.removeStackFromSlot(i);
|
| 339 |
|
| 340 | if (itemstack != null)
|
| 341 | {
|
| 342 | playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
|
| 343 | }
|
| 344 | }
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | public boolean canInteractWith(EntityPlayer playerIn)
|
| 349 | {
|
| 350 | return this.worldPointer.getBlockState(this.position).getBlock() != Blocks.enchanting_table ? false : playerIn.getDistanceSq((double)this.position.getX() + 0.5D, (double)this.position.getY() + 0.5D, (double)this.position.getZ() + 0.5D) <= 64.0D;
|
| 351 | }
|
| 352 |
|
| 353 | /**
|
| 354 | * Take a stack from the specified inventory slot.
|
| 355 | */
|
| 356 | public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
|
| 357 | {
|
| 358 | ItemStack itemstack = null;
|
| 359 | Slot slot = (Slot)this.inventorySlots.get(index);
|
| 360 |
|
| 361 | if (slot != null && slot.getHasStack())
|
| 362 | {
|
| 363 | ItemStack itemstack1 = slot.getStack();
|
| 364 | itemstack = itemstack1.copy();
|
| 365 |
|
| 366 | if (index == 0)
|
| 367 | {
|
| 368 | if (!this.mergeItemStack(itemstack1, 2, 38, true))
|
| 369 | {
|
| 370 | return null;
|
| 371 | }
|
| 372 | }
|
| 373 | else if (index == 1)
|
| 374 | {
|
| 375 | if (!this.mergeItemStack(itemstack1, 2, 38, true))
|
| 376 | {
|
| 377 | return null;
|
| 378 | }
|
| 379 | }
|
| 380 | else if (itemstack1.getItem() == Items.dye && EnumDyeColor.byDyeDamage(itemstack1.getMetadata()) == EnumDyeColor.BLUE)
|
| 381 | {
|
| 382 | if (!this.mergeItemStack(itemstack1, 1, 2, true))
|
| 383 | {
|
| 384 | return null;
|
| 385 | }
|
| 386 | }
|
| 387 | else
|
| 388 | {
|
| 389 | if (((Slot)this.inventorySlots.get(0)).getHasStack() || !((Slot)this.inventorySlots.get(0)).isItemValid(itemstack1))
|
| 390 | {
|
| 391 | return null;
|
| 392 | }
|
| 393 |
|
| 394 | if (itemstack1.hasTagCompound() && itemstack1.stackSize == 1)
|
| 395 | {
|
| 396 | ((Slot)this.inventorySlots.get(0)).putStack(itemstack1.copy());
|
| 397 | itemstack1.stackSize = 0;
|
| 398 | }
|
| 399 | else if (itemstack1.stackSize >= 1)
|
| 400 | {
|
| 401 | ((Slot)this.inventorySlots.get(0)).putStack(new ItemStack(itemstack1.getItem(), 1, itemstack1.getMetadata()));
|
| 402 | --itemstack1.stackSize;
|
| 403 | }
|
| 404 | }
|
| 405 |
|
| 406 | if (itemstack1.stackSize == 0)
|
| 407 | {
|
| 408 | slot.putStack((ItemStack)null);
|
| 409 | }
|
| 410 | else
|
| 411 | {
|
| 412 | slot.onSlotChanged();
|
| 413 | }
|
| 414 |
|
| 415 | if (itemstack1.stackSize == itemstack.stackSize)
|
| 416 | {
|
| 417 | return null;
|
| 418 | }
|
| 419 |
|
| 420 | slot.onPickupFromSlot(playerIn, itemstack1);
|
| 421 | }
|
| 422 |
|
| 423 | return itemstack;
|
| 424 | }
|
| 425 | }
|
| 426 |
|