1: <?php
2:
3:
4:
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: 18: 19:
20: abstract class BasicCli {
21: protected $owner;
22: 23: 24:
25: public function __construct($owner) {
26: $this->owner = $owner;
27: }
28: 29: 30: 31: 32: 33:
34: public function enableSCmd($cmd,$opts) {
35: $this->owner->registerScmd($cmd,[$this,"onSCommand"],$opts);
36: }
37: 38: 39: 40: 41: 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: 70: 71: 72: 73: 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: 85: 86: 87: 88: 89: 90: 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: 117: 118: 119: 120: 121: 122: 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: 147: 148: 149: 150: 151: 152: 153:
154: public function getState(CommandSender $player,$default) {
155:
156: return $this->owner->getState(get_class($this),$player,$default);
157: }
158: 159: 160: 161: 162: 163: 164: 165:
166: public function setState(CommandSender $player,$val) {
167:
168: $this->owner->setState(get_class($this),$player,$val);
169: }
170: 171: 172: 173: 174: 175: 176:
177: public function unsetState(CommandSender $player) {
178:
179: $this->owner->unsetState(get_class($this),$player);
180: }
181: }
182: