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

Around a player?

Comments in 'Plugin Development' started by Legoboy0215, Oct 5, 2015.

  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    How do I create a fake radius (Like a circle) and get all players inside it?
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    foreach($level->getPlayers() as $p){
    if(
    $p->distance(new Vector3($Centrex,$Centrey,$Centrez))) $players[] = $p
    }
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    What would centerx, y, z be?
  4. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    PHP:
    foreach ($sender->getLevel()->getPlayers() as $player) {
                if (
    $player->getName() === $sender->getName()) continue;
                
    $dist $player->distanceSquared($player);
                
    $radius 64 64;
                if (
    $dist $radius$players[] = $player->getDisplayName();
            }
    So this would place nearest to furthest player in order?
  6. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    No, that only gives you a list of players that are within a 64 block radius. You could:

    PHP:
                if ($dist $radius$players[$dist] = $player->getDisplayName();
    Then you could sort by keys... BTW, for performance you should do $radius = 64*64 outside the foreach.
    PEMapModder, Falk and Legoboy0215 like this.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or just hardcode 4096, and don't make it a variable :p
    And what if there are two players who are at the same position? This is not a rare issue. What if two players are at spawn, and both are still authenticating (frozen)? That will make one player overwrite another.
    What about this?
    PHP:
    $picked = [];
    $distSqMap = [];
    foreach(
    $sender->getLevel()->getPlayers() as $player){
      if(
    $player === $sender) continue;
      
    $distSq $player->distanceSquared($sender); // name it $distSq to avoid tricking yourself into believing it is the direct distance
      
    if($distSq 4096){
        
    $picked[$player->getId()] = $player;
        
    $distSqMap[$player->getId()] = $distSq;
      }
    }
    asort($distSqMap);
    // there should be a way to sort $picked into the right order, but for the sake of performance, if you are going to use $picked in ascending order of distance in the future (e.g. in a foreach), I recommend you to instead foreach upon $distSqMap. For example:
    [php]
    foreach(
    $distSqMap as $id => $distSq){
      
    $sender->sendMessage($picked[$id]->getDisplayName() . " is " sqrt($distSq) . " blocks from you.");
    }

Share This Page

Advertisement