Re-organise some Player methods

- group spawn-related methods
- group achievement-related methods
- move isSleeping()
- group name-related methods
- move Player->sendAllInventories()
- move Player->jump()
This commit is contained in:
Dylan K. Taylor 2017-10-09 10:07:24 +01:00
parent 748beaaaa7
commit ac7384a2b4

View File

@ -693,28 +693,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->sessionAdapter = new PlayerNetworkSessionAdapter($this->server, $this);
}
/**
* @param string $achievementId
*/
public function removeAchievement(string $achievementId){
if($this->hasAchievement($achievementId)){
$this->achievements[$achievementId] = false;
}
}
/**
* @param string $achievementId
*
* @return bool
*/
public function hasAchievement(string $achievementId) : bool{
if(!isset(Achievement::$list[$achievementId])){
return false;
}
return isset($this->achievements[$achievementId]) and $this->achievements[$achievementId] !== false;
}
/**
* @return bool
*/
@ -722,6 +700,21 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return $this->sessionAdapter !== null;
}
/**
* Gets the username
* @return string
*/
public function getName() : string{
return $this->username;
}
/**
* @return string
*/
public function getLowerCaseName() : string{
return $this->iusername;
}
/**
* Gets the "friendly" name to display of this player to use in the chat.
*
@ -769,11 +762,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true;
}
public function jump(){
$this->server->getPluginManager()->callEvent(new PlayerJumpEvent($this));
parent::jump();
}
/**
* Gets the player IP address
*
@ -797,13 +785,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return $this->newPosition !== null ? Position::fromObject($this->newPosition, $this->level) : $this->getPosition();
}
/**
* @return bool
*/
public function isSleeping() : bool{
return $this->sleeping !== null;
}
public function getInAirTicks() : int{
return $this->inAirTicks;
}
@ -865,26 +846,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
unset($this->loadQueue[$index]);
}
/**
* @return Position
*/
public function getSpawn(){
if($this->hasValidSpawnPosition()){
return $this->spawnPosition;
}else{
$level = $this->server->getDefaultLevel();
return $level->getSafeSpawn();
}
}
/**
* @return bool
*/
public function hasValidSpawnPosition() : bool{
return $this->spawnPosition instanceof WeakPosition and $this->spawnPosition->isValid();
}
public function sendChunk(int $x, int $z, BatchPacket $payload){
if(!$this->isConnected()){
return;
@ -1085,6 +1046,55 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true;
}
/**
* @return Position
*/
public function getSpawn(){
if($this->hasValidSpawnPosition()){
return $this->spawnPosition;
}else{
$level = $this->server->getDefaultLevel();
return $level->getSafeSpawn();
}
}
/**
* @return bool
*/
public function hasValidSpawnPosition() : bool{
return $this->spawnPosition instanceof WeakPosition and $this->spawnPosition->isValid();
}
/**
* Sets the spawnpoint of the player (and the compass direction) to a Vector3, or set it on another world with a
* Position object
*
* @param Vector3|Position $pos
*/
public function setSpawn(Vector3 $pos){
if(!($pos instanceof Position)){
$level = $this->level;
}else{
$level = $pos->getLevel();
}
$this->spawnPosition = new WeakPosition($pos->x, $pos->y, $pos->z, $level);
$pk = new SetSpawnPositionPacket();
$pk->x = (int) $this->spawnPosition->x;
$pk->y = (int) $this->spawnPosition->y;
$pk->z = (int) $this->spawnPosition->z;
$pk->spawnType = SetSpawnPositionPacket::TYPE_PLAYER_SPAWN;
$pk->spawnForced = false;
$this->dataPacket($pk);
}
/**
* @return bool
*/
public function isSleeping() : bool{
return $this->sleeping !== null;
}
/**
* @param Vector3 $pos
*
@ -1118,28 +1128,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true;
}
/**
* Sets the spawnpoint of the player (and the compass direction) to a Vector3, or set it on another world with a
* Position object
*
* @param Vector3|Position $pos
*/
public function setSpawn(Vector3 $pos){
if(!($pos instanceof Position)){
$level = $this->level;
}else{
$level = $pos->getLevel();
}
$this->spawnPosition = new WeakPosition($pos->x, $pos->y, $pos->z, $level);
$pk = new SetSpawnPositionPacket();
$pk->x = (int) $this->spawnPosition->x;
$pk->y = (int) $this->spawnPosition->y;
$pk->z = (int) $this->spawnPosition->z;
$pk->spawnType = SetSpawnPositionPacket::TYPE_PLAYER_SPAWN;
$pk->spawnForced = false;
$this->dataPacket($pk);
}
public function stopSleep(){
if($this->sleeping instanceof Vector3){
$b = $this->level->getBlock($this->sleeping);
@ -1161,6 +1149,19 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
}
/**
* @param string $achievementId
*
* @return bool
*/
public function hasAchievement(string $achievementId) : bool{
if(!isset(Achievement::$list[$achievementId])){
return false;
}
return isset($this->achievements[$achievementId]) and $this->achievements[$achievementId] !== false;
}
/**
* @param string $achievementId
*
@ -1187,6 +1188,15 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return false;
}
/**
* @param string $achievementId
*/
public function removeAchievement(string $achievementId){
if($this->hasAchievement($achievementId)){
$this->achievements[$achievementId] = false;
}
}
/**
* @return int
*/
@ -1578,6 +1588,11 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->newPosition = null;
}
public function jump(){
$this->server->getPluginManager()->callEvent(new PlayerJumpEvent($this));
parent::jump();
}
public function setMotion(Vector3 $mot){
if(parent::setMotion($mot)){
if($this->chunk !== null){
@ -2176,15 +2191,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true;
}
protected function sendAllInventories(){
foreach($this->windowIndex as $id => $inventory){
$inventory->sendContents($this);
if($inventory instanceof PlayerInventory){
$inventory->sendArmorContents($this);
}
}
}
/**
* Don't expect much from this handler. Most of it is roughly hacked and duct-taped together.
*
@ -3440,21 +3446,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
}
/**
* Gets the username
* @return string
*/
public function getName() : string{
return $this->username;
}
/**
* @return string
*/
public function getLowerCaseName() : string{
return $this->iusername;
}
public function kill(){
if(!$this->spawned){
return;
@ -3831,6 +3822,15 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
}
protected function sendAllInventories(){
foreach($this->windowIndex as $id => $inventory){
$inventory->sendContents($this);
if($inventory instanceof PlayerInventory){
$inventory->sendArmorContents($this);
}
}
}
public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){
$this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $newMetadataValue);
}