Added PlayerDataSaveEvent, resolved #487 (#920)

* Added PlayerDataSaveEvent, resolved #487

* Added getPlayer() to PlayerDataSaveEvent
may return an IPlayer

* Fixed quit messages
This TranslationContainer vs. string mess... >_<
This commit is contained in:
Dylan K. Taylor
2017-05-29 18:50:45 +01:00
committed by GitHub
parent 645d744e05
commit 0ad16c1919
4 changed files with 101 additions and 20 deletions

View File

@ -0,0 +1,78 @@
<?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\event\player;
use pocketmine\event\Cancellable;
use pocketmine\event\Event;
use pocketmine\IPlayer;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\Server;
/**
* Called when a player's data is about to be saved to disk.
*/
class PlayerDataSaveEvent extends Event implements Cancellable{
public static $handlerList = null;
/** @var CompoundTag */
protected $data;
/** @var string */
protected $playerName;
public function __construct(CompoundTag $nbt, string $playerName){
$this->data = $nbt;
$this->playerName = $playerName;
}
/**
* Returns the data to be written to disk as a CompoundTag
* @return CompoundTag
*/
public function getSaveData() : CompoundTag{
return $this->data;
}
/**
* @param CompoundTag $data
*/
public function setSaveData(CompoundTag $data){
$this->data = $data;
}
/**
* Returns the username of the player whose data is being saved. This is not necessarily an online player.
* @return string
*/
public function getPlayerName() : string{
return $this->playerName;
}
/**
* Returns the player whose data is being saved. This may be a Player or an OfflinePlayer.
* @return IPlayer (Player or OfflinePlayer)
*/
public function getPlayer() : IPlayer{
return Server::getInstance()->getOfflinePlayer($this->playerName);
}
}

View File

@ -21,6 +21,7 @@
namespace pocketmine\event\player;
use pocketmine\event\TranslationContainer;
use pocketmine\Player;
/**
@ -29,30 +30,29 @@ use pocketmine\Player;
class PlayerQuitEvent extends PlayerEvent{
public static $handlerList = null;
/** @var string */
/** @var TranslationContainer|string */
protected $quitMessage;
protected $autoSave = true;
public function __construct(Player $player, $quitMessage, $autoSave = true){
/**
* @param Player $player
* @param TranslationContainer|string $quitMessage
*/
public function __construct(Player $player, $quitMessage){
$this->player = $player;
$this->quitMessage = $quitMessage;
$this->autoSave = $autoSave;
}
/**
* @param TranslationContainer|string $quitMessage
*/
public function setQuitMessage($quitMessage){
$this->quitMessage = $quitMessage;
}
/**
* @return TranslationContainer|string
*/
public function getQuitMessage(){
return $this->quitMessage;
}
public function getAutoSave(){
return $this->autoSave;
}
public function setAutoSave($value = true){
$this->autoSave = (bool) $value;
}
}