Posibility of changing data for handlers, and canceling it

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-25 20:15:08 +01:00
parent bf24a937b2
commit 504b37cfaa
7 changed files with 72 additions and 21 deletions

View File

@ -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){

View File

@ -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"]);

View File

@ -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");
}

View File

@ -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;

View File

@ -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('

View File

@ -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;
}

View File

@ -0,0 +1,45 @@
<?php
/*
__PocketMine Plugin__
name=Reactor As Water
description=Replaces the Nether Reactor with Water
version=0.0.1
author=shoghicp
class=ReactorAsWater
api=false
*/
class ReactorAsWater{
private $api, $server;
public function __construct($api, $server = false){
$this->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;
}
}
}