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

Command Layout Help

Comments in 'Plugin Development' started by Tethered_, Jun 19, 2015.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I need some help with making FactionsPro's code a little more readable. I'd like to be able subdivide my folders so I have a cmd/ directory with create.php delete.php etc. I've looked at the code for PE Essentials, but my case is a little bit different because every command is run as /f with arguments after. Can anyone help me?
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    I am trying to figure this out too. Sounds hard though...
  3. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Yeah not even sure if I explained it right
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Try looking at the SubcommandMap utility in WorldEditArt.
  5. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    So you want a class/file just for the commands? If yes, you should take a look here: http://github.com/Gamecrafter/PocketMine-Plugins Most of my plugins have commands, and all of them use sub-commands. Looking at it might help.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes, but this thing is still messy. He is asking how to separate this messy class into different classes.

    Look at the old version of PocketFactions instead. It is a factions plugin that never worked because I never debugged it, but its general framework is OK.

    https://github.com/LegendOfMCPE/PocketFactions/tree/master/src/pocketfactions/utils/subcommand it contains two important classes, one is SubcommandMap, which extends Command and implements PluginIdentifyableCommand, but itself managing subcommands in the model like CommandMap. Subcommand has the model like the PocketMine Command class, but it doesn't extend the Command class but instead allows subcommands to extend it like to commands extend the Command class.

    This sounds a little messy, but this is how it would work. So basically, first make a class for your FactionCommand. In this FaftionCommand class, store a list of subcommands in a private array field, then populate the array with instances of each of your subcommands.

    Basic example:
    PHP:
    interface Subcommand{
      public function 
    getName();
      public function 
    getDescription();
      public function 
    getUsage();
      public function 
    hasPermission(CommandSender $sender0;
      public function 
    execute(array $argsCommandSender $sender);
    }
    class 
    SubcommandMap extends Command implements PluginIdentifyableCommand{
      private 
    $main;
      
    /** @var Subcommand[] */
      
    private $subcmds = [];
      public function 
    __construct(MainClass $main){
        
    $this->main=$main;
        
    $this->registerSubcommand(new CreateSubcommand($main));
        
    $this->registerSubcommand(new DisbandSubcommand($main));
        
    // too lazy to add parent::__construct()
      
    }
      public function 
    registerSubcommand(Subcommand $subcmd){
        
    $this->subcmds[$subcmd->getName()] = $subcmd;
      }
      
    // implement PocketMine API methods for Command and PluginIdentifyableCommand
      
    public function execute(CommandSender $sender$lbl, array $args){
        
    $subcmd array_shift($args);
        if(
    $subcmd === "help" or $subcmd === null){
          foreach(
    $this->subcmds as $sub){
            
    $sender->sendMessage("/f {$sub->getName()}{$sub->getDescription()} {$sub->getUsage()}");
          } 
    // sending them all in one will spam the user's screen, so improve your display in your plugin's implementation
        
    }elseif(isset($this->subcmds[$subcmd->getName()])){
          
    $this->subcmds[$subcmd->getName()]->execute($args$sender);
        }else{
          
    $sender->sendMessage("Unknown Command. Do /f help to display a list of available commands ");
      }
    }
     
    // I didn't add getPlugin. Implement it yourself.
    class CreateSubcommand implements Subcommand{
      
    // implement the methods
      // tip: click the class name in PHPStorm, click Alt+Enter and choose "Create method stubs"
    }
    class 
    DisbandSubcommand implements Subcommand{
      
    // blah blah blah
      // I usually use PHPStorm auto completion to do this :P
    }
    You should understand the code above easily if you understand about object-oriented programming.
    Tethered_ and iJoshuaHD like this.

Share This Page

Advertisement