Solved Need help with FloatingText

Discussion in 'Development' started by PotatoeTheDev, Mar 11, 2018.

  1. PotatoeTheDev

    PotatoeTheDev Witch

    Messages:
    63
    GitHub:
    potatoetrainyt
    Im trying to use floating text for my server, i looked around and found this
    PHP:
    // To create the particle$
    ftext = new FloatingTextParticle(Vector3 $positionint $textstring $title);
    // To spawn it so players can see it:
    $level->addParticle($ftext);
    then i tried doing myself, i made a new function and ran it in onEnable
    PHP:
        public function spawnText() {
            
    $infoParticle = new FloatingTextParticle(new Position(6710, - 963$this->getServer()->getLevelByName("SkyBlock")), "§l§bINFO""");
            
    $this->getServer()->getLevelByName("SkyBlock")->addParticle($infoParticle);
        } 
    i joined the server, there was no error and no text. If anyone could help i would appreciate it
     
    OnTheVerge likes this.
  2. iCirgio

    iCirgio Witch

    Messages:
    62
    GitHub:
    lolnova
    Full code
     
  3. PotatoeTheDev

    PotatoeTheDev Witch

    Messages:
    63
    GitHub:
    potatoetrainyt
    PHP:
    <?php
    declare(strict_types=1);
    namespace 
    Primo;
    use 
    Primo\Commands\BypassCommand;
    use 
    Primo\Events\Block\{BlockBreakBlockPlace};
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\level\particle\FloatingTextParticle;
    use 
    pocketmine\plugin\PluginBase;
    class 
    Loader extends PluginBase {
        const 
    PREFIX_GREEN "§l§7(§a!§7)§r§7 ";
        const 
    PREFIX_GOLD "§l§7(§6!§7)§r§7 ";
        const 
    PREFIX_RED "§l§7(§c!§7)§r§7 ";
        public function 
    onEnable() {
            @
    mkdir($this->getDataFolder());
            
    $this->saveResource("BypassedPlayers.txt");
            
    $this->loadCommands();
            
    $this->loadEvents();
            
    $this->spawnText();
        }
        public function 
    loadCommands() {
            
    $this->getServer()->getCommandMap()->registerAll("Primo", [
               new 
    BypassCommand("bypass"$this)
                ]);
        }
        public function 
    loadEvents() {
            
    $events = [
                new 
    BlockBreak($this),
                new 
    BlockPlace($this)
                ];
            foreach(
    $events as $event) {
                
    $this->getServer()->getPluginManager()->registerEvents($event$this);
            }
        }
        public function 
    spawnText() {
            
    $infoParticle = new FloatingTextParticle(new Position(6710, - 963$this->getServer()->getDefaultLevel()), """§l§bINFO"); 
            
    $this->getServer()->getDefaultLevel[()->addParticle($infoParticle);
        }
    }
     
  4. iCirgio

    iCirgio Witch

    Messages:
    62
    GitHub:
    lolnova
    Oh nvm. Your error is on Enable. Should be ran on Join not on Enable. Reason being you must set it when they join not when server loads plugin
     
  5. PotatoeTheDev

    PotatoeTheDev Witch

    Messages:
    63
    GitHub:
    potatoetrainyt
    ahh OK ty imma try
     
  6. di2134876

    di2134876 Spider Jockey

    Messages:
    26
    GitHub:
    dk1234987
    That would cause major lag, since the particle would spawn in the same place everytime a player joins.
     
  7. iCirgio

    iCirgio Witch

    Messages:
    62
    GitHub:
    lolnova
    No it wouldnt. It despawns when you leave......
     
  8. Keith

    Keith Spider Jockey Verified

    Messages:
    42
    GitHub:
    k3ithos
    Incorrect. It doesn’t despawn for everybody else when you leave.
    By using the current code:
    - A player joins, floating text particle is added
    - Another player joins, floating text particle is added to level again
    ...and so on.

    The first player will see two floating text particles (because it was added and sent to them twice) while the second player will only see one because the previous particle wasn’t sent to them. This continues and in the end, you’ll have many floating text particles added at the same spot (not sure if the first player would be happy about that!).

    To avoid this problem, all you need to do is add another argument to the code you’re already using to send the floating text particle to the player:
    PHP:
    $this->getServer()->getDefaultLevel()->addParticle($infoParticle, [$player]);
    ($player is an instance of Player)
    The 2nd argument to addParticle() (make sure you’re sending an array) allows you to choose which players to send the particle to. If you don’t set the 2nd argument, by default it’ll send the particle to everybody in the server. By using this code, you’re ensuring that you only send the floating text particle to the player who is meant to receive it (the player who has just joined, not everybody else).
     
    Levi, Edwardthedog2, SOFe and 2 others like this.
  9. iCirgio

    iCirgio Witch

    Messages:
    62
    GitHub:
    lolnova
    Oh, thanks for explaining that. never would have notice
     

Share This Page

  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.