1: <?php
2: namespace aliuly\common;
3: use aliuly\common\MPMU;
4: use pocketmine\command\RemoteConsoleCommandSender;
5: use pocketmine\command\ConsoleCommandSender;
6: use pocketmine\command\CommandSender;
7: use pocketmine\event\player\PlayerChatEvent;
8: use pocketmine\Player;
9: use pocketmine\command\PluginCommand;
10:
11: 12: 13:
14: abstract class Cmd {
15: 16: 17: 18: 19: 20: 21:
22: static public function exec($sender,$cmd,$show=true) {
23: if (!is_array($cmd)) $cmd= [ $cmd ];
24: foreach ($cmd as $c) {
25: if($show)$sender->sendMessage("CMD> $c");
26: $sender->getServer()->dispatchCommand($sender,$c);
27: }
28: }
29: 30: 31: 32: 33: 34: 35:
36: static public function system($server, $cmd) {
37: $rcon = new RemoteConsoleCommandSender;
38: $server->distpatchCommand($rcon,$cm);
39: return $rcon->getMessage();
40: }
41: 42: 43: 44: 45: 46:
47: static public function chat($sender,$msgs) {
48: if (!is_array($msgs)) $msgs= [ $msg ];
49: foreach ($msgs as $msg) {
50: $sender->getServer()->getPluginManager()->callEvent($ev = new PlayerChatEvent($sender,$msg));
51: if ($ev->isCancelled()) continue;
52: if (MPMU::apiVersion("1.12.0")) {
53: $s = $sender->getServer();
54: $s->broadcastMessage($s->getLanguage()->translateString(
55: $ev->getFormat(),
56: [$ev->getPlayer()->getDisplayName(), $ev->getMessage()]),
57: $ev->getRecipients());
58: } else {
59: $sender->getServer()->broadcastMessage(sprintf(
60: $ev->getFormat(),
61: $ev->getPlayer()->getDisplayName(),
62: $ev->getMessage()),$ev->getRecipients());
63: }
64: }
65: }
66: 67: 68: 69: 70: 71: 72:
73: static public function console($server,$cmd,$show=false) {
74: if (!is_array($cmd)) $cmd= [ $cmd ];
75: foreach ($cmd as $c) {
76: if ($show) $server->getLogger()->info("CMD> $cmd");
77: $server->dispatchCommand(new ConsoleCommandSender(),$c);
78: }
79: }
80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91:
92: static public function opexec(CommandSender $ctx, $cmdline) {
93: if (($cm = MPMU::startsWith($cmdline,"+op:")) !== null) {
94: if (!$ctx->isOp()) {
95: $ctx->setOp(true);
96: $ctx->getServer()->dispatchCommand($ctx,$cm);
97: $ctx->setOp(false);
98: return;
99: }
100: $ctx->getServer()->dispatchCommand($ctx,$cm);
101: return;
102: }
103: if (($cm = MPMU::startsWith($cmdline,"+console:")) !== null) {
104: $ctx->getServer()->dispatchCommand(new ConsoleCommandSender,$cm);
105: return;
106: }
107: if (($cm = MPMU::startsWith($cmdline,"+rcon:")) !== null) {
108: if ($ctx instanceof Player) {
109: $rcon = new RemoteConsoleCommandSender;
110: $ctx->getServer()->distpatchCommand($rcon,$cm);
111: if (trim($rcon->getMessage()) != "") $ctx->sendMessage($rcon->getMessage());
112: } else {
113: $ctx->getServer()->dispatchCommand($ctx,$cm);
114: }
115: return;
116: }
117: $ctx->getServer()->dispatchCommand($ctx,$cmdline);
118: }
119: 120: 121: 122: 123: 124: 125: 126:
127: static public function addCommand($plugin, $executor, $cmd, $yaml) {
128: $newCmd = new PluginCommand($cmd,$plugin);
129: if (isset($yaml["description"]))
130: $newCmd->setDescription($yaml["description"]);
131: if (isset($yaml["usage"]))
132: $newCmd->setUsage($yaml["usage"]);
133: if(isset($yaml["aliases"]) and is_array($yaml["aliases"])) {
134: $aliasList = [];
135: foreach($yaml["aliases"] as $alias) {
136: if(strpos($alias,":")!== false) {
137: $this->owner->getLogger()->info("Unable to load alias $alias");
138: continue;
139: }
140: $aliasList[] = $alias;
141: }
142: $newCmd->setAliases($aliasList);
143: }
144: if(isset($yaml["permission"]))
145: $newCmd->setPermission($yaml["permission"]);
146: if(isset($yaml["permission-message"]))
147: $newCmd->setPermissionMessage($yaml["permission-message"]);
148: $newCmd->setExecutor($executor);
149: $cmdMap = $plugin->getServer()->getCommandMap();
150: $cmdMap->register($plugin->getDescription()->getName(),$newCmd);
151: }
152: 153: 154: 155: 156:
157: static public function rmCommand($srv, $cmd) {
158: $cmdMap = $srv->getCommandMap();
159: $oldCmd = $cmdMap->getCommand($cmd);
160: if ($oldCmd === null) return false;
161: $oldCmd->setLabel($cmd."_disabled");
162: $oldCmd->unregister($cmdMap);
163: return true;
164: }
165: }
166: