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 21 commits into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,12 @@ public function onEntityInside(Entity $entity) : bool{
return true;
}

/**
* Called when an entity collide on this block
*/
public function onEntityCollide(Entity $entity, int $face) : void{
}
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::DOWN){
$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::DOWN && $entity instanceof Living && !$entity->isSneaking()){
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
$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);
Copy link
Member

Choose a reason for hiding this comment

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

Why is this different for the Y axis than X/Z?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially, I started testing with 0.001 for each axis but I found a bug on the Y axis with a slab. Indeed, if you put a block of magma under a slab and a player steps on this slab, with 0.001 on the Y axis, the collision is not detected so I put 0.1 to detect it

Copy link
Member

Choose a reason for hiding this comment

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

That doesn't make any sense. Is magma supposed to hurt you from underneath a slab? And if it is, why does 0.1 make a difference since the BB of a slab is 0.5 thick?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After some reseach, player take damage on magma block if block between player and magma block has AABB below or equal to 13/16 like trapdoor (https://minecraft.wiki/w/Magma_Block#Damage). So the y value is 1-13/16 = 3/16.

Copy link
Member

Choose a reason for hiding this comment

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

Then it seems like a collide function isn't appropriate for this. We need something more like onEntityNearby() or onEntityAdjacent().

foreach (Facing::ALL as $face) {
$block = $entityBlock->getSide($face);
foreach ($block->getCollisionBoxes() as $blockBox) {
if ($entityBox->intersectsWith($blockBox)) {
$block->onEntityCollide($this, $face);
break;
Dhaiven marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

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