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

give item to nearest player

Comments in 'Forum Discussion' started by creeperassassin, Nov 22, 2014.

  1. creeperassassin
    Offline

    creeperassassin Active Member

    Joined:
    Oct 16, 2014
    Posts:
    27
    is it possible to give an item to the nearest player if it is how would i do that?
    64FF00 likes this.
  2. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    Code:
    <?php
    
    namespace _64FF00\SOMETHINGUSELESS;
    
    use pocketmine\level\Location;
    
    use pocketmine\Player;
    
    use pocketmine\plugin\PluginBase;
    
    class SOMETHINGUSELESS extends PluginBase
    {
        // ...
    
        public function getNearestPlayer(Player $player)
        {
            $playerLocation = $player->getLocation();
        
            $targetPlayers = $this->getServer()->getOnlinePlayers();
        
            $nearestDistance = 0;
        
            foreach($targetPlayers as $targetPlayer)
            {
                $targetLocation = $target->getLocation();
            
                if($nearestDistance == 0 || $playerLocation->distance($targetLocation) < $nearestDistance)
                {
                    $nearestDistance = $playerLocation->distance($targetLocation);
                
                    $nearestPlayer = $targetPlayer;
                }
            }
        
            return $nearestPlayer;
        }
    
        // ...
    }
    ... though I might be wrong.
    Cristalix likes this.
  3. creeperassassin
    Offline

    creeperassassin Active Member

    Joined:
    Oct 16, 2014
    Posts:
    27
    what do you mean to make a plugin i working on leraning php

Share This Page

Advertisement