diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 2126e4dca..9aa7c7289 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -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(); } } diff --git a/src/event/BaseEvent.php b/src/event/BaseEvent.php index e0744ea11..fb65432f7 100644 --- a/src/event/BaseEvent.php +++ b/src/event/BaseEvent.php @@ -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; diff --git a/src/event/CancellableEvent.php b/src/event/CancellableEvent.php index affb4132a..11b64cb22 100644 --- a/src/event/CancellableEvent.php +++ b/src/event/CancellableEvent.php @@ -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{ + } \ No newline at end of file diff --git a/src/event/EventHandler.php b/src/event/EventHandler.php index 9a2cf5019..7498cfaff 100644 --- a/src/event/EventHandler.php +++ b/src/event/EventHandler.php @@ -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){ diff --git a/src/event/PluginEvent.php b/src/event/PluginEvent.php index c98573f80..d7a483be3 100644 --- a/src/event/PluginEvent.php +++ b/src/event/PluginEvent.php @@ -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{ - } \ No newline at end of file diff --git a/src/event/ServerEvent.php b/src/event/ServerEvent.php new file mode 100644 index 000000000..f99355828 --- /dev/null +++ b/src/event/ServerEvent.php @@ -0,0 +1,24 @@ +packet = $packet; + } + + public function getPacket(){ + return $this->packet; + } +} \ No newline at end of file diff --git a/src/event/server/RakNetPacketSendEvent.php b/src/event/server/RakNetPacketSendEvent.php new file mode 100644 index 000000000..ae3048087 --- /dev/null +++ b/src/event/server/RakNetPacketSendEvent.php @@ -0,0 +1,36 @@ +packet = $packet; + } + + public function getPacket(){ + return $this->packet; + } +} \ No newline at end of file diff --git a/src/event/server/UnknownPacketReceiveEvent.php b/src/event/server/UnknownPacketReceiveEvent.php new file mode 100644 index 000000000..322c6807f --- /dev/null +++ b/src/event/server/UnknownPacketReceiveEvent.php @@ -0,0 +1,36 @@ +packet = $packet; + } + + public function getPacket(){ + return $this->packet; + } +} \ No newline at end of file diff --git a/src/event/server/UnknownPacketSendEvent.php b/src/event/server/UnknownPacketSendEvent.php new file mode 100644 index 000000000..31820d881 --- /dev/null +++ b/src/event/server/UnknownPacketSendEvent.php @@ -0,0 +1,36 @@ +packet = $packet; + } + + public function getPacket(){ + return $this->packet; + } +} \ No newline at end of file diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index dfa9c6b89..1cd0cea6b 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -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; diff --git a/src/network/protocol/packet/ClientHandshakePacket.php b/src/network/protocol/packet/ClientHandshakePacket.php index 2cac9e8d4..b718ebfc8 100644 --- a/src/network/protocol/packet/ClientHandshakePacket.php +++ b/src/network/protocol/packet/ClientHandshakePacket.php @@ -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(); }