Automatically set Entity / Tile entity save identifiers

This commit is contained in:
Shoghi Cervantes 2014-11-04 12:04:08 +01:00
parent a5369b3570
commit 8fd6582e74
31 changed files with 49 additions and 74 deletions

View File

@ -794,7 +794,7 @@ class Block extends Position implements Metadatable{
/** /**
* @return int * @return int
*/ */
final public function getID(){ final public function getId(){
return $this->id; return $this->id;
} }

View File

@ -45,12 +45,6 @@ class Arrow extends Projectile{
parent::__construct($chunk, $nbt); parent::__construct($chunk, $nbt);
} }
protected function initEntity(){
$this->namedtag->id = new String("id", "Arrow");
parent::initEntity();
}
public function onUpdate($currentTick){ public function onUpdate($currentTick){
if($this->closed){ if($this->closed){
return false; return false;

View File

@ -26,7 +26,4 @@ use pocketmine\nbt\tag\String;
class Chicken extends Animal{ class Chicken extends Animal{
protected function initEntity(){
$this->namedtag->id = new String("id", "Chicken");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Cow extends Animal{ class Cow extends Animal{
protected function initEntity(){
$this->namedtag->id = new String("id", "Cow");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Creeper extends Monster implements Explosive{ class Creeper extends Monster implements Explosive{
protected function initEntity(){
$this->namedtag->id = new String("id", "Creeper");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Egg extends Projectile{ class Egg extends Projectile{
protected function initEntity(){
$this->namedtag->id = new String("id", "Egg");
}
} }

View File

@ -26,7 +26,5 @@ use pocketmine\inventory\InventoryHolder;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Enderman extends Monster implements InventoryHolder{ class Enderman extends Monster implements InventoryHolder{
protected function initEntity(){
$this->namedtag->id = new String("id", "Enderman");
}
} }

View File

@ -49,6 +49,7 @@ use pocketmine\nbt\tag\Double;
use pocketmine\nbt\tag\Enum; use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Float; use pocketmine\nbt\tag\Float;
use pocketmine\nbt\tag\Short; use pocketmine\nbt\tag\Short;
use pocketmine\nbt\tag\String;
use pocketmine\Network; use pocketmine\Network;
use pocketmine\network\protocol\MoveEntityPacket; use pocketmine\network\protocol\MoveEntityPacket;
use pocketmine\network\protocol\MovePlayerPacket; use pocketmine\network\protocol\MovePlayerPacket;
@ -69,6 +70,7 @@ abstract class Entity extends Location implements Metadatable{
public static $entityCount = 1; public static $entityCount = 1;
/** @var Entity[] */ /** @var Entity[] */
private static $knownEntities = []; private static $knownEntities = [];
private static $shortNames = [];
/** /**
* @var Player[] * @var Player[]
@ -250,13 +252,27 @@ abstract class Entity extends Location implements Metadatable{
} }
self::$knownEntities[$class->getShortName()] = $className; self::$knownEntities[$class->getShortName()] = $className;
self::$shortNames[$className] = $class->getShortName();
return true; return true;
} }
return false; return false;
} }
/**
* Returns the short save name
*
* @return string
*/
public function getSaveId(){
return self::$shortNames[static::class];
}
public function saveNBT(){ public function saveNBT(){
if(!($this instanceof Player)){
$this->namedtag->id = new String("id", $this->getSaveId());
}
$this->namedtag->Pos = new Enum("Pos", [ $this->namedtag->Pos = new Enum("Pos", [
new Double(0, $this->x), new Double(0, $this->x),
new Double(1, $this->y), new Double(1, $this->y),

View File

@ -50,7 +50,6 @@ class FallingSand extends Entity{
public $canCollide = false; public $canCollide = false;
protected function initEntity(){ protected function initEntity(){
$this->namedtag->id = new String("id", "FallingSand");
if(isset($this->namedtag->TileID)){ if(isset($this->namedtag->TileID)){
$this->blockId = $this->namedtag["TileID"]; $this->blockId = $this->namedtag["TileID"];
}elseif(isset($this->namedtag->Tile)){ }elseif(isset($this->namedtag->Tile)){

View File

@ -53,7 +53,6 @@ class Item extends Entity{
public $canCollide = false; public $canCollide = false;
protected function initEntity(){ protected function initEntity(){
$this->namedtag->id = new String("id", "Item");
$this->setMaxHealth(5); $this->setMaxHealth(5);
$this->setHealth($this->namedtag["Health"]); $this->setHealth($this->namedtag["Health"]);
if(isset($this->namedtag->Age)){ if(isset($this->namedtag->Age)){

View File

@ -24,8 +24,6 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Ocelot extends Animal implements Tameable{ class Ozelot extends Animal implements Tameable{
protected function initEntity(){
$this->namedtag->id = new String("id", "Ozelot");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Painting extends Hanging{ class Painting extends Hanging{
protected function initEntity(){
$this->namedtag->id = new String("id", "Painting");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Pig extends Animal implements Rideable{ class Pig extends Animal implements Rideable{
protected function initEntity(){
$this->namedtag->id = new String("id", "Pig");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class PigZombie extends Zombie{ class PigZombie extends Zombie{
protected function initEntity(){
$this->namedtag->id = new String("id", "PigZombie");
}
} }

View File

@ -47,7 +47,6 @@ class PrimedTNT extends Entity implements Explosive{
public $canCollide = false; public $canCollide = false;
protected function initEntity(){ protected function initEntity(){
$this->namedtag->id = new String("id", "PrimedTNT");
if(isset($this->namedtag->Fuse)){ if(isset($this->namedtag->Fuse)){
$this->fuse = $this->namedtag["Fuse"]; $this->fuse = $this->namedtag["Fuse"];
}else{ }else{

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Sheep extends Animal implements Colorable{ class Sheep extends Animal implements Colorable{
protected function initEntity(){
$this->namedtag->id = new String("id", "Sheep");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Silverfish extends Monster{ class Silverfish extends Monster{
protected function initEntity(){
$this->namedtag->id = new String("id", "Silverfish");
}
} }

View File

@ -24,7 +24,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Skeleton extends Monster implements ProjectileSource{ class Skeleton extends Monster implements ProjectileSource{
protected function initEntity(){
$this->namedtag->id = new String("id", "Skeleton");
}
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Slime extends Living{ class Slime extends Living{
protected function initEntity(){
$this->namedtag->id = new String("id", "Slime");
}
} }

View File

@ -63,11 +63,6 @@ class Snowball extends Projectile{
return $hasUpdate; return $hasUpdate;
} }
protected function initEntity(){
$this->namedtag->id = new String("id", "Snowball");
parent::initEntity();
}
public function spawnTo(Player $player){ public function spawnTo(Player $player){
$pk = new AddEntityPacket(); $pk = new AddEntityPacket();
$pk->type = Snowball::NETWORK_ID; $pk->type = Snowball::NETWORK_ID;

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Spider extends Monster{ class Spider extends Monster{
protected function initEntity(){
$this->namedtag->id = new String("id", "Spider");
}
} }

View File

@ -48,7 +48,6 @@ class Villager extends Creature implements NPC, Ageable{
protected function initEntity(){ protected function initEntity(){
parent::initEntity(); parent::initEntity();
$this->namedtag->id = new String("id", "Villager");
if(!isset($this->namedtag->Profession)){ if(!isset($this->namedtag->Profession)){
$this->setProfession(self::PROFESSION_GENERIC); $this->setProfession(self::PROFESSION_GENERIC);
} }

View File

@ -25,7 +25,5 @@ namespace pocketmine\entity;
use pocketmine\nbt\tag\String; use pocketmine\nbt\tag\String;
class Wolf extends Animal implements Tameable{ class Wolf extends Animal implements Tameable{
protected function initEntity(){
$this->namedtag->id = new String("id", "Wolf");
}
} }

View File

@ -36,10 +36,6 @@ class Zombie extends Monster{
public $length = 0.6; public $length = 0.6;
public $height = 1.8; public $height = 1.8;
protected function initEntity(){
$this->namedtag->id = new String("id", "Zombie");
}
public function getName(){ public function getName(){
return "Zombie"; return "Zombie";
} }

View File

@ -539,7 +539,7 @@ class Item{
} }
} }
final public function getID(){ final public function getId(){
return $this->id; return $this->id;
} }

View File

@ -307,7 +307,7 @@ class Level implements ChunkManager, Metadatable{
* *
* @return int * @return int
*/ */
final public function getID(){ final public function getId(){
return $this->levelId; return $this->levelId;
} }

View File

@ -66,7 +66,7 @@ class GenerationChunkManager implements ChunkManager{
/** /**
* @return int * @return int
*/ */
public function getID(){ public function getId(){
return $this->levelID; return $this->levelID;
} }

View File

@ -43,7 +43,6 @@ class Chest extends Spawnable implements InventoryHolder, Container{
protected $doubleInventory = null; protected $doubleInventory = null;
public function __construct(FullChunk $chunk, Compound $nbt){ public function __construct(FullChunk $chunk, Compound $nbt){
$nbt->id = new String("id", Tile::CHEST);
parent::__construct($chunk, $nbt); parent::__construct($chunk, $nbt);
$this->inventory = new ChestInventory($this); $this->inventory = new ChestInventory($this);

View File

@ -42,7 +42,6 @@ class Furnace extends Tile implements InventoryHolder, Container{
protected $inventory; protected $inventory;
public function __construct(FullChunk $chunk, Compound $nbt){ public function __construct(FullChunk $chunk, Compound $nbt){
$nbt->id = new String("id", Tile::FURNACE);
parent::__construct($chunk, $nbt); parent::__construct($chunk, $nbt);
$this->inventory = new FurnaceInventory($this); $this->inventory = new FurnaceInventory($this);

View File

@ -29,7 +29,6 @@ use pocketmine\nbt\tag\String;
class Sign extends Spawnable{ class Sign extends Spawnable{
public function __construct(FullChunk $chunk, Compound $nbt){ public function __construct(FullChunk $chunk, Compound $nbt){
$nbt->id = new String("id", Tile::SIGN);
if(!isset($nbt->Text1)){ if(!isset($nbt->Text1)){
$nbt->Text1 = new String("Text1", ""); $nbt->Text1 = new String("Text1", "");
} }

View File

@ -32,6 +32,7 @@ use pocketmine\level\Level;
use pocketmine\level\Position; use pocketmine\level\Position;
use pocketmine\nbt\tag\Compound; use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Int; use pocketmine\nbt\tag\Int;
use pocketmine\nbt\tag\String;
use pocketmine\utils\ChunkException; use pocketmine\utils\ChunkException;
abstract class Tile extends Position{ abstract class Tile extends Position{
@ -41,8 +42,8 @@ abstract class Tile extends Position{
public static $tileCount = 1; public static $tileCount = 1;
/** @var Tile[] */
private static $knownTiles = []; private static $knownTiles = [];
private static $shortNames = [];
/** @var Chunk */ /** @var Chunk */
public $chunk; public $chunk;
@ -88,12 +89,22 @@ abstract class Tile extends Position{
$class = new \ReflectionClass($className); $class = new \ReflectionClass($className);
if(is_a($className, Tile::class, true) and !$class->isAbstract()){ if(is_a($className, Tile::class, true) and !$class->isAbstract()){
self::$knownTiles[$class->getShortName()] = $className; self::$knownTiles[$class->getShortName()] = $className;
self::$shortNames[$className] = $class->getShortName();
return true; return true;
} }
return false; return false;
} }
/**
* Returns the short save name
*
* @return string
*/
public function getSaveId(){
return self::$shortNames[static::class];
}
public function __construct(FullChunk $chunk, Compound $nbt){ public function __construct(FullChunk $chunk, Compound $nbt){
if($chunk === null or $chunk->getProvider() === null){ if($chunk === null or $chunk->getProvider() === null){
throw new ChunkException("Invalid garbage Chunk given to Tile"); throw new ChunkException("Invalid garbage Chunk given to Tile");
@ -117,11 +128,12 @@ abstract class Tile extends Position{
$this->tickTimer = Timings::getTileEntityTimings($this); $this->tickTimer = Timings::getTileEntityTimings($this);
} }
public function getID(){ public function getId(){
return $this->id; return $this->id;
} }
public function saveNBT(){ public function saveNBT(){
$this->namedtag->id = new String("id", $this->getSaveId());
$this->namedtag->x = new Int("x", $this->x); $this->namedtag->x = new Int("x", $this->x);
$this->namedtag->y = new Int("y", $this->y); $this->namedtag->y = new Int("y", $this->y);
$this->namedtag->z = new Int("z", $this->z); $this->namedtag->z = new Int("z", $this->z);