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

Is my code correct?

Comments in 'Plugin Development' started by CoolGuyPL, Sep 28, 2014.

  1. CoolGuyPL
    Offline

    CoolGuyPL New Member

    Joined:
    Sep 24, 2014
    Posts:
    43
    Minecraft User:
    CoolGuyPlays
    Im trying to create a plugin which tells the ip of a player when prompted. is my code correct?
    PHP:
    <?php
    /**
    * Created by PhpStorm.
    * User: awesomec
    * Date: 9/28/2014
    * Time: 3:53 PM
    */
    namespace coolguy\ipchecker;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\player\Player;
    use 
    pocketmine\IPlayer;
    use 
    pocketmine\event\Event;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;

    class 
    ipchecker extends PluginBase{
       
    public function 
    onEnable(){   
           
    $this->getLogger()->info('Enabling IPchecker....');
           return 
    true;
        }
    public function 
    onDisable(){
        
    $this->getLogger()->info("Disabling IPchecker....");
    }

    public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
        if(
    strtolower($command->getName("ipcheck"))) {
            switch(
    $command->getName()):
                case 
    "ipcheck":
                   if(
    $sender instanceof Player){
                       
    $player->getServer()->getName()->getAddress;
                               break;
                   }
                  
           endswitch; 
                       
        }
           
        }
    }
    LDX likes this.
  2. CoolGuyPL
    Offline

    CoolGuyPL New Member

    Joined:
    Sep 24, 2014
    Posts:
    43
    Minecraft User:
    CoolGuyPlays
    Well i tried it on my server and it does not work as i thought it would. it keeps sending me the usage. please correct my code.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    getAddress() is a function. Functions should end with () (or with parameters inside).

    If you test your plugin, you will find that you are just duplicating the enabling message in onEnable().

    The if() check is useless. You are already checking it in the switch block. Why use that if() check? Also, look at Command.php which tells you what parameters getName() accepts and what it does.

    And why did you `use pocketmine\IPlayer;`?

    And it is pocketmine\Player not pocketmine\player\Player. Look at the source.
  4. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
    PHP:
    <?php
    /**
    * Created by PhpStorm.
    * User: awesomec
    * Date: 9/28/2014
    * Time: 3:53 PM
    */
    namespace coolguy\ipchecker;

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

    class 
    ipchecker extends PluginBase{

    public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
        switch(
    strtolower($command->getName())) {
            case 
    "ipcheck":
               if(
    $sender instanceof Player){
                   
    $sender->sendMessage($sender->getAddress());
                   break;
               }
           }
        }
    }
    CoolGuyPL and PluginMastered like this.
  5. CoolGuyPL
    Offline

    CoolGuyPL New Member

    Joined:
    Sep 24, 2014
    Posts:
    43
    Minecraft User:
    CoolGuyPlays
    thank you! uh how can i make it get the ip of the other player?
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Call the getAddress() function on the Player object you get by $server->getPlayer($name) instead.
  7. CoolGuyPL
    Offline

    CoolGuyPL New Member

    Joined:
    Sep 24, 2014
    Posts:
    43
    Minecraft User:
    CoolGuyPlays
    i dont understand.. when i write it, pocketmine crashes because i called the getAddress function on null.
  8. CoolGuyPL
    Offline

    CoolGuyPL New Member

    Joined:
    Sep 24, 2014
    Posts:
    43
    Minecraft User:
    CoolGuyPlays
    and i blame herobrine for it :0 :)
    LDX likes this.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I hope you didn't copy what I gave you exactly... Learn PHP.
    My signature is about the old pthreads bug, and you aren't even doing anything that could be related to it.

Share This Page

Advertisement