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: //: - API version checking
  4: //: - Misc shorcuts and pre-canned routines
  5: 
  6: namespace aliuly\common;
  7: use pocketmine\item\Item;
  8: use pocketmine\utils\TextFormat;
  9: use pocketmine\utils\MainLogger;
 10: use pocketmine\command\CommandSender;
 11: use pocketmine\Player;
 12: use aliuly\common\mc;
 13: 
 14: /**
 15:  * My PocketMine Utils class
 16:  */
 17: abstract class MPMU {
 18:     /** @var str[] $items Nice names for items */
 19:     static protected $items = [];
 20:     /** @const str VERSION plugin version string */
 21:     const VERSION = "1.92.0";
 22: 
 23:     /**
 24:      * libcommon library version.  If a version is provided it will check
 25:      * the version using apiCheck.
 26:      *
 27:      * @param str version Version to check
 28:      *
 29:      * @return str|bool
 30:      */
 31:     static public function version($version = "") {
 32:         if ($version == "") return self::VERSION;
 33:         return self::apiCheck(self::VERSION,$version);
 34:     }
 35:     /**
 36:      * Used to check the PocketMine API version
 37:      *
 38:      * @param str version Version to check
 39:      *
 40:      * @return str|bool
 41:      */
 42:     static public function apiVersion($version = "") {
 43:         if ($version == "") return \pocketmine\API_VERSION;
 44:         return self::apiCheck(\pocketmine\API_VERSION,$version);
 45:     }
 46:     /**
 47:      * Checks API compatibility from $api against $version.  $version is a
 48:      * string containing the version.  It can contain the following operators:
 49:      *
 50:      * >=, <=, <> or !=, =, !|~, <, >
 51:      *
 52:      * @param str api Installed API version
 53:      * @param str version API version to compare against
 54:      *
 55:      * @return bool
 56:      */
 57:     static public function apiCheck($api,$version) {
 58:         switch (substr($version,0,2)) {
 59:             case ">=":
 60:                 return version_compare($api,trim(substr($version,2))) >= 0;
 61:             case "<=":
 62:                 return version_compare($api,trim(substr($version,2))) <= 0;
 63:             case "<>":
 64:             case "!=":
 65:                 return version_compare($api,trim(substr($version,2))) != 0;
 66:         }
 67:         switch (substr($version,0,1)) {
 68:             case "=":
 69:                 return version_compare($api,trim(substr($version,1))) == 0;
 70:             case "!":
 71:             case "~":
 72:                 return version_compare($api,trim(substr($version,1))) != 0;
 73:             case "<":
 74:                 return version_compare($api,trim(substr($version,1))) < 0;
 75:             case ">":
 76:                 return version_compare($api,trim(substr($version,1))) > 0;
 77:         }
 78:         if (intval($api) != intval($version)) return 0;
 79:         return version_compare($api,$version) >= 0;
 80:     }
 81:     /**
 82:      * Returns a localized string for the gamemode
 83:      *
 84:      * @param int mode
 85:      * @return str
 86:      */
 87:     static public function gamemodeStr($mode) {
 88:         if (class_exists(__NAMESPACE__."\\mc",false)) {
 89:             switch ($mode) {
 90:                 case 0: return mc::_("Survival");
 91:                 case 1: return mc::_("Creative");
 92:                 case 2: return mc::_("Adventure");
 93:                 case 3: return mc::_("Spectator");
 94:             }
 95:             return mc::_("%1%-mode",$mode);
 96:         }
 97:         switch ($mode) {
 98:             case 0: return "Survival";
 99:             case 1: return "Creative";
100:             case 2: return "Adventure";
101:             case 3: return "Spectator";
102:         }
103:         return "$mode-mode";
104:     }
105:     /**
106:      * Check's player or sender's permissions and shows a message if appropriate
107:      *
108:      * @param CommandSender $sender
109:      * @param str $permission
110:      * @param bool $msg If false, no message is shown
111:      * @return bool
112:      */
113:     static public function access(CommandSender $sender, $permission,$msg=true) {
114:         if($sender->hasPermission($permission)) return true;
115:         if ($msg)
116:             $sender->sendMessage(mc::_("You do not have permission to do that."));
117:         return false;
118:     }
119:     /**
120:      * Check's if $sender is a player in game
121:      *
122:      * @param CommandSender $sender
123:      * @param bool $msg If false, no message is shown
124:      * @return bool
125:      */
126:     static public function inGame(CommandSender $sender,$msg = true) {
127:         if (!($sender instanceof Player)) {
128:             if ($msg) $sender->sendMessage(mc::_("You can only do this in-game"));
129:             return false;
130:         }
131:         return true;
132:     }
133:     /**
134:      * Takes a player and creates a string suitable for indexing
135:      *
136:      * @param Player|str $player - Player to index
137:      * @return str
138:      */
139:     static public function iName($player) {
140:         if ($player instanceof CommandSender) {
141:             $player = strtolower($player->getName());
142:         }
143:         return $player;
144:     }
145:     /**
146:      * Lile file_get_contents but for a Plugin resource
147:      *
148:      * @param Plugin $plugin
149:      * @param str $filename
150:      * @return str|null
151:      */
152:     static public function getResourceContents($plugin,$filename) {
153:         $fp = $plugin->getResource($filename);
154:         if($fp === null){
155:             return null;
156:         }
157:         $contents = stream_get_contents($fp);
158:         fclose($fp);
159:         return $contents;
160:     }
161:     /**
162:      * Call a plugin's function.
163:      *
164:      * If the $plug parameter is given a string, it will simply look for that
165:      * plugin.  If an array is provided, it is assumed to be of the form:
166:      *
167:      *   [ "plugin", "version" ]
168:      *
169:      * So then it will check that the plugin exists, and the version number
170:      * matches according to the rules from **apiCheck**.
171:      *
172:      * Also, if plugin contains an **api** property, it will use that as
173:      * the class for method calling instead.
174:      *
175:      * @param Server $server - pocketmine server instance
176:      * @param str|array $plug - plugin to call
177:      * @param str $method - method to call
178:      * @param mixed $default - If the plugin does not exist or it is not enable, this value is returned
179:      * @return mixed
180:      */
181:     static public function callPlugin($server,$plug,$method,$args,$default = null) {
182:         $v = null;
183:         if (is_array($plug)) list($plug,$v) = $plug;
184:         if (($plugin = $server->getPluginManager()->getPlugin($plug)) === null
185:              || !$plugin->isEnabled()) return $default;
186: 
187:         if ($v !== null && !self::apiCheck($plugin->getDescription()->getVersion(),$v)) return $default;
188:         if (property_exists($plugin,"api")) {
189:             $fn = [ $plugin->api , $method ];
190:         } else {
191:             $fn = [ $plugin, $method ];
192:         }
193:         if (!is_callable($fn)) return $default;
194:         return $fn(...$args);
195:     }
196:     /**
197:      * Register a command
198:      *
199:      * @param Plugin $plugin - plugin that "owns" the command
200:      * @param CommandExecutor $executor - object that will be called onCommand
201:      * @param str $cmd - Command name
202:      * @param array $yaml - Additional settings for this command.
203:      * @deprecated Moved to Cmd class
204:      */
205:     static public function addCommand($plugin, $executor, $cmd, $yaml) {
206:         $newCmd = new \pocketmine\command\PluginCommand($cmd,$plugin);
207:         if (isset($yaml["description"]))
208:             $newCmd->setDescription($yaml["description"]);
209:         if (isset($yaml["usage"]))
210:             $newCmd->setUsage($yaml["usage"]);
211:         if(isset($yaml["aliases"]) and is_array($yaml["aliases"])) {
212:             $aliasList = [];
213:             foreach($yaml["aliases"] as $alias) {
214:                 if(strpos($alias,":")!== false) {
215:                     $this->owner->getLogger()->info("Unable to load alias $alias");
216:                     continue;
217:                 }
218:                 $aliasList[] = $alias;
219:             }
220:             $newCmd->setAliases($aliasList);
221:         }
222:         if(isset($yaml["permission"]))
223:             $newCmd->setPermission($yaml["permission"]);
224:         if(isset($yaml["permission-message"]))
225:             $newCmd->setPermissionMessage($yaml["permission-message"]);
226:         $newCmd->setExecutor($executor);
227:         $cmdMap = $plugin->getServer()->getCommandMap();
228:         $cmdMap->register($plugin->getDescription()->getName(),$newCmd);
229:     }
230:     /**
231:      * Unregisters a command
232:      * @param Server|Plugin $obj - Access path to server instance
233:      * @param str $cmd - Command name to remove
234:      * @deprecated Moved to Cmd class
235:      */
236:     static public function rmCommand($srv, $cmd) {
237:         $cmdMap = $srv->getCommandMap();
238:         $oldCmd = $cmdMap->getCommand($cmd);
239:         if ($oldCmd === null) return false;
240:         $oldCmd->setLabel($cmd."_disabled");
241:         $oldCmd->unregister($cmdMap);
242:         return true;
243:     }
244:     /**
245:      * Send a PopUp, but takes care of checking if there are some
246:      * plugins that might cause issues.
247:      *
248:      * Currently only supports SimpleAuth and BasicHUD.
249:      *
250:      * @param Player $player
251:      * @param str $msg
252:      */
253:     static public function sendPopup($player,$msg) {
254:         $pm = $player->getServer()->getPluginManager();
255:         if (($sa = $pm->getPlugin("SimpleAuth")) !== null) {
256:             // SimpleAuth also has a HUD when not logged in...
257:             if ($sa->isEnabled() && !$sa->isPlayerAuthenticated($player)) return;
258:         }
259:         if (($hud = $pm->getPlugin("BasicHUD")) !== null) {
260:             // Send pop-ups through BasicHUD
261:             $hud->sendPopup($player,$msg);
262:             return;
263:         }
264:         $player->sendPopup($msg);
265:     }
266:     /**
267:      * Check prefixes
268:      * @param str $txt - input text
269:      * @param str $tok - keyword to test
270:      * @return str|null
271:      */
272:     static public function startsWith($txt,$tok) {
273:         $ln = strlen($tok);
274:         if (strtolower(substr($txt,0,$ln)) != $tok) return null;
275:         return trim(substr($txt,$ln));
276:     }
277:     /**
278:      * Look-up player
279:      * @param CommandSender $req
280:      * @param str $n
281:      */
282:     static public function getPlayer(CommandSender $c,$n) {
283:         $pl = $c->getServer()->getPlayer($n);
284:         if ($pl === null) $c->sendMessage(mc::_("%1% not found", $n));
285:         return $pl;
286:     }
287: 
288: }
289: 
API documentation generated by ApiGen