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

@ -3604,7 +3604,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->server->getPluginManager()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this);
if($this->joined){
//TODO: add events for player data saving
$this->save();
$this->server->getPluginManager()->callEvent($ev = new PlayerQuitEvent($this, $message));
@ -3705,7 +3704,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
}
$this->namedtag["playerGameType"] = $this->gamemode;
$this->namedtag["lastPlayed"] = new LongTag("lastPlayed", floor(microtime(true) * 1000));
$this->namedtag["lastPlayed"] = floor(microtime(true) * 1000);
if($this->username != "" and $this->namedtag instanceof CompoundTag){
$this->server->saveOfflinePlayerData($this->username, $this->namedtag, $async);

View File

@ -37,6 +37,7 @@ use pocketmine\entity\Entity;
use pocketmine\event\HandlerList;
use pocketmine\event\level\LevelInitEvent;
use pocketmine\event\level\LevelLoadEvent;
use pocketmine\event\player\PlayerDataSaveEvent;
use pocketmine\event\server\QueryRegenerateEvent;
use pocketmine\event\server\ServerCommandEvent;
use pocketmine\event\Timings;
@ -758,8 +759,6 @@ class Server{
$nbt->Motion->setTagType(NBT::TAG_Double);
$nbt->Rotation->setTagType(NBT::TAG_Float);
$this->saveOfflinePlayerData($name, $nbt);
return $nbt;
}
@ -769,11 +768,16 @@ class Server{
* @param CompoundTag $nbtTag
* @param bool $async
*/
public function saveOfflinePlayerData($name, CompoundTag $nbtTag, $async = false){
if($this->shouldSavePlayerData()){
public function saveOfflinePlayerData(string $name, CompoundTag $nbtTag, bool $async = false){
$ev = new PlayerDataSaveEvent($nbtTag, $name);
$ev->setCancelled(!$this->shouldSavePlayerData());
$this->pluginManager->callEvent($ev);
if(!$ev->isCancelled()){
$nbt = new NBT(NBT::BIG_ENDIAN);
try{
$nbt->setData($nbtTag);
$nbt->setData($ev->getSaveData());
if($async){
$this->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed()));

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;
}
}