Do async saving only when needed

This commit is contained in:
Shoghi Cervantes 2015-05-23 10:54:15 +02:00
parent 4f7aac50d3
commit be6b0656a0
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
2 changed files with 13 additions and 8 deletions

View File

@ -2807,7 +2807,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
/**
* Handles player data saving
*/
public function save(){
public function save($async = false){
if($this->closed){
throw new \InvalidStateException("Tried to save closed player");
}
@ -2830,7 +2830,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->namedtag["lastPlayed"] = floor(microtime(true) * 1000);
if($this->username != "" and $this->namedtag instanceof Compound){
$this->server->saveOfflinePlayerData($this->username, $this->namedtag);
$this->server->saveOfflinePlayerData($this->username, $this->namedtag, $async);
}
}
}

View File

@ -839,13 +839,18 @@ class Server{
/**
* @param string $name
* @param Compound $nbtTag
* @param bool $async
*/
public function saveOfflinePlayerData($name, Compound $nbtTag){
public function saveOfflinePlayerData($name, Compound $nbtTag, $async = false){
$nbt = new NBT(NBT::BIG_ENDIAN);
try{
$nbt->setData($nbtTag);
if($async){
$this->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed()));
}else{
file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed());
}
}catch(\Exception $e){
$this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()]));
if(\pocketmine\DEBUG > 1 and $this->logger instanceof MainLogger){
@ -1402,7 +1407,7 @@ class Server{
if(($player = $this->getPlayerExact($name)) instanceof Player){
$player->recalculatePermissions();
}
$this->operators->save();
$this->operators->save(true);
}
/**
@ -1422,7 +1427,7 @@ class Server{
*/
public function addWhitelist($name){
$this->whitelist->set(strtolower($name), true);
$this->whitelist->save();
$this->whitelist->save(true);
}
/**
@ -1740,7 +1745,7 @@ class Server{
}
$this->properties->save();
$this->properties->save(true);
if(!($this->getDefaultLevel() instanceof Level)){
$this->getLogger()->emergency($this->getLanguage()->translateString("pocketmine.level.defaultError"));
@ -2283,7 +2288,7 @@ class Server{
Timings::$worldSaveTimer->startTiming();
foreach($this->getOnlinePlayers() as $index => $player){
if($player->isOnline()){
$player->save();
$player->save(true);
}elseif(!$player->isConnected()){
$this->removePlayer($player);
}