I'd like to check if a player has played before and Im not sure how to do so. I could make a folder with a bunch of files of players names but I'd like to keep the files down to a minimum. Any suggestions? Thanks
PHP: public function onEnable(){ $this->players = new Config($this->getDataPath()."players.yml", Config::YAML, array());}public function onJoin(PlayerJoinEvent $event){ if($this->hasPlayedBefore($event->getPlayer() !== true){ $this->players["players"][] = $event->getPlayer()->getName(); }}public function hasPlayedBefore(Player $player){ $tmp = $this->players->getAll(); return (bool) isset($tmp["players"][$player->getName()]);}
I don't think getLastPlayed works. It checks if $player->namedtag is an instance of Compound, but I saw it initialized at constructor and I found no otherwise write usage to it, so I assume it should always return true.
PHP: public function hasPlayedBefore(Player $p){return file_exists("\players".strtolower($p->getName().".dat.");
1. I don't think "\players" is correct in syntax. 2. You do not know for sure if the current working directory (cwd) is the PocketMine root. You should use it relative to the PocketMine server data path instead. 3. I think you had an extra period at the end of the string. 4. You have a missing parenthesis. 5. The player file is created before the player joins.
I would do this PHP: Public function onEnable() { @mkdir($this->getDataFolder() . "Players/");}public function registerPlayer($player) { $this->player = new Config($this->getDataFolder() . "Players/" . strtolower($player->getName()) . ".yml", Config::YAML));}Public function hasPlayedBefore($player) { file_exists($this->player);}Public function onJoin(PlayerJoinEvent $event) { $player = $event->getPlayer(); $this->registerPlayer($player); $this->player = new Config($this->getDataFolder() . "Players/" . strtolower($player->getName()) . ".yml", Config::YAML)); If(!$this->hasPlayedBefore($player) { // Do something if player hasnt joined before} Else { // Do something if player has joined before}}
1. file_exists accepts a string representing the file path. You are passing a Config object. 2. $this->player doesn't work when multiple players are online. 3. You create the file when you create a new Config.
PHP: Public function hasPlayedBefore($player) {file_exists($this->player);} $this->hasPlayedBefore($player) would work for each individual player due to the fact that that it would be inside of the PlayerJoinEvent where: PHP: $this->player = new Config($this->getDataFolder() . "Players/" . strtolower($player->getName()) . ".yml", Config::YAML)
1. You are overwriting $this->player every time. 2. I don't see how you explained anything at all. Please read PHP documentation http://PHP.net/file-exists - it accepts a string, not a Config object. 3. As you have said, you initialized it in PlayerJoinEvent. So of course the file exists now that you create it when the player joins.
Have you tried it? I use a similar method, not the same one, to store player data for my server and it works fine.
I always create a folder called users, players or something else where I check if a player has joined for the first time (Look at CustomAlerts plugin source code) If you don't want to create files you can also check if the player file exists simply on the PocketMine/players folder. I never tested this but it should work