diff --git a/src/Player.php b/src/Player.php index 42631b0a..17f754b3 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1312,17 +1312,17 @@ class Player{ return; } if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){ - if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){ - $pk = new LoginStatusPacket; - $pk->status = 1; - $this->directDataPacket($pk); - }else{ - $pk = new LoginStatusPacket; - $pk->status = 2; - $this->directDataPacket($pk); - } - $this->close("Incorrect protocol #" . $packet->protocol1, false); - return; + if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){ + $pk = new LoginStatusPacket; + $pk->status = 1; + $this->directDataPacket($pk); + }else{ + $pk = new LoginStatusPacket; + $pk->status = 2; + $this->directDataPacket($pk); + } + $this->close("Incorrect protocol #" . $packet->protocol1, false); + return; } if(preg_match('#[^a-zA-Z0-9_]#', $this->username) > 0 or $this->username === "" or $this->iusername === "rcon" or $this->iusername === "console" or $this->iusername === "server"){ $this->close("Bad username", false); diff --git a/src/entity/Animal.php b/src/entity/Animal.php index 701eaddb..a761a1f4 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -27,6 +27,13 @@ abstract class Animal extends Creature implements Ageable, Breedable{ return $d; } + public function createSaveData(){ + $data = parent::createSaveData(); + $data["IsBaby"] = $this->isBaby(); + return $data; + + } + public function isInLove(){ return $this->inLove > 0; } diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 57c69e48..cb3989ff 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -19,6 +19,12 @@ abstract class Creature extends Living{ } } + public function createSaveData(){ + $data = parent::createSaveData(); + $data["State"] = @$this->getState(); + return $data; + } + public function getSpeedModifer(){ return $this->speedModifer * ($this->inPanic ? 1.4 : 0.7); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 49732c58..33150754 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -31,7 +31,8 @@ class Entity extends Position public $speedY; public $speedZ; public $speed; - public $last = array(0, 0, 0, 0, 0, 0); + public $lastX = 0, $lastY = 0, $lastZ = 0, $lastYaw = 0, $lastPitch = 0, $lastTime = 0; + public $last; public $yaw; public $pitch; public $dead; @@ -65,8 +66,9 @@ class Entity extends Position function __construct(Level $level, $eid, $class, $type = 0, $data = array()) { $this->random = new Random(); + $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables $this->canBeAttacked = false; - $this->hasKnockback = false; + $this->hasKnockback = false; $this->level = $level; $this->speedModifer = 0.7; $this->fallY = false; @@ -80,7 +82,7 @@ class Entity extends Position $this->data = $data; $this->status = 0; $this->health = 20; - $this->hasGravity = false; + $this->hasGravity = false; $this->dmgcounter = array(0, 0, 0); $this->air = 300; $this->fire = 0; @@ -122,8 +124,8 @@ class Entity extends Position $this->speedModifer = 1; $this->width = 1.2; $this->height = 1.9; - $this->hasKnockback = true; - $this->hasGravity = true; + $this->hasKnockback = true; + $this->hasGravity = true; $this->canBeAttacked = true; break; case ENTITY_ITEM: @@ -134,7 +136,7 @@ class Entity extends Position $this->meta = (int) $this->data["meta"]; $this->stack = (int) $this->data["stack"]; } - $this->hasGravity = true; + $this->hasGravity = true; $this->setHealth(5, "generic"); $this->server->schedule(6010, array( $this, @@ -147,7 +149,7 @@ class Entity extends Position $this->setHealth(PHP_INT_MAX, "generic"); $this->height = 0.98; $this->width = 0.98; - $this->hasGravity = true; + $this->hasGravity = true; break; case ENTITY_OBJECT: $this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x; @@ -1075,7 +1077,42 @@ class Entity extends Position unset($this->speedMeasure[key($this->speedMeasure)]); $this->speedMeasure[] = $this->speed; } - + /** + * @return array + */ + public function createSaveData(){ + $data = [ + "id" => $this->type, + "Health" => $this->health, + "Pos" => [ + 0 => $this->x, + 1 => $this->y, + 2 => $this->z, + ], + "Rotation" => [ + 0 => $this->yaw, + 1 => $this->pitch, + ], + + ]; + if($this->class === ENTITY_OBJECT){ + $data["TileX"] = $this->x; + $data["TileY"] = $this->y; + $data["TileZ"] = $this->z; + } + if($this->class === ENTITY_FALLING){ + $data["Tile"] = $this->data["Tile"]; + } + if($this->class === ENTITY_ITEM){ + $data["Item"] = [ + "id" => $this->type, + "Damage" => $this->meta, + "Count" => $this->stack, + ]; + } + return $data; + } + public function updateLast() { $this->last[0] = $this->x; diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 41ec3530..06b207fc 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -2,24 +2,32 @@ class Chicken extends Animal{ const TYPE = MOB_CHICKEN; + public $timeUntilEgg; function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); - $this->server->schedule(mt_rand(0,6000) + 6000, [$this, "dropAnEgg"]); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->setName('Chicken'); $this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7); $this->setSpeed(0.25); + $this->timeUntilEgg = mt_rand(0,6000) + 6000; $this->update(); } public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; } + + public function update(){ + if($this->timeUntilEgg-- <= 0){ + $this->dropAnEgg(); + $this->timeUntilEgg = mt_rand(0,6000) + 6000; + } + } + public function dropAnEgg(){ if($this->closed){ return; } ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(EGG, 0, 1)); - $this->server->schedule(mt_rand(0,6000) + 6000, [$this, "dropAnEgg"]); } public function getDrops(){ diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 5930c2ab..2a4c03f2 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -18,6 +18,13 @@ class Sheep extends Animal{ } } + public function createSaveData(){ + $data = parent::createSaveData(); + $data["Color"] = @$this->data["Color"]; + $data["Sheared"] = @$this->data["Sheared"]; + return $data; + } + public function eatGrass(){ $downBlock = $this->level->getBlock(new Vector3($this->x, $this->y-1, $this->z)); if($downBlock->getID() !== GRASS){ diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index fb571cfd..dbb28480 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -19,6 +19,12 @@ class Painting extends Hanging{ ]; } + public function createSaveData(){ + $data = parent::createSaveData(); + + $data["Motive"] = $this->data["Motive"]; + } + public function spawn($player){ $pk = new AddPaintingPacket; $pk->eid = $this->eid; diff --git a/src/world/Level.php b/src/world/Level.php index a7854a3a..09618c62 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -88,93 +88,7 @@ class Level{ if($extra !== false){ $entities = []; foreach($this->server->api->entity->getAll($this) as $entity){ - if($entity->class === ENTITY_MOB){ - $entities[] = [ - "id" => $entity->type, - "Color" => @$entity->data["Color"], - "Sheared" => @$entity->data["Sheared"], - "State" => @$entity->data["State"], - "IsBaby" => @$entity->data["IsBaby"], - "Health" => $entity->health, - "Pos" => [ - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ], - "Rotation" => [ - 0 => $entity->yaw, - 1 => $entity->pitch, - ], - ]; - }elseif($entity->class === ENTITY_OBJECT){ - if($entity->type === OBJECT_PAINTING){ - $entities[] = [ - "id" => $entity->type, - "TileX" => $entity->x, - "TileY" => $entity->y, - "TileZ" => $entity->z, - "Health" => $entity->health, - "Motive" => $entity->data["Motive"], - "Pos" => [ - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ], - "Rotation" => [ - 0 => $entity->yaw, - 1 => $entity->pitch, - ], - ]; - }else{ - $entities[] = [ - "id" => $entity->type, - "Health" => $entity->health, - "Pos" => [ - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ], - "Rotation" => [ - 0 => $entity->yaw, - 1 => $entity->pitch, - ], - ]; - } - }elseif($entity->class === ENTITY_FALLING){ - $entities[] = [ - "id" => $entity->type, - "Health" => $entity->health, - "Tile" => $entity->data["Tile"], - "Pos" => [ - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ], - "Rotation" => [ - 0 => 0, - 1 => 0, - ], - ]; - }elseif($entity->class === ENTITY_ITEM){ - $entities[] = [ - "id" => 64, - "Item" => [ - "id" => $entity->type, - "Damage" => $entity->meta, - "Count" => $entity->stack, - ], - "Health" => $entity->health, - "Pos" => [ - 0 => $entity->x, - 1 => $entity->y, - 2 => $entity->z, - ], - "Rotation" => [ - 0 => 0, - 1 => 0, - ], - ]; - } + $entities[] = $entity->createSaveData(); } $this->entities->setAll($entities); $this->entities->save();