Sending/Creating BossBar

Discussion in 'Low-Quality / Outdated' started by Lowkey, Apr 30, 2017.

  1. Lowkey

    Lowkey Slime

    Messages:
    94
    Since there is no BossBar function in PocketMine yet, you have to create this function yourself. Here's a tutorial on how to do it.

    I know there is already PLUGINS that have been created such as PocketEssential's OR XenialDan's BossBarAPI. This, however is a completely different tutorial.

    First you need to create your Main class, which you can do as such;

    PHP:
    use pocketmine\plugin\PluginBase;
    use 
    /** YOUR BOSSBAR CLASS THAT WE CREATE OURSELF*/
    PHP:
    class Main extends PluginBase {

    //This is our main class named "Main".
    //We now create our function named "getBossBar()" which just returns our BossBar class.

    public function getBossBar(){
    return new 
    BossBar($this);
    }

    We now need to create another class named BossEventPacket.
    PHP:
    use pocketmine\network\protocol\DataPacket;
    use 
    pocketmine\network\protocol\Info;
    use 
    pocketmine\utils\Binary;

    class 
    BossEventPacket extends DataPacket{

        const 
    BOSS_EVENT_PACKET 0x4a;

        const 
    NETWORK_ID self::BOSS_EVENT_PACKET;
          public 
    $eid;
        public 
    $type;

        public function 
    decode(){

        }

        public function 
    encode(){
            
    $this->reset();
            
    $this->putEntityId($this->eid);
            
    $this->putUnsignedVarInt($this->type);

    Now we shall create our BossBar class. This class will be named "BossBar".
    PHP:
    class BossBar {

       private 
    $plugin;

       public function 
    __construct(Main $plugin){
    $this->plugin $plugin;
    }
    //We have now created our constructor and we are 'linked' to the Main class.

    //Now, stuff gets complicated!

    //We will create 2 functions. "addBar", and "setTitle"

    use pocketmine\item\Item;
    use 
    pocketmine\network\mcpe\protocol\AddPlayerPacket;
    use 
    BossEventPacket;
    //Our BossEventPacket class.
    //Make sure the path is right
    use pocketmine\network\mcpe\protocol\MovePlayerPacket;
    use 
    pocketmine\network\mcpe\protocol\SetEntityDataPacket;
    use 
    pocketmine\Player;
    use 
    pocketmine\network\mcpe\protocol\RemoveEntityPacket;
    use 
    pocketmine\Server;
    use 
    pocketmine\network\mcpe\protocol\AddEntityPacket;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\utils\UUID;

    public function 
    addBar(array $players null){
            if(empty(
    $players)){
                
    $players $this->plugin->getPlayers();
            }
            
    $eid Entity::$entityCount++;
            
    $aepkt = new AddEntityPacket();
            
    $aepkt->metadata = [Entity::DATA_LEAD_HOLDER => [Entity::DATA_TYPE_LONG, -1], Entity::DATA_FLAG_SILENT => [Entity::DATA_TYPE_BYTE1], Entity::DATA_SCALE => [Entity::DATA_TYPE_FLOAT0],
                
    Entity::DATA_BOUNDING_BOX_WIDTH => [Entity::DATA_TYPE_FLOAT0], Entity::DATA_BOUNDING_BOX_HEIGHT => [Entity::DATA_TYPE_FLOAT0]];
            
    $aepkt->eid Entity::$entityCount++;
            
    $aepkt->type 52;
            
    $aepkt->yaw 0;
            
    $aepkt->pitch 0;
            foreach(
    $players as $player){
                
    $ppkt = clone $aepkt;
                
    $ppkt->$player->x;
                
    $ppkt->$player->y;
                
    $ppkt->$player->z;
                
    $player->dataPacket($ppkt);
            }
            
    $pk = new BossEventPacket();
            
    $pk->state 0;
            
    $pk->eid Entity::$entityCount++;
            
    Server::getInstance()->broadcastPacket($players$pk);
            return 
    $eid;
        }


        public function 
    setTitle(int $eidstring $string){
            
    $players $this->plugin->getServer()->getOnlinePlayers();
            
    $pk = new SetEntityDataPacket();
            
    $pk->metadata = [Entity::DATA_NAMETAG => [Entity::DATA_TYPE_STRING$string]];
            
    $pk->eid $eid;
            
    Server::getInstance()->broadcastPacket($players$pk);
            
    $pkt = new BossEventPacket();
            
    $pkt->eid $eid;
            
    $pkt->state 500;
            
    Server::getInstance()->broadcastPacket($players$pkt);

    //We have now created our BossBar. Please read the code to try and understand how this works.
    //We can set the text by calling setTitle as such;

    /** public function (your function)*/
    $this->setTitle(11000,"BossBar Created!")

    I hope this has helped you understand. Please give me a like if you have learnt something (anything). Just thought this needed to be out there. It's very complex, however.

    I would recommend people to also study PocketEssential's or XenialDan's BossBarAPI.

    You should also use a task to repeat the BossBar.
     
    Last edited: Apr 30, 2017
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,932
    GitHub:
    sof3
    To be honest this isn't a tutorial. This thread just lists some code to copy.
    I recommend making this into a library.
     
  3. Lowkey

    Lowkey Slime

    Messages:
    94
    I know, I really didn't explain it properly. I'm very sorry. How can I make this into a library?
     
  4. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    I cannot understand this... please edit & give more explanation. I like that you have put effort into this though!
     
  5. Kyd

    Kyd Zombie Pigman

    Messages:
    682
    Code:
    PocketMine-MP Crash Dump Fri May 5 20:59:52 BST 2017
    
    Error: Class Core\Packets\BossEventPacket contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (pocketmine\network\mcpe\protocol\DataPacket::handle)
    File: /plugins/NewCore/src/Core/Packets/BossEventPacket
    Line: 28
    Type: E_ERROR
    
    THIS CRASH WAS CAUSED BY A PLUGIN
    BAD PLUGIN: Core v3.0
    
    Code:
    [19]
    [20]     }
    [21]
    [22]     public function encode()
    [23]     {
    [24]         $this->reset();
    [25]         $this->putEntityId($this->eid);
    [26]         $this->putUnsignedVarInt($this->type);
    [27]     }
    [28] }
    [29]
    [30]
    [31]
    [32]
    [33]
    [34]
    [35]
    [36]
    [37]
    [38] 
    ??? This i get after I join my server
     
  6. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    296
    GitHub:
    TheDiamondYT1
    You do realise that you don't have to include a file that is in the same directory!?
     
  7. Kyd

    Kyd Zombie Pigman

    Messages:
    682
    Not It is in another folder
     
  8. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    296
    GitHub:
    TheDiamondYT1
    Look at the original post by @Lowkey.
    PHP:
    use BossEventPacket;
    This tells me that BossEventPacket.php is in the same directory as the BossBar class
     
  9. Kyd

    Kyd Zombie Pigman

    Messages:
    682
    It is still crashing
     
  10. Lowkey

    Lowkey Slime

    Messages:
    94
    Read what I said under it! I said you need to make sure your path is right.
     
  11. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    296
    GitHub:
    TheDiamondYT1
    PHP:
    use path\to\BossEventPacket;
     
    Last edited: May 8, 2017
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,932
    GitHub:
    sof3
    Let's not turn this thread into a PSR-0 tutorial, OK?
     
  13. Lowkey

    Lowkey Slime

    Messages:
    94
    No need, people would get confused.
     
  14. Sandertv

    Sandertv Skeleton Poggit Reviewer

    Messages:
    800
    GitHub:
    Sandertv
    Eeks! No .php in use statements!
     
    Thunder33345, jasonwynn10 and Lowkey like this.
  15. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    296
    GitHub:
    TheDiamondYT1
    LOL
     
  16. Kyd

    Kyd Zombie Pigman

    Messages:
    682
    My path to BossEventPacket is right so?
     
  17. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    730
    GitHub:
    awzaw
    It would be good if you could update this to reflect the changes in the latest API.
     
  18. PianoRalph04

    PianoRalph04 Witch

    Messages:
    58
    GitHub:
    MechRalph04
    Does this plugin have any commands?
     
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,932
    GitHub:
    sof3
    See @falk's SpoonDetector
     
    jasonwynn10 likes this.
  20. ZeeCraftPE

    ZeeCraftPE Creeper

    Messages:
    4
    can I just copy it? Hehe and add some new features?
     

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.