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

Updating Text Particle

Discussion in 'Development' started by rektpixel, Oct 12, 2017.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    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($contenttrue);
                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(4845, -876), """§eThere are §b" . ($totalPlayerCount) . " §eplayers online"));
                }
            }
        }
        
        public function 
    onRegenerate(QueryRegenerateEvent $ev){
            
    $this->updateTexts();
        }
     
  2. Aviv

    Aviv Baby Zombie

    Messages:
    156
    Use FloatingTextParticle::setText(string $text) and FloatingTextParticle::setTitle(string $title)
     
  3. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    o could you show me an example, I'm a bit nooby and confused
     
  4. Aviv

    Aviv Baby Zombie

    Messages:
    156
    PHP:
    $particle = new FloatingTextParticle(new Vector3(4845, -876), """§eThere are §b" . ($totalPlayerCount) . " §eplayers online");
    $level->addParticle($particle);
    Particle title is now: ""
    PHP:
    $particle->setTitle("Test");
    Particle title is now "Test"
     
    rektpixel likes this.
  5. Aviv

    Aviv Baby Zombie

    Messages:
    156
    BTW
    Argument 2 on FloatingTextParticle is the text, argument 3 is the title, so dont get confused
     
  6. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    oh when I do this the same thing occours
    PHP:
                    $level $this->getServer()->getLevelByName("newhub");
                    
    $particle = new FloatingTextParticle(new Vector3(4845, -876), """§eThere are §b" . ($totalPlayerCount) . " §eplayers online");
                    
    $level->addParticle($particle);
                    
    $particle->setTitle("§eThere are §b" . ($totalPlayerCount) . " §eplayers online");
     
  7. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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
     
  8. Aviv

    Aviv Baby Zombie

    Messages:
    156
    Example:
    PHP:
    public function onEnable(){
      
    $this->particle = new FloatingTextParticle(new Vector3(4845, -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");
    }
     
  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.