Help me fix it

Discussion in 'Help' started by Ngao, Feb 9, 2018.

  1. Ngao

    Ngao Silverfish

    Messages:
    18
    Could not pass event 'pocketmine\event\server\DataPacketReceiveEvent' to 'FormAPI v1.0.0': Call to a member function myMoney() on null on jojoe77777\FormAPI\FormAPI
    Error: "Call to a member function myMoney() on null" (EXCEPTION) in "plugins/EnchantUI_raw/src/ID/armor/antiknockback" at line 25
    PHP:
    <?php

    namespace ID\armor;

    use 
    pocketmine\Player;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\item\Armor;
    use 
    EnchantUI\Main;
    // EconomyAPI
    use onebone\economyapi\EconomyAPI;

    class 
    antiknockback{

    public 
    $eco;
        private 
    $plugin;

        public function 
    __construct(Main $plugin){
            
    $this->plugin $plugin;
        }
        
        public function 
    init(Player $sender){
            
    $eco $this->plugin->getServer()->getPluginManager()->getPlugin("EconomyAPI");
            
    $api $this->plugin->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $money $this->eco->myMoney($sender->getName());
            
    $form $api->createCustomForm(function (Player $sender, array $data){
                
    $result $data[0];
                
    $player $sender->getName();
           
    $item $sender->getInventory()->getItemInHand();
                if(
    $result != null){
                    if (
    $item instanceof Armor) {
                                        if(
    $money >= 5000){
                                           
    $this->eco->grantMoney($sender->getName(),-5000);
                            
    $this->plugin->getServer()->dispatchCommand(new ConsoleCommandSender"ce enchant antiknockback $result $player");
                          
    $sender->sendMessage($data[1] Enchant thành công!");
                                          return 
    true;
                                        }else{
                                           
    $sender->sendMessage("no money");
                                        }
            
            return 
    true;
                }else{
                    
    $sender->sendMessage("Enchant này chỉ có hiệu quả với Giáp");
                }
                }
            });
            
    $form->setTitle(TextFormat::BLUE "--= " TextFormat::RED "EnchantUI" TextFormat::BLUE " =--");
            
    $form->addSlider("Max Level:"11, -1, -1); #rare
            
    $form->addLabel("Giảm lượt knockdown xuống 25% cho mỗi bộ giáp");
            ;
    $form->sendToPlayer($sender);
        }
        }
     
  2. Flavius12

    Flavius12 Spider Jockey

    Messages:
    30
    GitHub:
    flavius12
    It happens because you are trying to access a class variable which is currently set to null. Replace
    PHP:
    $this->eco->myMoney($sender->getName());
    with
    PHP:
    $eco->myMoney($sender->getName());
    They are not the same. With $this->variable you access to a class variable while with $variable you access to a function variable (local).
    In your case you set the EconomyAPI instance into the function's variable $eco and you left the class variable "$this->eco" null.
    Keep also in mind that to pass variables inside closures you must specify them with the use construct.
    In essence you should replace:
    PHP:
    function (Player $sender, array $data){
    with
    PHP:
    function (Player $sender, array $data) use ($money){
    Also be careful when you use $this variable inside closures.
     
    Last edited: Feb 9, 2018
    corytortoise, Awzaw and Ngao like this.
  3. Ngao

    Ngao Silverfish

    Messages:
    18
    thank you and
    For this part I have to do "$this->eco->reduceMoney($sender->getName(),-5000);"
     
  4. NguyenDongQuy

    NguyenDongQuy Creeper

    Messages:
    2
    GitHub:
    HeoGM
    Để check tiền player thì sử dụng
    $eco->myMoney(<hàm get tên Player>);
    còn để kiểm tra tiền của player có đủ hay không thì vẫn có thể dùng luôn:
    if($eco->reduceMoney(<hàm get tên Player>, <số lượng>)){
    mà không cần qua
    if($eco->myMoney(<hàm get tên Player>) <so sánh> <số lượng>){
     

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.