Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onEntityCollide function when entity are above a block #6347

Open
wants to merge 13 commits into
base: stable
Choose a base branch
from
7 changes: 7 additions & 0 deletions src/block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,13 @@ public function onEntityInside(Entity $entity) : bool{
return true;
}

/**
* Called when an entity collide on this block
*/
public function onEntityCollide(Entity $entity, int $face) : void{
//NOOP
}
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a direction vector describing which way an entity intersecting this block should be pushed.
* This is used by liquids to push entities in liquid currents.
Expand Down
7 changes: 7 additions & 0 deletions src/block/Cactus.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public function onEntityInside(Entity $entity) : bool{
return true;
}

public function onEntityCollide(Entity $entity, int $face) : void{
if($face === Facing::UP){
$ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_CONTACT, 1);
$entity->attack($ev);
}
}

private function canBeSupportedAt(Block $block) : bool{
$supportBlock = $block->getSide(Facing::DOWN);
if(!$supportBlock->hasSameTypeId($this) && !$supportBlock->hasTypeTag(BlockTypeTags::SAND)){
Expand Down
10 changes: 3 additions & 7 deletions src/block/Magma.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,19 @@
use pocketmine\entity\Living;
use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\math\Facing;

class Magma extends Opaque{

public function getLightLevel() : int{
return 3;
}

public function hasEntityCollision() : bool{
return true;
}

public function onEntityInside(Entity $entity) : bool{
if($entity instanceof Living && !$entity->isSneaking()){
public function onEntityCollide(Entity $entity, int $face) : void{
if($face === Facing::UP && $entity instanceof Living && !$entity->isSneaking()){
$ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1);
$entity->attack($ev);
}
return true;
}

public function burnsForever() : bool{
Expand Down
12 changes: 12 additions & 0 deletions src/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,18 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{
$ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_VOID, 10);
$this->attack($ev);
$hasUpdate = true;
}else{
$entityBlock = $this->getWorld()->getBlock($this->getLocation());
$entityBox = $this->getBoundingBox()->expandedCopy(0.001, 0.1, 0.001);
foreach (Facing::ALL as $face) {
$block = $entityBlock->getSide($face);
foreach ($block->getCollisionBoxes() as $blockBox) {
if ($entityBox->intersectsWith($blockBox)) {
$block->onEntityCollide($this, Facing::opposite($face));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this case add this?

$hasUpdate = true;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be but if i realy good understand, hasUpdate variable is set to true only if entity is update. However, in function onCollide, we are not sure that entity is update.
So, i don't realy sure

break;
}
}
}
}

if($this->isOnFire() && $this->doOnFireTick($tickDiff)){
Expand Down