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

What's the best way to implement a spawn radius?

Comments in 'Plugin Development' started by DaPorkchop, Aug 25, 2016.

  1. DaPorkchop
    Offline

    DaPorkchop New Member

    Joined:
    Sep 17, 2015
    Posts:
    1
    Minecraft User:
    DaPorkchop_
    I have a small server for me and a couple of friends. We messed up spawn completely, and are now having quite a difficult job getting away from it. I'm planning on writing a plugin that teleports a player to a random set of safe coordinates when they respawn (assuming their spawn point != 0,0 i.e. spawn). What would be the mos efficient (for speed and code readablility) way to do this? For now i just wait for PlayerRespawnEvent, check the coords, and if they are not 0,0 run the following:

    Code:
    if ($this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY(), $this->getSpawn()->getZ()))->getId() == Block::AIR && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() + 1, $this->getSpawn()->getZ()))->getId() == Block::AIR && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::AIR && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::LAVA && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::STILL_LAVA && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::FIRE)    {
                                    echo("safe");
                                $pos = $this->getSpawn();
                            } else {                                echo("not safe");                                $this->setSpawn(new Vector3(rand(-127, 127), rand(10, 127), rand(-127, 127)));
                                while (!($this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY(), $this->getSpawn()->getZ()))->getId() == Block::AIR && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() + 1, $this->getSpawn()->getZ()))->getId() == Block::AIR && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::AIR && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::LAVA && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::STILL_LAVA && $this->level->getBlock(new Vector3($this->getSpawn()->getX(), $this->getSpawn()->getY() - 1, $this->getSpawn()->getZ()))->getId() != Block::FIRE))    {
                                    echo("try");
                                    $this->setSpawn(new Vector3(rand(-127, 127), rand(10, 127), rand(-127, 127)));
                                }
                                $pos = $this->getSpawn();
                            }
    
    and teleport them to $pos. Is there a better way to do this?
  2. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    This thread is in the wrong place. It should be in "Plugin Development".
    As for your problem, I'm a little confused. Can you not get it to work at all? Or does it work, but you just want it to be faster?
  3. Yeelze
    Offline

    Yeelze New Member

    Joined:
    Apr 2, 2016
    Posts:
    31
    Minecraft User:
    Yeelze
    PHP:
    $player->teleport($this->getServer()->getDefaultLevel()->getSafeSpawn());
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  5. Ragnok1234
    Offline

    Ragnok1234 Active Member

    Joined:
    Jul 18, 2016
    Posts:
    53
    Minecraft User:
    Ragnok123
    Maybe he wants this:

    Write
    Code:
    $this->radius = 100;
    in onEnable() function
    And then make example :
    Code:
    public function randomTeleport(PlayerMoveEvent $e) {
            $entity = $e->getPlayer();
            $v = new Vector3($entity->getLevel()->getSpawnLocation()->getX(), $entity->getPosition()->getY(), $entity->getLevel()->getSpawnLocation()->getZ());
                $rand = mt_rand(1, 3);
                   switch($rand){
                         case 1:
                                  $pos = 0, 0, 0;
                              break;
                        case 2:
                                   $pos = 50, 50, 50;
                               break;
                        case 3:
                                   $pos = 100, 100, 100;
                               break;
                              }
                if(floor($entity->distance($v)) >= $this->radius) {
                    $e->setCancelled();
                    $entity->teleport(new Vector3($pos));
                    $entity->sendMessage("┬žeRandom teleport :)");
            }
        }

Share This Page

Advertisement