Skip to content

Commit e9dc9fd

Browse files
committed
Version 2.2
1 parent e66ee88 commit e9dc9fd

File tree

4 files changed

+149
-128
lines changed

4 files changed

+149
-128
lines changed

plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: TapToDo
22
main: taptodo\TapToDo
3-
version: 2.1.2
3+
version: 2.2
44
author: Falk
55
api: [1.0.0]
66
load: POSTWORLD

src/taptodo/Block.php

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
<?php
22
namespace taptodo;
33

4-
use pocketmine\command\ConsoleCommandSender;
54
use pocketmine\level\Position;
65
use pocketmine\Player;
76

87
class 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
}

src/taptodo/Command.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@ class Command {
1010
const AS_PLAYER_TYPE = 1;
1111
const AS_OP_TYPE = 2;
1212

13+
/** @var mixed */
1314
private $originalCommand;
15+
/** @var mixed */
1416
private $compiledCommand;
1517
private $executionMode;
1618
/** @var TapToDo */
1719
private $plugin;
1820
public function __construct($command, TapToDo $plugin){
1921
$this->originalCommand = $command;
20-
$this->compiledCommand = null;
2122
$this->plugin = $plugin;
2223
$this->compile();
2324
}
2425
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;
26+
if($this->executionMode == null) {
27+
$this->executionMode = Command::AS_PLAYER_TYPE;
28+
$this->compiledCommand = $this->originalCommand;
29+
$this->compiledCommand = str_replace("%safe", "", $this->compiledCommand);
30+
if (strpos($this->compiledCommand, "%pow") !== false && ($this->compiledCommand = str_replace("%pow", "", $this->compiledCommand))) {
31+
$this->executionMode = Command::AS_CONSOLE_TYPE;
32+
} elseif (strpos($this->compiledCommand, "%op") !== false && ($c = str_replace("%op", "", $this->compiledCommand))) {
33+
$this->executionMode = Command::AS_OP_TYPE;
34+
}
3335
}
3436
}
3537
public function execute(Player $player){
@@ -44,20 +46,35 @@ public function execute(Player $player){
4446
$command = str_replace("%ip", $player->getAddress(), $command);
4547
$command = str_replace("%n", $player->getDisplayName(), $command);
4648

47-
if($type === Block::AS_OP_TYPE && $player->isOp()) $type = Block::AS_PLAYER_TYPE;
49+
if($type === Command::AS_OP_TYPE && $player->isOp()) $type = Command::AS_PLAYER_TYPE;
4850

4951
switch ($type) {
50-
case Block::AS_CONSOLE_TYPE:
52+
case Command::AS_CONSOLE_TYPE:
5153
$this->plugin->getServer()->dispatchCommand(new ConsoleCommandSender(), $command);
5254
break;
53-
case Block::AS_OP_TYPE:
55+
case Command::AS_OP_TYPE:
5456
$player->setOp(true);
5557
$this->plugin->getServer()->dispatchCommand($player, $command);
5658
$player->setOp(false);
5759
break;
58-
case Block::AS_PLAYER_TYPE:
60+
case Command::AS_PLAYER_TYPE:
5961
$this->plugin->getServer()->dispatchCommand($player, $command);
6062
break;
6163
}
6264
}
65+
66+
/**
67+
* @return mixed
68+
*/
69+
public function getOriginalCommand(){
70+
return $this->originalCommand;
71+
}
72+
73+
/**
74+
* @return null
75+
*/
76+
public function getCompiledCommand(){
77+
return $this->compiledCommand;
78+
}
79+
6380
}

0 commit comments

Comments
 (0)