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)
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)
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
By here is what I know PHP: <?phpnamespace 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
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; }}
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"); } } } } }
PHP: <?phpnamespace 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
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.