mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
Make Player->getFirstPlayed() and Player->getLastPlayed() return DateTimeImmutable (#6042)
This commit is contained in:
parent
d4d7d02067
commit
e32a90be72
@ -23,13 +23,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\player;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface IPlayer{
|
||||
|
||||
public function getName() : string;
|
||||
|
||||
public function getFirstPlayed() : ?int;
|
||||
public function getFirstPlayed() : ?DateTimeImmutable;
|
||||
|
||||
public function getLastPlayed() : ?int;
|
||||
public function getLastPlayed() : ?DateTimeImmutable;
|
||||
|
||||
public function hasPlayedBefore() : bool;
|
||||
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\player;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\LongTag;
|
||||
|
||||
@ -36,12 +37,12 @@ class OfflinePlayer implements IPlayer{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getFirstPlayed() : ?int{
|
||||
return ($this->namedtag !== null && ($firstPlayedTag = $this->namedtag->getTag(Player::TAG_FIRST_PLAYED)) instanceof LongTag) ? $firstPlayedTag->getValue() : null;
|
||||
public function getFirstPlayed() : ?DateTimeImmutable{
|
||||
return ($this->namedtag !== null && ($firstPlayedTag = $this->namedtag->getTag(Player::TAG_FIRST_PLAYED)) instanceof LongTag) ? new DateTimeImmutable('@' . $firstPlayedTag->getValue() / 1000) : null;
|
||||
}
|
||||
|
||||
public function getLastPlayed() : ?int{
|
||||
return ($this->namedtag !== null && ($lastPlayedTag = $this->namedtag->getTag(Player::TAG_LAST_PLAYED)) instanceof LongTag) ? $lastPlayedTag->getValue() : null;
|
||||
public function getLastPlayed() : ?DateTimeImmutable{
|
||||
return ($this->namedtag !== null && ($lastPlayedTag = $this->namedtag->getTag(Player::TAG_LAST_PLAYED)) instanceof LongTag) ? new DateTimeImmutable('@' . $lastPlayedTag->getValue() / 1000) : null;
|
||||
}
|
||||
|
||||
public function hasPlayedBefore() : bool{
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\player;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use pocketmine\block\BaseSign;
|
||||
use pocketmine\block\Bed;
|
||||
use pocketmine\block\BlockTypeTags;
|
||||
@ -225,8 +226,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
|
||||
protected int $messageCounter = 2;
|
||||
|
||||
protected int $firstPlayed;
|
||||
protected int $lastPlayed;
|
||||
protected DateTimeImmutable $firstPlayed;
|
||||
protected DateTimeImmutable $lastPlayed;
|
||||
protected GameMode $gamemode;
|
||||
|
||||
/**
|
||||
@ -368,8 +369,12 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
}
|
||||
));
|
||||
|
||||
$this->firstPlayed = $nbt->getLong(self::TAG_FIRST_PLAYED, $now = (int) (microtime(true) * 1000));
|
||||
$this->lastPlayed = $nbt->getLong(self::TAG_LAST_PLAYED, $now);
|
||||
$now = (int) (microtime(true) * 1000);
|
||||
$createDateTimeImmutable = static function(string $tag) use ($nbt, $now) : DateTimeImmutable{
|
||||
return new DateTimeImmutable('@' . $nbt->getLong($tag, $now) / 1000);
|
||||
};
|
||||
$this->firstPlayed = $createDateTimeImmutable(self::TAG_FIRST_PLAYED);
|
||||
$this->lastPlayed = $createDateTimeImmutable(self::TAG_LAST_PLAYED);
|
||||
|
||||
if(!$this->server->getForceGamemode() && ($gameModeTag = $nbt->getTag(self::TAG_GAME_MODE)) instanceof IntTag){
|
||||
$this->internalSetGameMode(GameModeIdMap::getInstance()->fromId($gameModeTag->getValue()) ?? GameMode::SURVIVAL); //TODO: bad hack here to avoid crashes on corrupted data
|
||||
@ -428,19 +433,19 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
/**
|
||||
* TODO: not sure this should be nullable
|
||||
*/
|
||||
public function getFirstPlayed() : ?int{
|
||||
public function getFirstPlayed() : ?DateTimeImmutable{
|
||||
return $this->firstPlayed;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: not sure this should be nullable
|
||||
*/
|
||||
public function getLastPlayed() : ?int{
|
||||
public function getLastPlayed() : ?DateTimeImmutable{
|
||||
return $this->lastPlayed;
|
||||
}
|
||||
|
||||
public function hasPlayedBefore() : bool{
|
||||
return $this->lastPlayed - $this->firstPlayed > 1; // microtime(true) - microtime(true) may have less than one millisecond difference
|
||||
return ((int) $this->firstPlayed->diff($this->lastPlayed)->format('%s')) > 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2328,7 +2333,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
}
|
||||
|
||||
$nbt->setInt(self::TAG_GAME_MODE, GameModeIdMap::getInstance()->toId($this->gamemode));
|
||||
$nbt->setLong(self::TAG_FIRST_PLAYED, $this->firstPlayed);
|
||||
$nbt->setLong(self::TAG_FIRST_PLAYED, (int) $this->firstPlayed->format('Uv'));
|
||||
$nbt->setLong(self::TAG_LAST_PLAYED, (int) floor(microtime(true) * 1000));
|
||||
|
||||
return $nbt;
|
||||
|
Loading…
x
Reference in New Issue
Block a user