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: 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:  * Utility class to execute commands|chat's as player or console
 13:  */
 14: abstract class Cmd {
 15:     /**
 16:      * Execute a command as a given player
 17:      *
 18:      * @param Player|CommandSender $sender - Entity to impersonate
 19:      * @param str[]|str $cmd - commands to execute
 20:      * @param bool $show - show commands being executed
 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:      * Execute a command capturing output
 31:      *
 32:      * @param Server $server
 33:      * @param str $cmd - command to execute
 34:      * @return str
 35:      */
 36:     static public function system($server, $cmd) {
 37:         $rcon = new RemoteConsoleCommandSender;
 38:         $server->distpatchCommand($rcon,$cm);
 39:         return $rcon->getMessage();
 40:     }
 41:     /**
 42:      * Chat a message as a given player
 43:      *
 44:      * @param Player|CommandSender $sender - Entity to impersonate
 45:      * @param str[]|str $msg - messages to send
 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:      * Execute commands as console
 68:      *
 69:      * @param Server $server - pocketmine\Server instance
 70:      * @param str[]|str $cmd - commands to execute
 71:      * @param bool $show - show commands being executed
 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:      * Handles command prefixes before dispatching commands.
 82:      *
 83:      * The following prefixes are recognized:
 84:      * - "+op:", temporarily gives the player Op (if the player is not Op yet)
 85:      * - "+console:", runs the command as if it was run from the console.
 86:      * - "+rcon:", runs the command as if it was run from a RemoteConsole,
 87:      *   capturing all output which is then send to the player.
 88:      *
 89:      * @param CommandSender $ctx - running context
 90:      * @param str $cmdline - command line to execute
 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:      * Register a command
121:      *
122:      * @param Plugin $plugin - plugin that "owns" the command
123:      * @param CommandExecutor $executor - object that will be called onCommand
124:      * @param str $cmd - Command name
125:      * @param array $yaml - Additional settings for this command.
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:      * Unregisters a command
154:      * @param Server|Plugin $obj - Access path to server instance
155:      * @param str $cmd - Command name to remove
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: 
API documentation generated by ApiGen