11<?php
22namespace taptodo ;
33
4- use pocketmine \command \ConsoleCommandSender ;
54use pocketmine \level \Position ;
65use pocketmine \Player ;
76
87class Block{
9- const AS_CONSOLE_TYPE = 0 ;
10- const AS_PLAYER_TYPE = 1 ;
11- const AS_OP_TYPE = 2 ;
12-
13- private $ pos , $ cmd , $ name , $ plugin ;
8+ /** @var Command[] */
9+ private $ commands ;
10+ /** @var Position */
11+ private $ position ;
12+ /** @var mixed */
13+ private $ name ;
14+ /** @var TapToDo */
15+ private $ plugin ;
1416 public $ id ;
1517 public function __construct (Position $ position , array $ commands , TapToDo $ main , $ id , $ name = false ){
16- $ this ->pos = $ position ;
17- $ this ->cmd = [];
18+ $ this ->position = $ position ;
19+ $ this ->commands = [];
1820 $ this ->plugin = $ main ;
1921 $ this ->name = $ name ;
2022 $ this ->id = $ id ;
@@ -26,26 +28,18 @@ public function addCommands($cmds){
2628 $ cmds = [$ cmds ];
2729 }
2830 foreach ($ cmds as $ c ) {
29- $ type = Block::AS_PLAYER_TYPE ;
30- $ c = str_replace ("%safe " , "" , $ c );
31- if (strpos ($ c , "%pow " ) !== false && ($ c = str_replace ("%pow " , "" , $ c ))) {
32- $ type = Block::AS_CONSOLE_TYPE ;
33- }
34- elseif (strpos ($ c , "%op " ) !== false && ($ c = str_replace ("%op " , "" , $ c ))){
35- $ type = Block::AS_OP_TYPE ;
36- }
37- $ this ->cmd [] = [$ c , $ type ];
31+ $ this ->commands [] = new Command ($ c , $ this ->plugin );
3832 }
3933 $ this ->plugin ->saveBlock ($ this );
4034 }
4135 public function addCommand ($ cmd ){
4236 $ this ->addCommands ([$ cmd ]);
4337 }
44- public function delCommand ($ cmd ){
38+ public function deleteCommand ($ cmd ){
4539 $ ret = false ;
46- for ($ i = 0 ; $ i < count ($ this ->cmd ); $ i++ ){
47- if ($ this ->cmd [$ i ][ 0 ] === $ cmd ){
48- unset($ this ->cmd [$ i ]);
40+ for ($ i = count ($ this ->commands ); $ i >= 0 ; $ i -- ){
41+ if ($ this ->commands [$ i ]-> getOriginalCommand () === $ cmd || $ this -> commands [ $ i ]-> getCompiledCommand () === $ cmd ){
42+ unset($ this ->commands [$ i ]);
4943 $ ret = true ;
5044 }
5145 }
@@ -54,55 +48,41 @@ public function delCommand($cmd){
5448 }
5549 return $ ret ;
5650 }
57- public function runCommands (Player $ p ){
58- foreach ($ this ->cmd as $ c ){
59- $ type = $ c [1 ];
60- $ c = $ c [0 ];
61-
62- $ c = str_replace ("%p " , $ p ->getName (), $ c );
63- $ c = str_replace ("%x " , $ p ->getX (), $ c );
64- $ c = str_replace ("%y " , $ p ->getY (), $ c );
65- $ c = str_replace ("%z " , $ p ->getZ (), $ c );
66-
67- if ($ type === Block::AS_OP_TYPE && $ p ->isOp ()) $ type = Block::AS_PLAYER_TYPE ;
68-
69- switch ($ type ) {
70- case Block::AS_CONSOLE_TYPE :
71- $ this ->plugin ->getServer ()->dispatchCommand (new ConsoleCommandSender (), $ c );
72- break ;
73- case Block::AS_OP_TYPE :
74- $ p ->setOp (true );
75- $ this ->plugin ->getServer ()->dispatchCommand ($ p , $ c );
76- $ p ->setOp (false );
77- break ;
78- case Block::AS_PLAYER_TYPE :
79- $ this ->plugin ->getServer ()->dispatchCommand ($ p , $ c );
80- break ;
81- }
82- //$this->plugin->getLogger()->info($c);
51+ public function executeCommands (Player $ player ){
52+ foreach ($ this ->commands as $ command ){
53+ $ command ->execute ($ player );
8354 }
8455 }
85- public function nameBlock ($ name ){
56+ public function setName ($ name ){
8657 $ this ->name = $ name ;
8758 }
8859 public function getCommands (){
8960 $ out = [];
90- foreach ($ this ->cmd as $ cmd ) $ out [] = $ cmd [ 0 ] ;
61+ foreach ($ this ->commands as $ command ) $ out [] = $ command -> getOriginalCommand () ;
9162 return $ out ;
9263 }
9364 public function getName (){
9465 return $ this ->name ;
9566 }
67+
68+ /**
69+ * @return Position
70+ * @deprecated
71+ */
9672 public function getPos (){
97- return $ this ->pos ;
73+ return $ this ->position ;
74+ }
75+ public function getPosition (){
76+ return $ this ->position ;
9877 }
9978 public function toArray (){
100- $ arr = array (
101- 'x ' => $ this ->getPos ()->getX (),
102- 'y ' => $ this ->getPos ()->getY (),
103- 'z ' => $ this ->getPos ()->getZ (),
104- 'level ' => $ this ->getPos ()->getLevel ()->getName (),
105- 'commands ' => $ this ->getCommands ());
79+ $ arr = [
80+ 'x ' => $ this ->getPosition ()->getX (),
81+ 'y ' => $ this ->getPosition ()->getY (),
82+ 'z ' => $ this ->getPosition ()->getZ (),
83+ 'level ' => $ this ->getPosition ()->getLevel ()->getName (),
84+ 'commands ' => $ this ->getCommands ()
85+ ];
10686 if ($ this ->name !== false ) $ arr ["name " ] = $ this ->name ;
10787 return $ arr ;
10888 }
0 commit comments