1: <?php
2:
3:
4:
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: 16:
17: abstract class MPMU {
18:
19: static protected $items = [];
20:
21: const VERSION = "1.92.0";
22:
23: 24: 25: 26: 27: 28: 29: 30:
31: static public function version($version = "") {
32: if ($version == "") return self::VERSION;
33: return self::apiCheck(self::VERSION,$version);
34: }
35: 36: 37: 38: 39: 40: 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: 48: 49: 50: 51: 52: 53: 54: 55: 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: 83: 84: 85: 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: 107: 108: 109: 110: 111: 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: 121: 122: 123: 124: 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: 135: 136: 137: 138:
139: static public function iName($player) {
140: if ($player instanceof CommandSender) {
141: $player = strtolower($player->getName());
142: }
143: return $player;
144: }
145: 146: 147: 148: 149: 150: 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: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 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: 198: 199: 200: 201: 202: 203: 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: 232: 233: 234: 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: 246: 247: 248: 249: 250: 251: 252:
253: static public function sendPopup($player,$msg) {
254: $pm = $player->getServer()->getPluginManager();
255: if (($sa = $pm->getPlugin("SimpleAuth")) !== null) {
256:
257: if ($sa->isEnabled() && !$sa->isPlayerAuthenticated($player)) return;
258: }
259: if (($hud = $pm->getPlugin("BasicHUD")) !== null) {
260:
261: $hud->sendPopup($player,$msg);
262: return;
263: }
264: $player->sendPopup($msg);
265: }
266: 267: 268: 269: 270: 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: 279: 280: 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: