Skip to content

Commit

Permalink
Version 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
falkirks committed Aug 18, 2015
1 parent e66ee88 commit e9dc9fd
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 128 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: TapToDo
main: taptodo\TapToDo
version: 2.1.2
version: 2.2
author: Falk
api: [1.0.0]
load: POSTWORLD
Expand Down
92 changes: 36 additions & 56 deletions src/taptodo/Block.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php
namespace taptodo;

use pocketmine\command\ConsoleCommandSender;
use pocketmine\level\Position;
use pocketmine\Player;

class Block{
const AS_CONSOLE_TYPE = 0;
const AS_PLAYER_TYPE = 1;
const AS_OP_TYPE = 2;

private $pos, $cmd, $name, $plugin;
/** @var Command[] */
private $commands;
/** @var Position */
private $position;
/** @var mixed */
private $name;
/** @var TapToDo */
private $plugin;
public $id;
public function __construct(Position $position, array $commands, TapToDo $main, $id, $name = false){
$this->pos = $position;
$this->cmd = [];
$this->position = $position;
$this->commands = [];
$this->plugin = $main;
$this->name = $name;
$this->id = $id;
Expand All @@ -26,26 +28,18 @@ public function addCommands($cmds){
$cmds = [$cmds];
}
foreach ($cmds as $c) {
$type = Block::AS_PLAYER_TYPE;
$c = str_replace("%safe", "", $c);
if (strpos($c, "%pow") !== false && ($c = str_replace("%pow", "", $c))) {
$type = Block::AS_CONSOLE_TYPE;
}
elseif(strpos($c, "%op") !== false && ($c = str_replace("%op", "", $c))){
$type = Block::AS_OP_TYPE;
}
$this->cmd[] = [$c, $type];
$this->commands[] = new Command($c, $this->plugin);
}
$this->plugin->saveBlock($this);
}
public function addCommand($cmd){
$this->addCommands([$cmd]);
}
public function delCommand($cmd){
public function deleteCommand($cmd){
$ret = false;
for($i = 0; $i < count($this->cmd); $i++){
if($this->cmd[$i][0] === $cmd){
unset($this->cmd[$i]);
for($i = count($this->commands); $i >= 0; $i--){
if($this->commands[$i]->getOriginalCommand() === $cmd || $this->commands[$i]->getCompiledCommand() === $cmd){
unset($this->commands[$i]);
$ret = true;
}
}
Expand All @@ -54,55 +48,41 @@ public function delCommand($cmd){
}
return $ret;
}
public function runCommands(Player $p){
foreach($this->cmd as $c){
$type = $c[1];
$c = $c[0];

$c = str_replace("%p", $p->getName(), $c);
$c = str_replace("%x", $p->getX(), $c);
$c = str_replace("%y", $p->getY(), $c);
$c = str_replace("%z", $p->getZ(), $c);

if($type === Block::AS_OP_TYPE && $p->isOp()) $type = Block::AS_PLAYER_TYPE;

switch ($type) {
case Block::AS_CONSOLE_TYPE:
$this->plugin->getServer()->dispatchCommand(new ConsoleCommandSender(), $c);
break;
case Block::AS_OP_TYPE:
$p->setOp(true);
$this->plugin->getServer()->dispatchCommand($p, $c);
$p->setOp(false);
break;
case Block::AS_PLAYER_TYPE:
$this->plugin->getServer()->dispatchCommand($p, $c);
break;
}
//$this->plugin->getLogger()->info($c);
public function executeCommands(Player $player){
foreach($this->commands as $command){
$command->execute($player);
}
}
public function nameBlock($name){
public function setName($name){
$this->name = $name;
}
public function getCommands(){
$out = [];
foreach($this->cmd as $cmd) $out[] = $cmd[0];
foreach($this->commands as $command) $out[] = $command->getOriginalCommand();
return $out;
}
public function getName(){
return $this->name;
}

/**
* @return Position
* @deprecated
*/
public function getPos(){
return $this->pos;
return $this->position;
}
public function getPosition(){
return $this->position;
}
public function toArray(){
$arr = array(
'x' => $this->getPos()->getX(),
'y' => $this->getPos()->getY(),
'z' => $this->getPos()->getZ(),
'level' => $this->getPos()->getLevel()->getName(),
'commands' => $this->getCommands());
$arr = [
'x' => $this->getPosition()->getX(),
'y' => $this->getPosition()->getY(),
'z' => $this->getPosition()->getZ(),
'level' => $this->getPosition()->getLevel()->getName(),
'commands' => $this->getCommands()
];
if($this->name !== false) $arr["name"] = $this->name;
return $arr;
}
Expand Down
43 changes: 30 additions & 13 deletions src/taptodo/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@ class Command {
const AS_PLAYER_TYPE = 1;
const AS_OP_TYPE = 2;

/** @var mixed */
private $originalCommand;
/** @var mixed */
private $compiledCommand;
private $executionMode;
/** @var TapToDo */
private $plugin;
public function __construct($command, TapToDo $plugin){
$this->originalCommand = $command;
$this->compiledCommand = null;
$this->plugin = $plugin;
$this->compile();
}
public function compile(){
$this->executionMode = Block::AS_PLAYER_TYPE;
$this->compiledCommand = $this->originalCommand;
$this->compiledCommand = str_replace("%safe", "", $this->compiledCommand);
if (strpos($this->compiledCommand, "%pow") !== false && ($this->compiledCommand = str_replace("%pow", "", $this->compiledCommand))) {
$this->executionMode = Command::AS_CONSOLE_TYPE;
}
elseif(strpos($this->compiledCommand, "%op") !== false && ($c = str_replace("%op", "", $this->compiledCommand))){
$this->executionMode = Command::AS_OP_TYPE;
if($this->executionMode == null) {
$this->executionMode = Command::AS_PLAYER_TYPE;
$this->compiledCommand = $this->originalCommand;
$this->compiledCommand = str_replace("%safe", "", $this->compiledCommand);
if (strpos($this->compiledCommand, "%pow") !== false && ($this->compiledCommand = str_replace("%pow", "", $this->compiledCommand))) {
$this->executionMode = Command::AS_CONSOLE_TYPE;
} elseif (strpos($this->compiledCommand, "%op") !== false && ($c = str_replace("%op", "", $this->compiledCommand))) {
$this->executionMode = Command::AS_OP_TYPE;
}
}
}
public function execute(Player $player){
Expand All @@ -44,20 +46,35 @@ public function execute(Player $player){
$command = str_replace("%ip", $player->getAddress(), $command);
$command = str_replace("%n", $player->getDisplayName(), $command);

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

switch ($type) {
case Block::AS_CONSOLE_TYPE:
case Command::AS_CONSOLE_TYPE:
$this->plugin->getServer()->dispatchCommand(new ConsoleCommandSender(), $command);
break;
case Block::AS_OP_TYPE:
case Command::AS_OP_TYPE:
$player->setOp(true);
$this->plugin->getServer()->dispatchCommand($player, $command);
$player->setOp(false);
break;
case Block::AS_PLAYER_TYPE:
case Command::AS_PLAYER_TYPE:
$this->plugin->getServer()->dispatchCommand($player, $command);
break;
}
}

/**
* @return mixed
*/
public function getOriginalCommand(){
return $this->originalCommand;
}

/**
* @return null
*/
public function getCompiledCommand(){
return $this->compiledCommand;
}

}
Loading

1 comment on commit e9dc9fd

@MrGamerPro
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks for the quick fix!

Please sign in to comment.