Final structure with example events

This commit is contained in:
Shoghi Cervantes 2014-02-10 18:08:25 +01:00
parent a4cbb2f938
commit 4b14d5d900
12 changed files with 221 additions and 40 deletions

View File

@ -102,7 +102,6 @@ class ServerAPI{
//Init all the events
foreach(get_declared_classes() as $class){
if(is_subclass_of($class, "BaseEvent") and property_exists($class, "handlers") and property_exists($class, "handlerPriority")){
echo $class;
$class::unregisterAll();
}
}

View File

@ -28,8 +28,8 @@ abstract class BaseEvent{
/**
* Any callable event must declare the static variables
*
* private static $handlers;
* private static $handlerPriority;
* public static $handlers;
* public static $handlerPriority;
*
* Not doing so will deny the proper event initialization
*/
@ -59,6 +59,17 @@ abstract class BaseEvent{
$this->status = BaseEvent::ALLOW & ($forceAllow === true ? BaseEvent::FORCE : 0);
}
public function isCancelled(){
return ($this->status & 0x7FFFFFFF) === BaseEvent::DENY;
}
public function setCancelled($forceCancel = false){
if($this instanceof CancellableEvent){
$this->status = BaseEvent::DENY & ($forceCancel === true ? BaseEvent::FORCE : 0);
}
return false;
}
public function isNormal(){
return $this->status === BaseEvent::NORMAL;
}
@ -72,16 +83,16 @@ abstract class BaseEvent{
}
public static function getHandlerList(){
return self::$handlers;
return static::$handlers;
}
public static function getPriorityList(){
return self::$handlerPriority;
return static::$handlerPriority;
}
public static function unregisterAll(){
self::$handlers = array();
self::$handlerPriority = array();
static::$handlers = array();
static::$handlerPriority = array();
}
public function register(callable $handler, $priority = EventPriority::NORMAL){
@ -89,32 +100,32 @@ abstract class BaseEvent{
return false;
}
$identifier = Utils::getCallableIdentifier($handler);
if(isset(self::$handlers[$identifier])){ //Already registered
if(isset(static::$handlers[$identifier])){ //Already registered
return false;
}else{
self::$handlers[$identifier] = $handler;
if(!isset(self::$handlerPriority[(int) $priority])){
self::$handlerPriority[(int) $priority] = array();
static::$handlers[$identifier] = $handler;
if(!isset(static::$handlerPriority[(int) $priority])){
static::$handlerPriority[(int) $priority] = array();
}
self::$handlerPriority[(int) $priority][$identifier] = $handler;
static::$handlerPriority[(int) $priority][$identifier] = $handler;
return true;
}
}
public function unregister(callable $handler, $priority = EventPriority::NORMAL){
$identifier = Utils::getCallableIdentifier($handler);
if(isset(self::$handlers[$identifier])){
if(isset(self::$handlerPriority[(int) $priority][$identifier])){
unset(self::$handlerPriority[(int) $priority][$identifier]);
if(isset(static::$handlers[$identifier])){
if(isset(static::$handlerPriority[(int) $priority][$identifier])){
unset(static::$handlerPriority[(int) $priority][$identifier]);
}else{
for($priority = EventPriority::MONITOR; $priority <= EventPriority::LOWEST; ++$priority){
unset(self::$handlerPriority[$priority][$identifier]);
if(count(self::$handlerPriority[$priority]) === 0){
unset(self::$handlerPriority[$priority]);
unset(static::$handlerPriority[$priority][$identifier]);
if(count(static::$handlerPriority[$priority]) === 0){
unset(static::$handlerPriority[$priority]);
}
}
}
unset(self::$handlers[$identifier]);
unset(static::$handlers[$identifier]);
return true;
}else{
return false;

View File

@ -19,12 +19,10 @@
*
*/
abstract class CancellableEvent extends BaseEvent{
public function isCancelled(){
return ($this->status & 0x7FFFFFFF) === BaseEvent::DENY;
}
public function setCancelled($forceCancel = false){
$this->status = BaseEvent::DENY & ($forceCancel === true ? BaseEvent::FORCE : 0);
}
/**
* Events that can be cancelled must use the interface CancellableEvent
*/
interface CancellableEvent{
}

View File

@ -23,7 +23,7 @@ abstract class EventHandler{
public static function callEvent(BaseEvent $event){
$status = BaseEvent::NORMAL;
foreach($event::getPriorityList() as $priority => $handlerList){
foreach($event::$handlerPriority as $priority => $handlerList){
if(count($handlerList) > 0){
$event->setPrioritySlot($priority);
foreach($handlerList as $handler){

View File

@ -21,13 +21,9 @@
/**
* Plugins that create events must use one of these classes as the base
* Plugins that create events must use this class as the base
*/
abstract class PluginEvent extends BaseEvent{
}
abstract class CancellablePluginEvent extends CancellableEvent{
}

24
src/event/ServerEvent.php Normal file
View File

@ -0,0 +1,24 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
abstract class ServerEvent extends BaseEvent{
}

View File

@ -0,0 +1,36 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class RakNetPacketReceiveEvent extends ServerEvent implements CancellableEvent{
public static $handlers;
public static $handlerPriority;
private $packet;
public function __construct(RakNetPacket $packet){
$this->packet = $packet;
}
public function getPacket(){
return $this->packet;
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class RakNetPacketSendEvent extends ServerEvent implements CancellableEvent{
public static $handlers;
public static $handlerPriority;
private $packet;
public function __construct(RakNetPacket $packet){
$this->packet = $packet;
}
public function getPacket(){
return $this->packet;
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class UnknownPacketReceiveEvent extends ServerEvent implements CancellableEvent{
public static $handlers;
public static $handlerPriority;
private $packet;
public function __construct(Packet $packet){
$this->packet = $packet;
}
public function getPacket(){
return $this->packet;
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class UnknownPacketSendEvent extends ServerEvent implements CancellableEvent{
public static $handlers;
public static $handlerPriority;
private $packet;
public function __construct(Packet $packet){
$this->packet = $packet;
}
public function getPacket(){
return $this->packet;
}
}

View File

@ -60,7 +60,9 @@ class MinecraftInterface{
if($parser->packet !== false){
$parser->packet->ip = $source;
$parser->packet->port = $port;
return $parser->packet;
if(EventHandler::callEvent(new RakNetPacketReceiveEvent($parser->packet)) !== BaseEvent::DENY){
return $parser->packet;
}
}
return false;
}elseif($pid === 0xfe and $buffer{1} === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){
@ -74,16 +76,23 @@ class MinecraftInterface{
$packet->ip = $source;
$packet->port = $port;
$packet->buffer = $buffer;
if(ServerAPI::request()->api->dhandle("server.unknownpacket.$pid", $packet) !== true){
console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2);
}
EventHandler::callEvent(new UnknownPacketReceiveEvent($packet));
return false;
}
}
public function writePacket(Packet $packet){
if($packet instanceof RakNetPacket){
$codec = new RakNetCodec($packet);
if(EventHandler::callEvent(new RakNetPacketSendEvent($packet)) === BaseEvent::DENY){
return 0;
}
$codec = new RakNetCodec($packet);
}elseif($packet instanceof QueryPacket){
}else{
if(EventHandler::callEvent(new UnknownPacketSendEvent($packet)) === BaseEvent::DENY){
return 0;
}
}
$write = $this->socket->write($packet->buffer, $packet->ip, $packet->port);
$this->bandwidth[1] += $write;

View File

@ -25,7 +25,7 @@ class ClientHandshakePacket extends RakNetDataPacket{
public $port;
public $dataArray0;
public $dataArray;
public $timespamp;
public $timestamp;
public $session2;
public $session;
@ -39,7 +39,7 @@ class ClientHandshakePacket extends RakNetDataPacket{
$this->port = $this->getShort(true);
$this->dataArray0 = $this->get($this->getByte());
$this->dataArray = $this->getDataArray(9);
$this->timespamp = $this->get(2);
$this->timestamp = $this->get(2);
$this->session2 = $this->getLong();
$this->session = $this->getLong();
}