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

Teleport players upon respawn

Comments in 'Plugin Development' started by minebuilder0110, Oct 8, 2015.

  1. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    I am developing a plugin where it teleports to the world spawn if one die in that world set in the config. But I am stuck:
    PHP:
    <?php
    namespace ReturnToWorldSpawn;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\Player;
    use 
    pocketmine\level\Level\getSpawnLocation;  
    use 
    pocketmine\event\player\PlayerRespawnEvent;

    class 
    ReturnToWorldSpawn extends PluginBase implements Listener{
           public function 
    onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    onJoin(PlayerJoinEvent $event){
        
    $event->getPlayer()->sendMessage("Hello.");
    //Just a residue code from testing out listeners :P
        
    }
        public function 
    onRespawn(PlayerRespawnEvent $event) {
            
    $levelname $event->getEntity()->getPlayer()->getLevel()->getName();
            
    $player $event->getEntity()->getPlayer();
            
    $config $this->getConfig();
            
    $RTWS $config->get("world-spawn");
            if(
    in_array($levelname$RTWS)){
                
    $spawn $event->getEntity()->getPlayer()->getLevel()->getSpawnLocation();
    //?
                
    $player->sendMessage("Teleporting you back to world $RTWS...");
                return 
    true;
            }
            else{
                return 
    true;
            }
        }
    }
    So it is faulty in two ways:
    There is no function called getEntity, so how would I get that player in this event?
    How would I set one's spawn in this instance?
  2. Barrington
    Offline

    Barrington New Member

    Joined:
    Jul 31, 2015
    Posts:
    41
    Minecraft User:
    Berrington
    $this->getConfig()->get("world-spawn")
  3. Barrington
    Offline

    Barrington New Member

    Joined:
    Jul 31, 2015
    Posts:
    41
    Minecraft User:
    Berrington
    if($p instanceof Player){}
  4. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Ummmmm,
    $config = $this->getConfig();
    $RTWS = $config->get("world-spawn');
    It is properly defined...
    Can you explain what you mean by that? I'm a bit of a noob.
  5. Barrington
    Offline

    Barrington New Member

    Joined:
    Jul 31, 2015
    Posts:
    41
    Minecraft User:
    Berrington
    I am Russian , and you can not understand me
  6. Barrington
    Offline

    Barrington New Member

    Joined:
    Jul 31, 2015
    Posts:
    41
    Minecraft User:
    Berrington
    You want to get the player ?
    So:
    $p = $event->getEntity();
    if($p instanceof Player){
    // something to do with the player
    }
  7. Barrington
    Offline

    Barrington New Member

    Joined:
    Jul 31, 2015
    Posts:
    41
    Minecraft User:
    Berrington
    In general, I do not understand ) ) 0
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    onRespawn has a getPlayer method.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You don't need to return any values for event handlers.
    Hotshot_9930 likes this.
  10. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    PHP:
    $pos = new Position($x$y$z$this->getServer()->getLevelByName($RTWS));
    $player->teleport($pos);
  11. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Does it? Hm, I may have missed that out when I was reading PlayerRespawnEvent.php. Thank you, I will try it later :p
    By the way, do you know what does it mean by:
    PHP:
    foreach($var1 as $var2 => $var3)
    ?
    I do know how to use foreach loop without
    PHP:
    => $var3
    this bit at least.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PlayerRespawnEvent extends PlayerEvent, which has a getPlayer method.
    foreach($array as $value){}
    foreach($array as $key => $value){}
    $key is the key of the current iterating element.
    I recommend you to read more about arrays first. https://php.net/arrays

Share This Page

Advertisement