1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Player Joins and something happens

Discussion in 'Development' started by Daniel Pereira, Mar 1, 2018.

  1. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    Hello,
    I would like to ask here how I can I make this into pmmp plugin code:

    If player never Joined the Server {
    Making Him spawn in a Position of the Default Map (xyz);
    Give him a Welcome message!;
    } else {
    Nothing
    }

    I'm building an open medieval world where Factions wars for settlements, Players gather resources, buy equipment, have an economy and explore. And I need to make a quick tutorial to introduce a Player to the game in the server by spawning him in a small island where he will learn a tutorial for the game and progressively the plugin will tell him his objectives. Well, I think starting by giving a message and spawning him in the island is where I need to start. I want to learn some few things on pocketmine plugin development and this doesn't seem very hard for me but I need just bit help. Thanks!
     
  2. Hello Daniel,
    This is something that can be done with events and a data store, the first step would be to setup a event every time a player join’s the game this can be done using the PlayerJoinEvent. The second step would be checking the data store to see if the player has already logged in before, if the player is not in the data store send them a message and add them to the data store.

    Vocabulary:
    • “data store” means “a database or file holding information”
     
  3. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Use this function in the player class to determine if the player has played before.
     
  4. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    Hello and thank you both for the reply! Well starting with the function I'm not seeing much where I put what but correct me here and explain why when you can! Appreciate all the help :)

    PHP:
    public function hasPlayedBefore() : bool{

            return 
    $this->playedBefore;

        if (!(
    playedBefore)) {
           
    $this->getPlayer()->sendMessage('Hello lad and welcome to Swords & Castles!');
        }

        }
     
  5. Hello Daniel,
    The function that you have will not do anything as it is not hooked to events, follow my instructions above.
     
    Muqsit likes this.
  6. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    True! My bad. And to make them hook, I've seen from others codes that you do something like this, please correct me or if you find any other better answer let me know of it!

    PHP:
    public function hasPlayedBefore(PlayerJoinEvent $event) : bool{

            return 
    $this->playedBefore;
            
          
    $player $event->getPlayer();

        if (!(
    $player as playedBefore)) {
           
    $this->getPlayer()->sendMessage('Hello lad and welcome to Swords & Castles!');
        }

        } 
     
  7. Hey, I’ll make a code snippet for you soon..
     
  8. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    Alright. Will appreciate it a lot!
     
    Deleted member 2188 likes this.
  9. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    I thought my code was easy to understand.... Guess not, here!
    PHP:
        public function playerJoinEvent(PlayerJoinEvent $ev){
            
    $player $ev->getPlayer();

            if(
    $player->hasPlayedBefore() == true){ // player has join the server before
                
    $player->sendMessage("Welcome Back, " .  $player->getName());
            } else { 
    // player has never played before
                
    $player->sendMessage("Welcome to the Server! Have a good first day!");
            }
        }
     
  10. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    PHP:
    ...

        
    /**
         * @param \pocketmine\event\player\PlayerJoinEvent $event
         */
        
    public function onPlayerJoin(\pocketmine\event\player\PlayerJoinEvent $event) : void{
            
    $player $event->getPlayer();
            if(!
    $player->hasPlayedBefore()){
                
    $player->sendMessage("Your first join welcome message here");
            }else{
                
    $player->sendMessage("You are not new, welcome again");
            }
        }
     
  11. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    Why the "==" loosely check? Useless. Player::hasPlayedBefore() returns a boolean. Can be simplified with this expression:

    true:
    PHP:
    ($player->hasPlayedBefore())
    false:
    PHP:
    (!$player->hasPlayedBefore())
     
  12. Huh, I did not that there was a function for it. I has assumed that you would need to low the player.
     
  13. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    PHP:
    public function onJoin(\pocketmine\event\player\PlayerJoinEvent $e) {
        
    $player $e->getPlayer();
        if(
    $player->getFirstPlayed() == $player->getLastPlayed()) {
            
    $world Server::getInstance()->getLevelByName("Teleporting world here");
            
    $x 1// X
            
    $x 1// Y
            
    $x 1// Z
            
    $player->teleport(new Position($x$y$z$world));
            
    $player->sendMessage("Welcome server.");

        }else{
            
    $player->sendMessage("Again hi ".$player->getName()."!");

        }

    }
     
    Eren5960 likes this.
  14. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    Thanks a lot everyone! Specialy @Teamblocket , that explained my errors and the sintax I need to use! Thanks again.
     
  15. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    That's just so the OP can understand it better.
     
    Daniel Pereira likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.