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

Solved Getting this error and I don't know how to fix it:

Comments in 'Plugin Development' started by Hittmana, Jan 27, 2016.

  1. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    E_ERROR - Call to a member function sendMessage() on string

    here is the plugin code:
    PHP:
    <?php

    namespace Hittmana\TradePro;

    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\TranslationContainer;
    use 
    pocketmine\utils\TextFormat;

    class 
    MainClass extends PluginBase implements Listener
    {
        public function 
    onEnable()
        {
            
    $this->getLogger()->info("Test enabled");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    onDisable()
        {
            
    $this->getLogger()->info("Test disabled");
        }
    public function 
    onCommand(CommandSender $senderCommand $command$label, array $args)
    {
        if(
    $sender instanceof Player)
        {
             
    $name $sender->getName();
             if (
    $command->getName() == "test")
             {
                
    $this->getLogger()->info("IT WORKS!!" $name "USED IT!!!!");
                
    $recipient $args[0];
                
    $trader $sender->getName();
                
    $recipient->sendMessage($trader " wants to trade with you!");  
                
    $sender->sendMessage("You sent a trade request " $recipient ". Lets hope he says YES!");
                return 
    true;
        }
        else
        {
        
    $this->getLogger()->info("Command must be run in-game or by a player :(");
        return 
    false;
       }
      }
    }
    }
  2. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    You aren't checking if $recipient is a player.
  3. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    oh ok thanks! so I use
    PHP:
    if($sender instanceof Player){ 
    other code
    To check correct?
  4. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    I said recipient, not sender.

    PHP:
    if(!$recipient instanceof Player){
     
    $sender->sendMessage("Player not found");
    }
    else{
     
    //code
    }
  5. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    ok thanks! Just out of curiosity because I'm new why is there a ! before $recipient ?
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    That means NOT
    PEMapModder and XFuryMCPE like this.
  7. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Ok thanks so much for the help!
    XFuryMCPE likes this.
  8. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    Mhm
  9. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    $recipent is $args[0], $args[0] is a part of a typed, command, means that args is a string, so you need to do $recipent = $this->getServer()->getPlayer($args[0]);
    XFuryMCPE and PocketKiller like this.
  10. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Args isn't a string. $args is an array, which you get the first(counting starts from 0 at programming) entry from: $args[0] and the entry is a string. If you want $args as a string, you have to use implode().
    PEMapModder likes this.
  11. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Ok now it doesn't find a player at all!
    Here is the code:
    PHP:
    <?php

    namespace Hittmana\TradePro;

    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\TranslationContainer;
    use 
    pocketmine\utils\TextFormat;

    class 
    MainClass extends PluginBase implements Listener
    {
        public function 
    onEnable()
        {
            
    $this->getLogger()->info("Test enabled");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    onDisable()
        {
            
    $this->getLogger()->info("Test disabled");
        }
    public function 
    onCommand(CommandSender $senderCommand $command$label, array $args)
    {
        if(
    $sender instanceof Player)
        {
             
    $name $sender->getName();
             if (
    $command->getName() == "test")
             {
                
    $this->getLogger()->info("IT WORKS!!" $name "USED IT!!!!");
              
                
    $recipient $args[0];
                 if(
    $recipient instanceof Player)
                     {
                 
    $trader $sender->getName();
                
    $recipient->sendMessage($trader " wants to trade with you!");  
                
    $sender->sendMessage("You sent a trade request " $recipient ". Lets hope he says YES!");
                return 
    true
                }
                else
                {
                
    $sender->sendMessage("Player not found");
                }
        }
        else
        {
        
    $this->getLogger()->info("Command must be run in-game or by a player :(");
        return 
    false;
       }
      }
    }
    }
  12. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
  13. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    How do i fix the plugin not working?
  14. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    … Look at the error!!!
    Call to member fubction on string]
  15. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Not that thats fixed
  16. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    nvm
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    $args[0] is the player name that the command sender typed. It has absolutely nothing to do with a player object. Just from a logical sense, not even talking about API knowledge, there is no way that PHP/PocketMine/whoever would know that that certain command argument would be related to a player. (Say, maybe it may be a world name, a plugin name, or whatever? Who knows?)
    PocketKiller likes this.

Share This Page

Advertisement