From 18654c37128bd8f7706119adf774890872fb2020 Mon Sep 17 00:00:00 2001 From: DavyCraft648 <67502532+DavyCraft648@users.noreply.github.com> Date: Sat, 2 Mar 2024 15:52:48 +0700 Subject: [PATCH] Implement crawling --- src/entity/Living.php | 16 +++++++-- src/event/player/PlayerToggleCrawlEvent.php | 40 +++++++++++++++++++++ src/player/Player.php | 18 +++++++--- 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/event/player/PlayerToggleCrawlEvent.php diff --git a/src/entity/Living.php b/src/entity/Living.php index f9872fa39..12aa20302 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -124,6 +124,7 @@ abstract class Living extends Entity{ protected bool $sneaking = false; protected bool $gliding = false; protected bool $swimming = false; + protected bool $crawling = false; protected function getInitialDragMultiplier() : float{ return 0.02; } @@ -268,11 +269,21 @@ public function setSwimming(bool $value = true) : void{ $this->recalculateSize(); } + public function isCrawling() : bool{ + return $this->crawling; + } + + public function setCrawling(bool $value = true) : void{ + $this->crawling = $value; + $this->networkPropertiesDirty = true; + $this->recalculateSize(); + } + private function recalculateSize() : void{ $size = $this->getInitialSizeInfo(); - if($this->isSwimming() || $this->isGliding()){ + if($this->isSwimming() || $this->isGliding() || $this->isCrawling()){ $width = $size->getWidth(); - $this->setSize((new EntitySizeInfo($width, $width, $width * 0.9))->scale($this->getScale())); + $this->setSize((new EntitySizeInfo(5 / 8, $width, 5 / 8 * 0.9))->scale($this->getScale())); }elseif($this->isSneaking()){ $this->setSize((new EntitySizeInfo(3 / 4 * $size->getHeight(), $size->getWidth(), 3 / 4 * $size->getEyeHeight()))->scale($this->getScale())); }else{ @@ -894,6 +905,7 @@ protected function syncNetworkData(EntityMetadataCollection $properties) : void{ $properties->setGenericFlag(EntityMetadataFlags::SPRINTING, $this->sprinting); $properties->setGenericFlag(EntityMetadataFlags::GLIDING, $this->gliding); $properties->setGenericFlag(EntityMetadataFlags::SWIMMING, $this->swimming); + $properties->setGenericFlag(EntityMetadataFlags::CRAWLING, $this->crawling); } protected function onDispose() : void{ diff --git a/src/event/player/PlayerToggleCrawlEvent.php b/src/event/player/PlayerToggleCrawlEvent.php new file mode 100644 index 000000000..a07dc5367 --- /dev/null +++ b/src/event/player/PlayerToggleCrawlEvent.php @@ -0,0 +1,40 @@ +player = $player; + } + + public function isCrawling() : bool{ + return $this->isCrawling; + } +} diff --git a/src/player/Player.php b/src/player/Player.php index 5c000d565..58248f66e 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -72,6 +72,7 @@ use pocketmine\event\player\PlayerPostChunkSendEvent; use pocketmine\event\player\PlayerQuitEvent; use pocketmine\event\player\PlayerRespawnEvent; +use pocketmine\event\player\PlayerToggleCrawlEvent; use pocketmine\event\player\PlayerToggleFlightEvent; use pocketmine\event\player\PlayerToggleGlideEvent; use pocketmine\event\player\PlayerToggleSneakEvent; @@ -1994,10 +1995,6 @@ public function toggleFlight(bool $fly) : bool{ return true; } - public function toggleCrawl(bool $crawl) : bool{ - return false; - } - public function toggleGlide(bool $glide) : bool{ if($glide === $this->gliding){ return true; @@ -2024,6 +2021,19 @@ public function toggleSwim(bool $swim) : bool{ return true; } + public function toggleCrawl(bool $crawl) : bool{ + if($crawl === $this->crawling){ + return true; + } + $ev = new PlayerToggleCrawlEvent($this, $crawl); + $ev->call(); + if($ev->isCancelled()){ + return false; + } + $this->setCrawling($crawl); + return true; + } + public function emote(string $emoteId) : void{ $currentTick = $this->server->getTick(); if($currentTick - $this->lastEmoteTick > 5){