From 1045088668a72c08729b1971e8f8e48b4d721da1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 23 Mar 2019 12:12:30 +0000 Subject: [PATCH] Move more packet handling logic out of Player there is now only (1) packet handler remaining in Player. The reason I haven't targeted this is because it needs improvements of its own. --- src/pocketmine/Player.php | 56 +------------------ .../mcpe/handler/SimpleSessionHandler.php | 35 +++++++++++- 2 files changed, 35 insertions(+), 56 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index b64d9e285..62a0e135f 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -102,9 +102,7 @@ use pocketmine\network\mcpe\protocol\AvailableCommandsPacket; use pocketmine\network\mcpe\protocol\BookEditPacket; use pocketmine\network\mcpe\protocol\ChunkRadiusUpdatedPacket; use pocketmine\network\mcpe\protocol\ClientboundPacket; -use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\LevelEventPacket; -use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; use pocketmine\network\mcpe\protocol\MobEffectPacket; use pocketmine\network\mcpe\protocol\ModalFormRequestPacket; use pocketmine\network\mcpe\protocol\MovePlayerPacket; @@ -1891,31 +1889,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, return true; } - public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{ - //TODO: add events so plugins can change this - $this->getLevel()->broadcastPacketToViewers($this, $packet); - return true; - } - - public function handleEntityEvent(EntityEventPacket $packet) : bool{ - $this->doCloseInventory(); - - switch($packet->event){ - case EntityEventPacket::EATING_ITEM: - if($packet->data === 0){ - return false; - } - - $this->sendDataPacket($packet); - $this->server->broadcastPacket($this->getViewers(), $packet); - break; - default: - return false; - } - - return true; - } - public function equipItem(int $hotbarSlot) : bool{ if(!$this->inventory->isHotbarSlot($hotbarSlot)){ $this->inventory->sendContents($this); @@ -2325,6 +2298,9 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, public function toggleFlight(bool $fly) : void{ $ev = new PlayerToggleFlightEvent($this, $fly); + if(!$this->allowFlight){ + $ev->setCancelled(); + } $ev->call(); if($ev->isCancelled()){ $this->sendSettings(); @@ -2354,32 +2330,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, $this->level->dropItem($this->add(0, 1.3, 0), $item, $this->getDirectionVector()->multiply(0.4), 40); } - public function handleAdventureSettings(AdventureSettingsPacket $packet) : bool{ - if($packet->entityUniqueId !== $this->getId()){ - return false; //TODO - } - - $handled = false; - - $isFlying = $packet->getFlag(AdventureSettingsPacket::FLYING); - if($isFlying and !$this->allowFlight){ - $this->kick($this->server->getLanguage()->translateString("kick.reason.cheat", ["%ability.flight"])); - return true; - }elseif($isFlying !== $this->isFlying()){ - $this->toggleFlight($isFlying); - $handled = true; - } - - if($packet->getFlag(AdventureSettingsPacket::NO_CLIP) and !$this->allowMovementCheats and !$this->isSpectator()){ - $this->kick($this->server->getLanguage()->translateString("kick.reason.cheat", ["%ability.noclip"])); - return true; - } - - //TODO: check other changes - - return $handled; - } - public function handleBookEdit(BookEditPacket $packet) : bool{ /** @var WritableBook $oldBook */ $oldBook = $this->inventory->getItem($packet->inventorySlot); diff --git a/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php b/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php index 9c2899b06..724f19ea3 100644 --- a/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php +++ b/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php @@ -135,7 +135,21 @@ class SimpleSessionHandler extends SessionHandler{ } public function handleEntityEvent(EntityEventPacket $packet) : bool{ - return $this->player->handleEntityEvent($packet); + $this->player->doCloseInventory(); + + switch($packet->event){ + case EntityEventPacket::EATING_ITEM: //TODO: ignore this and handle it server-side + if($packet->data === 0){ + return false; + } + + $this->player->broadcastEntityEvent(EntityEventPacket::EATING_ITEM, $packet->data); + break; + default: + return false; + } + + return true; } public function handleInventoryTransaction(InventoryTransactionPacket $packet) : bool{ @@ -397,7 +411,21 @@ class SimpleSessionHandler extends SessionHandler{ } public function handleAdventureSettings(AdventureSettingsPacket $packet) : bool{ - return $this->player->handleAdventureSettings($packet); + if($packet->entityUniqueId !== $this->player->getId()){ + return false; //TODO: operators can change other people's permissions using this + } + + $handled = false; + + $isFlying = $packet->getFlag(AdventureSettingsPacket::FLYING); + if($isFlying !== $this->player->isFlying()){ + $this->player->toggleFlight($isFlying); + $handled = true; + } + + //TODO: check for other changes + + return $handled; } public function handleBlockEntityData(BlockEntityDataPacket $packet) : bool{ @@ -541,7 +569,8 @@ class SimpleSessionHandler extends SessionHandler{ } public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{ - return $this->player->handleLevelSoundEvent($packet); + $this->player->getLevel()->broadcastPacketToViewers($this->player->asVector3(), $packet); + return true; } public function handleNetworkStackLatency(NetworkStackLatencyPacket $packet) : bool{