Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with DataPacket

Comments in 'Plugin Development' started by KaitoDoDo, Apr 20, 2016.

  1. KaitoDoDo
    Offline

    KaitoDoDo New Member

    Joined:
    Feb 10, 2016
    Posts:
    17
    Minecraft User:
    KaitoDoDoYT
    I need set Placed blocks on AddItemEntityPacket, i can do this?
  2. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    What do you mean?
  3. KaitoDoDo
    Offline

    KaitoDoDo New Member

    Joined:
    Feb 10, 2016
    Posts:
    17
    Minecraft User:
    KaitoDoDoYT
    i changed player packet to block, but this appear as floating block model, i need change floating block to Placed block model.
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    BlockHunt? :p

    Use Level::setBlock(Vector3 $pos, Block $block)
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    ^This would cause lag.

    Instead, Just send a falling entity packet and update its movement to sync with the players movement with Level::addEntityMovement($chunkX, $chunkZ, $x, $y, $z, $yaw, $pitch);
    Last edited: Apr 21, 2016
    HotFireyDeath likes this.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    False statement.

    This is my entity Block class for BlockHunt
    PHP:
    <?php
    namespace sbh\Entity;

    use 
    sc\Utils\SupremeTextFormat as Text;

    use 
    pocketmine\entity\Living;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\entity\FallingSand;
    use 
    pocketmine\block\Block as BlockBlock;

    use 
    pocketmine\event\entity\EntityBlockChangeEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;

    use 
    pocketmine\item\Item as ItemItem;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\nbt\tag\ByteTag;
    use 
    pocketmine\nbt\tag\IntTag;
    use 
    pocketmine\nbt\tag\LongTag;
    use 
    pocketmine\network\Network;
    use 
    pocketmine\network\protocol\AddEntityPacket;
    use 
    pocketmine\network\protocol\RemoveEntityPacket;
    use 
    pocketmine\network\protocol\SetEntityMotionPacket;
    use 
    pocketmine\Player;

    class 
    Block extends Living {
       
       const 
    NETWORK_ID 66;

       const 
    DATA_BLOCK_INFO 20;

       public 
    $width 0.98;
       public 
    $length 0.98;
       public 
    $height 0.98;

       protected 
    $blockId 0;
       protected 
    $damage;
       protected 
    $solid false;
      protected 
    $solidBlocks = [];
       protected 
    $solidBlock null;
       protected 
    $hide false;
       protected 
    $replaced 0// What block did this entity cover when turning into solid block ?

       
    protected $petrifaction 1;
       protected 
    $maxPetrifaction 100;
       private 
    $hided false;

       public 
    $canCollide false;
      private 
    $owner;

      public function 
    setOwner(Player $player){
         
    $this->owner $player;
         
    $this->id $player->getId();
       }

       public function 
    setSolid($value){
         
    $this->solid = (bool) $value;
       }

       public function 
    onUpdate($currentTick){
         
    $this->level->addEntityMovement($this->owner->>> 4$this->owner->>> 4$this->getId(), $this->owner->x$this->owner->0.49000001$this->z$this->owner->yaw$this->owner->pitch$this->owner->yaw);
         if(!
    $this->owner instanceof Player or !$this->owner->isAlive() or !$this->owner->isConnected()){
           unset(
    $this->owner->entityBlock);
           foreach(
    $this->solidBlocks as $k => $pos) {
             if(
    $this->level->getBlock($pos) === $this->solidBlock){
               
    $this->level->setBlock($posBlockBlock::get(0));
               unset(
    $this->solidBlocks[$k]);
               
    $this->solidBlock null;
             }
           }
           
    $this->close();
           return;
         }
         if(
    $this->x  != $this->lastX or $this->!= $this->lastY or $this->!= $this->lastZ){
           
    $this->resetPetrifaction();
         }

           
    $this->lastX $this->x;
           
    $this->lastY $this->y;
           
    $this->lastZ $this->z;
         
         if(
    $this->solid){
           if(!
    $this->isOnEdge()){
             
    $pos = new Vector3($this->getFloorX(), $this->getFloorY() + 0.6$this->getFloorZ());
             if(
    $this->isPlaceTaken($pos) === false) {
               
    $this->hide true;
               
    $block BlockBlock::get($this->getBlock(), $this->getDamage());
               
    $this->replaced $this->level->getBlock($pos);
               
    $this->level->setBlock($posBlockBlock::get($this->getBlock(), $this->getDamage()));
               
    $this->solidBlocks[] = $pos;
               
    $this->solidBlock $this->level->getBlock($pos);
               
    $this->level->setBlock($pos$block);
               if (
    $this->hided === false$this->getOwner()->sendMessage("> You are now solid");
             } else {
               
    $this->owner->sendTip(Text::GRAY."You can't hide here");
               
    $this->solid false;
               --
    $this->petrifaction;
             }
           } else {
             
    $this->owner->sendTip(Text::GRAY."You are standing on edge");
           }
         } else {
           
    $this->hide false;
         }
         if(
    $this->hide){
           if(!
    $this->hided){
             
    $pk = new RemoveEntityPacket();
             
    $pk->eid $this->getId();

             
    $this->server->broadcastPacket($this->server->getOnlinePlayers(), $pk);
             
    $this->hided true;
           }
         } else {
           if(
    $this->hided){
             
    $this->spawnToAll();
             foreach(
    $this->solidBlocks as $k => $pos) {
               if(
    $this->level->getBlock($pos) === $this->solidBlock){
                 
    $this->level->setBlock($pos$this->replaced);
                 unset(
    $this->solidBlocks[$k]);
                 
    $this->solidBlock null;
               }
             }
             
    $this->hided false;
           }
         }

         if(
    $this->solid){
           if(
    $this->petrifaction $this->maxPetrifaction){
             
    $this->solid false;
           }
         } else {
           if(
    $this->petrifaction >= $this->maxPetrifaction){
             
    $this->solid true;
           } else {
             
    $this->getOwner()->setXpProgress($this->petrifaction $this->maxPetrifaction);
             
    $this->petrifaction++;
           }
         }
         
    $this->setPosition($this->getOwner()->getPosition());
         return 
    true;
       }

       protected function 
    initEntity(){
         
    parent::initEntity();

         if(isset(
    $this->namedtag->TileID)){
           
    $this->blockId $this->namedtag["TileID"];
         }elseif(isset(
    $this->namedtag->Tile)){
           
    $this->blockId $this->namedtag["Tile"];
           
    $this->namedtag["TileID"] = new IntTag("TileID"$this->blockId);
         }

         if(isset(
    $this->namedtag->Data)){
           
    $this->damage $this->namedtag["Data"];
         }

         if(isset(
    $this->namedtag->Owner)){
           
    $this->setOwner($this->namedtag["Owner"]);
           if(
    $this->owner instanceof Player === false){
             
    $this->close();
           }
         } else {
           
    $this->close();
           return;
         }

         if(isset (
    $this->owner->entityBlock)){
           
    // Remove old Owner's entity block
           
    $this->owner->entityBlock->close();
         } else {
           
    $this->owner->entityBlock $this;
         }

         if(
    $this->blockId === 0){
           
    $this->close();
           return;
         }
         
    $this->lastX $this->x;
         
    $this->lastY $this->y;
         
    $this->lastZ $this->z;
         
    $this->setDataProperty(self::DATA_BLOCK_INFOself::DATA_TYPE_INT$this->getBlock() | ($this->getDamage() << 8));
         
    $this->spawnToALl();
       }

       public function 
    isPlaceTaken(Vector3 $pos){
         
    $b $this->level->getBlockIdAt($pos->x$pos->y$pos->z);
         
    $ignore = [0897859]; // Air, Water, Snow etc. should be ignored :)
         
    return in_array($b$ignoretrue) === false;
       }

       public function 
    canCollideWith(Entity $entity){
         return 
    false;
       }

       public function 
    changeBlock($id$damage){
         
    $this->resetPetrifaction();
         
    $this->blockId $id;
         
    $this->damage $damage;
         
    $pk = new RemoveEntityPacket();
         
    $pk->eid $this->getId();

         
    $this->server->broadcastPacket($this->server->getOnlinePlayers(), $pk);
         
    $this->spawnToAll();
       }

       public function 
    attack($damageEntityDamageEvent $source){
         if(
    $source->getCause() === EntityDamageEvent::CAUSE_VOID){
           
    parent::attack($damage$source);
         }
       }

       public function 
    getBlock(){
         return 
    $this->blockId;
       }

       public function 
    getOwner() : Player {
         return 
    $this->owner;
       }

       public function 
    getDamage(){
         return 
    $this->damage;
       }

       public function 
    saveNBT(){
         
    $this->namedtag->TileID = new IntTag("TileID"$this->blockId);
         
    $this->namedtag->Data = new ByteTag("Data"$this->damage);
         
    $this->namedtag->Owner = new LongTag("Owner"$this->owner);
       }

       public function 
    spawnTo(Player $player){
         
    $pk = new AddEntityPacket();
         
    $pk->type FallingSand::NETWORK_ID;
         
    $pk->eid $this->getId();
         
    $pk->$this->x;
         
    $pk->$this->1.62;
         
    $pk->$this->z;
         
    $pk->speedX $this->motionX;
         
    $pk->speedY $this->motionY;
         
    $pk->speedZ $this->motionZ;
         
    $pk->yaw $this->yaw;
         
    $pk->pitch $this->pitch;
         
    $pk->metadata = [FallingSand::DATA_BLOCK_INFO => [Entity::DATA_TYPE_INT$this->getBlock() | $this->getDamage() << 8]];
         
    $player->dataPacket($pk);

         
    parent::spawnTo($player);
       }

       public function 
    spawnToAll(){
         foreach(
    $this->getViewers() as $e){
           
    $this->spawnTo($e);
         }
       }

       public function 
    getName(){
         return 
    BlockBlock::get($this->getBlock(), $this->getDamage(), 1)->getName()." Entity";
       }

       public function 
    resetPetrifaction(){
         
    $this->petrifaction 1// It can not be zero or it will cause errors
       
    }

       public function 
    isOnEdge(){
         
    $flX $this->x;
         
    $flZ $this->z;
         
    $newX $flX 0.20;
         
    $newZ $flZ 0.20;
         if(
    intval($flX) < intval($newX) or intval($flX) > intval($newX) or intval($flZ) < intval($newZ) or intval($flZ) > intval($newZ)) return true;
         return 
    false;
       }

       public function 
    __destruct(){
         if(
    $this->owner->isConnected()){
           foreach(
    $this->server->getOnlinePlayers() as $player){
             
    $player->showPlayer($player);
           }
         }
         
    $this->close();
       }

    }
    Skullex and KaitoDoDo like this.
  7. KaitoDoDo
    Offline

    KaitoDoDo New Member

    Joined:
    Feb 10, 2016
    Posts:
    17
    Minecraft User:
    KaitoDoDoYT
    I take some of your code and works, now i need improve the code, which is the line to set static block?, the block is moving when player isnt.
    Sorry my english and thanks
  8. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Just add this class into your plugin and use code below to disguise
    PHP:
    /**
       * Will turn player into block!
       * Hiding block should be done with function Block::hide();
       *
       * @param Player $player
       * @param Integer $id
       * @param Integer $damage
       */
       
    public function disguise(Player $player$id$damage){
         
    # Hide
         
    foreach($this->getServer()->getOnlinePlayers() as $for){
           if(
    $player !== $for$for->hidePlayer($player);
         }
         
    # Add Block entity
         
    $entity Entity::createEntity("Block"$player->getLevel()->getChunk($player->>> 4$player->>> 4), new CompoundTag("", [
               
    "Pos" => new ListTag("Pos", [
                 new 
    DoubleTag(""$player->x),
                 new 
    DoubleTag(""$player->y),
                 new 
    DoubleTag(""$player->z)
               ]),
               
    "Motion" => new ListTag("Motion", [
                 new 
    DoubleTag(""0),
                 new 
    DoubleTag(""0),
                 new 
    DoubleTag(""0)
               ]),
               
    "Rotation" => new ListTag("Rotation", [
                 new 
    FloatTag(""0),
                 new 
    FloatTag(""0)
               ]),
               
    "TileID" => new IntTag("TileID"$id),
               
    "Data" => new ByteTag("Data"$damage),
               
    "Owner" => new LongTag("Owner"$player),
         ]));

         foreach(
    $this->getServer()->getOnlinePlayers() as $for){
           
    $entity->spawnTo($for);
         }
         
    # Save
         
    $player->entityBlock $entity;

         return 
    true;
         }
    Skullex and KaitoDoDo like this.
  9. KaitoDoDo
    Offline

    KaitoDoDo New Member

    Joined:
    Feb 10, 2016
    Posts:
    17
    Minecraft User:
    KaitoDoDoYT
    ok, very thanks man
    Primus likes this.
  10. KaitoDoDo
    Offline

    KaitoDoDo New Member

    Joined:
    Feb 10, 2016
    Posts:
    17
    Minecraft User:
    KaitoDoDoYT
    one question man, Is possible hide in all places?, thanks in advance
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Sorry, I didn't quite understand your question. Could you please eloborate?
  12. KaitoDoDo
    Offline

    KaitoDoDo New Member

    Joined:
    Feb 10, 2016
    Posts:
    17
    Minecraft User:
    KaitoDoDoYT
    ok, my english is bad. In the plugin, players cant hide everytime, spam of messages "> You are now solid". I need players can hide anywhere. I hope you understand.
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    I wasn't expecting that you will use all code :D

    Sorry, I don't have time yet to maintenance the code :(
  14. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    For the sake of your own convenience, avoid naming your class with the same name of another existing class, especially one that you will use in your code. Otherwise, have fun:
    https://github.com/PocketMine/Pocke...e7e72b4e/src/pocketmine/entity/Zombie.php#L26
    https://github.com/PocketMine/Pocke...etmine/level/generator/populator/Tree.php#L27
    https://github.com/PocketMine/Pocke...e/level/particle/FloatingTextParticle.php#L25
  15. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Code was written long time ago but I still have that habit and I'm not going to change that, that's why PHP have namespaces and 'as'.
  16. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Just reminding you that if you use IDEs, you are having some trouble with auto-completion, or if other people read your code without (naturally they won't) looking at the use statements, they would be confused and need to check your use statements to confirm.
    Primus likes this.

Share This Page

Advertisement