Skip to content

Commit d3117ce

Browse files
author
Falkirks
committed
Version 2.0.0
1 parent 3712351 commit d3117ce

File tree

5 files changed

+107
-19
lines changed

5 files changed

+107
-19
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: 1.0.4
3+
version: 2.0.0
44
author: Falk
55
api: [1.0.0]
66
load: POSTWORLD

resources/blocks.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# _____ _____ ______
2+
# |_ _| |_ _| | _ \
3+
# | | __ _ _ __ | | ___ | | | |___
4+
# | |/ _` | '_ \| |/ _ \| | | / _ \
5+
# | | (_| | |_) | | (_) | |/ / (_) |
6+
# \_/\__,_| .__/\_/\___/|___/ \___/
7+
# | |
8+
# |_| Execute commands on block tap!
9+
# ** When you add a blocks this annoying message will be removed (It's a feature)
10+
version: 1
11+
blocks: []

src/taptodo/Block.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function __construct(Position $position, array $commands, TapToDo $main,
1717
}
1818
public function addCommand($cmd){
1919
$this->cmd[] = $cmd;
20+
$this->plugin->saveBlock($this);
2021
}
2122
public function delCommand($cmd){
2223
$ret = false;
@@ -26,6 +27,9 @@ public function delCommand($cmd){
2627
$ret = true;
2728
}
2829
}
30+
if($ret){
31+
$this->plugin->saveBlock($this);
32+
}
2933
return $ret;
3034
}
3135
public function runCommands(Player $p){
@@ -34,19 +38,20 @@ public function runCommands(Player $p){
3438
$c = str_replace("%x", $p->getX(), $c);
3539
$c = str_replace("%y", $p->getY(), $c);
3640
$c = str_replace("%z", $p->getZ(), $c);
37-
if (strpos($c, "%safe") !== false) {
38-
$c = str_replace("%safe", "", $c);
39-
$this->plugin->getServer()->dispatchCommand($p, $c);
41+
42+
if (strpos($c, "%pow") !== false && ($c = str_replace("%pow", "", $c))) {
43+
$this->plugin->getServer()->dispatchCommand(new ConsoleCommandSender(), $c);
4044
}
41-
elseif(strpos($c, "%op") !== false){
42-
$c = str_replace("%op", "", $c);
45+
elseif(strpos($c, "%op") !== false && ($c = str_replace("%op", "", $c)) && !$p->isOp()){
4346
$p->setOp(true);
4447
$this->plugin->getServer()->dispatchCommand($p, $c);
4548
$p->setOp(false);
4649
}
4750
else{
48-
$this->plugin->getServer()->dispatchCommand(new ConsoleCommandSender(), $c);
51+
$c = str_replace("%safe", "", $c); //Partial backwards compatibility
52+
$this->plugin->getServer()->dispatchCommand($p, $c);
4953
}
54+
$this->plugin->getLogger()->info($c);
5055
}
5156
}
5257
public function nameBlock($name){
@@ -63,11 +68,11 @@ public function getPos(){
6368
}
6469
public function toArray(){
6570
$arr = array(
66-
"x" => $this->getPos()->getX(),
67-
"y" => $this->getPos()->getY(),
68-
"z" => $this->getPos()->getZ(),
69-
"level" => $this->getPos()->getLevel()->getName(),
70-
"commands" => $this->getCommands());
71+
'x' => $this->getPos()->getX(),
72+
'y' => $this->getPos()->getY(),
73+
'z' => $this->getPos()->getZ(),
74+
'level' => $this->getPos()->getLevel()->getName(),
75+
'commands' => $this->getCommands());
7176
if($this->name !== false) $arr["name"] = $this->name;
7277
return $arr;
7378
}

src/taptodo/ConfigUpdater.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace taptodo;
3+
4+
use pocketmine\utils\Config;
5+
6+
class ConfigUpdater{
7+
/** @var Config */
8+
private $config;
9+
/** @var TapToDo */
10+
private $tapToDo;
11+
const CONFIG_VERSION = 1;
12+
public function __construct(Config $config, TapToDo $tapToDo){
13+
$this->config = $config;
14+
$this->tapToDo = $tapToDo;
15+
$this->version = ($this->config->get("version") === false ? 0 : $this->config->get("version"));
16+
}
17+
public function checkConfig(){
18+
if($this->version > ConfigUpdater::CONFIG_VERSION){
19+
$this->tapToDo->getLogger()->warning("The config loaded is not supported. It may not function correctly. ");
20+
}
21+
while($this->version < ConfigUpdater::CONFIG_VERSION){
22+
switch($this->version){
23+
case 0:
24+
$this->tapToDo->getLogger()->info("Updating config from version 0 to 1...");
25+
$blocks = $this->config->getAll();
26+
foreach($blocks as $id => $block){
27+
foreach($block["commands"] as $i => $command){
28+
if(strpos($command, "%safe") === false && strpos($command, "%op") === false){
29+
$command .= "%pow";
30+
}
31+
$block["commands"][$i] = str_replace("%safe", "", $command);
32+
}
33+
$blocks[$id] = $block;
34+
}
35+
unlink($this->tapToDo->getDataFolder() . "blocks.yml");
36+
$this->tapToDo->saveResource("blocks.yml");
37+
$this->config = new Config($this->tapToDo->getDataFolder() . "blocks.yml", Config::YAML);
38+
$this->config->set("version", 1);
39+
$this->config->set("blocks", $blocks);
40+
$this->config->save();
41+
$this->version = 1;
42+
break;
43+
}
44+
}
45+
return $this->config;
46+
}
47+
}

src/taptodo/TapToDo.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
use pocketmine\utils\Config;
1313

1414
class TapToDo extends PluginBase implements CommandExecutor, Listener{
15-
public $s, $b, $config;
15+
public $s;
16+
/** @var Config */
17+
public $config;
18+
public $b;
1619
public function onEnable(){
1720
@mkdir($this->getDataFolder());
1821
$this->s = [];
22+
$this->b = [];
23+
$this->saveResource("blocks.yml");
1924
$this->config = new Config($this->getDataFolder() . "blocks.yml", Config::YAML, array());
25+
$this->config = (new ConfigUpdater($this->config, $this))->checkConfig();
2026
$this->getServer()->getPluginManager()->registerEvents($this, $this);
2127
$this->parseBlockData();
2228
}
@@ -90,8 +96,8 @@ public function onCommand(CommandSender $sender, Command $cmd, $label, array $ar
9096
}
9197
else{
9298
if($sender instanceof Player){
93-
if(isset($args[1])){
94-
if($sender->hasPermission("taptodo.command." . $args[1])){
99+
if(isset($args[0])){
100+
if($sender->hasPermission("taptodo.command." . $args[0])){
95101
$this->s[$sender->getName()] = $args;
96102
$sender->sendMessage("Tap a block to complete action...");
97103
return true;
@@ -208,7 +214,7 @@ public function getBlock($x, $y, $z, $level){
208214
}
209215
public function parseBlockData(){
210216
$this->b = [];
211-
foreach($this->config->getAll() as $i => $block){
217+
foreach($this->config->get("blocks") as $i => $block){
212218
if($this->getServer()->isLevelLoaded($block["level"])){
213219
$pos = new Position($block["x"], $block["y"], $block["z"], $this->getServer()->getLevelByName($block["level"]));
214220
if(isset($block["name"])) $this->b[$pos->__toString()] = new Block($pos, $block["commands"], $this->config, $block["name"]);
@@ -220,19 +226,23 @@ public function parseBlockData(){
220226
}
221227
}
222228
public function removeBlock(Block $block){
223-
$this->config->remove($block->id);
229+
$blocks = $this->config->get("blocks");
230+
unset($blocks[$block->id]);
231+
$this->config->set("blocks", $blocks);
224232
$this->config->save();
225233
$this->parseBlockData();
226234
}
227235
public function addBlock(Position $p, $cmd){
228-
$block = new Block(new Position($p->getX(), $p->getY(), $p->getZ(), $p->getLevel()), [$cmd], $this, count($this->config->getAll()));
236+
$block = new Block(new Position($p->getX(), $p->getY(), $p->getZ(), $p->getLevel()), [$cmd], $this, count($this->config->get("blocks")));
229237
$this->saveBlock($block);
230238
$this->config->save();
231239
return $block;
232240
}
233241
public function saveBlock(Block $block){
234242
$this->b[$block->getPos()->getX() . ":" . $block->getPos()->getY() . ":" . $block->getPos()->getZ() . ":" . $block->getPos()->getLevel()->getName()] = $block;
235-
$this->config->set($block->id, $block->toArray());
243+
$blocks = $this->config->get("blocks");
244+
$blocks[$block->id] = $block->toArray();
245+
$this->config->set("blocks", $blocks);
236246
}
237247
public function onDisable(){
238248
$this->getLogger()->info("Saving blocks...");
@@ -241,4 +251,19 @@ public function onDisable(){
241251
}
242252
$this->config->save();
243253
}
254+
255+
/**
256+
* @return mixed
257+
*/
258+
public function getConfig(){
259+
return $this->config;
260+
}
261+
262+
/**
263+
* @param mixed $config
264+
*/
265+
public function setConfig($config){
266+
$this->config = $config;
267+
}
268+
244269
}

0 commit comments

Comments
 (0)