Overview

Namespaces

  • aliuly
    • common
      • selectors
    • loader
  • xPaw

Classes

  • aliuly\common\ArmorItems
  • aliuly\common\BasicCli
  • aliuly\common\BasicHelp
  • aliuly\common\BasicPlugin
  • aliuly\common\ChatSession
  • aliuly\common\Cmd
  • aliuly\common\CmdSelector
  • aliuly\common\ExpandVars
  • aliuly\common\FastTransfer
  • aliuly\common\FileUtils
  • aliuly\common\FreezeSession
  • aliuly\common\GetMotd
  • aliuly\common\GetMotdAsyncTask
  • aliuly\common\InvisibleSession
  • aliuly\common\InvUtils
  • aliuly\common\ItemName
  • aliuly\common\mc
  • aliuly\common\mc2
  • aliuly\common\MoneyAPI
  • aliuly\common\MPMU
  • aliuly\common\Npc
  • aliuly\common\PermUtils
  • aliuly\common\PluginAsyncTask
  • aliuly\common\PluginCallbackTask
  • aliuly\common\PMScript
  • aliuly\common\QueryAsyncTask
  • aliuly\common\Rcon
  • aliuly\common\RconTask
  • aliuly\common\selectors\All
  • aliuly\common\selectors\AllEntity
  • aliuly\common\selectors\BaseSelector
  • aliuly\common\selectors\Random
  • aliuly\common\Session
  • aliuly\common\ShieldSession
  • aliuly\common\ShoppingCart
  • aliuly\common\SignUtils
  • aliuly\common\SkinUtils
  • aliuly\common\SpySession
  • aliuly\common\SubCommandMap
  • aliuly\common\TPUtils
  • aliuly\loader\Main
  • xPaw\MinecraftQuery

