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

Solved Spawn protection

Discussion in 'Development' started by TheGhan, Apr 12, 2017.

  1. TheGhan

    TheGhan Silverfish

    Messages:
    15
    I want to protect every block in a 12-metre radius from a team spawn point. I currently have the following code:
    PHP:
            public function onBlockBreak(BlockBreakEvent $event){
            
    $player $event->getPlayer();
            
    $mapa $player->getLevel()->getFolderName();
            if(
    in_array($mapa,$this->arena)){
                
    $config = new Config($this->getDataFolder() . "/config.yml"Config::YAML);
                
    $level $this->getServer()->getLevelByName($mapa);
                                if(
    $this->checkTeam($player) == "blue"){
                    
    $team "blue";
                                    
    $thespawn $config->get($mapa "SpawnBLUE");
                                    
    $x $event->getBlock()->getX();
                                    
    $z $event->getBlock()->getZ();
                                    if ((
    $x<$thespawn[0]+12) || ($x<$thespawn[0]-12) || ($z<$thespawn[2]+12) || ($z<$thespawn[2]-12)){
                                        
    $event->setCancelled(true);                
                                    }                   
                   }elseif(
    $this->checkTeam($player) == "red"){
                    
    $team "red";
                                    
    $thespawn $config->get($mapa "SpawnRED");               
                                    
    $x $event->getBlock()->getX();
                                    
    $z $event->getBlock()->getZ();
                                    if ((
    $x<$thespawn[0]+12) || ($x<$thespawn[0]-12) || ($z<$thespawn[2]+12) || ($z<$thespawn[2]-12)){
                                        
    $event->setCancelled(true);
                                    }                   
                                }
            }
        }
       
        public function 
    onBlockPlace(BlockPlaceEvent $event){
            
    $player $event->getPlayer();
            
    $namemap $player->getLevel()->getFolderName();
            if(
    in_array($namemap,$this->arena)){
                        
    $config = new Config($this->getDataFolder() . "/config.yml"Config::YAML);
                        
    $level $this->getServer()->getLevelByName($namemap);
                                if(
    $this->checkTeam($player) == "blue"){
                    
    $team "blue";
                                    
    $thespawn $config->get($namemap "SpawnBLUE");
                                    
    $x $event->getBlock()->getX();
                                    
    $z $event->getBlock()->getZ();
                                    if ((
    $x<$thespawn[0]+12) || ($x<$thespawn[0]-12) || ($z<$thespawn[2]+12) || ($z<$thespawn[2]-12)){
                                        
    $event->setCancelled(true);                
                                    }                   
                   }elseif(
    $this->checkTeam($player) == "red"){
                    
    $team "red";
                                    
    $thespawn $config->get($namemap "SpawnRED");               
                                    
    $x $event->getBlock()->getX();
                                    
    $z $event->getBlock()->getZ();
                                    if ((
    $x<$thespawn[0]+12) || ($x<$thespawn[0]-12) || ($z<$thespawn[2]+12) || ($z<$thespawn[2]-12)){
                                        
    $event->setCancelled(true);
                                    }                   
                                }
            }
        }
    I'm still confused about the math involved. Can anybody help me out?
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    If you want to cancel it within a radius, you're better off using distance() or distanceSquared()

    PHP:
    if($event->getBlock()->distance($position) <= 12) {
      
    $event->setCancelled();
    }
     
    corytortoise likes this.
  3. TheGhan

    TheGhan Silverfish

    Messages:
    15
    Working fine now that I use this code:
    PHP:
    $spawn = new Position($thespawn[0],$thespawn[1],$thespawn[2],$level);
    if(
    $event->getBlock()->distance($spawn) <= 12) {
        
    $event->setCancelled(true);                
    }
    Thanks @Sandertv!
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    No problem! Glad it works.
     
    corytortoise 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.