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

Why i cant setXpLevel() ?

Discussion in 'Development' started by NapolGamer TH, Oct 27, 2018.

  1. NapolGamer TH

    NapolGamer TH Silverfish

    Messages:
    15
    GitHub:
    napolgamerth
    I cant add xp level
    code:
    PHP:
    <?php

    namespace core\Task;

    use 
    pocketmine\scheduler\Task;
    use 
    core\Main;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\entity\Human;

    class 
    MainTask extends Task implements Listener {

        public function 
    __construct(Main $plugin){
            
    $this->plugin $plugin;
        }

        public function 
    onRun(int $currentTick){
            foreach(
    $this->plugin->getServer()->getOnlinePlayers() as $player){
                
    $human Human::class;
                
    $exp $human->getXpLevel() + 1;
                
    $human->setXpLevel((int)$exp);
                
    $player->sendPopup("§e+§a1§bXp§r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
        }
    }
    IMG_20181027_165401.jpg
     
    Last edited: Oct 27, 2018
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    The code you posted should be in PHP tags like so:
    Code:
    [PHP]code here[/PHP]
    , to make it properly readable to others.

    When I look at your code there's numerous errors. First of all, on the class declaration line:
    PHP:
    class MainTask extends Task implements Listener {
    Your task does not listen for events so remove the Listener implementation like so:
    PHP:
    class MainTask extends Task {
    Secondly, you're assigning a dynamic class property, which you might not want to do for the sake of readability. Add this right under your class declaration:
    PHP:
        private $plugin;
    Then for your actual issue, you're setting $human to Human::class, which is a string. ::class is a reserved 'constant' for classes which simply returns the path to the class. You already have a Human however, because Player extends Human. You can remove this line:
    PHP:
     $human Human::class; 
    And you can rename further usages of $human to $player.

    To improve your code further, you can look at this part:
    PHP:
    $exp $player->getXpLevel() + 1;
    $player->setXpLevel((int)$exp);
    getXpLevel() already returns an integer, so you can simply remove the (int) cast, like so:
    PHP:
    $exp $player->getXpLevel() + 1;
    $player->setXpLevel($exp);
     
    Last edited: Oct 27, 2018
  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.