Exceptions

  • xPaw\MinecraftQueryException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: //= api-features
  3: //: - Paginated output
  4: //: - Command and sub command dispatchers
  5: 
  6: namespace aliuly\common;
  7: 
  8: use pocketmine\command\ConsoleCommandSender;
  9: use pocketmine\command\CommandSender;
 10: use pocketmine\command\Command;
 11: use pocketmine\command\PluginCommand;
 12: use pocketmine\Player;
 13: 
 14: use pocketmine\utils\TextFormat;
 15: 
 16: /**
 17:  * Implements Basic CLI common functionality.  It is useful for plugins
 18:  * that implement multiple commands or sub-commands
 19:  */
 20: abstract class BasicCli {
 21:     protected $owner;
 22:     /**
 23:      * @param BasicPlugin @owner - Plugin that owns this module
 24:      */
 25:     public function __construct($owner) {
 26:         $this->owner = $owner;
 27:     }
 28:   /**
 29:      * Register this class as a sub-command.  See BasicPlugin for details.
 30:      *
 31:      * @param str $cmd - sub-command to register
 32:      * @param mixed[] $opts - additional options for registering sub-command
 33:      */
 34:     public function enableSCmd($cmd,$opts) {
 35:         $this->owner->registerScmd($cmd,[$this,"onSCommand"],$opts);
 36:     }
 37:     /**
 38:      * Register this class as a command.
 39:      *
 40:      * @param str $cmd - command to register
 41:      * @param mixed[] $yaml - options for command
 42:      */
 43:     public function enableCmd($cmd,$yaml) {
 44:         $newCmd = new PluginCommand($cmd,$this->owner);
 45:         if (isset($yaml["description"]))
 46:             $newCmd->setDescription($yaml["description"]);
 47:         if (isset($yaml["usage"]))
 48:             $newCmd->setUsage($yaml["usage"]);
 49:         if(isset($yaml["aliases"]) and is_array($yaml["aliases"])) {
 50:             $aliasList = [];
 51:             foreach($yaml["aliases"] as $alias) {
 52:                 if(strpos($alias,":")!== false) {
 53:                     $this->owner->getLogger()->info("Unable to load alias $alias");
 54:                     continue;
 55:                 }
 56:                 $aliasList[] = $alias;
 57:             }
 58:             $newCmd->setAliases($aliasList);
 59:         }
 60:         if(isset($yaml["permission"]))
 61:             $newCmd->setPermission($yaml["permission"]);
 62:         if(isset($yaml["permission-message"]))
 63:             $newCmd->setPermissionMessage($yaml["permission-message"]);
 64:         $newCmd->setExecutor($this);
 65:         $cmdMap = $this->owner->getServer()->getCommandMap();
 66:         $cmdMap->register($this->owner->getDescription()->getName(),$newCmd);
 67:     }
 68:     /**
 69:      * Use for paginaged output implementation.
 70:      * This gets the player specified page number that we want to Display
 71:      *
 72:      * @param str[] $args - Passed arguments
 73:      * @return int page number
 74:      */
 75:     protected function getPageNumber(array &$args) {
 76:         $pageNumber = 1;
 77:         if (count($args) && is_numeric($args[count($args)-1])) {
 78:             $pageNumber = (int)array_pop($args);
 79:             if($pageNumber <= 0) $pageNumber = 1;
 80:         }
 81:         return $pageNumber;
 82:     }
 83:     /**
 84:      * Use for paginaged output implementation.
 85:      * Shows a bunch of line in paginated output.
 86:      *
 87:      * @param CommandSender $sender - entity that we need to display text to
 88:      * @param int $pageNumber - page that we need to display
 89:      * @param str[] $txt - Array containing one element per output line
 90:      * @return bool true
 91:      */
 92:     protected function paginateText(CommandSender $sender,$pageNumber,array $txt) {
 93:         $hdr = array_shift($txt);
 94:         if($sender instanceof ConsoleCommandSender){
 95:             $sender->sendMessage( TextFormat::GREEN.$hdr.TextFormat::RESET);
 96:             foreach ($txt as $ln) $sender->sendMessage($ln);
 97:             return true;
 98:         }
 99:         $pageHeight = 5;
100:         $lineCount = count($txt);
101:         $pageCount = intval($lineCount/$pageHeight) + ($lineCount % $pageHeight ? 1 : 0);
102:         $hdr = TextFormat::GREEN.$hdr. TextFormat::RESET;
103:         if ($pageNumber > $pageCount) {
104:             $sender->sendMessage($hdr);
105:             $sender->sendMessage("Only $pageCount pages available");
106:             return true;
107:         }
108:         $hdr .= TextFormat::RED." ($pageNumber of $pageCount)";
109:         $sender->sendMessage($hdr);
110:         for ($ln = ($pageNumber-1)*$pageHeight;$ln < $lineCount && $pageHeight--;++$ln) {
111:             $sender->sendMessage($txt[$ln]);
112:         }
113:         return true;
114:     }
115:     /**
116:      * Use for paginaged output implementation.
117:      * Formats and paginates a table
118:      *
119:      * @param CommandSender $sender - entity that we need to display text to
120:      * @param int $pageNumber - page that we need to display
121:      * @param str[][] $txt - Array containing one element per cell
122:      * @return bool true
123:      */
124:     protected function paginateTable(CommandSender $sender,$pageNumber,array $tab) {
125:         $cols = [];
126:         for($i=0;$i < count($tab[0]);$i++) $cols[$i] = strlen($tab[0][$i]);
127:         foreach ($tab as $row) {
128:             for($i=0;$i < count($row);$i++) {
129:                 if (($l=strlen($row[$i])) > $cols[$i]) $cols[$i] = $l;
130:             }
131:         }
132:         $txt = [];
133:         $fmt = "";
134:         foreach ($cols as $c) {
135:             if (strlen($fmt) > 0) $fmt .= " ";
136:             $fmt .= "%-".$c."s";
137:         }
138:         foreach ($tab as $row) {
139:             $txt[] = sprintf($fmt,...$row);
140:         }
141:         return $this->paginateText($sender,$pageNumber,$txt);
142:     }
143: 
144:     //////////////////////////////////////////////////////////////////////
145:     /**
146:    * Entry point for BasicPlugin state functionality.  This makes it module
147:      * specific.
148:      * Retrieves the state.
149:      *
150:      * @param CommandSender $player - entity that we need state from
151:      * @param mixed $default - Default value to return if no state found
152:      * @return mixed $state
153:      */
154:     public function getState(CommandSender $player,$default) {
155:         //echo __METHOD__.",".__LINE__." - ".get_class($this)."\n";//##DEBUG
156:         return $this->owner->getState(get_class($this),$player,$default);
157:     }
158:     /**
159:    * Entry point for BasicPlugin state functionality.  This makes it module
160:      * specific.
161:      * Sets the state.
162:      *
163:      * @param CommandSender $player - entity that we need to set state
164:      * @param mixed $val - Value to use for the state
165:      */
166:     public function setState(CommandSender $player,$val) {
167:         //echo __METHOD__.",".__LINE__." - ".get_class($this)."\n";//##DEBUG
168:         $this->owner->setState(get_class($this),$player,$val);
169:     }
170:     /**
171:    * Entry point for BasicPlugin state functionality.  This makes it module
172:      * specific.
173:      * UnSets the state.
174:      *
175:      * @param CommandSender $player - entity that we need to unset state
176:      */
177:     public function unsetState(CommandSender $player) {
178:         //echo __METHOD__.",".__LINE__." - ".get_class($this)."\n";//##DEBUG
179:         $this->owner->unsetState(get_class($this),$player);
180:     }
181: }
182: 
API documentation generated by ApiGen