crodriguespt/SAMPHP — pull request #9 by BonhommeG (2014-12-27 15:10:13+00:00)
Title: Created a new alternative class for per-player events.

Created a new alternative class for per-player events.
The main difference from the standard core/modelevent.php is the ability to dynamically create and delete events with using variables.

But before using the need to remove declaration of old ModelEvent and delete some code starting with "$callbackNames = array();" until the end of file in core/player.php.

Example of usage:
```php
CMD::register('/test', function (Player $player) {
$player->keytest1 = PlayerEvent::create($player, 'KeyStateChange', function (Player $player, $keys) {
$player->sendClientMessage(-1, 'test1');
});
$player->keytest2 = PlayerEvent::create($player, 'KeyStateChange', function (Player $player, $keys) {
$player->sendClientMessage(-1, 'test2');
});
});

CMD::register('/dtest1', function (Player $player) {
$player->keytest1->destroy();
});

CMD::register('/dtest2', function (Player $player) {
$player->keytest2->destroy();
});
```

Create core/playerevent.php and insert code below and require this file in core/player.php:
```php
id][$event])) {
return static::$instances[$player->id][$event];
}
return null;
}

public static function create(Player $player, $event, $callback) {
$id = 0; while (isset(static::$instances[$player->id][$event][$id])) {
$id++;
}
return static::$instances[$player->id][$event][$id] = new static($id, $player, $event, $callback);
}

protected function __construct($id, $player, $event, $callback) {
$this->id = $id;
$this->player = $player;
$this->event = $event;
$this->callback = $callback;
}

public function fire() {
$args = func_get_args();
call_user_func_array($this->callback, $args);
}

public function destroy() {
unset(static::$instances[$this->player->id][$this->event][$this->id]);
}
}

foreach($callbackList as $callback) {
$prefix = "OnPlayer";
$prefix_len = strlen($prefix);

if(substr($callback, 0, $prefix_len) == $prefix) {
$intern = substr($callback, $prefix_len);
Event::on(substr($callback, 2), function($player) use($intern) {
if (($event = PlayerEvent::find($player, $intern))) {
$args = func_get_args();
foreach ($event as $callback) {
call_user_func_array(array($callback, 'fire'), $args);
}
}
});
}
}
```