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

Check if player is in area

Discussion in 'Development' started by Aenteritas, Oct 15, 2019.

  1. Aenteritas

    Aenteritas Silverfish

    Messages:
    22
    Hey there!

    How can i check if a player is in a area of 50 blocks from the world spawn?
    I need it for a farmworld protection.

    My current code:
    PHP:

    <?php

    namespace aenoxic\listener;

    use 
    aenoxic\LordTown;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\event\Listener;

    class 
    FarmweltProtection implements Listener {

        public 
    $plugin;

        public function 
    __construct(LordTown $plugin){
            
    $this->plugin $plugin;
        }

        public function 
    onBreakFW(BlockBreakEvent $event){
            
    $player $event->getPlayer();
            
    $level $this->plugin->getServer()->getLevelByName("Liquide")->getSafeSpawn();
            
    $levelx $this->plugin->getServer()->getLevelByName("RED")->getSafeSpawn();

            for (
    $x $level->getFloorX() - 100$x <= $level->getFloorX() + 100$x++){
                for (
    $y $level->getFloorY() - 100$y <= $level->getFloorY() + 100$y++){
                    for (
    $z $level->getFloorZ() - 100$z <= $level->getFloorZ() + 100$z++){
                        
    $event->setCancelled();
                    }
                }
            }
        }
    }
    Can anyone help me with some cool examples? :D

    Thanks alot!
     
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Vector3->distance(Vector3) I guess, I can't understand your code.
     
  3. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    If you want check square area, you can use this code

    PHP:
    if($player->getX() < $spawnX+50 && $player->getX() > $spawnX-50) {
      if(
    $player->getZ() < $spawnZ+50 && $player->getZ() > $spawnZ-50) {
        
    // code
      
    }
    }
     
  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.