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

Solved Teleport when walking on a selected block.

Comments in 'Plugin Development' started by haimann951, Jul 21, 2015.

  1. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    I´m new to pocketmine plugin development.
    I would like to get the player teleported when walks on a block. Could someone of you give me an example on how to do this?

    Thanks
  2. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Here you go:
    PHP:
    public function onPlayerMove(PlayerMoveEvent $event){

            
    $player $event->getPlayer();
         
            
    $target = new Vector3($x$y$z);
         
            if(
    $player->getFloorX() === $blockX && $player->getFloorY() === $blockY && $player->getFloorZ() === $blockY){
                
    $player->teleport($target);
            }
    }
    Make sure to add the PlayerMoveEvent using:
    PHP:
    use pocketmine\event\player\PlayerMoveEvent;
    PEMapModder likes this.
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Player::getFloorY() etc is a coordinate. You can use
    PHP:
    $block $event->getPlayer()->getLevel()->getBlock($event->getPlayer()->floor()->subtract(01))->getId();
    if(
    $block === Block::EMERALD_BLOCK){
    //Teleport
    }
    PEMapModder likes this.
  4. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    How can I specify the coordinates for the target and the block?
  5. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Yeah that's good if you only have one instance of the desired block, if you use the other method it will work with multiple instances of the same block.
  6. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    Ignore the post above.
  7. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    public function onPlayerMove(PlayerMoveEvent $event){

    $player = $event->getPlayer();

    $blockX = 0;
    $blockY = 5;
    $blockZ = 10;


    $target = new Vector3(0, 5, 0);

    if($player->getFloorX() === $blockX && $player->getFloorY() === $blockY && $player->getFloorZ() === $blockZ){
    $player->teleport($target);
    }


    }


    I coded this and when I walk on the block X = 0, Y = 5, Z = 10 nothing happens. But it should teleport to X = 0; Y = 5 and Z = 10.
    What I´m doing wrong?
  8. GmWM
    Offline

    GmWM Active Member Plugin Developer

    Joined:
    Sep 8, 2013
    Posts:
    181
    Plugins:
    3
    PHP:
    use pocketmine\player\event\PlayerMoveEvent;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\math\Vector3;

    public function 
    onMove(PlayerMoveEvent $event) {
            
    $player $event->getPlayer();
            
    $block $event->getPlayer()->getLevel()->getBlock($event->getPlayer()->floor()->subtract(01));
            if(
    $block instanceof Block) {
                
    $id $block->getId();
                
    $meta $block->getDamage();
                if(
    $id === 133 && $meta === 0) { //133 - Emerald Block, Use other blocks with damage too.
                    
    $target = new Vector3(050);
                    
    $player->teleport($target);
                }
            }
        }
    Learn PHP and check the API before you start developing.
    PEMapModder likes this.
  9. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    This doesn´t work too when I walk on the Emerald Block.
  10. GmWM
    Offline

    GmWM Active Member Plugin Developer

    Joined:
    Sep 8, 2013
    Posts:
    181
    Plugins:
    3
    Let me see the whole code.
  11. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    <?php

    namespace main;

    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerMoveEvent;
    use pocketmine\math\Vector3;


    class main extends PluginBase implements Listener {




    public function onEnable() {
    $this->getLogger()->info("Plugin enabled!");
    }


    public function onMove(PlayerMoveEvent $event) {
    $player = $event->getPlayer();
    $block = $event->getPlayer()->getLevel()->getBlock($event->getPlayer()->floor()->subtract(0, 1));
    if($block instanceof Block) {
    $id = $block->getId();
    $meta = $block->getDamage();
    if($id === 133) { //133 - Emerald Block
    $target = new Vector3(0, 5, 0);
    $player->teleport($target);
    }
    }
    }

    public function onDisable()
    {
    $this->getLogger()->info("Plugin unloaded!");
    }

    }
  12. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Add this:
    PHP:
    use pocketmine/block/Block;
    As @GmWM said, learn the API
    PEMapModder and GmWM like this.
  13. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    Where is the best to learn it?
  14. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    You should learn it too. He also needs to register the Events in onEnable():
    PHP:
    $this->getServer()->getPluginManager()->registerEvents($this$this);
  15. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    Thanks everyone!
  16. GmWM
    Offline

    GmWM Active Member Plugin Developer

    Joined:
    Sep 8, 2013
    Posts:
    181
    Plugins:
    3
    So it's working now, right? :)
  17. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    Yeah. Maybe you can tell me how to do it with coordinates?:)
  18. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    *Facepalm* Learning the API would help a lot!
    Legoboy0215 likes this.
  19. haimann951
    Offline

    haimann951 New Member

    Joined:
    Mar 15, 2015
    Posts:
    18
    Minecraft User:
    haimann951
    Where can I learn the API?
  20. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    There is so many ways for example, looking in a plugin would help, it has helped me a lot. Also reading the docs help. There is other ways to learn it that I haven't mentioned.

Share This Page

Advertisement