public function onUpdate()
{
if (!$this->getBlock() instanceof HopperBlock) {
return false;
}
//Pickup dropped items
//This can happen at any time regardless of cooldown
$area = clone $this->getBlock()->getBoundingBox();
//Area above hopper to draw items from
$area->maxY = ceil($area->maxY) + 1;
//Account for full block above, not just 1 + 5/8
foreach ($this->getLevel()->getChunkEntities($this->getBlock()->x >> 4, $this->getBlock()->z >> 4) as $entity) {
if (!$entity instanceof DroppedItem or !$entity->isAlive()) {
continue;
}
if (!$entity->boundingBox->intersectsWith($area)) {
continue;
}
$item = $entity->getItem();
if (!$item instanceof Item) {
continue;
}
if ($item->getCount() < 1) {
$entity->kill();
continue;
}
if ($this->inventory->canAddItem($item)) {
$this->inventory->addItem($item);
$entity->kill();
}
}
if (!$this->canUpdate()) {
//Hoppers only update CONTENTS every 8th tick
$this->namedtag->TransferCooldown->setValue($this->namedtag->TransferCooldown->getValue() - 1);
return true;
}
//Suck items from above tile inventories
$source = $this->getLevel()->getTile($this->getBlock()->getSide(Vector3::SIDE_UP));
if ($source instanceof Tile and $source instanceof InventoryHolder) {
$inventory = $source->getInventory();
$item = clone $inventory->getItem($inventory->firstOccupied());
$item->setCount(1);
if ($this->inventory->canAddItem($item)) {
$this->inventory->addItem($item);
$inventory->removeItem($item);
$this->resetCooldownTicks();
if ($source instanceof Hopper) {
$source->resetCooldownTicks();
}
}
}
//Feed item into target inventory
//Do not do this if there's a hopper underneath this hopper, to follow vanilla behaviour
if (!$this->getLevel()->getTile($this->getBlock()->getSide(Vector3::SIDE_DOWN)) instanceof Hopper) {
$target = $this->getLevel()->getTile($this->getBlock()->getSide($this->getBlock()->getDamage()));
if ($target instanceof Tile and $target instanceof InventoryHolder) {
$inv = $target->getInventory();
foreach ($this->inventory->getContents() as $item) {
if ($item->getId() === Item::AIR or $item->getCount() < 1) {
continue;
}
$targetItem = clone $item;
$targetItem->setCount(1);
if ($inv->canAddItem($targetItem)) {
$inv->addItem($targetItem);
$this->inventory->removeItem($targetItem);
$this->resetCooldownTicks();
if ($target instanceof Hopper) {
$target->resetCooldownTicks();
}
break;
}
}
}
}
return true;
}