1: <?php
2: namespace aliuly\grabbag\api;
3:
4: use aliuly\grabbag\Main as GrabBagPlugin;
5: use pocketmine\Player;
6: use pocketmine\IPlayer;
7: use pocketmine\level\Level;
8: use pocketmine\level\Position;
9: use pocketmine\entity\Human;
10: use pocketmine\command\CommandSender;
11:
12: use aliuly\common\mc;
13: use aliuly\common\ExpandVars;
14: use aliuly\common\PMScript;
15: use aliuly\common\MPMU;
16:
17:
18: /**
19: * GrabBag API
20: *
21: * Example Usage:
22: *
23: * Check if GrabBag is installed....
24: * <code>
25: * $api = null;
26: * if (($plugin = $server->getPluginManager()->getPlugin("GrabBag") !== null) && $plugin->isEnabled() && MPMU::apiCheck($plugin->getDescription()->getVersion(),"2.3")) {
27: * if ($plugin->api->getFeature("freeze-thaw")) $api = $plugin->api;
28: * }
29: * </code>
30: *
31: * Call an API function:
32: *
33: * <code>
34: * $api->freeze($player);
35: * </code>
36: */
37: class GrabBag {
38: protected $plugin;
39: protected $vars;
40: protected $interp;
41: /**
42: * @param GrabBagPlugin $owner - plugin that owns this session
43: */
44: public function __construct(GrabBagPlugin $owner) {
45: $this->plugin = $owner;
46: $this->vars = null;
47: $this->interp = null;
48: }
49: /**
50: * Check if module is available...
51: * This will throw an exception if the module is not available
52: * @param str $module - module name
53: * @return mixed|null
54: */
55: public function getModule($module) {
56: $vp = $this->plugin->getModule($module);
57: if ($vp === null) throw new \RuntimeException("Missing module: " . $module);
58: return $vp;
59: }
60: /**
61: * Check if feature is supported and has been enabled in the GrabBag
62: * configuration file.
63: * @param str $feature - module name
64: * @return bool
65: */
66: public function getFeature($feature) {
67: if (!in_array($feature,[
68: "freeze-thaw", "invisible", "after-at", "cmd-alias", "blowup",
69: "chat-utils", "followers", "mute-unmute", "opms-rpt", "reop",
70: "shield", "slay", "spawn", "srvmode", "summon-dismiss",
71: "throw", "pushtp-poptp", "homes", "tprequest", "warps", "plenty",
72: "chat-scribe",
73: "ServerList",
74: ])) return false;
75: if ($this->plugin->getModule($feature) === null) return false;
76: return true;
77: }
78: /**
79: * Currently un-implemented
80: */
81: public function getVars() {
82: if ($this->vars === null) {
83: $this->vars = new ExpandVars($this->plugin);
84: $this->vars->define("{GrabBag}", $this->plugin->getDescription()->getVersion());
85: $this->vars->define("{libcommon}", MPMU::version());
86: }
87: return $this->vars;
88: }
89: /**
90: * Currently un-implemented
91: */
92: public function getInterp() {
93: if ($this->interp == null) {
94: $this->interp = new PMScript($this->plugin,$this->getVars());
95: }
96: return $this->interp;
97: }
98:
99: //////////////////////////////////////////////////////////////
100: // CmdFreeze
101: //////////////////////////////////////////////////////////////
102: /**
103: * Checks if hard or soft freezing
104: * @return bool
105: */
106: public function isHardFreeze() {
107: return $this->getModule("freeze-thaw")->isHardFreeze();
108: }
109: /**
110: * Sets hard or soft freezing
111: * @param bool $hard - if true (default) hard freeze is in effect.
112: */
113: public function setHardFreeze($hard = true) {
114: $this->getModule("freeze-thaw")->setHardFreeze($hard);
115: }
116: /**
117: * Checks if player is frozen
118: * @param Player $player - player to check
119: * @return bool
120: */
121: public function isFrozen(Player $player) {
122: return $this->getModule("freeze-thaw")->isFrozen($player);
123: }
124: /**
125: * Freeze given player
126: * @param Player $player - player to freeze
127: * @param bool $freeze - if true (default) freeze, if false, thaw.
128: */
129: public function freeze(Player $player, $freeze = true) {
130: $this->getModule("freeze-thaw")->freeze($player,$freeze);
131: }
132: /**
133: * Return a list of frozen players
134: * @return str[]
135: */
136: public function getFrosties() {
137: return $this->getModule("freeze-thaw")->getFrosties();
138: }
139: //////////////////////////////////////////////////////////////
140: // CmdInvisible
141: //////////////////////////////////////////////////////////////
142: /**
143: * Make player invisible
144: * @param Player $player - player to change
145: * @param bool $invis - if true (default) invisible, if false, visible.
146: */
147: public function invisible(Player $player, $invis) {
148: if ($invis) {
149: if (!$this->getModule("invisible")->isInvisible($player))
150: $this->getModule("invisible")->activate($player);
151: } else {
152: if ($this->getModule("invisible")->isInvisible($player))
153: $this->getModule("invisible")->deactivate($player);
154: }
155: }
156: /**
157: * Check if player is invisible...
158: * @param Player $player - player to check
159: */
160: public function isInvisible(Player $player) {
161: return $this->getModule("invisible")->isInvisible($player);
162: }
163: //////////////////////////////////////////////////////////////
164: // CmdAfterAt
165: //////////////////////////////////////////////////////////////
166: /**
167: * Schedule a command to be run
168: * @param int $secs - execute after this number of seconds
169: * @param str $cmdline - command line to execute
170: */
171: public function after($cmdline,$secs) {
172: $this->getModule("after-at")->schedule($secs,$cmdline);
173: }
174: //////////////////////////////////////////////////////////////
175: // CmdAlias
176: //////////////////////////////////////////////////////////////
177: /**
178: * Define a command alias
179: * @param str $alias - alias name
180: * @param str $cmdline - command line to execute
181: * @param bool $force - overwrite existing commands
182: * @return bool - true on succes, false on failure
183: */
184: public function alias($alias, $cmdline,$force = false) {
185: return $this->getModule("cmd-alias")->addAlias($alias,$cmdline,$force);
186: }
187: //////////////////////////////////////////////////////////////
188: // CmdBlowUp
189: //////////////////////////////////////////////////////////////
190: /**
191: * Blow player up
192: * @param Player $player - victim
193: * @param int $yield - explosion power
194: * @param bool $magic - don't affect blocks
195: * @return bool - true on succes, false on failure
196: */
197: public function blowPlayer(Player $player,$yield,$magic = false) {
198: return $this->getModule("blowup")->blowPlayer($player,$yield,$magic);
199: }
200: //////////////////////////////////////////////////////////////
201: // CmdChatMgr
202: //////////////////////////////////////////////////////////////
203: /**
204: * Enable/Disable Chat globally
205: * @param bool $mode - true, chat is active, false, chat is disabled
206: */
207: public function setGlobalChat($mode) {
208: $this->getModule("chat-utils")->setGlobalChat($mode);
209: }
210: /**
211: * Returns global chat status
212: * @return bool
213: */
214: public function getGlobalChat() {
215: return $this->getModule("chat-utils")->getGlobalChat();
216: }
217: /**
218: * Enable/Disable player's chat
219: * @param Player $player
220: * @param bool $mode - true, chat is active, false, chat is disabled
221: */
222: public function setPlayerChat(Player $player,$mode) {
223: $this->getModule("chat-utils")->setPlayerChat($player,$mode);
224: }
225: /**
226: * Returns player's chat status
227: * @param Player $player
228: * @return bool
229: */
230: public function getPlayerChat(Player $player) {
231: return $this->getModule("chat-utils")->getPlayerChat($player);
232: }
233: //////////////////////////////////////////////////////////////
234: // CmdFollowMgr
235: //////////////////////////////////////////////////////////////
236: /**
237: * Returns players that are leading others
238: * @return str[]
239: */
240: public function getLeaders() {
241: return $this->getModule("followers")->getLeaders();
242: }
243: /**
244: * Returns followers of a certain leader
245: * @param Player $leader
246: * @return str[]
247: */
248: public function getFollowers(Player $leader) {
249: return $this->getModule("followers")->getFollowers($leader);
250: }
251: /**
252: * Make a player follow another
253: * @param Player $follower
254: * @param Player $leader
255: */
256: public function follow(Player $follower, Player $leader) {
257: $this->getModule("followers")->follow($follower,$leader);
258: }
259: /**
260: * Stop a player from following
261: * @param Player $follower
262: */
263: public function stopFollowing(Player $follower) {
264: $this->getModule("followers")->stopFollowing($follower);
265: }
266: /**
267: * Remove all folowers from a leader
268: * @param Player $leader
269: */
270: public function stopLeading(Player $leader) {
271: $this->getModule("followers")->stopLeading($leader);
272: }
273: //////////////////////////////////////////////////////////////
274: // CmdMuteMgr
275: //////////////////////////////////////////////////////////////
276: /**
277: * Returns the list of muted players
278: * @return str[]
279: */
280: public function getMutes() {
281: return $this->getModule("mute-unmute")->getMutes();
282: }
283: /**
284: * Mute/UnMute a player
285: * @param Player $player
286: * @param bool $mode - true is muted, false is unmuted
287: */
288: public function setMute(Player $player,$mode) {
289: $this->getModule("mute-unmute")->setMute($player, $mode);
290: }
291: /**
292: * Returns a player mute status
293: * @param Player $player
294: * @return bool
295: */
296: public function getMute(Player $player) {
297: return $this->getModule("mute-unmute")->getMute($player);
298: }
299: //////////////////////////////////////////////////////////////
300: // CmdOpMsg
301: //////////////////////////////////////////////////////////////
302: /**
303: * File a report
304: * @param CommandSender $c
305: * @param str $report
306: */
307: public function fileReport(CommandSender $c, $report) {
308: $this->getModule("opms-rpt")->rptCmd($player, [ ">", $report]);
309: }
310: //////////////////////////////////////////////////////////////
311: // CmdReOp
312: //////////////////////////////////////////////////////////////
313: /**
314: * Return player's reop status
315: * @param Player $target
316: * @return bool
317: */
318: public function isReOp(Player $target) {
319: return $this->getModule("reop")->isReOp($target);
320: }
321: /**
322: * Toggle player's reop
323: * @param Player $target
324: */
325: public function reopPlayer(Player $target) {
326: $this->getModule("reop")->reopPlayer($target);
327: }
328: //////////////////////////////////////////////////////////////
329: // CmdShieldMgr
330: //////////////////////////////////////////////////////////////
331: /**
332: * Return player's shield status
333: * @param Player $target
334: * @return bool
335: */
336: public function isShielded(Player $target) {
337: return $this->getModule("shield")->isShielded($target);
338: }
339: /**
340: * Turn on/off shields
341: * @param Player $target
342: * @param bool $mode - true is shielded, false is not
343: */
344: public function setShield(Player $target, $mode) {
345: $this->getModule("shield")->setShield($target, $mode);
346: }
347: //////////////////////////////////////////////////////////////
348: // CmdPlenty
349: //////////////////////////////////////////////////////////////
350: /**
351: * Return player's plenty status
352: * @param Player $target
353: * @return bool
354: */
355: public function hasPlenty(Player $target) {
356: return $this->getModule("plenty")->hasPlenty($target);
357: }
358: /**
359: * Turn on/off plenty
360: * @param Player $target
361: * @param bool $mode - true is shielded, false is not
362: */
363: public function setPlenty(Player $target, $mode) {
364: $this->getModule("plenty")->setPlenty($target, $mode);
365: }
366: //////////////////////////////////////////////////////////////
367: // CmdSlay
368: //////////////////////////////////////////////////////////////
369: /**
370: * Kills a player with optional message
371: * @param Player $victim
372: * @param str $msg
373: */
374: public function slay(Player $victim, $msg = "") {
375: $this->getModule("slay")->slay($victim,$msg);
376: }
377: //////////////////////////////////////////////////////////////
378: // CmdSpawn
379: //////////////////////////////////////////////////////////////
380: /**
381: * Teleport a player to world spawn
382: * @param Player $player
383: */
384: public function tpSpawn(Player $player) {
385: $this->getModule("spawn")->tpSpawn($player);
386: }
387: //////////////////////////////////////////////////////////////
388: // CmdSrvMoDeMgr
389: //////////////////////////////////////////////////////////////
390: /**
391: * Return the current service mode status
392: * @return false|str
393: */
394: public function getServiceMode() {
395: return $this->getModule("srvmode")->getServiceMode();
396: }
397: /**
398: * Change the service mode
399: * @param str $msg
400: */
401: public function setServiceMode($msg) {
402: $this->getModule("srvmode")->setServiceMode($msg);
403: }
404: /**
405: * Exists service mode
406: */
407: public function unsetServiceMode() {
408: $this->getModule("srvmode")->unsetServiceMode();
409: }
410: //////////////////////////////////////////////////////////////
411: // CmdSummon
412: //////////////////////////////////////////////////////////////
413: /**
414: * Teleport a player to the summoner's vicinity
415: * @param Player $summoner
416: * @param Player $victim
417: */
418: public function summonPlayer(Player $summoner,Player $victim) {
419: $this->getModule("summon-dismiss")->cmdSummon($summoner,[$victim->getName()]);
420: }
421: /**
422: * Dismiss a previously summoned player
423: * @param Player $summoner
424: * @param Player $victim
425: */
426: public function dismissPlayer(Player $summoner,Player $victim) {
427: $this->getModule("summon-dismiss")->cmdDismiss($summoner,[$victim->getName()]);
428: }
429: /**
430: * Dismiss all summoned players
431: * @param Player $summoner
432: */
433: public function dismissAll(Player $summoner) {
434: $this->getModule("summon-dismiss")->cmdDismiss($summoner,["--all"]);
435: }
436: //////////////////////////////////////////////////////////////
437: // CmdThrow
438: //////////////////////////////////////////////////////////////
439: /**
440: * Throw player up in the air.
441: * @param Player $victim
442: */
443: public function throwPlayer(Player $victim) {
444: $this->getModule("throw")->throwPlayer($victim);
445: }
446: //////////////////////////////////////////////////////////////
447: // CmdTpStack
448: //////////////////////////////////////////////////////////////
449: /**
450: * Save position to stack
451: * @param Player $player
452: */
453: public function pushTp(Player $player) {
454: $this->getModule("pushtp-poptp")->cmdPushTp($player,[]);
455: }
456: /**
457: * Restore position from stack
458: * @param Player $player
459: */
460: public function popTp(Player $player) {
461: $this->getModule("pushtp-poptp")->cmdPopTp($player,[]);
462: }
463: //////////////////////////////////////////////////////////////
464: // Teleport Request API
465: //////////////////////////////////////////////////////////////
466: public function TpAsk(Player $a, Player $b) {
467: $this->getModule("tprequest")->cmdTpAsk($a,$b);
468: }
469: public function TpHere(Player $a, Player $b) {
470: $this->getModule("tprequest")->cmdTpHere($a,$b);
471: }
472: public function TpDecline(Player $a, Player $b) {
473: $this->getModule("tprequest")->cmdDecline($a,$b);
474: }
475: public function TpAccept(Player $a, Player $b) {
476: $this->getModule("tprequest")->cmdAccept($a,$b);
477: }
478:
479: //////////////////////////////////////////////////////////////
480: // Homes API
481: //////////////////////////////////////////////////////////////
482: /**
483: * Get the player's home on the provided level.
484: * @param Player $player
485: * @param Level $level
486: */
487: public function getHome(IPlayer $player, Level $level) {
488: return $this->getModule("homes")->getHome($player,$level);
489: }
490: /**
491: * Set the player's home in the level provided in $pos
492: * @param Player $player
493: * @param Position $pos
494: */
495: public function setHome(IPlayer $player, Position $pos) {
496: $this->getModule("homes")->setHome($player,$pos);
497: }
498: /**
499: * Delete the player's home on the provided level.
500: * @param Player $player
501: * @param Level $level
502: */
503: public function delHome(IPlayer $player, Level $level) {
504: $this->getModule("homes")->getHome($player,$level);
505: }
506: //////////////////////////////////////////////////////////////
507: // Warps
508: //////////////////////////////////////////////////////////////
509: /**
510: * Return a list of warps
511: * @return str[]
512: */
513: public function getWarps() {
514: return $this->getModule("warps")->getWarps();
515: }
516: /**
517: * Return a warp definiton or null
518: * @param str $name
519: * @return Position
520: */
521: public function getWarp($name) {
522: return $this->getModule("warps")->getWarp($name);
523: }
524: /**
525: * Save a warp
526: * @param str $name
527: * @param Position $pos
528: * @return bool - true on succes, false on failure
529: */
530: public function setWarp($name, Position $pos) {
531: return $this->getModule("warps")->setWarp($name,$pos);
532: }
533: /**
534: * Delete a warp
535: * @param str $name
536: * @return bool - true on succes, false on failure
537: */
538: public function delWarp($name) {
539: return $this->getModule("warps")->delWarp($name);
540: }
541: //////////////////////////////////////////////////////////////
542: // Spy Session
543: //////////////////////////////////////////////////////////////
544: /**
545: * Get SpySession object
546: * @return SpySession
547: */
548: public function getSpySession() {
549: return $this->getModule("chat-scribe")->getSpySession();
550: }
551:
552: //////////////////////////////////////////////////////////////
553: // ServerList
554: //////////////////////////////////////////////////////////////
555: /**
556: * Get server ids
557: */
558: public function getServerIds() {
559: return $this->getModule("ServerList")->getIds();
560: }
561: /**
562: * Add Server
563: * @param str $id - Server Id
564: * @param array $attrs - Server attributes
565: * @return bool - true on success, false on error
566: */
567: public function addServer($id,array $attrs) {
568: return $this->getModule("ServerList")->addServer($id,$attrs);
569: }
570: /**
571: * Remove Server
572: * @param str $id - Server Id
573: * @return bool - true on success, false on error
574: */
575: public function removeServer($id) {
576: return $this->getModule("ServerList")->rmServer($id);
577: }
578: /**
579: * Get Server attributes
580: * @param str $id - Server Id
581: * @return array - attributes
582: */
583: public function getServer($id) {
584: return $this->getModule("ServerList")->getServer($id);
585: }
586: /**
587: * Get Server attribute
588: * @param str $id - Server Id
589: * @param str $attr - attribute to get
590: * @param mixed $default - value to return if tag is not found
591: * @return mixed
592: */
593: public function getServerAttr($id,$attr,$default=null) {
594: return $this->getModule("ServerList")->getServerAttr($id,$attr,$default);
595: }
596: /**
597: * @param str $id - Server Id
598: * @param str $tag - tag
599: * @param mixed $attrs - Server attributes
600: * @return bool - true on success, false on error
601: */
602: public function addQueryData($id,$tag,$attrs) {
603: return $this->getModule("ServerList")->addQueryData($id,$tag,$attrs);
604: }
605: /**
606: * Remove Server query data
607: * @param str $id - Server Id
608: * @param null|str $tag - data tag
609: * @return bool - true on success, false on error
610: */
611: public function delQueryData($id,$tag = null) {
612: return $this->getModule("ServerList")->delQueryData($id,$tag);
613: }
614: /**
615: * Get Query data
616: * @param str $id - Server Id
617: * @param null|str $tag
618: * @param mixed $default - default value to return if data is not found
619: * @return array|null
620: */
621: public function getQueryData($id,$tag=null,$default =null) {
622: return $this->getModule("ServerList")->getQueryData($id,$tag,$default);
623: }
624:
625: }
626: