Added PlayerCreationEvent

This commit is contained in:
Shoghi Cervantes 2015-01-03 16:38:00 +01:00
parent 074c8b876d
commit 7ef8edccf4
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
3 changed files with 134 additions and 2 deletions

View File

@ -0,0 +1,127 @@
<?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/
*
*
*/
namespace pocketmine\event\player;
use pocketmine\event\Event;
use pocketmine\network\SourceInterface;
use pocketmine\Player;
/**
* Allows the creation of players overriding the base Player class
*/
class PlayerCreationEvent extends Event{
public static $handlerList = null;
public static $eventPool = [];
public static $nextEvent = 0;
/** @var SourceInterface */
private $interface;
/** @var mixed */
private $clientId;
/** @var string */
private $address;
/** @var int */
private $port;
/** @var Player::class */
private $baseClass;
/** @var Player::class */
private $playerClass;
/**
* @param SourceInterface $interface
* @param Player::class $baseClass
* @param Player::class $playerClass
* @param mixed $clientId
* @param string $address
* @param int $port
*/
public function __construct(SourceInterface $interface, $baseClass, $playerClass, $clientId, $address, $port){
$this->interface = $interface;
$this->clientId = $clientId;
$this->address = $address;
$this->port = $port;
if(!is_subclass_of($baseClass, Player::class, true)){
throw new \RuntimeException("Base class $baseClass must extend " . Player::class);
}
$this->baseClass = $baseClass;
if(!is_subclass_of($playerClass, Player::class, true)){
throw new \RuntimeException("Class $playerClass must extend " . Player::class);
}
$this->playerClass = $playerClass;
}
/**
* @return SourceInterface
*/
public function getInterface(){
return $this->interface;
}
/**
* @return string
*/
public function getAddress(){
return $this->address;
}
/**
* @return int
*/
public function getPort(){
return $this->port;
}
/**
* @return mixed
*/
public function getClientId(){
return $this->clientId;
}
/**
* @return Player::class
*/
public function getBaseClass(){
return $this->baseClass;
}
/**
* @return Player::class
*/
public function getPlayerClass(){
return $this->playerClass;
}
public function setPlayerClass($class){
if(!is_subclass_of($class, $this->baseClass, true)){
throw new \RuntimeException("Class $class must extend " . $this->baseClass);
}
$this->playerClass = $class;
}
}

View File

@ -24,6 +24,7 @@
*/
namespace pocketmine\network;
use pocketmine\event\player\PlayerCreationEvent;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\network\protocol\AddItemEntityPacket;
use pocketmine\network\protocol\AddMobPacket;
@ -167,7 +168,11 @@ class RakLibInterface implements ServerInstance, SourceInterface{
}
public function openSession($identifier, $address, $port, $clientID){
$player = new Player($this, null, $address, $port);
$ev = new PlayerCreationEvent($this, Player::class, Player::class, null, $address, $port);
$this->server->getPluginManager()->callEvent($ev);
$class = $ev->getPlayerClass();
$player = new $class($this, $ev->getClientId(), $ev->getAddress(), $ev->getPort());
$this->players[$identifier] = $player;
$this->identifiersACK[$identifier] = 0;
$this->identifiers->attach($player, $identifier);

@ -1 +1 @@
Subproject commit 658529ec44f30ce210f5cf42d2280e51332fe3d0
Subproject commit 7e1bc7d62b7ff024f8a468973af8d3ab71e63dcc