rewrite of entity save system

This commit is contained in:
GameHerobrine 2022-11-13 01:53:06 +03:00
parent 2bae9d8f42
commit 3af7ee98b4
8 changed files with 93 additions and 108 deletions

View file

@ -1312,17 +1312,17 @@ class Player{
return; return;
} }
if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){ if($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL){
if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){ if($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL){
$pk = new LoginStatusPacket; $pk = new LoginStatusPacket;
$pk->status = 1; $pk->status = 1;
$this->directDataPacket($pk); $this->directDataPacket($pk);
}else{ }else{
$pk = new LoginStatusPacket; $pk = new LoginStatusPacket;
$pk->status = 2; $pk->status = 2;
$this->directDataPacket($pk); $this->directDataPacket($pk);
} }
$this->close("Incorrect protocol #" . $packet->protocol1, false); $this->close("Incorrect protocol #" . $packet->protocol1, false);
return; 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"){ 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); $this->close("Bad username", false);

View file

@ -27,6 +27,13 @@ abstract class Animal extends Creature implements Ageable, Breedable{
return $d; return $d;
} }
public function createSaveData(){
$data = parent::createSaveData();
$data["IsBaby"] = $this->isBaby();
return $data;
}
public function isInLove(){ public function isInLove(){
return $this->inLove > 0; return $this->inLove > 0;
} }

View file

@ -19,6 +19,12 @@ abstract class Creature extends Living{
} }
} }
public function createSaveData(){
$data = parent::createSaveData();
$data["State"] = @$this->getState();
return $data;
}
public function getSpeedModifer(){ public function getSpeedModifer(){
return $this->speedModifer * ($this->inPanic ? 1.4 : 0.7); return $this->speedModifer * ($this->inPanic ? 1.4 : 0.7);
} }

View file

@ -31,7 +31,8 @@ class Entity extends Position
public $speedY; public $speedY;
public $speedZ; public $speedZ;
public $speed; 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 $yaw;
public $pitch; public $pitch;
public $dead; public $dead;
@ -65,8 +66,9 @@ class Entity extends Position
function __construct(Level $level, $eid, $class, $type = 0, $data = array()) function __construct(Level $level, $eid, $class, $type = 0, $data = array())
{ {
$this->random = new Random(); $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->canBeAttacked = false;
$this->hasKnockback = false; $this->hasKnockback = false;
$this->level = $level; $this->level = $level;
$this->speedModifer = 0.7; $this->speedModifer = 0.7;
$this->fallY = false; $this->fallY = false;
@ -80,7 +82,7 @@ class Entity extends Position
$this->data = $data; $this->data = $data;
$this->status = 0; $this->status = 0;
$this->health = 20; $this->health = 20;
$this->hasGravity = false; $this->hasGravity = false;
$this->dmgcounter = array(0, 0, 0); $this->dmgcounter = array(0, 0, 0);
$this->air = 300; $this->air = 300;
$this->fire = 0; $this->fire = 0;
@ -122,8 +124,8 @@ class Entity extends Position
$this->speedModifer = 1; $this->speedModifer = 1;
$this->width = 1.2; $this->width = 1.2;
$this->height = 1.9; $this->height = 1.9;
$this->hasKnockback = true; $this->hasKnockback = true;
$this->hasGravity = true; $this->hasGravity = true;
$this->canBeAttacked = true; $this->canBeAttacked = true;
break; break;
case ENTITY_ITEM: case ENTITY_ITEM:
@ -134,7 +136,7 @@ class Entity extends Position
$this->meta = (int) $this->data["meta"]; $this->meta = (int) $this->data["meta"];
$this->stack = (int) $this->data["stack"]; $this->stack = (int) $this->data["stack"];
} }
$this->hasGravity = true; $this->hasGravity = true;
$this->setHealth(5, "generic"); $this->setHealth(5, "generic");
$this->server->schedule(6010, array( $this->server->schedule(6010, array(
$this, $this,
@ -147,7 +149,7 @@ class Entity extends Position
$this->setHealth(PHP_INT_MAX, "generic"); $this->setHealth(PHP_INT_MAX, "generic");
$this->height = 0.98; $this->height = 0.98;
$this->width = 0.98; $this->width = 0.98;
$this->hasGravity = true; $this->hasGravity = true;
break; break;
case ENTITY_OBJECT: case ENTITY_OBJECT:
$this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x; $this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x;
@ -1075,6 +1077,41 @@ class Entity extends Position
unset($this->speedMeasure[key($this->speedMeasure)]); unset($this->speedMeasure[key($this->speedMeasure)]);
$this->speedMeasure[] = $this->speed; $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() public function updateLast()
{ {

View file

@ -2,24 +2,32 @@
class Chicken extends Animal{ class Chicken extends Animal{
const TYPE = MOB_CHICKEN; const TYPE = MOB_CHICKEN;
public $timeUntilEgg;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){ function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $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->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic");
$this->setName('Chicken'); $this->setName('Chicken');
$this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7); $this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7);
$this->setSpeed(0.25); $this->setSpeed(0.25);
$this->timeUntilEgg = mt_rand(0,6000) + 6000;
$this->update(); $this->update();
} }
public function isFood($id){ public function isFood($id){
return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; 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(){ public function dropAnEgg(){
if($this->closed){ if($this->closed){
return; 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)); 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(){ public function getDrops(){

View file

@ -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(){ public function eatGrass(){
$downBlock = $this->level->getBlock(new Vector3($this->x, $this->y-1, $this->z)); $downBlock = $this->level->getBlock(new Vector3($this->x, $this->y-1, $this->z));
if($downBlock->getID() !== GRASS){ if($downBlock->getID() !== GRASS){

View file

@ -19,6 +19,12 @@ class Painting extends Hanging{
]; ];
} }
public function createSaveData(){
$data = parent::createSaveData();
$data["Motive"] = $this->data["Motive"];
}
public function spawn($player){ public function spawn($player){
$pk = new AddPaintingPacket; $pk = new AddPaintingPacket;
$pk->eid = $this->eid; $pk->eid = $this->eid;

View file

@ -88,93 +88,7 @@ class Level{
if($extra !== false){ if($extra !== false){
$entities = []; $entities = [];
foreach($this->server->api->entity->getAll($this) as $entity){ foreach($this->server->api->entity->getAll($this) as $entity){
if($entity->class === ENTITY_MOB){ $entities[] = $entity->createSaveData();
$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,
],
];
}
} }
$this->entities->setAll($entities); $this->entities->setAll($entities);
$this->entities->save(); $this->entities->save();