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

spawn floatingTextParticle in a certain world

Discussion in 'Development' started by Levi, Aug 15, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how do i spawn floatingTexts in one world only? or maybe 2?

    PHP:
    public function onPlayerJoin(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
            
    $level $player->getLevel();
            
    $vector = new Vector3(606060);
            
    $level->addParticle(new FloatingTextParticle($vector"Test"));
         }
     
    Last edited: Aug 15, 2017
  2. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    i tried

    PHP:
    $level $this->getServer()->getLevelByName("mines");
           
    $player $event->getPlayer();
           
    $vector = new Vector3(606060);
            
    $level->addParticle(new FloatingTextParticle($vector"Test"));
    but for some reason the texts spawns in all worlds ;/
     
  3. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    this is a known problem. you can work around it by despawning the FTPs to players that are not in the world. A very crappy and bad example is my FTP plugin which does this, but i wrote that 2yrs ago and was just learning php back then.
    But if it helps, here it is:
     

    Attached Files:

  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    This might help you!
    PHP:
    const REMOVE_TEXT_PACKET 0;
    const 
    ADD_TEXT_PACKET 1;

    private 
    $floatingTextLevelId = -1;

    /** @var Position $pos */
    $particle = new FloatingTextParticle($pos"Test");
    $this->floatingTextLevelId $pos->getLevel()->getId();
    $packets $particle->encode();

    /**
     * @param EntityLevelChangeEvent $e
     * @priority HIGH
     * @ignoreCancelled true
     */
    public function onLevelChange(EntityLevelChangeEvent $e){
        
    $entity $e->getEntity();
        if(
    $entity instanceof Player){
            if(
    $e->getOrigin()->getId() === $this->floatingTextLevelId){
                
    $entity->dataPacket($packets[self::REMOVE_TEXT_PACKET]);
            }elseif(
    $e->getTarget()->getId() === $this->floatingTextLevelId){
                
    $entity->dataPacket($packets[self::ADD_TEXT_PACKET]);
            }
        }
    }
    Explanation:
    The floating text is constructed using Position instead of Vector3 and the Position's levelId is cached to a class property $floatingTextLevelId.

    FloatingTextParticle::encode() returns 2 packets enclosed in an array. The first being RemoveEntityPacket and the second one - AddEntityPacket.

    On EntityLevelChangeEvent, we first check whether the entity is a player to avoid unnecessary checks and errors while calling dataPacket() function.
    If the origin level is same as the floating text's level, then that means the player is leaving the floating text's level. If the target level is the same as the floating text's level, then that means the player is teleporting to the floating text's level.
     
    Levi likes this.
  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.