Added Event allocation pool, updated SPL with Class::onClassLoaded()

This commit is contained in:
Shoghi Cervantes
2014-10-28 10:47:40 +01:00
parent 144a871c07
commit 350cee3d41
111 changed files with 327 additions and 109 deletions

View File

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