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.
This commit is contained in:
Dylan K. Taylor 2019-03-23 12:12:30 +00:00
parent 1bc37a1a8a
commit 1045088668
2 changed files with 35 additions and 56 deletions

View File

@ -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);

View File

@ -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{