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

Solved pocketmine\utils\Config::__construct wants string?!

Discussion in 'Development' started by TBNRShadowDev, Nov 7, 2017.

  1. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    Hi.
    So I programmed this plugin, whose intended function is to use EconomyAPI to pay players the amount of money I defined in config.yml.
    My Main class:
    PHP:
    <?php
    /**
     * Created by PhpStorm.
     * User: tbnrs
     * Date: 11/4/2017
     * Time: 12:40 PM
     */

    namespace TBNRShadowDev\TaskPay;


    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\utils\InvalidCommandSyntaxException;

    class 
    Main extends PluginBase implements Listener
    {
        public function 
    onEnable()
        {
            
    $this->getServer()->getLogger("TaskPay by TBNRShadowDev Enabled.");
            
    $this->saveResource("config.yml"false);
            
    $this->task();
        }
        public function 
    task(){
            
    $task = new PayTask($this);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask($task300*20); // Counted in ticks (1 second = 20 ticks)
        
    }
        public function 
    getConfigObject(){
            
    $config $this->getConfig();
            return 
    $config;
    }}
    And my PayTask:
    PHP:
    <?php
    /**
     * Created by PhpStorm.
     * User: tbnrs
     * Date: 11/4/2017
     * Time: 12:44 PM
     */

    namespace TBNRShadowDev\TaskPay;

    use 
    TBNRShadowDev\TaskPay\Main;
    use 
    onebone\economyapi\EconomyAPI;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Player;

    class 
    PayTask extends PluginTask
    {
            protected 
    $plugin;
            public function 
    __construct(Main $loader){
                    
    parent::__construct($loader);
            }

        
    /**
         * Actions to execute when run
         *
         * @param int $currentTick
         *
         * @return void
         */
        
    public function onRun(int $currentTick)
        {
            
    $main = new Main;
            
    $config $main->getConfigObject();
            
    $players $config->getAll();
            foreach(
    $players as $keys => $p){
                
    $m $config->get($p);
                
    $player $main->getServer()->getPlayer($p);
                if(
    $player->isOnline()){
                    
    EconomyAPI::getInstance()->addMoney($p$m);
                    
    $player->sendMessage("You have received your pay of $" $m);
                }
            }
        }
    }
    And it give this error in the log:
    Code:
    2017-11-08 [12:24:27] [Server thread/CRITICAL]: Could not execute task TBNRShadowDev\TaskPay\PayTask: Argument 1 passed to pocketmine\utils\Config::__construct() must be of the type string, null given, called in phar:///home/chronos/Desktop/flServer/PocketMine-MP.phar/src/pocketmine/plugin/PluginBase.php on line 264
    2017-11-08 [12:24:27] [Server thread/CRITICAL]: TypeError: "Argument 1 passed to pocketmine\utils\Config::__construct() must be of the type string, null given, called in phar:///home/chronos/Desktop/flServer/PocketMine-MP.phar/src/pocketmine/plugin/PluginBase.php on line 264" (EXCEPTION) in "src/pocketmine/utils/Config" at line 82
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #0 src/pocketmine/plugin/PluginBase(264): pocketmine\utils\Config->__construct(NULL )
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #1 src/pocketmine/plugin/PluginBase(244): pocketmine\plugin\PluginBase->reloadConfig()
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #2 TaskPay/src/TBNRShadowDev/TaskPay/Main(31): pocketmine\plugin\PluginBase->getConfig()
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #3 TaskPay/src/TBNRShadowDev/TaskPay/PayTask(33): TBNRShadowDev\TaskPay\Main->getConfigObject()
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #4 src/pocketmine/scheduler/TaskHandler(160): TBNRShadowDev\TaskPay\PayTask->onRun(integer 1)
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #5 src/pocketmine/scheduler/ServerScheduler(326): pocketmine\scheduler\TaskHandler->run(integer 1)
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #6 src/pocketmine/Server(2506): pocketmine\scheduler\ServerScheduler->mainThreadHeartbeat(integer 1)
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #7 src/pocketmine/Server(2253): pocketmine\Server->tick()
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #8 src/pocketmine/Server(2132): pocketmine\Server->tickProcessor()
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #9 src/pocketmine/Server(1714): pocketmine\Server->start()
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #10 src/pocketmine/PocketMine(558): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string phar:///home/chronos/Desktop/flServer/PocketMine-MP.phar/, string /home/chronos/Desktop/flServer/, string /home/chronos/Desktop/flServer/plugins/)
    2017-11-08 [12:24:27] [Server thread/DEBUG]: #11 /home/chronos/Desktop/flServer/PocketMine-MP.phar(1): require(string phar:///home/chronos/Desktop/flServer/PocketMine-MP.phar/src/pocketmine/PocketMine.php)
    
    What should I do?
     
  2. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Your problem is caused by you creating a new Main object, so don't do that. Instead, use the existing property $this->owner to get the Main class or add a Config parameter to the constructor of your PayTask. Also, remove Main::getConfigObject() as all it does is wrap the PluginBase::getConfig() function.
     
    Last edited: Nov 12, 2017
    SOFe, HimbeersaftLP and jasonwynn10 like this.
  3. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    Thank you.
    Your help was greatly appreciated, I will try that now.
     
  4. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    Okay, after thorough testing, the solution partially worked! But... Now it starts to say "Call to a member function isOnline() on null" and will not give me money. I have done my config.yml correctly, thoroughly checked that my name was correct, but it will not say that I'm online when I actually am. How shall I resolve that?
     
  5. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    check if $p instanceof Player and $p->isOnline().
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    I assume that your config looks like this:
    Code:
    steve: 123
    sandertv: 152
    tbnrshadow: 1337
    
    which would mean, that with your code, you're getting all players in an array that looks like this: ["steve" => 123, "sandertv" => 152, "tbnrshadow" => 1337]
    PHP:
    $players $config->getAll();
    Then you're using foreach on it, so thay would mean $keys is the playername and $p is the amount of money to give him/her:
    PHP:
            foreach($players as $keys => $p){
    Now is, where the wrong part starts, you're trying to get the amount of money the player get's, from the config, which ofc won't work, since our $p is already that amount, I would completely remove that line, since you already have both the amount and the player name in $keys and $p
    PHP:
                $m $config->get($p);
    Then you're trying to get the player that has the amount of money as name, which won't work either (returns null), use $keys instead of $p here:
    PHP:
                $player $main->getServer()->getPlayer($p);
    And here you're trying to check if null is online, you should instead check, if $player != null
    PHP:
                if($player->isOnline()){
    The usage of addMoney is addMoney($player, $amount), so you should use addMoney($player, $p) or addMoney($keys, $p) instead of this:
    PHP:
                    EconomyAPI::getInstance()->addMoney($p$m);
    The rest is just fine like it is:
    PHP:
                    $player->sendMessage("You have received your pay of $" $m);
                }
            }
     
  7. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    Thanks so much for your help guys!
    All worked as intended!
     
    HimbeersaftLP likes this.
  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.