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

Help making a player tracker

Discussion in 'Plugin Help' started by WEATHERCRAFTYT1, Oct 1, 2020.

  1. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    Hello I need to make a player tracker for my future server but the plugins that already have it always crashes so I tried to make my own. Want can I do here since I am still learning php.
    Code:
    <?php
    
    namespace WEATHERCRAFTYT1\PlayerCompass;
    
    use pocketmine\network\mcpe\protocol\types\DimensionIds;
    use pocketmine\Player;
    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerItemHeldEvent;
    use pocketmine\item\ItemIds;
    use pocketmine\level\Position;
    use pocketmine\math\Vector3;
    use pocketmine\network\mcpe\protocol\SetSpawnPositionPacket;
    use pocketmine\plugin\PluginBase;
    
    class NearestPlayerCompass extends PluginBase implements Listener{
    
        public function onEnable(){
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
      }
     
      public function onItemHeld(PlayerItemHeldEvent $event){
            $player = $event->getPlayer();
            if($event->getItem()->getId() === ItemIds::COMPASS)
     
  2. dadodasyra

    dadodasyra Witch

    Messages:
    68
    GitHub:
    dadodasyra
    Pocketmine contains functions to find players around a target player but they are not very qualitative. I think this is not a good plugin for which one started to develop. It's already fairly high level (or so easily possible but not very optimized: Collect all the players and compare their coordinates)
     
  3. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    I know this is not what I supposed to learn but I really want to get this over with so I don’t need to worry about this
     
  4. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Simply get the nearest player with Player->distance(Player)
     
  5. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    Player->distance(Player)?
     
  6. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    By here is what I know
    PHP:
    <?php

    namespace WEATHERCRAFTYT1;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\TextFormat;

    class 
    Main extends PluginBase implements Listener {

        
    /**
         * Enables the plugin
         */
        
    public function onEnable() {
            
    $this->loadArena();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        
    /**
         * @param PlayerItemHeldEvent $event
         */
        
    public function onHold(PlayerItemHeldEvent $event) {
            
    $player $event->getPlayer();
            if(
    $event->getItem()->getId() === ItemIds::COMPASS){
                 
    $z $this->getServer()->getPlayer($player)->getZ();
                 
    $x $this->getServer()->getPlayer($player)->getX();
    I haven’t thought of using Player->distance(Player) and also want the code to say the x y z of the player
     
    Last edited: Oct 5, 2020
  7. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Nevermind, I wrote it incorrectly
    Vector3->distance(Vector3) returns a float which represents the distance between the two vectors, to get the nearest player you could use something like this
    PHP:
    $player Player;
    $nearest_player null;
    foreach(
    $player->getLevel()->getPlayers() as $level_player) {
        if(
    $nearest_player === null) {
            
    $nearest_player $level_player;
        } elseif(
    $player->distance($level_player) < $player->distance($nearest_player)) {
            
    $nearest_player $level_player;
        }
    }
     
    Primus likes this.
  8. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    If I want to send the message to the player the nearest player + distance what do I put
     
  9. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
  10. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    Hello thanks for the guide I was able to get the message but it tells me that NearestPlayer is Player(20) any way to help with that.
    Code:
    <?php
    
    namespace WEATHERCRAFTYT1;
    
    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerItemHeldEvent;
    use pocketmine\plugin\PluginBase;
    use pocketmine\item\ItemIds;
    use pocketmine\Server;
    use pocketmine\Player;
    use pocketmine\utils\TextFormat;
    
    class Main extends PluginBase implements Listener {
    
        /**
         * Enables the plugin
         */
        public function onEnable() {
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
        }
    
        /**
         * @param PlayerItemHeldEvent $event
         */
        public function onHold(PlayerItemHeldEvent $event) {
            if($event->getItem()->getId() === ItemIds::COMPASS){
                $player = $event->getPlayer();
                $nearest_player = null;
                foreach($player->getLevel()->getPlayers() as $level_player) {
                    if($nearest_player === null) {
                        $nearest_player = $level_player;
                        $player->sendMessage("§aNearest Player $nearest_player");
                    } elseif($player->distance($level_player) < $player->distance($nearest_player)) {
                        $nearest_player = $level_player;
                        
                        $player->sendMessage("§aNearest Player $nearest_player");
                    }
                }
            }
        }
    }
     
  11. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Player->getName()
     
    Primus likes this.
  12. Primus

    Primus Zombie Pigman

    Messages:
    749
    Player::getDisplayName(); ?
     
  13. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
  14. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    PHP:
    <?php

    namespace WEATHERCRAFTYT1;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\item\ItemIds;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\TextFormat;

    class 
    Main extends PluginBase implements Listener {

        
    /**
         * Enables the plugin
         */
        
    public function onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        
    /**
         * @param PlayerItemHeldEvent $event
         */
        
    public function onHold(PlayerItemHeldEvent $event) {
            if(
    $event->getItem()->getId() === ItemIds::COMPASS){
                
    $player $event->getPlayer();
                
    $nearest_player null;
                foreach(
    $player->getLevel()->getPlayers() as $level_player) {
                    if(
    $nearest_player === null) {
                        
    $nearest_player $level_player;
                        
    $name $level_player->getName();
                        
    $player->sendMessage("§aYour compass is now tracking $name");
                    } elseif(
    $player->distance($level_player) < $player->distance($nearest_player)) {
                        
    $nearest_player $level_player;
                        
    $player->sendMessage("§aYour compass is now tracking $name");
                    }
                }
            }
        }
    }
     
    Hello when I get the player name it says Now Tracking the name that is tracking instead of the name he is tracking
     
  15. Primus

    Primus Zombie Pigman

    Messages:
    749
    PHP:
    foreach($player->getLevel()->getPlayers() as $level_player) {
    That line will return all players that are inside that level including the one with the compass. So it's obvious that when reading distance from two identical points on the plane, it is going to be zero which by logic above is the nearest player.

    Exclude the player who issued the code from the loop. Afterwards check if nearest player was actually found, this might happen if you're the only in the level.
     
  16. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    Man this will take a long while. I don’t even know what to do. Lemme see what I can do
     
  17. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
  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.