22declare(strict_types=1);
24namespace pocketmine\player;
33use Symfony\Component\Filesystem\Path;
34use
function file_exists;
36use
function strtolower;
37use
function zlib_decode;
38use
function zlib_encode;
39use
const ZLIB_ENCODING_GZIP;
46 public function __construct(
50 private function getPlayerDataPath(
string $username) :
string{
51 return Path::join($this->path, strtolower($username) .
'.dat');
54 private function handleCorruptedPlayerData(
string $name) :
void{
55 $path = $this->getPlayerDataPath($name);
56 rename($path, $path .
'.bak');
59 public function hasData(
string $name) : bool{
60 return file_exists($this->getPlayerDataPath($name));
64 $name = strtolower($name);
65 $path = $this->getPlayerDataPath($name);
67 if(!file_exists($path)){
73 }
catch(\RuntimeException $e){
78 }
catch(\ErrorException $e){
79 $this->handleCorruptedPlayerData($name);
80 throw new PlayerDataLoadException(
"Failed to decompress raw player data for \"$name\": " . $e->getMessage(), 0, $e);
84 return (
new BigEndianNbtSerializer())->read($decompressed)->mustGetCompoundTag();
85 }
catch(NbtDataException $e){
86 $this->handleCorruptedPlayerData($name);
87 throw new PlayerDataLoadException(
"Failed to decode NBT data for \"$name\": " . $e->getMessage(), 0, $e);
93 $contents = Utils::assumeNotFalse(zlib_encode($nbt->write(
new TreeRoot($data)), ZLIB_ENCODING_GZIP),
"zlib_encode() failed unexpectedly");
95 Filesystem::safeFilePutContents($this->getPlayerDataPath($name), $contents);
96 }
catch(\RuntimeException $e){
static trapAndRemoveFalse(\Closure $closure, int $levels=E_WARNING|E_NOTICE)
saveData(string $name, CompoundTag $data)
static fileGetContents(string $fileName, bool $useIncludePath=false, $context=null, int $offset=0, ?int $length=null)