mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-04 17:06:16 +00:00
Added Event allocation pool, updated SPL with Class::onClassLoaded()
This commit is contained in:
@ -24,16 +24,27 @@
|
||||
*/
|
||||
namespace pocketmine\event;
|
||||
|
||||
use pocketmine\plugin\PluginManager;
|
||||
|
||||
abstract class Event{
|
||||
|
||||
/**
|
||||
* Any callable event must declare the static variable
|
||||
*
|
||||
* public static $handlerList = null;
|
||||
* public static $eventPool = [];
|
||||
* public static $nextEvent = 0;
|
||||
*
|
||||
* Not doing so will deny the proper event initialization
|
||||
*/
|
||||
|
||||
/** @var Event[] */
|
||||
public static $eventPool = [];
|
||||
public static $nextEvent = 0;
|
||||
|
||||
/** @var Event[] */
|
||||
private static $knownEvents = [];
|
||||
|
||||
protected $eventName = null;
|
||||
private $isCancelled = false;
|
||||
|
||||
@ -41,7 +52,7 @@ abstract class Event{
|
||||
* @return string
|
||||
*/
|
||||
final public function getEventName(){
|
||||
return $this->eventName !== null ? get_class($this) : $this->eventName;
|
||||
return $this->eventName !== null ? static::class : $this->eventName;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,4 +96,40 @@ abstract class Event{
|
||||
return static::$handlerList;
|
||||
}
|
||||
|
||||
public static function clearEventPool(){
|
||||
static::$nextEvent = 0;
|
||||
}
|
||||
|
||||
public static function clearAllPools(){
|
||||
foreach(self::$knownEvents as $event){
|
||||
$event::clearEventPool();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function createEvent(...$params){
|
||||
if(static::$nextEvent >= count(static::$eventPool)){
|
||||
static::$eventPool[] = new static(...$params);
|
||||
return static::$eventPool[static::$nextEvent++];
|
||||
}
|
||||
$ev = static::$eventPool[static::$nextEvent++];
|
||||
$ev->__construct(...$params);
|
||||
if($ev instanceof Cancellable){
|
||||
$ev->setCancelled(false);
|
||||
}
|
||||
return $ev;
|
||||
}
|
||||
|
||||
public static function onClassLoaded(){
|
||||
self::$knownEvents[static::class] = static::class;
|
||||
}
|
||||
|
||||
public static function getKnownEvents(){
|
||||
return self::$knownEvents;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user