New way to spawn entities/tiles using a global register table, allow overriding default entity/tile classes via classes

This commit is contained in:
Shoghi Cervantes
2014-10-28 13:09:27 +01:00
parent a5b85c549a
commit 34ae760def
18 changed files with 154 additions and 94 deletions

View File

@@ -62,9 +62,12 @@ use pocketmine\Server;
abstract class Entity extends Location implements Metadatable{
const NETWORK_ID = -1;
public static $entityCount = 1;
/** @var Entity[] */
private static $knownEntities = [];
/**
* @var Player[]
@@ -219,6 +222,39 @@ abstract class Entity extends Location implements Metadatable{
}
/**
* @param int|string $type
* @param FullChunk $chunk
* @param Compound $nbt
* @param $args
*
* @return Entity
*/
public static function createEntity($type, FullChunk $chunk, Compound $nbt, ...$args){
if(isset(self::$knownEntities[$type])){
$class = self::$knownEntities[$type];
return new $class($chunk, $nbt, ...$args);
}
return null;
}
public static function registerEntity($className, $force = false){
$class = new \ReflectionClass($className);
if(is_a($className, Entity::class, true) and !$class->isAbstract()){
if($className::NETWORK_ID !== -1){
self::$knownEntities[$className::NETWORK_ID] = $className;
}elseif(!$force){
return false;
}
self::$knownEntities[$class->getShortName()] = $className;
return true;
}
return false;
}
public function saveNBT(){
$this->namedtag->Pos = new Enum("Pos", [
new Double(0, $this->x),

View File

@@ -35,8 +35,7 @@ use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\network\protocol\SetEntityMotionPacket;
use pocketmine\Player;
class FallingBlock extends Entity{
class FallingSand extends Entity{
const NETWORK_ID = 66;
public $width = 0.98;
@@ -54,8 +53,7 @@ class FallingBlock extends Entity{
$this->namedtag->id = new String("id", "FallingSand");
if(isset($this->namedtag->TileID)){
$this->blockId = $this->namedtag["TileID"];
}
elseif(isset($this->namedtag->Tile)){
}elseif(isset($this->namedtag->Tile)){
$this->blockId = $this->namedtag["Tile"];
$this->namedtag["TileID"] = new Int("TileID", $this->blockId);
}
@@ -151,7 +149,7 @@ class FallingBlock extends Entity{
public function spawnTo(Player $player){
$pk = AddEntityPacket::getFromPool();
$pk->type = FallingBlock::NETWORK_ID;
$pk->type = FallingSand::NETWORK_ID;
$pk->eid = $this->getID();
$pk->x = $this->x;
$pk->y = $this->y;

View File

@@ -25,7 +25,7 @@ use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\event\entity\ItemDespawnEvent;
use pocketmine\event\entity\ItemSpawnEvent;
use pocketmine\item\Item;
use pocketmine\item\Item as ItemItem;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound;
@@ -35,12 +35,13 @@ use pocketmine\network\protocol\AddItemEntityPacket;
use pocketmine\network\protocol\SetEntityMotionPacket;
use pocketmine\Player;
class DroppedItem extends Entity{
class Item extends Entity{
const NETWORK_ID = 64;
protected $owner = null;
protected $thrower = null;
protected $pickupDelay = 0;
/** @var Item */
/** @var ItemItem */
protected $item;
public $width = 0.25;
@@ -67,7 +68,7 @@ class DroppedItem extends Entity{
if(isset($this->namedtag->Thrower)){
$this->thrower = $this->namedtag["Thrower"];
}
$this->item = Item::get($this->namedtag->Item["id"], $this->namedtag->Item["Damage"], $this->namedtag->Item["Count"]);
$this->item = ItemItem::get($this->namedtag->Item["id"], $this->namedtag->Item["Damage"], $this->namedtag->Item["Count"]);
$this->server->getPluginManager()->callEvent(ItemSpawnEvent::createEvent($this));
@@ -166,7 +167,7 @@ class DroppedItem extends Entity{
}
/**
* @return Item
* @return ItemItem
*/
public function getItem(){
return $this->item;