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

instanceof Does'nt work

Comments in 'Plugin Development' started by Primus, May 6, 2015.

  1. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    PHP:
    <?php

    namespace CheckHealth;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\server\Server;

    class 
    CheckHealth extends PluginBase{
     public function 
    onEnable(){
      
    $this->getLogger()->info("Enabled.");
     }
     public function 
    onDisable(){
      
    $this->getLogger()->info("Disabled.");
     }
     public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){
      switch(
    $cmd->getName()){
            case 
    "hp":
            if(!isset(
    $args[0]) && $sender instanceof Player){
             
    $sender->sendMessage("[Doctor] You have [".$sender->getHealth()."/"$sender->getMaxHealth()."] health points.");
             return 
    true;
            }
          elseif(isset(
    $args[0]) && $args[0] instanceof Player){
             
    $tarPlayer $this->getServer()->getPlayer($args[0]);
             
    $tarName $tarPlayer->getName();
             
    $sender->sendMessage("[Doctor] "$tarName ." currently has ["$this->getServer()->getPlayer($args[0])->getHealth()."/"$this->getServer()->getPlayer($args[0])->getMaxHealth() ."]");
             return 
    true;
            }
       }
      }
     }
    When i run command as Player it shows me usage why it also happens when i run command as Console. Instanceof was working fine except on this plugin :(.
    Hoping for fast reply.
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    case "hp":
            if(isset(
    $args[0])){
             
    $player $args[0];
             if(
    $player $this->getServer()->getPlayer($args[0]){
            
    $sender->sendMessage("[Doctor] "$player->getName()." currently has ["$player->getHealth()."/".  $player->getMaxHealth() ."]");
            return 
    true;
            }
            }
          else{
             
    $sender->sendMessage("[Doctor] You have [".$sender->getHealth()."/"$sender->getMaxHealth()."] health points.");
             return 
    true;
            }
  3. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Try this please :

    Code:
    <?php
    
    namespace CheckHealth;
    
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    use pocketmine\Player;
    use pocketmine\plugin\PluginBase;
    use pocketmine\server\Server;
    
    class CheckHealth extends PluginBase{
     public function onEnable(){
      $this->getLogger()->info("Enabled.");
     }
     public function onDisable(){
      $this->getLogger()->info("Disabled.");
     }
     public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
      if(strtolower($command->getName()) === "hp"){
            
            if(!isset($args[0]) && $sender instanceof Player){
             $sender->sendMessage("[Doctor] You have [$sender->getHealth()."/". $sender->getMaxHealth()] health points.");
             return true;
            }
          elseif(isset($args[0]) && $args[0] instanceof Player){
             $tarPlayer = $this->getServer()->getPlayer($args[0]);
             $tarName = $tarPlayer->getName();
             $sender->sendMessage("[Doctor] ". $tarName ." currently has [". $this->getServer()->getPlayer($args[0])->getHealth()."/". $this->getServer()->getPlayer($args[0])->getMaxHealth()" ]");
             return true;
            }
       }
      }
     }
    
    I believe works as long as your own commands were called right and you have the plugin.yml set up correctly.
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    PHP:
    <?php

    namespace CheckHealth;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\server\Server;

    class 
    CheckHealth extends PluginBase{
     public function 
    onEnable(){
      
    $this->getLogger()->info("Enabled.");
     }
     public function 
    onDisable(){
      
    $this->getLogger()->info("Disabled.");
     }
     public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){
      switch(
    $cmd->getName()){
       case 
    "hp":
            if(isset(
    $args[0])){
             
    $player $args[0];
             if(
    $player $this->getServer()->getPlayer($args[0]){
            
    $sender->sendMessage("[Doctor] "$player->getName()." currently has ["$player->getHealth()."/".  $player->getMaxHealth() ."]");
            return 
    true;
            }
            }
          else{
             
    $sender->sendMessage("[Doctor] You have [".$sender->getHealth()."/"$sender->getMaxHealth()."] health points.");
             return 
    true;
            }
       }
      }
     }
    Now console says
    Code:
    [Server] 16:23:24 [INFO] Loading source plugin Doctor v1.0.0
    [Server] 
    [Server] Parse error: syntax error, unexpected '{' in /storage/emulated/0/PocketMine/plugins/CheckHealth/src/CheckHealth/CheckHealth.php on line 23
    
    First i did Copy n Paste. Then i tried to rewrite and no good still same error. Help...
    now i will test out @HotFireyDeath.
    HotFireyDeath likes this.
  5. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,434
    Plugins:
    14
    You're missing a closing parenthesis on this line.
    PHP:
    if($player $this->getServer()->getPlayer($args[0]){
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    It's still pointing at line 23 (fixed 25 @LDX)
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    (Maybe not the exact wording)
  8. I_Is_Payton_
    Offline

    I_Is_Payton_ Active Member Plugin Developer

    Joined:
    Jan 18, 2014
    Posts:
    76
    Plugins:
    1
    Minecraft User:
    I_Is_Payton_
    Sorry I'm late! This is an easy fix; just simply use this instead of instanceof Player.

    PHP:
    <?php
    namespace CheckHealth;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\server\Server;

    class 
    CheckHealth extends PluginBase {
    public function 
    onEnable() {
    $this->getLogger()->info("Enabled.");
    }
    public function 
    onDisable() {
    $this->getLogger()->info("Disabled.");
    }
    public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args) {
    switch (
    $cmd->getName()) {
    case 
    "hp":
    if (!isset(
    $args[0]) && $sender !== null) {
    $sender->sendMessage("[Doctor] You have [".$sender->getHealth()."/"$sender->getMaxHealth()."] health points.");
    return 
    true;
    }
    elseif (isset(
    $args[0]) && $args[0] == $this->getServer()->getPlayer($args[0])) {
    $tarPlayer $this->getServer()->getPlayer($args[0]);
    $tarName $tarPlayer->getName();
    $sender->sendMessage("[Doctor] "$tarName ." currently has ["$this->getServer()->getPlayer($args[0])->getHealth()."/"$this->getServer()->getPlayer($args[0])->getMaxHealth() ."]");
    return 
    true;
    }
    }
    }
    }
    If this does not work, (I was busy and didn't have time to read over it) then shoot me an error message :)
    If your problem has been solved, sorry for answering :c
    If I have fixed your problem, I feel really special :D (Shoot me a like and PM me any further questions if you want!)
  9. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    instanceof is a PHP function and it does work, the way your using it is wrong and that's the reason you seem to think that it doesn't work.
  10. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    $args[0] can't be an instance of Player class. It's a string. The correct code should be:
    PHP:
    $player $this->getServer()->getPlayer($args[0]);
    So $player will be a Player class instance or null (if the player is not on the server)
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    This is even more wrong.
  12. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    You can't use instanceof on a string. What you want to check must be an object. :p
    PEMapModder likes this.
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Dude omg :D It was 4 month ago :p
  14. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Still having the error? :3
  15. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    omg No!
  16. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    .. at line 23 you forgot an )
  17. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    OMG, Ik. 3 Month ago i was asking help here more frequently now i can write without looking at docs or php manual.
  18. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    I didn't notice the date. Usually when I'm rushing I just look at your post and if there is something wrong with it I'll tell you, no matter how old it is. :p
    Taha_The_Hacker likes this.
  19. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741

Share This Page

Advertisement