Split PlayerActionPacket handling into two classes, death is now a session state

This commit is contained in:
Dylan K. Taylor
2018-07-20 18:11:29 +01:00
parent f626b9e8a0
commit 015ee90571
4 changed files with 128 additions and 113 deletions

View File

@ -25,6 +25,7 @@ namespace pocketmine\network\mcpe;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\event\server\DataPacketSendEvent;
use pocketmine\network\mcpe\handler\DeathSessionHandler;
use pocketmine\network\mcpe\handler\LoginSessionHandler;
use pocketmine\network\mcpe\handler\PreSpawnSessionHandler;
use pocketmine\network\mcpe\handler\ResourcePacksSessionHandler;
@ -173,4 +174,12 @@ class NetworkSession{
//TODO: split this up even further
$this->setHandler(new SimpleSessionHandler($this->player));
}
public function onDeath() : void{
$this->setHandler(new DeathSessionHandler($this->player, $this));
}
public function onRespawn() : void{
$this->setHandler(new SimpleSessionHandler($this->player));
}
}

View File

@ -0,0 +1,61 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\handler;
use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\PlayerActionPacket;
use pocketmine\network\mcpe\protocol\RespawnPacket;
use pocketmine\Player;
class DeathSessionHandler extends SessionHandler{
/** @var Player */
private $player;
/** @var NetworkSession */
private $session;
public function __construct(Player $player, NetworkSession $session){
$this->player = $player;
$this->session = $session;
}
public function setUp() : void{
$pk = new RespawnPacket();
$pk->position = $this->player->getOffsetPosition($this->player->getSpawn());
$this->session->sendDataPacket($pk);
}
public function handlePlayerAction(PlayerActionPacket $packet) : bool{
switch($packet->action){
case PlayerActionPacket::ACTION_RESPAWN:
$this->player->respawn();
return true;
case PlayerActionPacket::ACTION_DIMENSION_CHANGE_REQUEST:
//TODO: players send this when they die in another dimension
break;
}
return false;
}
}

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\handler;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\AdventureSettingsPacket;
use pocketmine\network\mcpe\protocol\AnimatePacket;
use pocketmine\network\mcpe\protocol\BlockEntityDataPacket;
@ -123,7 +124,58 @@ class SimpleSessionHandler extends SessionHandler{
}
public function handlePlayerAction(PlayerActionPacket $packet) : bool{
return $this->player->handlePlayerAction($packet);
$pos = new Vector3($packet->x, $packet->y, $packet->z);
switch($packet->action){
case PlayerActionPacket::ACTION_START_BREAK:
$this->player->startBreakBlock($pos, $packet->face);
break;
case PlayerActionPacket::ACTION_ABORT_BREAK:
case PlayerActionPacket::ACTION_STOP_BREAK:
$this->player->stopBreakBlock($pos);
break;
case PlayerActionPacket::ACTION_START_SLEEPING:
//unused
break;
case PlayerActionPacket::ACTION_STOP_SLEEPING:
$this->player->stopSleep();
break;
case PlayerActionPacket::ACTION_JUMP:
$this->player->jump();
return true;
case PlayerActionPacket::ACTION_START_SPRINT:
$this->player->toggleSprint(true);
return true;
case PlayerActionPacket::ACTION_STOP_SPRINT:
$this->player->toggleSprint(false);
return true;
case PlayerActionPacket::ACTION_START_SNEAK:
$this->player->toggleSneak(true);
return true;
case PlayerActionPacket::ACTION_STOP_SNEAK:
$this->player->toggleSneak(false);
return true;
case PlayerActionPacket::ACTION_START_GLIDE:
case PlayerActionPacket::ACTION_STOP_GLIDE:
break; //TODO
case PlayerActionPacket::ACTION_CONTINUE_BREAK:
$this->player->continueBreakBlock($pos, $packet->face);
break;
case PlayerActionPacket::ACTION_START_SWIMMING:
break; //TODO
case PlayerActionPacket::ACTION_STOP_SWIMMING:
//TODO: handle this when it doesn't spam every damn tick (yet another spam bug!!)
break;
default:
$this->player->getServer()->getLogger()->debug("Unhandled/unknown player action type " . $packet->action . " from " . $this->player->getName());
return false;
}
$this->player->setUsingItem(false);
return true;
}
public function handleEntityFall(EntityFallPacket $packet) : bool{