mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-01 07:39:57 +00:00
Final structure with example events
This commit is contained in:
parent
a4cbb2f938
commit
4b14d5d900
@ -102,7 +102,6 @@ class ServerAPI{
|
|||||||
//Init all the events
|
//Init all the events
|
||||||
foreach(get_declared_classes() as $class){
|
foreach(get_declared_classes() as $class){
|
||||||
if(is_subclass_of($class, "BaseEvent") and property_exists($class, "handlers") and property_exists($class, "handlerPriority")){
|
if(is_subclass_of($class, "BaseEvent") and property_exists($class, "handlers") and property_exists($class, "handlerPriority")){
|
||||||
echo $class;
|
|
||||||
$class::unregisterAll();
|
$class::unregisterAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,8 +28,8 @@ abstract class BaseEvent{
|
|||||||
/**
|
/**
|
||||||
* Any callable event must declare the static variables
|
* Any callable event must declare the static variables
|
||||||
*
|
*
|
||||||
* private static $handlers;
|
* public static $handlers;
|
||||||
* private static $handlerPriority;
|
* public static $handlerPriority;
|
||||||
*
|
*
|
||||||
* Not doing so will deny the proper event initialization
|
* 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);
|
$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(){
|
public function isNormal(){
|
||||||
return $this->status === BaseEvent::NORMAL;
|
return $this->status === BaseEvent::NORMAL;
|
||||||
}
|
}
|
||||||
@ -72,16 +83,16 @@ abstract class BaseEvent{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function getHandlerList(){
|
public static function getHandlerList(){
|
||||||
return self::$handlers;
|
return static::$handlers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getPriorityList(){
|
public static function getPriorityList(){
|
||||||
return self::$handlerPriority;
|
return static::$handlerPriority;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function unregisterAll(){
|
public static function unregisterAll(){
|
||||||
self::$handlers = array();
|
static::$handlers = array();
|
||||||
self::$handlerPriority = array();
|
static::$handlerPriority = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(callable $handler, $priority = EventPriority::NORMAL){
|
public function register(callable $handler, $priority = EventPriority::NORMAL){
|
||||||
@ -89,32 +100,32 @@ abstract class BaseEvent{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$identifier = Utils::getCallableIdentifier($handler);
|
$identifier = Utils::getCallableIdentifier($handler);
|
||||||
if(isset(self::$handlers[$identifier])){ //Already registered
|
if(isset(static::$handlers[$identifier])){ //Already registered
|
||||||
return false;
|
return false;
|
||||||
}else{
|
}else{
|
||||||
self::$handlers[$identifier] = $handler;
|
static::$handlers[$identifier] = $handler;
|
||||||
if(!isset(self::$handlerPriority[(int) $priority])){
|
if(!isset(static::$handlerPriority[(int) $priority])){
|
||||||
self::$handlerPriority[(int) $priority] = array();
|
static::$handlerPriority[(int) $priority] = array();
|
||||||
}
|
}
|
||||||
self::$handlerPriority[(int) $priority][$identifier] = $handler;
|
static::$handlerPriority[(int) $priority][$identifier] = $handler;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function unregister(callable $handler, $priority = EventPriority::NORMAL){
|
public function unregister(callable $handler, $priority = EventPriority::NORMAL){
|
||||||
$identifier = Utils::getCallableIdentifier($handler);
|
$identifier = Utils::getCallableIdentifier($handler);
|
||||||
if(isset(self::$handlers[$identifier])){
|
if(isset(static::$handlers[$identifier])){
|
||||||
if(isset(self::$handlerPriority[(int) $priority][$identifier])){
|
if(isset(static::$handlerPriority[(int) $priority][$identifier])){
|
||||||
unset(self::$handlerPriority[(int) $priority][$identifier]);
|
unset(static::$handlerPriority[(int) $priority][$identifier]);
|
||||||
}else{
|
}else{
|
||||||
for($priority = EventPriority::MONITOR; $priority <= EventPriority::LOWEST; ++$priority){
|
for($priority = EventPriority::MONITOR; $priority <= EventPriority::LOWEST; ++$priority){
|
||||||
unset(self::$handlerPriority[$priority][$identifier]);
|
unset(static::$handlerPriority[$priority][$identifier]);
|
||||||
if(count(self::$handlerPriority[$priority]) === 0){
|
if(count(static::$handlerPriority[$priority]) === 0){
|
||||||
unset(self::$handlerPriority[$priority]);
|
unset(static::$handlerPriority[$priority]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unset(self::$handlers[$identifier]);
|
unset(static::$handlers[$identifier]);
|
||||||
return true;
|
return true;
|
||||||
}else{
|
}else{
|
||||||
return false;
|
return false;
|
||||||
|
@ -19,12 +19,10 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
abstract class CancellableEvent extends BaseEvent{
|
/**
|
||||||
public function isCancelled(){
|
* Events that can be cancelled must use the interface CancellableEvent
|
||||||
return ($this->status & 0x7FFFFFFF) === BaseEvent::DENY;
|
*/
|
||||||
}
|
|
||||||
|
interface CancellableEvent{
|
||||||
|
|
||||||
public function setCancelled($forceCancel = false){
|
|
||||||
$this->status = BaseEvent::DENY & ($forceCancel === true ? BaseEvent::FORCE : 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -23,7 +23,7 @@ abstract class EventHandler{
|
|||||||
|
|
||||||
public static function callEvent(BaseEvent $event){
|
public static function callEvent(BaseEvent $event){
|
||||||
$status = BaseEvent::NORMAL;
|
$status = BaseEvent::NORMAL;
|
||||||
foreach($event::getPriorityList() as $priority => $handlerList){
|
foreach($event::$handlerPriority as $priority => $handlerList){
|
||||||
if(count($handlerList) > 0){
|
if(count($handlerList) > 0){
|
||||||
$event->setPrioritySlot($priority);
|
$event->setPrioritySlot($priority);
|
||||||
foreach($handlerList as $handler){
|
foreach($handlerList as $handler){
|
||||||
|
@ -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 PluginEvent extends BaseEvent{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class CancellablePluginEvent extends CancellableEvent{
|
|
||||||
|
|
||||||
}
|
|
24
src/event/ServerEvent.php
Normal file
24
src/event/ServerEvent.php
Normal 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{
|
||||||
|
|
||||||
|
}
|
36
src/event/server/RakNetPacketReceiveEvent.php
Normal file
36
src/event/server/RakNetPacketReceiveEvent.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
36
src/event/server/RakNetPacketSendEvent.php
Normal file
36
src/event/server/RakNetPacketSendEvent.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
36
src/event/server/UnknownPacketReceiveEvent.php
Normal file
36
src/event/server/UnknownPacketReceiveEvent.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
36
src/event/server/UnknownPacketSendEvent.php
Normal file
36
src/event/server/UnknownPacketSendEvent.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -60,8 +60,10 @@ class MinecraftInterface{
|
|||||||
if($parser->packet !== false){
|
if($parser->packet !== false){
|
||||||
$parser->packet->ip = $source;
|
$parser->packet->ip = $source;
|
||||||
$parser->packet->port = $port;
|
$parser->packet->port = $port;
|
||||||
|
if(EventHandler::callEvent(new RakNetPacketReceiveEvent($parser->packet)) !== BaseEvent::DENY){
|
||||||
return $parser->packet;
|
return $parser->packet;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}elseif($pid === 0xfe and $buffer{1} === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){
|
}elseif($pid === 0xfe and $buffer{1} === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){
|
||||||
$packet = new QueryPacket;
|
$packet = new QueryPacket;
|
||||||
@ -74,16 +76,23 @@ class MinecraftInterface{
|
|||||||
$packet->ip = $source;
|
$packet->ip = $source;
|
||||||
$packet->port = $port;
|
$packet->port = $port;
|
||||||
$packet->buffer = $buffer;
|
$packet->buffer = $buffer;
|
||||||
if(ServerAPI::request()->api->dhandle("server.unknownpacket.$pid", $packet) !== true){
|
EventHandler::callEvent(new UnknownPacketReceiveEvent($packet));
|
||||||
console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2);
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function writePacket(Packet $packet){
|
public function writePacket(Packet $packet){
|
||||||
if($packet instanceof RakNetPacket){
|
if($packet instanceof RakNetPacket){
|
||||||
|
if(EventHandler::callEvent(new RakNetPacketSendEvent($packet)) === BaseEvent::DENY){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
$codec = new RakNetCodec($packet);
|
$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);
|
$write = $this->socket->write($packet->buffer, $packet->ip, $packet->port);
|
||||||
$this->bandwidth[1] += $write;
|
$this->bandwidth[1] += $write;
|
||||||
|
@ -25,7 +25,7 @@ class ClientHandshakePacket extends RakNetDataPacket{
|
|||||||
public $port;
|
public $port;
|
||||||
public $dataArray0;
|
public $dataArray0;
|
||||||
public $dataArray;
|
public $dataArray;
|
||||||
public $timespamp;
|
public $timestamp;
|
||||||
public $session2;
|
public $session2;
|
||||||
public $session;
|
public $session;
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ class ClientHandshakePacket extends RakNetDataPacket{
|
|||||||
$this->port = $this->getShort(true);
|
$this->port = $this->getShort(true);
|
||||||
$this->dataArray0 = $this->get($this->getByte());
|
$this->dataArray0 = $this->get($this->getByte());
|
||||||
$this->dataArray = $this->getDataArray(9);
|
$this->dataArray = $this->getDataArray(9);
|
||||||
$this->timespamp = $this->get(2);
|
$this->timestamp = $this->get(2);
|
||||||
$this->session2 = $this->getLong();
|
$this->session2 = $this->getLong();
|
||||||
$this->session = $this->getLong();
|
$this->session = $this->getLong();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user