Server: add getPlayerDataPath(), reduce logic duplication

This commit is contained in:
Dylan K. Taylor 2020-06-14 12:40:24 +01:00
parent df13e967fd
commit 0029efa370

View File

@ -693,29 +693,32 @@ class Server{
return $result; return $result;
} }
private function getPlayerDataPath(string $username) : string{
return $this->getDataPath() . '/players/' . strtolower($username) . '.dat';
}
/** /**
* Returns whether the server has stored any saved data for this player. * Returns whether the server has stored any saved data for this player.
*/ */
public function hasOfflinePlayerData(string $name) : bool{ public function hasOfflinePlayerData(string $name) : bool{
$name = strtolower($name); return file_exists($this->getPlayerDataPath($name));
return file_exists($this->getDataPath() . "players/$name.dat");
} }
public function getOfflinePlayerData(string $name) : CompoundTag{ public function getOfflinePlayerData(string $name) : CompoundTag{
$name = strtolower($name); $name = strtolower($name);
$path = $this->getDataPath() . "players/"; $path = $this->getPlayerDataPath($name);
if($this->shouldSavePlayerData()){ if($this->shouldSavePlayerData()){
if(file_exists($path . "$name.dat")){ if(file_exists($path)){
try{ try{
$nbt = new BigEndianNBTStream(); $nbt = new BigEndianNBTStream();
$compound = $nbt->readCompressed(file_get_contents($path . "$name.dat")); $compound = $nbt->readCompressed(file_get_contents($path));
if(!($compound instanceof CompoundTag)){ if(!($compound instanceof CompoundTag)){
throw new \RuntimeException("Invalid data found in \"$name.dat\", expected " . CompoundTag::class . ", got " . (is_object($compound) ? get_class($compound) : gettype($compound))); throw new \RuntimeException("Invalid data found in \"$name.dat\", expected " . CompoundTag::class . ", got " . (is_object($compound) ? get_class($compound) : gettype($compound)));
} }
return $compound; return $compound;
}catch(\Throwable $e){ //zlib decode error / corrupt data }catch(\Throwable $e){ //zlib decode error / corrupt data
rename($path . "$name.dat", $path . "$name.dat.bak"); rename($path, $path . '.bak');
$this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerCorrupted", [$name])); $this->logger->notice($this->getLanguage()->translateString("pocketmine.data.playerCorrupted", [$name]));
} }
}else{ }else{
@ -776,7 +779,7 @@ class Server{
if(!$ev->isCancelled()){ if(!$ev->isCancelled()){
$nbt = new BigEndianNBTStream(); $nbt = new BigEndianNBTStream();
try{ try{
file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed($ev->getSaveData())); file_put_contents($this->getPlayerDataPath($name), $nbt->writeCompressed($ev->getSaveData()));
}catch(\Throwable $e){ }catch(\Throwable $e){
$this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()])); $this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()]));
$this->logger->logException($e); $this->logger->logException($e);