| 1 | package net.minecraft.tileentity;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.block.Block;
|
| 5 | import net.minecraft.block.BlockChest;
|
| 6 | import net.minecraft.block.BlockHopper;
|
| 7 | import net.minecraft.entity.Entity;
|
| 8 | import net.minecraft.entity.item.EntityItem;
|
| 9 | import net.minecraft.entity.player.EntityPlayer;
|
| 10 | import net.minecraft.entity.player.InventoryPlayer;
|
| 11 | import net.minecraft.inventory.Container;
|
| 12 | import net.minecraft.inventory.ContainerHopper;
|
| 13 | import net.minecraft.inventory.IInventory;
|
| 14 | import net.minecraft.inventory.ISidedInventory;
|
| 15 | import net.minecraft.item.ItemStack;
|
| 16 | import net.minecraft.nbt.NBTTagCompound;
|
| 17 | import net.minecraft.nbt.NBTTagList;
|
| 18 | import net.minecraft.util.AxisAlignedBB;
|
| 19 | import net.minecraft.util.BlockPos;
|
| 20 | import net.minecraft.util.EntitySelectors;
|
| 21 | import net.minecraft.util.EnumFacing;
|
| 22 | import net.minecraft.util.ITickable;
|
| 23 | import net.minecraft.util.MathHelper;
|
| 24 | import net.minecraft.world.World;
|
| 25 |
|
| 26 | public class TileEntityHopper extends TileEntityLockable implements IHopper, ITickable
|
| 27 | {
|
| 28 | private ItemStack[] inventory = new ItemStack[5];
|
| 29 | private String customName;
|
| 30 | private int transferCooldown = -1;
|
| 31 |
|
| 32 | public void readFromNBT(NBTTagCompound compound)
|
| 33 | {
|
| 34 | super.readFromNBT(compound);
|
| 35 | NBTTagList nbttaglist = compound.getTagList("Items", 10);
|
| 36 | this.inventory = new ItemStack[this.getSizeInventory()];
|
| 37 |
|
| 38 | if (compound.hasKey("CustomName", 8))
|
| 39 | {
|
| 40 | this.customName = compound.getString("CustomName");
|
| 41 | }
|
| 42 |
|
| 43 | this.transferCooldown = compound.getInteger("TransferCooldown");
|
| 44 |
|
| 45 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 46 | {
|
| 47 | NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
| 48 | int j = nbttagcompound.getByte("Slot");
|
| 49 |
|
| 50 | if (j >= 0 && j < this.inventory.length)
|
| 51 | {
|
| 52 | this.inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | public void writeToNBT(NBTTagCompound compound)
|
| 58 | {
|
| 59 | super.writeToNBT(compound);
|
| 60 | NBTTagList nbttaglist = new NBTTagList();
|
| 61 |
|
| 62 | for (int i = 0; i < this.inventory.length; ++i)
|
| 63 | {
|
| 64 | if (this.inventory[i] != null)
|
| 65 | {
|
| 66 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 67 | nbttagcompound.setByte("Slot", (byte)i);
|
| 68 | this.inventory[i].writeToNBT(nbttagcompound);
|
| 69 | nbttaglist.appendTag(nbttagcompound);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | compound.setTag("Items", nbttaglist);
|
| 74 | compound.setInteger("TransferCooldown", this.transferCooldown);
|
| 75 |
|
| 76 | if (this.hasCustomName())
|
| 77 | {
|
| 78 | compound.setString("CustomName", this.customName);
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | /**
|
| 83 | * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
|
| 84 | * hasn't changed and skip it.
|
| 85 | */
|
| 86 | public void markDirty()
|
| 87 | {
|
| 88 | super.markDirty();
|
| 89 | }
|
| 90 |
|
| 91 | /**
|
| 92 | * Returns the number of slots in the inventory.
|
| 93 | */
|
| 94 | public int getSizeInventory()
|
| 95 | {
|
| 96 | return this.inventory.length;
|
| 97 | }
|
| 98 |
|
| 99 | /**
|
| 100 | * Returns the stack in the given slot.
|
| 101 | */
|
| 102 | public ItemStack getStackInSlot(int index)
|
| 103 | {
|
| 104 | return this.inventory[index];
|
| 105 | }
|
| 106 |
|
| 107 | /**
|
| 108 | * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
|
| 109 | */
|
| 110 | public ItemStack decrStackSize(int index, int count)
|
| 111 | {
|
| 112 | if (this.inventory[index] != null)
|
| 113 | {
|
| 114 | if (this.inventory[index].stackSize <= count)
|
| 115 | {
|
| 116 | ItemStack itemstack1 = this.inventory[index];
|
| 117 | this.inventory[index] = null;
|
| 118 | return itemstack1;
|
| 119 | }
|
| 120 | else
|
| 121 | {
|
| 122 | ItemStack itemstack = this.inventory[index].splitStack(count);
|
| 123 |
|
| 124 | if (this.inventory[index].stackSize == 0)
|
| 125 | {
|
| 126 | this.inventory[index] = null;
|
| 127 | }
|
| 128 |
|
| 129 | return itemstack;
|
| 130 | }
|
| 131 | }
|
| 132 | else
|
| 133 | {
|
| 134 | return null;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | /**
|
| 139 | * Removes a stack from the given slot and returns it.
|
| 140 | */
|
| 141 | public ItemStack removeStackFromSlot(int index)
|
| 142 | {
|
| 143 | if (this.inventory[index] != null)
|
| 144 | {
|
| 145 | ItemStack itemstack = this.inventory[index];
|
| 146 | this.inventory[index] = null;
|
| 147 | return itemstack;
|
| 148 | }
|
| 149 | else
|
| 150 | {
|
| 151 | return null;
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | /**
|
| 156 | * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
|
| 157 | */
|
| 158 | public void setInventorySlotContents(int index, ItemStack stack)
|
| 159 | {
|
| 160 | this.inventory[index] = stack;
|
| 161 |
|
| 162 | if (stack != null && stack.stackSize > this.getInventoryStackLimit())
|
| 163 | {
|
| 164 | stack.stackSize = this.getInventoryStackLimit();
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | /**
|
| 169 | * Get the name of this object. For players this returns their username
|
| 170 | */
|
| 171 | public String getName()
|
| 172 | {
|
| 173 | return this.hasCustomName() ? this.customName : "container.hopper";
|
| 174 | }
|
| 175 |
|
| 176 | /**
|
| 177 | * Returns true if this thing is named
|
| 178 | */
|
| 179 | public boolean hasCustomName()
|
| 180 | {
|
| 181 | return this.customName != null && this.customName.length() > 0;
|
| 182 | }
|
| 183 |
|
| 184 | public void setCustomName(String customNameIn)
|
| 185 | {
|
| 186 | this.customName = customNameIn;
|
| 187 | }
|
| 188 |
|
| 189 | /**
|
| 190 | * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
|
| 191 | */
|
| 192 | public int getInventoryStackLimit()
|
| 193 | {
|
| 194 | return 64;
|
| 195 | }
|
| 196 |
|
| 197 | /**
|
| 198 | * Do not make give this method the name canInteractWith because it clashes with Container
|
| 199 | */
|
| 200 | public boolean isUseableByPlayer(EntityPlayer player)
|
| 201 | {
|
| 202 | return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
|
| 203 | }
|
| 204 |
|
| 205 | public void openInventory(EntityPlayer player)
|
| 206 | {
|
| 207 | }
|
| 208 |
|
| 209 | public void closeInventory(EntityPlayer player)
|
| 210 | {
|
| 211 | }
|
| 212 |
|
| 213 | /**
|
| 214 | * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
|
| 215 | */
|
| 216 | public boolean isItemValidForSlot(int index, ItemStack stack)
|
| 217 | {
|
| 218 | return true;
|
| 219 | }
|
| 220 |
|
| 221 | /**
|
| 222 | * Like the old updateEntity(), except more generic.
|
| 223 | */
|
| 224 | public void update()
|
| 225 | {
|
| 226 | if (this.worldObj != null && !this.worldObj.isRemote)
|
| 227 | {
|
| 228 | --this.transferCooldown;
|
| 229 |
|
| 230 | if (!this.isOnTransferCooldown())
|
| 231 | {
|
| 232 | this.setTransferCooldown(0);
|
| 233 | this.updateHopper();
|
| 234 | }
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | public boolean updateHopper()
|
| 239 | {
|
| 240 | if (this.worldObj != null && !this.worldObj.isRemote)
|
| 241 | {
|
| 242 | if (!this.isOnTransferCooldown() && BlockHopper.isEnabled(this.getBlockMetadata()))
|
| 243 | {
|
| 244 | boolean flag = false;
|
| 245 |
|
| 246 | if (!this.isEmpty())
|
| 247 | {
|
| 248 | flag = this.transferItemsOut();
|
| 249 | }
|
| 250 |
|
| 251 | if (!this.isFull())
|
| 252 | {
|
| 253 | flag = captureDroppedItems(this) || flag;
|
| 254 | }
|
| 255 |
|
| 256 | if (flag)
|
| 257 | {
|
| 258 | this.setTransferCooldown(8);
|
| 259 | this.markDirty();
|
| 260 | return true;
|
| 261 | }
|
| 262 | }
|
| 263 |
|
| 264 | return false;
|
| 265 | }
|
| 266 | else
|
| 267 | {
|
| 268 | return false;
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | private boolean isEmpty()
|
| 273 | {
|
| 274 | for (ItemStack itemstack : this.inventory)
|
| 275 | {
|
| 276 | if (itemstack != null)
|
| 277 | {
|
| 278 | return false;
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | return true;
|
| 283 | }
|
| 284 |
|
| 285 | private boolean isFull()
|
| 286 | {
|
| 287 | for (ItemStack itemstack : this.inventory)
|
| 288 | {
|
| 289 | if (itemstack == null || itemstack.stackSize != itemstack.getMaxStackSize())
|
| 290 | {
|
| 291 | return false;
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | return true;
|
| 296 | }
|
| 297 |
|
| 298 | private boolean transferItemsOut()
|
| 299 | {
|
| 300 | IInventory iinventory = this.getInventoryForHopperTransfer();
|
| 301 |
|
| 302 | if (iinventory == null)
|
| 303 | {
|
| 304 | return false;
|
| 305 | }
|
| 306 | else
|
| 307 | {
|
| 308 | EnumFacing enumfacing = BlockHopper.getFacing(this.getBlockMetadata()).getOpposite();
|
| 309 |
|
| 310 | if (this.isInventoryFull(iinventory, enumfacing))
|
| 311 | {
|
| 312 | return false;
|
| 313 | }
|
| 314 | else
|
| 315 | {
|
| 316 | for (int i = 0; i < this.getSizeInventory(); ++i)
|
| 317 | {
|
| 318 | if (this.getStackInSlot(i) != null)
|
| 319 | {
|
| 320 | ItemStack itemstack = this.getStackInSlot(i).copy();
|
| 321 | ItemStack itemstack1 = putStackInInventoryAllSlots(iinventory, this.decrStackSize(i, 1), enumfacing);
|
| 322 |
|
| 323 | if (itemstack1 == null || itemstack1.stackSize == 0)
|
| 324 | {
|
| 325 | iinventory.markDirty();
|
| 326 | return true;
|
| 327 | }
|
| 328 |
|
| 329 | this.setInventorySlotContents(i, itemstack);
|
| 330 | }
|
| 331 | }
|
| 332 |
|
| 333 | return false;
|
| 334 | }
|
| 335 | }
|
| 336 | }
|
| 337 |
|
| 338 | /**
|
| 339 | * Returns false if the inventory has any room to place items in
|
| 340 | */
|
| 341 | private boolean isInventoryFull(IInventory inventoryIn, EnumFacing side)
|
| 342 | {
|
| 343 | if (inventoryIn instanceof ISidedInventory)
|
| 344 | {
|
| 345 | ISidedInventory isidedinventory = (ISidedInventory)inventoryIn;
|
| 346 | int[] aint = isidedinventory.getSlotsForFace(side);
|
| 347 |
|
| 348 | for (int k = 0; k < aint.length; ++k)
|
| 349 | {
|
| 350 | ItemStack itemstack1 = isidedinventory.getStackInSlot(aint[k]);
|
| 351 |
|
| 352 | if (itemstack1 == null || itemstack1.stackSize != itemstack1.getMaxStackSize())
|
| 353 | {
|
| 354 | return false;
|
| 355 | }
|
| 356 | }
|
| 357 | }
|
| 358 | else
|
| 359 | {
|
| 360 | int i = inventoryIn.getSizeInventory();
|
| 361 |
|
| 362 | for (int j = 0; j < i; ++j)
|
| 363 | {
|
| 364 | ItemStack itemstack = inventoryIn.getStackInSlot(j);
|
| 365 |
|
| 366 | if (itemstack == null || itemstack.stackSize != itemstack.getMaxStackSize())
|
| 367 | {
|
| 368 | return false;
|
| 369 | }
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | return true;
|
| 374 | }
|
| 375 |
|
| 376 | /**
|
| 377 | * Returns false if the specified IInventory contains any items
|
| 378 | */
|
| 379 | private static boolean isInventoryEmpty(IInventory inventoryIn, EnumFacing side)
|
| 380 | {
|
| 381 | if (inventoryIn instanceof ISidedInventory)
|
| 382 | {
|
| 383 | ISidedInventory isidedinventory = (ISidedInventory)inventoryIn;
|
| 384 | int[] aint = isidedinventory.getSlotsForFace(side);
|
| 385 |
|
| 386 | for (int i = 0; i < aint.length; ++i)
|
| 387 | {
|
| 388 | if (isidedinventory.getStackInSlot(aint[i]) != null)
|
| 389 | {
|
| 390 | return false;
|
| 391 | }
|
| 392 | }
|
| 393 | }
|
| 394 | else
|
| 395 | {
|
| 396 | int j = inventoryIn.getSizeInventory();
|
| 397 |
|
| 398 | for (int k = 0; k < j; ++k)
|
| 399 | {
|
| 400 | if (inventoryIn.getStackInSlot(k) != null)
|
| 401 | {
|
| 402 | return false;
|
| 403 | }
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | return true;
|
| 408 | }
|
| 409 |
|
| 410 | public static boolean captureDroppedItems(IHopper p_145891_0_)
|
| 411 | {
|
| 412 | IInventory iinventory = getHopperInventory(p_145891_0_);
|
| 413 |
|
| 414 | if (iinventory != null)
|
| 415 | {
|
| 416 | EnumFacing enumfacing = EnumFacing.DOWN;
|
| 417 |
|
| 418 | if (isInventoryEmpty(iinventory, enumfacing))
|
| 419 | {
|
| 420 | return false;
|
| 421 | }
|
| 422 |
|
| 423 | if (iinventory instanceof ISidedInventory)
|
| 424 | {
|
| 425 | ISidedInventory isidedinventory = (ISidedInventory)iinventory;
|
| 426 | int[] aint = isidedinventory.getSlotsForFace(enumfacing);
|
| 427 |
|
| 428 | for (int i = 0; i < aint.length; ++i)
|
| 429 | {
|
| 430 | if (pullItemFromSlot(p_145891_0_, iinventory, aint[i], enumfacing))
|
| 431 | {
|
| 432 | return true;
|
| 433 | }
|
| 434 | }
|
| 435 | }
|
| 436 | else
|
| 437 | {
|
| 438 | int j = iinventory.getSizeInventory();
|
| 439 |
|
| 440 | for (int k = 0; k < j; ++k)
|
| 441 | {
|
| 442 | if (pullItemFromSlot(p_145891_0_, iinventory, k, enumfacing))
|
| 443 | {
|
| 444 | return true;
|
| 445 | }
|
| 446 | }
|
| 447 | }
|
| 448 | }
|
| 449 | else
|
| 450 | {
|
| 451 | for (EntityItem entityitem : func_181556_a(p_145891_0_.getWorld(), p_145891_0_.getXPos(), p_145891_0_.getYPos() + 1.0D, p_145891_0_.getZPos()))
|
| 452 | {
|
| 453 | if (putDropInInventoryAllSlots(p_145891_0_, entityitem))
|
| 454 | {
|
| 455 | return true;
|
| 456 | }
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | return false;
|
| 461 | }
|
| 462 |
|
| 463 | /**
|
| 464 | * Pulls from the specified slot in the inventory and places in any available slot in the hopper. Returns true if
|
| 465 | * the entire stack was moved
|
| 466 | */
|
| 467 | private static boolean pullItemFromSlot(IHopper hopper, IInventory inventoryIn, int index, EnumFacing direction)
|
| 468 | {
|
| 469 | ItemStack itemstack = inventoryIn.getStackInSlot(index);
|
| 470 |
|
| 471 | if (itemstack != null && canExtractItemFromSlot(inventoryIn, itemstack, index, direction))
|
| 472 | {
|
| 473 | ItemStack itemstack1 = itemstack.copy();
|
| 474 | ItemStack itemstack2 = putStackInInventoryAllSlots(hopper, inventoryIn.decrStackSize(index, 1), (EnumFacing)null);
|
| 475 |
|
| 476 | if (itemstack2 == null || itemstack2.stackSize == 0)
|
| 477 | {
|
| 478 | inventoryIn.markDirty();
|
| 479 | return true;
|
| 480 | }
|
| 481 |
|
| 482 | inventoryIn.setInventorySlotContents(index, itemstack1);
|
| 483 | }
|
| 484 |
|
| 485 | return false;
|
| 486 | }
|
| 487 |
|
| 488 | /**
|
| 489 | * Attempts to place the passed EntityItem's stack into the inventory using as many slots as possible. Returns false
|
| 490 | * if the stackSize of the drop was not depleted.
|
| 491 | */
|
| 492 | public static boolean putDropInInventoryAllSlots(IInventory p_145898_0_, EntityItem itemIn)
|
| 493 | {
|
| 494 | boolean flag = false;
|
| 495 |
|
| 496 | if (itemIn == null)
|
| 497 | {
|
| 498 | return false;
|
| 499 | }
|
| 500 | else
|
| 501 | {
|
| 502 | ItemStack itemstack = itemIn.getEntityItem().copy();
|
| 503 | ItemStack itemstack1 = putStackInInventoryAllSlots(p_145898_0_, itemstack, (EnumFacing)null);
|
| 504 |
|
| 505 | if (itemstack1 != null && itemstack1.stackSize != 0)
|
| 506 | {
|
| 507 | itemIn.setEntityItemStack(itemstack1);
|
| 508 | }
|
| 509 | else
|
| 510 | {
|
| 511 | flag = true;
|
| 512 | itemIn.setDead();
|
| 513 | }
|
| 514 |
|
| 515 | return flag;
|
| 516 | }
|
| 517 | }
|
| 518 |
|
| 519 | /**
|
| 520 | * Attempts to place the passed stack in the inventory, using as many slots as required. Returns leftover items
|
| 521 | */
|
| 522 | public static ItemStack putStackInInventoryAllSlots(IInventory inventoryIn, ItemStack stack, EnumFacing side)
|
| 523 | {
|
| 524 | if (inventoryIn instanceof ISidedInventory && side != null)
|
| 525 | {
|
| 526 | ISidedInventory isidedinventory = (ISidedInventory)inventoryIn;
|
| 527 | int[] aint = isidedinventory.getSlotsForFace(side);
|
| 528 |
|
| 529 | for (int k = 0; k < aint.length && stack != null && stack.stackSize > 0; ++k)
|
| 530 | {
|
| 531 | stack = insertStack(inventoryIn, stack, aint[k], side);
|
| 532 | }
|
| 533 | }
|
| 534 | else
|
| 535 | {
|
| 536 | int i = inventoryIn.getSizeInventory();
|
| 537 |
|
| 538 | for (int j = 0; j < i && stack != null && stack.stackSize > 0; ++j)
|
| 539 | {
|
| 540 | stack = insertStack(inventoryIn, stack, j, side);
|
| 541 | }
|
| 542 | }
|
| 543 |
|
| 544 | if (stack != null && stack.stackSize == 0)
|
| 545 | {
|
| 546 | stack = null;
|
| 547 | }
|
| 548 |
|
| 549 | return stack;
|
| 550 | }
|
| 551 |
|
| 552 | /**
|
| 553 | * Can this hopper insert the specified item from the specified slot on the specified side?
|
| 554 | */
|
| 555 | private static boolean canInsertItemInSlot(IInventory inventoryIn, ItemStack stack, int index, EnumFacing side)
|
| 556 | {
|
| 557 | return !inventoryIn.isItemValidForSlot(index, stack) ? false : !(inventoryIn instanceof ISidedInventory) || ((ISidedInventory)inventoryIn).canInsertItem(index, stack, side);
|
| 558 | }
|
| 559 |
|
| 560 | /**
|
| 561 | * Can this hopper extract the specified item from the specified slot on the specified side?
|
| 562 | */
|
| 563 | private static boolean canExtractItemFromSlot(IInventory inventoryIn, ItemStack stack, int index, EnumFacing side)
|
| 564 | {
|
| 565 | return !(inventoryIn instanceof ISidedInventory) || ((ISidedInventory)inventoryIn).canExtractItem(index, stack, side);
|
| 566 | }
|
| 567 |
|
| 568 | /**
|
| 569 | * Insert the specified stack to the specified inventory and return any leftover items
|
| 570 | */
|
| 571 | private static ItemStack insertStack(IInventory inventoryIn, ItemStack stack, int index, EnumFacing side)
|
| 572 | {
|
| 573 | ItemStack itemstack = inventoryIn.getStackInSlot(index);
|
| 574 |
|
| 575 | if (canInsertItemInSlot(inventoryIn, stack, index, side))
|
| 576 | {
|
| 577 | boolean flag = false;
|
| 578 |
|
| 579 | if (itemstack == null)
|
| 580 | {
|
| 581 | inventoryIn.setInventorySlotContents(index, stack);
|
| 582 | stack = null;
|
| 583 | flag = true;
|
| 584 | }
|
| 585 | else if (canCombine(itemstack, stack))
|
| 586 | {
|
| 587 | int i = stack.getMaxStackSize() - itemstack.stackSize;
|
| 588 | int j = Math.min(stack.stackSize, i);
|
| 589 | stack.stackSize -= j;
|
| 590 | itemstack.stackSize += j;
|
| 591 | flag = j > 0;
|
| 592 | }
|
| 593 |
|
| 594 | if (flag)
|
| 595 | {
|
| 596 | if (inventoryIn instanceof TileEntityHopper)
|
| 597 | {
|
| 598 | TileEntityHopper tileentityhopper = (TileEntityHopper)inventoryIn;
|
| 599 |
|
| 600 | if (tileentityhopper.mayTransfer())
|
| 601 | {
|
| 602 | tileentityhopper.setTransferCooldown(8);
|
| 603 | }
|
| 604 |
|
| 605 | inventoryIn.markDirty();
|
| 606 | }
|
| 607 |
|
| 608 | inventoryIn.markDirty();
|
| 609 | }
|
| 610 | }
|
| 611 |
|
| 612 | return stack;
|
| 613 | }
|
| 614 |
|
| 615 | /**
|
| 616 | * Returns the IInventory that this hopper is pointing into
|
| 617 | */
|
| 618 | private IInventory getInventoryForHopperTransfer()
|
| 619 | {
|
| 620 | EnumFacing enumfacing = BlockHopper.getFacing(this.getBlockMetadata());
|
| 621 | return getInventoryAtPosition(this.getWorld(), (double)(this.pos.getX() + enumfacing.getFrontOffsetX()), (double)(this.pos.getY() + enumfacing.getFrontOffsetY()), (double)(this.pos.getZ() + enumfacing.getFrontOffsetZ()));
|
| 622 | }
|
| 623 |
|
| 624 | /**
|
| 625 | * Returns the IInventory for the specified hopper
|
| 626 | */
|
| 627 | public static IInventory getHopperInventory(IHopper hopper)
|
| 628 | {
|
| 629 | return getInventoryAtPosition(hopper.getWorld(), hopper.getXPos(), hopper.getYPos() + 1.0D, hopper.getZPos());
|
| 630 | }
|
| 631 |
|
| 632 | public static List<EntityItem> func_181556_a(World p_181556_0_, double p_181556_1_, double p_181556_3_, double p_181556_5_)
|
| 633 | {
|
| 634 | return p_181556_0_.<EntityItem>getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(p_181556_1_ - 0.5D, p_181556_3_ - 0.5D, p_181556_5_ - 0.5D, p_181556_1_ + 0.5D, p_181556_3_ + 0.5D, p_181556_5_ + 0.5D), EntitySelectors.selectAnything);
|
| 635 | }
|
| 636 |
|
| 637 | /**
|
| 638 | * Returns the IInventory (if applicable) of the TileEntity at the specified position
|
| 639 | */
|
| 640 | public static IInventory getInventoryAtPosition(World worldIn, double x, double y, double z)
|
| 641 | {
|
| 642 | IInventory iinventory = null;
|
| 643 | int i = MathHelper.floor_double(x);
|
| 644 | int j = MathHelper.floor_double(y);
|
| 645 | int k = MathHelper.floor_double(z);
|
| 646 | BlockPos blockpos = new BlockPos(i, j, k);
|
| 647 | Block block = worldIn.getBlockState(blockpos).getBlock();
|
| 648 |
|
| 649 | if (block.hasTileEntity())
|
| 650 | {
|
| 651 | TileEntity tileentity = worldIn.getTileEntity(blockpos);
|
| 652 |
|
| 653 | if (tileentity instanceof IInventory)
|
| 654 | {
|
| 655 | iinventory = (IInventory)tileentity;
|
| 656 |
|
| 657 | if (iinventory instanceof TileEntityChest && block instanceof BlockChest)
|
| 658 | {
|
| 659 | iinventory = ((BlockChest)block).getLockableContainer(worldIn, blockpos);
|
| 660 | }
|
| 661 | }
|
| 662 | }
|
| 663 |
|
| 664 | if (iinventory == null)
|
| 665 | {
|
| 666 | List<Entity> list = worldIn.getEntitiesInAABBexcluding((Entity)null, new AxisAlignedBB(x - 0.5D, y - 0.5D, z - 0.5D, x + 0.5D, y + 0.5D, z + 0.5D), EntitySelectors.selectInventories);
|
| 667 |
|
| 668 | if (list.size() > 0)
|
| 669 | {
|
| 670 | iinventory = (IInventory)list.get(worldIn.rand.nextInt(list.size()));
|
| 671 | }
|
| 672 | }
|
| 673 |
|
| 674 | return iinventory;
|
| 675 | }
|
| 676 |
|
| 677 | private static boolean canCombine(ItemStack stack1, ItemStack stack2)
|
| 678 | {
|
| 679 | return stack1.getItem() != stack2.getItem() ? false : (stack1.getMetadata() != stack2.getMetadata() ? false : (stack1.stackSize > stack1.getMaxStackSize() ? false : ItemStack.areItemStackTagsEqual(stack1, stack2)));
|
| 680 | }
|
| 681 |
|
| 682 | /**
|
| 683 | * Gets the world X position for this hopper entity.
|
| 684 | */
|
| 685 | public double getXPos()
|
| 686 | {
|
| 687 | return (double)this.pos.getX() + 0.5D;
|
| 688 | }
|
| 689 |
|
| 690 | /**
|
| 691 | * Gets the world Y position for this hopper entity.
|
| 692 | */
|
| 693 | public double getYPos()
|
| 694 | {
|
| 695 | return (double)this.pos.getY() + 0.5D;
|
| 696 | }
|
| 697 |
|
| 698 | /**
|
| 699 | * Gets the world Z position for this hopper entity.
|
| 700 | */
|
| 701 | public double getZPos()
|
| 702 | {
|
| 703 | return (double)this.pos.getZ() + 0.5D;
|
| 704 | }
|
| 705 |
|
| 706 | public void setTransferCooldown(int ticks)
|
| 707 | {
|
| 708 | this.transferCooldown = ticks;
|
| 709 | }
|
| 710 |
|
| 711 | public boolean isOnTransferCooldown()
|
| 712 | {
|
| 713 | return this.transferCooldown > 0;
|
| 714 | }
|
| 715 |
|
| 716 | public boolean mayTransfer()
|
| 717 | {
|
| 718 | return this.transferCooldown <= 1;
|
| 719 | }
|
| 720 |
|
| 721 | public String getGuiID()
|
| 722 | {
|
| 723 | return "minecraft:hopper";
|
| 724 | }
|
| 725 |
|
| 726 | public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
|
| 727 | {
|
| 728 | return new ContainerHopper(playerInventory, this, playerIn);
|
| 729 | }
|
| 730 |
|
| 731 | public int getField(int id)
|
| 732 | {
|
| 733 | return 0;
|
| 734 | }
|
| 735 |
|
| 736 | public void setField(int id, int value)
|
| 737 | {
|
| 738 | }
|
| 739 |
|
| 740 | public int getFieldCount()
|
| 741 | {
|
| 742 | return 0;
|
| 743 | }
|
| 744 |
|
| 745 | public void clear()
|
| 746 | {
|
| 747 | for (int i = 0; i < this.inventory.length; ++i)
|
| 748 | {
|
| 749 | this.inventory[i] = null;
|
| 750 | }
|
| 751 | }
|
| 752 | }
|
| 753 |
|