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: 
  4: use pocketmine\plugin\Plugin;
  5: use pocketmine\Player;
  6: 
  7: use aliuly\common\ItemName;
  8: use aliuly\common\MPMU;
  9: use pocketmine\utils\TextFormat;
 10: use pocketmine\utils\Utils;
 11: use pocketmine\item\Item;
 12: 
 13: /**
 14:  * Common variable expansion.  You can use this for custom messages and/or
 15:  * custom commands.
 16:  *
 17:  * Plugins can extend this infrastructure by declaring the following functions:
 18:  *
 19:  * public function getSysVarsV1(array &$vars);
 20:  *
 21:  * public function getPlayerVarsV1(Player $player, array &$vars);
 22:  *
 23:  * Otherwise they can call the functions: registerSysVars or registerPlayerVars.
 24:  *
 25:  */
 26: class ExpandVars {
 27:   /** @const str getSysVarsFn This is the function signature for SysVars */
 28:   const getSysVarsFn = "getSysVarsV1";
 29:   /** @const str getPlayerVarsFn This is the function signature for PlayerVars */
 30:   const getPlayerVarsFn = "getPlayerVarsV1";
 31: 
 32:   /** @var callable[] Callables to create player specific variables */
 33:   protected $playerExtensions;
 34:   /** @var callable[] Callables to create server wide variables */
 35:   protected $sysExtensions;
 36:   /** @var array API table */
 37:   protected $apitable;
 38:   /** @var str[] static _constants_ */
 39:   protected $consts;
 40:   /** @var Server pocketmine server context */
 41:   protected $owner;
 42:   /**
 43:    * @param Server $server - server context
 44:    */
 45:   public function __construct(Plugin $owner) {
 46:     $this->owner = $owner;
 47:     $this->playerExtensions = null;
 48:     $this->sysExtensions = null;
 49:     $this->apitable = [];
 50:     $this->consts = [
 51:       "{NL}" => "\n",
 52:       "{BLACK}" => TextFormat::BLACK,
 53:       "{DARK_BLUE}" => TextFormat::DARK_BLUE,
 54:       "{DARK_GREEN}" => TextFormat::DARK_GREEN,
 55:       "{DARK_AQUA}" => TextFormat::DARK_AQUA,
 56:       "{DARK_RED}" => TextFormat::DARK_RED,
 57:       "{DARK_PURPLE}" => TextFormat::DARK_PURPLE,
 58:       "{GOLD}" => TextFormat::GOLD,
 59:       "{GRAY}" => TextFormat::GRAY,
 60:       "{DARK_GRAY}" => TextFormat::DARK_GRAY,
 61:       "{BLUE}" => TextFormat::BLUE,
 62:       "{GREEN}" => TextFormat::GREEN,
 63:       "{AQUA}" => TextFormat::AQUA,
 64:       "{RED}" => TextFormat::RED,
 65:       "{LIGHT_PURPLE}" => TextFormat::LIGHT_PURPLE,
 66:       "{YELLOW}" => TextFormat::YELLOW,
 67:       "{WHITE}" => TextFormat::WHITE,
 68:       "{OBFUSCATED}" => TextFormat::OBFUSCATED,
 69:       "{BOLD}" => TextFormat::BOLD,
 70:       "{STRIKETHROUGH}" => TextFormat::STRIKETHROUGH,
 71:       "{UNDERLINE}" => TextFormat::UNDERLINE,
 72:       "{ITALIC}" => TextFormat::ITALIC,
 73:       "{RESET}" => TextFormat::RESET,
 74:       "{10SPACE}" => str_repeat(" ",10),
 75:       "{20SPACE}" => str_repeat(" ",20),
 76:       "{30SPACE}" => str_repeat(" ",30),
 77:       "{40SPACE}" => str_repeat(" ",40),
 78:       "{50SPACE}" => str_repeat(" ",50),
 79:       "{MOTD}" => $owner->getServer()->getMotd(),
 80:     ];
 81:   }
 82:   /**
 83:    * If GrabBag is available, try to get a single shared instance of
 84:    * ExpandVars
 85:    */
 86:   static public function getCommonVars(Plugin $owner) {
 87:     $pm = $owner->getServer()->getPluginManager();
 88:     if (($gb = $pm->getPlugin("GrabBag")) !== null) {
 89:       if ($gb->isEnabled() && MPMU::apiCheck($gb->getDescription()->getVersion(),"2.3")) {
 90:         $vars =  $gb->api->getVars();
 91:         if ($vars instanceof ExpandVars) return $vars;
 92:       }
 93:     }
 94:     return new ExpandVars($owner);
 95:   }
 96: 
 97: 
 98:   /**
 99:    * Define additional constants on the fly...
100:    * @param str $name
101:    * @param str $value
102:    */
103:   public function define($str,$value) {
104:     $this->consts[$str] = $value;
105:   }
106:   public function getServer() {
107:     return $this->owner->getServer();
108:   }
109:   public function getConsts() {
110:     return $this->consts;
111:   }
112:   /**
113:    * Register API
114:    * @param str $apiname - API id
115:    * @param mixed $ptr - API object
116:    */
117:   public function registerApi($apiname,$ptr) {
118:     $this->apitable[$apiname] = $ptr;
119:   }
120:   /**
121:    * Return API entry, if not found it will throw a RuntimeException
122:    * @param str $apiname - API id
123:    * @param bool $exception - if true raise exemption on error
124:    * @return mixed
125:    */
126:   public function api($apiname,$exception = true) {
127:     if (isset($this->apitable[$apiname])) return $this->apitable[$apiname];
128:     throw new \RuntimeException("Missing API ".$apiname);
129:   }
130:   /**
131:    * Scan loaded plugins and identifies which plugins have an entry
132:    * point to variable expansions...
133:    */
134:    protected function autoloadExtensions($mode) {
135:     $tab = [];
136:     foreach ($this->getServer()->getPluginManager()->getPlugins() as $plug) {
137:       if (!$plug->isEnabled()) continue;
138:       $cb = [ $plug, $mode ];
139:       if (is_callable($cb)) $tab[] = $cb;
140:     }
141:     return $tab;
142:   }
143: 
144:   ///////////////////////////////////////////////////////////////////////////
145:   // System variables
146:   ///////////////////////////////////////////////////////////////////////////
147: 
148:   /**
149:    * Used to initialize the system wide variables table
150:    */
151:    protected function initSysVars() {
152:     if ($this->sysExtensions !== null) return;
153:     $this->sysExtensions = $this->autoloadExtensions(self::getSysVarsFn);
154:     $this->sysExtensions[] =  [ $this, "stdSysVars" ];
155:     if (\pocketmine\DEBUG > 1)  $this->sysExtensions[] = [ $this, "debugSysVars"];
156:     $pm = $this->getServer()->getPluginManager();
157:     if (($kr = $pm->getPlugin("KillRate")) !== null) {
158:       if (MPMU::apiCheck($kr->getDescription()->getVersion(),"1.1")) {
159:         $this->registerApi("KillRate-1.1",$kr);
160:         $this->sysExtensions[] = [ $this , "kr1SysVars" ];
161:       }
162:     }
163:   }
164: 
165:   /**
166:    * Register a callback function that define system wide variable expansions
167:    * @param Server $server - reference to pocketmine server
168:    * @param callable $fn - callable should have as argumens (Server $server, array &$vars)
169:    */
170:   public function registerSysVars(callable $fn) {
171:     $this->initSysVars();
172:     $this->sysExtensions[] = $fn;
173:   }
174: 
175:   /**
176:    * Main entry point for system wide variable defintions
177:    * @param array &$vars - receives variable defintions
178:    */
179:   public function sysVars(array &$vars) {
180:     $this->initSysVars();
181:     foreach ($this->sysExtensions as $cb) {
182:       $cb($vars);
183:     }
184:   }
185:   /**
186:    * Shorter entry point for system wide variable defintions
187:    * @param array &$vars - receives variable defintions
188:    */
189:   public function sysVarsShort(array &$vars) {
190:     foreach ($this->sysExtensions as $cb) {
191:       $cb($vars);
192:     }
193:   }
194: 
195:   ///////////////////////////////////////////////////////////////////////////
196:   // System variable definitions
197:   ///////////////////////////////////////////////////////////////////////////
198: 
199:   /**
200:    * Basic system wide variable definitions
201:    * @param array &$vars - variables
202:    */
203:   public function stdSysVars(array &$vars) {
204:     foreach ([
205:               "{tps}" => $this->getServer()->getTicksPerSecond(),
206:               "{tickUsage}" => $this->getServer()->getTickUsage(),
207:               "{numPlayers}" => count($this->getServer()->getOnlinePlayers()),
208:     ] as $a => $b) {
209:       $vars[$a] = $b;
210:     }
211:   }
212:   /**
213:    * @param array &$vars - variables
214:    */
215:   public function debugSysVars(&$vars) {
216:     $server = $this->getServer();
217:     // Enable debugging variables...
218:     $time = floor(microtime(true) - \pocketmine\START_TIME);
219:     $uptime = "";
220:     $q = "";
221:     foreach ([
222:       [ "sec", 60, "secs"],
223:       [ "min", 60,  "mins"],
224:       [ "hour", 24, "hours"],
225:       [ "day", 0, "days"],
226:     ] as $f) {
227:         if ($f[1]) {
228:           $e = floor($time % $f[1]);
229:           $time = floor($time / $f[1]);
230:         } else {
231:           $e = $time;
232:           $time = 0;
233:         }
234:         if ($e) {
235:           $r = $e == 1 ? $f[0] : $f[2];
236:           $uptime = $e." ".$r . $q . $uptime;
237:           $q = ", ";
238:         }
239:         if ($time == 0) break;
240:     }
241:     $vars["{uptime}"] = $uptime;
242:     $vars["{netup}"] = round($server->getNetwork()->getUpload()/1024,2);
243:     $vars["{netdown}"] = round($server->getNetwork()->getUpload()/1024,2);
244:     $vars["{threads}"] = Utils::getThreadCount();
245:     $mUsage = Utils::getMemoryUsage(true);
246:     $vars["{mainmem}"] = number_format(round($mUsage[0]/1024)/1024,2 );
247:     $vars["{memuse}"] = number_format(round($mUsage[1]/1024)/1024,2 );
248:     $vars["{maxmem}"] = number_format(round($mUsage[2]/1024)/1024,2 );
249:     $rUsage = Utils::getRealMemoryUsage();
250:     $vars["{heapmem}"] = number_format(round($rUsage[0]/1024)/1024,2 );
251:   }
252:   /**
253:    * KillRate v1.1 sysvars compatibility
254:    * @param array &$vars - variables
255:    */
256:   public function kr1SysVars(array &$vars) {
257:     $ranks = $this->api("killrate-1.1")->getRankings(10);
258:     if ($ranks == null) {
259:       $vars["{tops}"] = "N/A";
260:       $vars["{top10}"] = "N/A";
261:     } else {
262:       $vars["{tops}"] = "";
263:       $vars["{top10}"] = "";
264:       $i = 1; $q = "";
265:       foreach ($ranks as $r) {
266:         if ($i <= 3) {
267:           $vars["{tops}"] .= $q.$i.". ".substr($r["player"],0,8).
268:                    " ".$r["count"];
269:           $q = "   ";
270:         }
271:         $vars["{top10}"] .= $i.". ".$r["player"]." ".$r["count"]."\n";
272:         ++$i;
273:       }
274:     }
275: 
276:   }
277: 
278:   ///////////////////////////////////////////////////////////////////////////
279:   // Player variables
280:   ///////////////////////////////////////////////////////////////////////////
281: 
282:   /**
283:    * Used to initialize the player specific variables table
284:    */
285:   protected function initPlayerVars() {
286:     if ($this->$playerExtensions !== null) return;
287:     $this->playerExtensions = $this->autoloadExtensions(self::getPlayerVarsFn);
288:     $this->playerExtensions[] = [ $this , "stdPlayerVars" ];
289:     $this->playerExtensions[] = [ $this , "invPlayerVars" ];
290:     $pm = $this->getServer()->getPluginManager();
291:     if (($kr = $pm->getPlugin("KillRate")) !== null) {
292:       if (MPMU::apiCheck($kr->getDescription()->getVersion(),"1.1")) {
293:         $this->registerApi("KillRate-1.1",$kr);
294:         $this->playerExtensions[] = [ $this , "kr1PlayerVars" ];
295:       }
296:     }
297:     if (($pp = $pm->getPlugin("PurePerms")) !== null) {
298:       $this->registerApi("PurePerms",$pp);
299:       $this->playerExtensions[] = [ $this , "purePermsPlayerVars" ];
300:     }
301:     if (($ru = $pm->getPlugin("RankUp")) !== null) {
302:       $this->registerApi("RankUp",$ru);
303:       $this->playerExtensions[] = [ $this , "rankUpPlayerVars" ];
304:     }
305:     if (($mm = $pm->getPlugin("GoldStd")) !== null) {
306:       $this->registerApi("money", $mm);
307:       $this->playerExtensions[] = [ $this ,"moneyPlayerVarsGoldStd" ];
308:     } elseif (($mm = $pm->getPlugin("PocketMoney")) !== null) {
309:       $this->registerApi("money", $mm);
310:       $this->playerExtensions[] = [ $this , "moneyPlayerVarsPocketMoney" ];
311:     } elseif (($mm = $pm->getPlugin("MassiveEconomy")) !== null) {
312:       $this->registerApi("money", $mm);
313:       $this->playerExtensions[] = [ $this , "moneyPlayerVarsMassiveEconomy" ];
314:     } elseif (($mm = $pm->getPlugin("EconomyAPI")) !== null) {
315:       $this->registerApi("money", $mm);
316:       $this->playerExtensions[] = [ $this, "moneyPlayerVarsEconomysApi" ];
317:     }
318:   }
319:   /**
320:    * Register a callback function that define player specific variable expansions
321:    * @param callable $fn - callable should have as argumens (Player $player, array &$vars)
322:    */
323:   public function registerPlayerVars(callable $fn) {
324:     $this->initPlayerVars();
325:     $this->playerExtensions[] = $fn;
326:   }
327:   /**
328:    * Main entry point for player specifc variable defintions
329:    * @param Player $player - reference to pocketmine Player
330:    * @param array &$vars - receives variable defintions
331:    */
332:   public function playerVars(Player $player, array &$vars) {
333:     $this->initPlayerVars();
334:     foreach ($this->playerExtensions as $cb) {
335:       $cb($this,$player,$vars);
336:     }
337:   }
338:   /**
339:    * Shorter entry point for player specifc variable defintions
340:    * @param Player $player - reference to pocketmine Player
341:    * @param array &$vars - receives variable defintions
342:    */
343:   public function playerVarsShort(Player $player, array &$vars) {
344:     foreach ($this->playerExtensions as $cb) {
345:       $cb($this,$player,$vars);
346:     }
347:   }
348:   ///////////////////////////////////////////////////////////////////////////
349:   // Player variable definitions
350:   ///////////////////////////////////////////////////////////////////////////
351:   /**
352:    * Basic player specific variable definitions
353:    * @param Player $player - reference to pocketmine Player
354:    * @param array &$vars - receives variable defintions
355:    */
356:   public function stdPlayerVars(Player $player,array &$vars) {
357:     foreach ([
358:               "{player}" => $player->getName(),
359:               "{displayName}" => $player->getDisplayName(),
360:               "{world}" => $player->getLevel()->getName(),
361:               "{x}" => (int)$player->getX(),
362:               "{y}" => (int)$player->getY(),
363:               "{z}" => (int)$player->getZ(),
364:               "{yaw}" => (int)$player->getYaw(),
365:               "{pitch}" => (int)$player->getPitch(),
366:               "{bearing}" => self::bearing($player->getYaw()),
367:     ] as $a => $b) {
368:       $vars[$a] = $b;
369:     }
370:   }
371:   /** Inventory related variables
372:    * @param Player $player - reference to pocketmine Player
373:    * @param array &$vars - receives variable defintions
374:    */
375:   public function invPlayerVars(Player $player,array &$vars) {
376:     $item = clone $player->getInventory()->getItemInHand();
377:     if ($item->getId() == Item::AIR) {
378:       $vars["{item}"] = "";
379:       $vars["{itemid}"] = "";
380:     } else {
381:       $vars["{item}"] = ItemName::str($item);
382:       $vars["{itemid}"] = $item->getId();
383:     }
384:   }
385: 
386:   /** KillRate-1.1 compatible player variables
387:    * @param Player $player - reference to pocketmine Player
388:    * @param array &$vars - receives variable defintions
389:    */
390:   public function kr1PlayerVars(Player $player,array &$vars) {
391:     $vars["{score}"] = $this->api("killrate-1.1")->getScore($player);
392:   }
393:   /** PocketMoney Support
394:    * @param Player $player - reference to pocketmine Player
395:    * @param array &$vars - receives variable defintions
396:    */
397:   public function moneyPlayerVarsPocketMoney(Player $player,array &$vars) {
398:     $vars["{money}"] = $$this->api("money")->getMoney($player->getName());
399:   }
400:   /** MassiveEconomy Support
401:    * @param Player $player - reference to pocketmine Player
402:    * @param array &$vars - receives variable defintions
403:    */
404:   public function moneyPlayerVarsMassiveEconomy(Player $player,array &$vars) {
405:     $vars["{money}"] = $this->api("money")->getMoney($player->getName());
406:   }
407:   /** EconomysAPI Support
408:    * @param Player $player - reference to pocketmine Player
409:    * @param array &$vars - receives variable defintions
410:    */
411:   public function moneyPlayerVarsEconomysApi(Player $player,array &$vars) {
412:     $vars["{money}"] = $this->api("money")->mymoney($player->getName());
413:   }
414:   /** GoldStd Support
415:    * @param Player $player - reference to pocketmine Player
416:    * @param array &$vars - receives variable defintions
417:    */
418:   public function moneyPlayerVarsGoldStd(Player $player,array &$vars) {
419:     $vars["{money}"] = $this->api("money")->getMoney($player);
420:   }
421:   /** PurePerms compatibility
422:    * @param Player $player - reference to pocketmine Player
423:    * @param array &$vars - receives variable defintions
424:    */
425:   public function purePermsPlayerVars(Player $player,array &$vars) {
426:     $vars["{group}"] = $this->api("PurePerms")->getUser($player)->getGroup()->getName();
427:   }
428: 
429:   /** RankUp compatibility
430:    * @param Player $player - reference to pocketmine Player
431:    * @param array &$vars - receives variable defintions
432:    */
433:   public function rankUpPlayerVars(Player $player,array &$vars) {
434:     $vars["{rank}"] = $this->api("RankUp")->getPermManager()->getGroup();
435:   }
436:   ///////////////////////////////////////////////////////////////////////////
437:   // Misc Support functions
438:   ///////////////////////////////////////////////////////////////////////////
439:   /**
440:    * Convert bearings in degrees into points in compass
441:    * @param float $deg - yaw
442:    * @return str
443:    */
444:   static public function bearing($deg) {
445:     // Determine bearing
446:     if (22.5 <= $deg && $deg < 67.5) {
447:       return "NW";
448:     } elseif (67.5 <= $deg && $deg < 112.5) {
449:       return "N";
450:     } elseif (112.5 <= $deg && $deg < 157.5) {
451:       return "NE";
452:     } elseif (157.5 <= $deg && $deg < 202.5) {
453:       return "E";
454:     } elseif (202.5 <= $deg && $deg < 247.5) {
455:       return "SE";
456:     } elseif (247.5 <= $deg && $deg < 292.5) {
457:       return "S";
458:     } elseif (292.5 <= $deg && $deg < 337.5) {
459:       return "SW";
460:     } else {
461:       return "W";
462:     }
463:     return (int)$deg;
464:   }
465: 
466: }
467: 
API documentation generated by ApiGen