Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Check if a player has joined before

Comments in 'Plugin Development' started by Pman1220, Aug 29, 2015.

  1. Pman1220
    Offline

    Pman1220 Active Member Plugin Developer

    Joined:
    Nov 21, 2013
    Posts:
    123
    Plugins:
    1
    Minecraft User:
    Pman1220
    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
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    $player->hasPlayedBefore() <- I think it's that one, haven't tested yet.
  3. PixelGuy75
    Offline

    PixelGuy75 Notable Member Plugin Developer

    Joined:
    Feb 9, 2014
    Posts:
    316
    Plugins:
    2
    Minecraft User:
    PixelGuy75
    I tried that before and it doesn't work.
    CraftYourBukkit and Pman1220 like this.
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    You could create an Array, a Database, or Files.
    EvolSoft and hoyinm14mc like this.
  5. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    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()]);
    }
    Last edited: Aug 30, 2015
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why not use enum...
  7. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    idk...
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Yes it surely does. And can u tell me bit mre about enums?
  10. Pman1220
    Offline

    Pman1220 Active Member Plugin Developer

    Joined:
    Nov 21, 2013
    Posts:
    123
    Plugins:
    1
    Minecraft User:
    Pman1220
    Would it be possible to get the players folder and check if the file exists there?
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Just create your own file. Or make a pull request on PM.
    hoyinm14mc likes this.
  12. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    public function hasPlayedBefore(Player $p){
    return 
    file_exists("\players".strtolower($p->getName().".dat.");
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
    jojoe77777 likes this.
  14. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    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
    }
    }
    Last edited: Aug 31, 2015
    DunxandMinecraft likes this.
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  16. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    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)
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  18. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Have you tried it? I use a similar method, not the same one, to store player data for my server and it works fine.
  19. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Nvm. I get what your saying now.
  20. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    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 ;)
    Hotshot_9930 likes this.

Share This Page

Advertisement