I'm trying to display the online count on a floating text, which worked. but the difficulty is that on QueryRegenerateEvent it just adds another text over the original text, I just want it to update. how can I do this? this is the code: PHP: public function updateTexts(){ $config = $this->getConfig()->get("servers"); foreach($config as $servers){ $content = file_get_contents("http://mcapi.ca/query/".$servers."/mcpe"); $decode = json_decode($content, true); if(!isset($decode["error"])){ $this->maxCount = $this->maxCount + $decode["players"]["max"]; $this->playerCount = $this->playerCount + $decode["players"]["online"]; $localTotalPlayers = count($this->getServer()->getOnlinePlayers()); $totalPlayerCount = $localTotalPlayers + $this->playerCount; $level = $this->getServer()->getLevelByName("newhub"); $level->addParticle(new FloatingTextParticle(new Vector3(48, 45, -876), "", "§eThere are §b" . ($totalPlayerCount) . " §eplayers online")); } } } public function onRegenerate(QueryRegenerateEvent $ev){ $this->updateTexts(); }
PHP: $particle = new FloatingTextParticle(new Vector3(48, 45, -876), "", "§eThere are §b" . ($totalPlayerCount) . " §eplayers online");$level->addParticle($particle); Particle title is now: "" PHP: $particle->setTitle("Test"); Particle title is now "Test"
oh when I do this the same thing occours PHP: $level = $this->getServer()->getLevelByName("newhub"); $particle = new FloatingTextParticle(new Vector3(48, 45, -876), "", "§eThere are §b" . ($totalPlayerCount) . " §eplayers online"); $level->addParticle($particle); $particle->setTitle("§eThere are §b" . ($totalPlayerCount) . " §eplayers online");
Because you keep adding the particle, I'd suggest putting the particle in a global variable outside the function, adding it once to the player joined, and updating it with setTitle()/setText() ONLY
Example: PHP: public function onEnable(){ $this->particle = new FloatingTextParticle(new Vector3(48, 45, -876), "", "§eThere are §b" . ($totalPlayerCount) . " §eplayers online");}public function onPlayerJoin(PlayerJoinEvent $e){ $e->getPlayer()->getLevel()->addParticle($this->particle, [$event->getPlayer()]);}public function updateParticle(){ $this->particle->setTitle("New Title");}