1+ <?php
2+ namespace taptodo ;
3+
4+
5+ use pocketmine \command \ConsoleCommandSender ;
6+ use pocketmine \Player ;
7+
8+ class Command {
9+ const AS_CONSOLE_TYPE = 0 ;
10+ const AS_PLAYER_TYPE = 1 ;
11+ const AS_OP_TYPE = 2 ;
12+
13+ private $ originalCommand ;
14+ private $ compiledCommand ;
15+ private $ executionMode ;
16+ /** @var TapToDo */
17+ private $ plugin ;
18+ public function __construct ($ command , TapToDo $ plugin ){
19+ $ this ->originalCommand = $ command ;
20+ $ this ->compiledCommand = null ;
21+ $ this ->plugin = $ plugin ;
22+ $ this ->compile ();
23+ }
24+ public function compile (){
25+ $ this ->executionMode = Block::AS_PLAYER_TYPE ;
26+ $ this ->compiledCommand = $ this ->originalCommand ;
27+ $ this ->compiledCommand = str_replace ("%safe " , "" , $ this ->compiledCommand );
28+ if (strpos ($ this ->compiledCommand , "%pow " ) !== false && ($ this ->compiledCommand = str_replace ("%pow " , "" , $ this ->compiledCommand ))) {
29+ $ this ->executionMode = Command::AS_CONSOLE_TYPE ;
30+ }
31+ elseif (strpos ($ this ->compiledCommand , "%op " ) !== false && ($ c = str_replace ("%op " , "" , $ this ->compiledCommand ))){
32+ $ this ->executionMode = Command::AS_OP_TYPE ;
33+ }
34+ }
35+ public function execute (Player $ player ){
36+ $ command = $ this ->compiledCommand ;
37+ $ type = $ this ->executionMode ;
38+
39+ $ command = str_replace ("%p " , $ player ->getName (), $ command );
40+ $ command = str_replace ("%x " , $ player ->getX (), $ command );
41+ $ command = str_replace ("%y " , $ player ->getY (), $ command );
42+ $ command = str_replace ("%z " , $ player ->getZ (), $ command );
43+ $ command = str_replace ("%l " , $ player ->getLevel ()->getName (), $ command );
44+ $ command = str_replace ("%ip " , $ player ->getAddress (), $ command );
45+ $ command = str_replace ("%n " , $ player ->getDisplayName (), $ command );
46+
47+ if ($ type === Block::AS_OP_TYPE && $ player ->isOp ()) $ type = Block::AS_PLAYER_TYPE ;
48+
49+ switch ($ type ) {
50+ case Block::AS_CONSOLE_TYPE :
51+ $ this ->plugin ->getServer ()->dispatchCommand (new ConsoleCommandSender (), $ command );
52+ break ;
53+ case Block::AS_OP_TYPE :
54+ $ player ->setOp (true );
55+ $ this ->plugin ->getServer ()->dispatchCommand ($ player , $ command );
56+ $ player ->setOp (false );
57+ break ;
58+ case Block::AS_PLAYER_TYPE :
59+ $ this ->plugin ->getServer ()->dispatchCommand ($ player , $ command );
60+ break ;
61+ }
62+ }
63+ }
0 commit comments