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

Health

Comments in 'Plugin Development' started by Vladislav, May 3, 2016.

  1. Vladislav
    Offline

    Vladislav New Member

    Joined:
    May 7, 2015
    Posts:
    17
    Minecraft User:
    CMEPTb5
    Why if I enter in the settings PureChat {HP}, then not shows the life of a player?


    PHP:
    <?php

    Vlad 
    namespace;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\player\PlayerChatEvent;

    class 
    Main extends PluginBase implements Listener {

    public function 
    onEnable() {
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }

    public function 
    onPlayerChat(PlayerChatEvent $event) {
    $player $event->getPlayer();
    $hp $player->getHealth();
    $hp str_replace("{HP}"$player->getHealth(), $hp);
    return 
    true;
    }
    }
  2. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Where's the string to replace?
    PHP:
    str_replace("{hp}"$player->getHealth(), "{hp} This is a message.");
    minebuilder0110 likes this.
  3. Vladislav
    Offline

    Vladislav New Member

    Joined:
    May 7, 2015
    Posts:
    17
    Minecraft User:
    CMEPTb5
    That is, I need to replace my code for yours? And {HP} in PureChat will work to highlight the number of hearts?
  4. Vladislav
    Offline

    Vladislav New Member

    Joined:
    May 7, 2015
    Posts:
    17
    Minecraft User:
    CMEPTb5
    Can you give me an example code?
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Player::getHealth() returns player's current health in integer so don't expect to find "{HP}" in a number.

    You probably want to do following
    PHP:
    /**
    * @param PlayerChatEvent $e
    * @priority MONITOR
    * @ignoreCancelled false
    */
    public function onChat(PlayerChatEvent $e){
       if(
    $e->isCancelled()) return; // Make sure you don't force player chat
       
    $format $e->getFormat();
       
    $format str_replace("{HP}"$e->getPlayer()->getHealth(), $msg);
       
    $e->setFormat($format);
    }
    Events are void functions that doesn't expect to catch anything if you return something.

    And here you can learn more about namespaces.
    Last edited: May 3, 2016
    HotFireyDeath likes this.
  6. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    It's you who wrote that code, are you saying that you don't understand what he is trying to say?
    HotFireyDeath likes this.

Share This Page

Advertisement