From 504b37cfaaa59b2d1641575909ab17c44536b333 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Tue, 25 Dec 2012 20:15:08 +0100 Subject: [PATCH] Posibility of changing data for handlers, and canceling it --- classes/API/BlockAPI.php | 6 ++-- classes/API/EntityAPI.php | 4 +-- classes/API/PluginAPI.php | 4 +-- classes/Entity.class.php | 2 +- classes/Player.class.php | 11 +++--- classes/PocketMinecraftServer.class.php | 21 +++++++----- example/ReactorAsWater.php | 45 +++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 example/ReactorAsWater.php diff --git a/classes/API/BlockAPI.php b/classes/API/BlockAPI.php index 2364c3d61..64ed9632b 100644 --- a/classes/API/BlockAPI.php +++ b/classes/API/BlockAPI.php @@ -39,9 +39,9 @@ class BlockAPI{ } public function init(){ - $this->server->addHandler("world.block.update", array($this, "updateBlockRemote")); - $this->server->addHandler("player.block.break", array($this, "blockBreak")); - $this->server->addHandler("player.block.action", array($this, "blockAction")); + $this->server->addHandler("world.block.update", array($this, "updateBlockRemote"), 1); + $this->server->addHandler("player.block.break", array($this, "blockBreak"), 1); + $this->server->addHandler("player.block.action", array($this, "blockAction"), 1); } public function blockBreak($data, $event){ diff --git a/classes/API/EntityAPI.php b/classes/API/EntityAPI.php index 1a06adb44..1b8372c34 100644 --- a/classes/API/EntityAPI.php +++ b/classes/API/EntityAPI.php @@ -29,7 +29,7 @@ class EntityAPI{ private $server; function __construct($server){ $this->server = $server; - $this->server->addHandler("onPlayerDeath", array($this, "handle"), 1); + $this->server->addHandler("player.death", array($this, "handle"), 1); } public function init(){ @@ -38,7 +38,7 @@ class EntityAPI{ public function handle($data, $event){ switch($event){ - case "onPlayerDeath": + case "player.death": $message = $data["name"]; if(is_numeric($data["cause"]) and isset($this->entities[$data["cause"]])){ $e = $this->api->entity->get($data["cause"]); diff --git a/classes/API/PluginAPI.php b/classes/API/PluginAPI.php index fc627a781..a5f8f8054 100644 --- a/classes/API/PluginAPI.php +++ b/classes/API/PluginAPI.php @@ -57,10 +57,10 @@ class PluginAPI extends stdClass{ } $info[$i] = $v; } - if(!isset($info["name"]) or !isset($info["version"]) or !isset($info["class"])){ + if(!isset($info["name"]) or !isset($info["version"]) or !isset($info["class"]) or !isset($info["author"])){ console("[ERROR] [PluginAPI] Failed parsing of ".basename($file)); } - console("[INFO] [PluginAPI] Loading plugin \"".$info["name"]."\" ".$info["version"]); + console("[INFO] [PluginAPI] Loading plugin \"".$info["name"]."\" ".$info["version"]." by ".$info["author"]); if(eval($content) === false or !class_exists($info["class"])){ console("[ERROR] [PluginAPI] Failed loading plugin"); } diff --git a/classes/Entity.class.php b/classes/Entity.class.php index 01d7ca431..8c50069a7 100644 --- a/classes/Entity.class.php +++ b/classes/Entity.class.php @@ -220,7 +220,7 @@ class Entity extends stdClass{ if($this->health <= 0 and $this->dead === false){ $this->dead = true; if($this->player !== false){ - $this->server->handle("onPlayerDeath", array("name" => $this->name, "cause" => $cause)); + $this->server->handle("player.death", array("name" => $this->name, "cause" => $cause)); } }elseif($this->health > 0){ $this->dead = false; diff --git a/classes/Player.class.php b/classes/Player.class.php index c0940d5d7..7bc3264e0 100644 --- a/classes/Player.class.php +++ b/classes/Player.class.php @@ -300,12 +300,13 @@ class Player{ $this->server->trigger("entity.move", $this->eid); } break; - case MC_PLAYER_EQUIPMENT: - console("[DEBUG] EID ".$this->eid." has now ".$data["block"].":".$data["meta"]." in their hands!", true, true, 2); + case MC_PLAYER_EQUIPMENT: $data["eid"] = $this->eid; - $this->equipment[0] = $data["block"]; - $this->equipment[1] = $data["meta"]; - $this->server->trigger("player.equipment.change", $data); + if($this->server->handle("player.equipment.change", $data) !== false){ + $this->equipment[0] = $data["block"]; + $this->equipment[1] = $data["meta"]; + console("[DEBUG] EID ".$this->eid." has now ".$data["block"].":".$data["meta"]." in their hands!", true, true, 2); + } break; case MC_REQUEST_CHUNK: $this->actionQueue(' diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 14ce17375..faf103a40 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -192,20 +192,25 @@ class PocketMinecraftServer extends stdClass{ return $this->handCnt++; } - public function handle($event, $data){ + public function handle($event, &$data){ $this->preparedSQL->selectHandlers->reset(); $this->preparedSQL->selectHandlers->clear(); $this->preparedSQL->selectHandlers->bindValue(":name", $event, SQLITE3_TEXT); $handlers = $this->preparedSQL->selectHandlers->execute(); - if($handlers === false or $handlers === true){ - return $this->trigger($event, $data); - } $result = true; - while(false !== ($hn = $handlers->fetchArray(SQLITE3_ASSOC)) and $result !== false){ - $hnid = (int) $hn["ID"]; - $result = call_user_func($this->handlers[$hnid], $data, $event); + if($handlers !== false and $handlers !== true){ + while(false !== ($hn = $handlers->fetchArray(SQLITE3_ASSOC)) and $result !== false){ + $handler = $this->handlers[(int) $hn["ID"]]; + if(is_array($handler)){ + $method = $handler[1]; + $result = $handler[0]->$method($data, $event); + }else{ + $result = $handler($data, $event); + } + } } - $handlers->finalize(); + $handlers->finalize(); + $this->trigger($event, $data); return $result; } diff --git a/example/ReactorAsWater.php b/example/ReactorAsWater.php new file mode 100644 index 000000000..9f95881cd --- /dev/null +++ b/example/ReactorAsWater.php @@ -0,0 +1,45 @@ +api = $api; + $this->server = $server; + } + + public function init(){ + $this->server->addHandler("player.block.action", array($this, "handle"), 15); //Priority higher that API + $this->server->addHandler("player.equipment.change", array($this, "handle")); + } + + + public function handle(&$data, $event){ + switch($event){ + case "player.equipment.change": + if($data["block"] === 247){ + $this->api->player->getByEID($data["eid"])->eventHandler("[ReactorAsWater] You'll place water with the Nether Reactor", "server.chat"); + $data["block"] = 9; + $data["meta"] = 0; + } + break; + case "player.block.action": + if($data["block"] === 247){ //nether reactor + $data["block"] = 9; //water source + $data["meta"] = 0; + } + break; + } + } + +} \ No newline at end of file