From 961dbd0be3e1564af408c21e566a5aa55ff826d8 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Tue, 14 May 2024 20:41:29 +0200 Subject: [PATCH 01/15] Add onEntityCollide function --- src/block/Block.php | 6 ++++++ src/block/Magma.php | 3 +-- src/entity/Entity.php | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index dbc269c6301..1c2ed401fc8 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -868,6 +868,12 @@ public function onEntityInside(Entity $entity) : bool{ return true; } + /** + * Called when an entity's is above this block + */ + public function onEntityCollide(Entity $entity) : void{ + } + /** * 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. diff --git a/src/block/Magma.php b/src/block/Magma.php index d2f309325d4..4f0e05e6620 100644 --- a/src/block/Magma.php +++ b/src/block/Magma.php @@ -38,12 +38,11 @@ public function hasEntityCollision() : bool{ return true; } - public function onEntityInside(Entity $entity) : bool{ + public function onEntityCollide(Entity $entity): void{ if($entity instanceof Living && !$entity->isSneaking()){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1); $entity->attack($ev); } - return true; } public function burnsForever() : bool{ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index c55a8716cae..ef3c4764a53 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -658,6 +658,9 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_VOID, 10); $this->attack($ev); $hasUpdate = true; + }elseif($this->isOnGround()){ + $block = $this->getWorld()->getBlock($this->getLocation()->subtract(0, 1, 0)); + $block->onEntityCollide($this); } if($this->isOnFire() && $this->doOnFireTick($tickDiff)){ From 9f5ced28b8df4a123f25ee999cd6e321fea60a62 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Tue, 14 May 2024 20:47:25 +0200 Subject: [PATCH 02/15] Cs --- src/block/Magma.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/block/Magma.php b/src/block/Magma.php index 4f0e05e6620..c6625b16086 100644 --- a/src/block/Magma.php +++ b/src/block/Magma.php @@ -38,7 +38,7 @@ public function hasEntityCollision() : bool{ return true; } - public function onEntityCollide(Entity $entity): void{ + public function onEntityCollide(Entity $entity) : void{ if($entity instanceof Living && !$entity->isSneaking()){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1); $entity->attack($ev); From b1b0a64b254d646ef3ef52a17d284045296cb033 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Wed, 15 May 2024 13:37:48 +0200 Subject: [PATCH 03/15] Fix collision bug --- src/entity/Entity.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index ef3c4764a53..ee48e7a2415 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -660,7 +660,13 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $hasUpdate = true; }elseif($this->isOnGround()){ $block = $this->getWorld()->getBlock($this->getLocation()->subtract(0, 1, 0)); - $block->onEntityCollide($this); + $entityBox = $this->getBoundingBox()->addCoord(0, -0.00001, 0); + foreach ($block->getCollisionBoxes() as $blockBox) { + if ($entityBox->intersectsWith($blockBox)) { + $block->onEntityCollide($this); + break; + } + } } if($this->isOnFire() && $this->doOnFireTick($tickDiff)){ From 9927ab10c175006cb0a867327daa0e7c9940b7c9 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sat, 29 Jun 2024 13:37:13 +0200 Subject: [PATCH 04/15] Work for all faces --- src/block/Block.php | 4 ++-- src/block/Magma.php | 5 +++-- src/entity/Entity.php | 15 +++++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index 1c2ed401fc8..b13b0298abd 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -869,9 +869,9 @@ public function onEntityInside(Entity $entity) : bool{ } /** - * Called when an entity's is above this block + * Called when an entity collide on this block */ - public function onEntityCollide(Entity $entity) : void{ + public function onEntityCollide(Entity $entity, int $face) : void{ } /** diff --git a/src/block/Magma.php b/src/block/Magma.php index c6625b16086..c0dd207124a 100644 --- a/src/block/Magma.php +++ b/src/block/Magma.php @@ -27,6 +27,7 @@ use pocketmine\entity\Living; use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageEvent; +use pocketmine\math\Facing; class Magma extends Opaque{ @@ -38,8 +39,8 @@ public function hasEntityCollision() : bool{ return true; } - public function onEntityCollide(Entity $entity) : void{ - if($entity instanceof Living && !$entity->isSneaking()){ + public function onEntityCollide(Entity $entity, int $face) : void{ + if($face === Facing::DOWN && $entity instanceof Living && !$entity->isSneaking()){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1); $entity->attack($ev); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index ee48e7a2415..b07bba6e882 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -658,13 +658,16 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_VOID, 10); $this->attack($ev); $hasUpdate = true; - }elseif($this->isOnGround()){ - $block = $this->getWorld()->getBlock($this->getLocation()->subtract(0, 1, 0)); + }else{ + $entityBlock = $this->getWorld()->getBlock($this->getLocation()); $entityBox = $this->getBoundingBox()->addCoord(0, -0.00001, 0); - foreach ($block->getCollisionBoxes() as $blockBox) { - if ($entityBox->intersectsWith($blockBox)) { - $block->onEntityCollide($this); - break; + foreach (Facing::ALL as $face) { + $block = $entityBlock->getSide($face); + foreach ($block->getCollisionBoxes() as $blockBox) { + if ($entityBox->intersectsWith($blockBox)) { + $block->onEntityCollide($this, $face); + break; + } } } } From b8df87337336b06b3ba53e78583ee6877e629468 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sat, 29 Jun 2024 14:19:57 +0200 Subject: [PATCH 05/15] Fix bugs --- src/block/Cactus.php | 7 +++++++ src/block/Magma.php | 4 ---- src/entity/Entity.php | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/block/Cactus.php b/src/block/Cactus.php index 6f2b04c8a38..38d3d68b7d7 100644 --- a/src/block/Cactus.php +++ b/src/block/Cactus.php @@ -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)){ diff --git a/src/block/Magma.php b/src/block/Magma.php index c0dd207124a..8cbd6888c0a 100644 --- a/src/block/Magma.php +++ b/src/block/Magma.php @@ -35,10 +35,6 @@ public function getLightLevel() : int{ return 3; } - public function hasEntityCollision() : bool{ - return true; - } - public function onEntityCollide(Entity $entity, int $face) : void{ if($face === Facing::DOWN && $entity instanceof Living && !$entity->isSneaking()){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index b07bba6e882..8f0ae753941 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -660,7 +660,7 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $hasUpdate = true; }else{ $entityBlock = $this->getWorld()->getBlock($this->getLocation()); - $entityBox = $this->getBoundingBox()->addCoord(0, -0.00001, 0); + $entityBox = $this->getBoundingBox()->expandedCopy(0.001, 0.1, 0.001); foreach (Facing::ALL as $face) { $block = $entityBlock->getSide($face); foreach ($block->getCollisionBoxes() as $blockBox) { From 50da8c57cf96211af5a39e56e2e9817c57e09f47 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Tue, 2 Jul 2024 15:57:33 +0200 Subject: [PATCH 06/15] $face is now face of block --- composer.json | 2 +- src/block/Cactus.php | 2 +- src/block/Magma.php | 2 +- src/entity/Entity.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index f14070552d9..f5a25efbea1 100644 --- a/composer.json +++ b/composer.json @@ -87,7 +87,7 @@ "@php build/generate-bedrockdata-path-consts.php", "@php build/generate-biome-ids.php", "@php build/generate-block-serializer-consts.php vendor/pocketmine/bedrock-data/canonical_block_states.nbt", - "@php build/generate-item-type-names.php vendor/pocketmine/bedrock-data/required_item_list.json", + "@php build/generatme-item-type-names.php vendor/pocketmine/bedrock-data/required_item_list.json", "@php build/generate-known-translation-apis.php", "@php build/generate-pocketmine-yml-property-consts.php", "@php build/generate-registry-annotations.php src" diff --git a/src/block/Cactus.php b/src/block/Cactus.php index 38d3d68b7d7..f7b802b5684 100644 --- a/src/block/Cactus.php +++ b/src/block/Cactus.php @@ -62,7 +62,7 @@ public function onEntityInside(Entity $entity) : bool{ } public function onEntityCollide(Entity $entity, int $face) : void{ - if($face === Facing::DOWN){ + if($face === Facing::UP){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_CONTACT, 1); $entity->attack($ev); } diff --git a/src/block/Magma.php b/src/block/Magma.php index 8cbd6888c0a..f956a2b4836 100644 --- a/src/block/Magma.php +++ b/src/block/Magma.php @@ -36,7 +36,7 @@ public function getLightLevel() : int{ } public function onEntityCollide(Entity $entity, int $face) : void{ - if($face === Facing::DOWN && $entity instanceof Living && !$entity->isSneaking()){ + if($face === Facing::UP && $entity instanceof Living && !$entity->isSneaking()){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1); $entity->attack($ev); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 8f0ae753941..9102eceeaf5 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -665,7 +665,7 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $block = $entityBlock->getSide($face); foreach ($block->getCollisionBoxes() as $blockBox) { if ($entityBox->intersectsWith($blockBox)) { - $block->onEntityCollide($this, $face); + $block->onEntityCollide($this, Facing::opposite($face)); break; } } From 9c1fecb1f0b82ad1093184f10c0e4341cb1534b2 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Tue, 2 Jul 2024 16:02:01 +0200 Subject: [PATCH 07/15] Oups --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f5a25efbea1..f14070552d9 100644 --- a/composer.json +++ b/composer.json @@ -87,7 +87,7 @@ "@php build/generate-bedrockdata-path-consts.php", "@php build/generate-biome-ids.php", "@php build/generate-block-serializer-consts.php vendor/pocketmine/bedrock-data/canonical_block_states.nbt", - "@php build/generatme-item-type-names.php vendor/pocketmine/bedrock-data/required_item_list.json", + "@php build/generate-item-type-names.php vendor/pocketmine/bedrock-data/required_item_list.json", "@php build/generate-known-translation-apis.php", "@php build/generate-pocketmine-yml-property-consts.php", "@php build/generate-registry-annotations.php src" From 708d713013ffaa71521b32b0bbd87c48f08999c1 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Thu, 18 Jul 2024 15:04:42 +0200 Subject: [PATCH 08/15] Add NOOP --- src/block/Block.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/block/Block.php b/src/block/Block.php index b13b0298abd..0fa355c1118 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -872,6 +872,7 @@ public function onEntityInside(Entity $entity) : bool{ * Called when an entity collide on this block */ public function onEntityCollide(Entity $entity, int $face) : void{ + //NOOP } /** From fbd0d02ee9b6e1cf771dbb2b7cd332b7a2422de8 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Sat, 30 Nov 2024 14:02:00 +0100 Subject: [PATCH 09/15] Add hasUpdate to true when player collide with block --- src/entity/Entity.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 9102eceeaf5..b89bcdec4ca 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -666,6 +666,7 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ foreach ($block->getCollisionBoxes() as $blockBox) { if ($entityBox->intersectsWith($blockBox)) { $block->onEntityCollide($this, Facing::opposite($face)); + $hasUpdate = true; break; } } From c9339f915aae2f9e58405f5e0c31b239f54d5815 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:10:17 +0100 Subject: [PATCH 10/15] onEntityCollide now return bool to update hasUpdate var --- src/block/Block.php | 6 ++++-- src/block/Cactus.php | 5 ++++- src/block/Magma.php | 5 ++++- src/entity/Entity.php | 3 +-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index 0fa355c1118..0c33ea2b69b 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -870,9 +870,11 @@ public function onEntityInside(Entity $entity) : bool{ /** * Called when an entity collide on this block + * + * @return bool Whether the entity has been updated */ - public function onEntityCollide(Entity $entity, int $face) : void{ - //NOOP + public function onEntityCollide(Entity $entity, int $face) : bool{ + return false; } /** diff --git a/src/block/Cactus.php b/src/block/Cactus.php index f7b802b5684..83138dd1156 100644 --- a/src/block/Cactus.php +++ b/src/block/Cactus.php @@ -61,11 +61,14 @@ public function onEntityInside(Entity $entity) : bool{ return true; } - public function onEntityCollide(Entity $entity, int $face) : void{ + public function onEntityCollide(Entity $entity, int $face) : bool{ if($face === Facing::UP){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_CONTACT, 1); $entity->attack($ev); + return true; } + + return false; } private function canBeSupportedAt(Block $block) : bool{ diff --git a/src/block/Magma.php b/src/block/Magma.php index f956a2b4836..5113e814ff5 100644 --- a/src/block/Magma.php +++ b/src/block/Magma.php @@ -35,11 +35,14 @@ public function getLightLevel() : int{ return 3; } - public function onEntityCollide(Entity $entity, int $face) : void{ + public function onEntityCollide(Entity $entity, int $face) : bool{ if($face === Facing::UP && $entity instanceof Living && !$entity->isSneaking()){ $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_FIRE, 1); $entity->attack($ev); + return true; } + + return false; } public function burnsForever() : bool{ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index b89bcdec4ca..2554fef57d3 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -665,8 +665,7 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $block = $entityBlock->getSide($face); foreach ($block->getCollisionBoxes() as $blockBox) { if ($entityBox->intersectsWith($blockBox)) { - $block->onEntityCollide($this, Facing::opposite($face)); - $hasUpdate = true; + $hasUpdate = $block->onEntityCollide($this, Facing::opposite($face)); break; } } From 9a4257ddb11185ec3e5c49f75f9ac28139638744 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:13:42 +0100 Subject: [PATCH 11/15] Refactor, use collidesWithBB function --- src/entity/Entity.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 2554fef57d3..52c16e9b0bf 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -663,11 +663,8 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $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)) { - $hasUpdate = $block->onEntityCollide($this, Facing::opposite($face)); - break; - } + if ($block->collidesWithBB($entityBox)) { + $hasUpdate = $block->onEntityCollide($this, Facing::opposite($face)); } } } From 06f452598746bcb2c3ec0797d8383824ccd3f558 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:17:03 +0100 Subject: [PATCH 12/15] Optimisation, remove useless variable --- src/entity/Entity.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 52c16e9b0bf..17f624c55c2 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -659,10 +659,9 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $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); + $block = $this->getWorld()->getBlock($this->getLocation()->getSide($face)); if ($block->collidesWithBB($entityBox)) { $hasUpdate = $block->onEntityCollide($this, Facing::opposite($face)); } From ed3f49aa89ea8ca59588d777777d854ca6170a73 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:43:59 +0100 Subject: [PATCH 13/15] Fix cs --- src/block/Block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/block/Block.php b/src/block/Block.php index 8168f0cafa9..907b7fc32ee 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -872,7 +872,7 @@ public function onEntityInside(Entity $entity) : bool{ /** * Called when an entity collide on this block - * + * * @return bool Whether the entity has been updated */ public function onEntityCollide(Entity $entity, int $face) : bool{ From 8ef46a8d2ecd426ec9596a897d50bc8e9d20749d Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 13:41:58 +0100 Subject: [PATCH 14/15] Change AABB entity size --- src/entity/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 0cffd4d85aa..12659d087e1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -662,7 +662,7 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ $this->attack($ev); $hasUpdate = true; }else{ - $entityBox = $this->getBoundingBox()->expandedCopy(0.001, 0.1, 0.001); + $entityBox = $this->getBoundingBox()->expandedCopy(0.00001, 3 / 16, 0.00001); foreach (Facing::ALL as $face) { $block = $this->getWorld()->getBlock($this->getLocation()->getSide($face)); if ($block->collidesWithBB($entityBox)) { From a78e8dd5d4ca07f6d36c84aed313835d3d14198c Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 13:59:46 +0100 Subject: [PATCH 15/15] Fix $hasUpdate new update just if value is true --- src/entity/Entity.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 12659d087e1..20878191ea8 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -666,7 +666,9 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{ foreach (Facing::ALL as $face) { $block = $this->getWorld()->getBlock($this->getLocation()->getSide($face)); if ($block->collidesWithBB($entityBox)) { - $hasUpdate = $block->onEntityCollide($this, Facing::opposite($face)); + if ($block->onEntityCollide($this, Facing::opposite($face))) { + $hasUpdate = true; + } } } }