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

Solved Make entity spawn for 1 player

Discussion in 'Development' started by RyanShaw, Aug 15, 2018.

  1. RyanShaw

    RyanShaw Witch

    Messages:
    69
    Hey, I'm trying to make it so the human im spawning only spawns to the one player only so no one else can see it. I've tried spawnTo and its only worked like a couple of times when testing. And ive also tried to remove the spawned entity for everyone else whos online and it still doesnt work. This code is executed over player join event and it shows no errors. It just spawns the entity for everyone still.
    PHP:
            $skin $player->getSkin();
            
    $data $skin->getSkinData();
            
    $id $skin->getSkinId();
                
    $nbt = new CompoundTag("", [
                    
    "Pos" => new ListTag("Pos", [
                        new 
    DoubleTag(""126.549),
                        new 
    DoubleTag(""20),
                        new 
    DoubleTag(""184.527)
                    ]),
                    
    "Motion" => new ListTag("Motion", [
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0)
                    ]),
                    
    "Rotation" => new ListTag("Rotation", [
                        new 
    FloatTag(""90),
                        new 
    FloatTag(""0)
                    ]),
                    
    "Skin" => new CompoundTag("Skin", [
                            
    "Data" => new StringTag("Data"$data),
                            
    "Name" => new StringTag("Name"$id)
                        ] )                
                         ]);
                
    $entity Entity::createEntity("Human"$this->getServer()->getDefaultLevel(), $nbt);
                
    $entity->setNameTag("§l§6".$player->getName());
                
    $entity->spawnTo($player);//this worked like 2 times before
        
    }
    PHP:
                $entity->spawnToAll($player);
                foreach(
    $this->getServer()->getOnlinePlayers() as $online){
                If(
    $player->getName() != $online->getName()){
                
    $entity->despawnFrom($player);
    }
    }
    Any ideas?
    Edit - My ultimate goal here is to have it so everyone sees a human entity at the same coords but it displays their own skin.
     
    Last edited: Aug 16, 2018
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Possibly make the entity invisible, spawnToAll, then set the same entity visible, and spawnTo the main Player? If spawnTo doesn't work at all, though, that may be a useless suggestion. I'm not sure why spawnTo only works some of the time.
     
  3. RyanShaw

    RyanShaw Witch

    Messages:
    69
    I don't think setting an entity invisible and visible again would really do anything? I did try using AddPlayerPacket instead today but I've only seemed to be crashing with no errors when i spawn it. I did see the player packet spawn tho before i crashed. Maybe I'm missing something?
    PHP:
            $player $event->getPlayer();
            
    $uuid $player->getUniqueId();
            
    $pk = new AddPlayerPacket();
            
    $pk->entityRuntimeId Entity::$entityCount++;
            
    $pk->uuid $uuid;
            
    $pk->username $player->getName();
            
    $pk->item ItemFactory::get(ItemIds::AIR);
            
    $pk->metadata = array();
            
    $pk->yaw 0;
            
    $pk->pitch 0;
            
    $pk->position = new Position(255,5255,$this->getServer()->getDefaultLevel());
            
    $player->dataPacket($pk); 
     
  4. RyanShaw

    RyanShaw Witch

    Messages:
    69
    Just another update on this thread, I've recently tried using PlayerSkinPacket() when people join to make the entity display their skin. It does display players skin on the entity but it seems to crash people have the time when testing
    PHP:
    $pk = new PlayerSkinPacket();
    $pk->uuid $entity->getUniqueId();
    $pk->skin = clone $player->getSkin();
    $player->dataPacket($pk);
    I'm really determined to get this done so any input is appreciated :oops:
     
  5. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Any errors on crash?
     
  6. RyanShaw

    RyanShaw Witch

    Messages:
    69
    No only when the final packet is sent crashes occur sometimes
     
  7. RyanShaw

    RyanShaw Witch

    Messages:
    69
    Yeah the conclusion here is this does not work on join event unless you use it in a delayed task lol Works fine now :)
     
  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.