Improved network packets allocation

This commit is contained in:
Shoghi Cervantes 2014-10-27 20:30:33 +01:00
parent 3f5b129cf5
commit db82f76c11
69 changed files with 366 additions and 222 deletions

View File

@ -581,7 +581,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return; return;
} }
$pk = new FullChunkDataPacket; $pk = FullChunkDataPacket::getFromPool();
$pk->chunkX = $x; $pk->chunkX = $x;
$pk->chunkZ = $z; $pk->chunkZ = $z;
$pk->data = $payload; $pk->data = $payload;
@ -645,7 +645,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->blocked = false; $this->blocked = false;
$pk = new SetTimePacket; $pk = SetTimePacket::getFromPool();
$pk->time = $this->level->getTime(); $pk->time = $this->level->getTime();
$pk->started = $this->level->stopTime == false; $pk->started = $this->level->stopTime == false;
$this->dataPacket($pk); $this->dataPacket($pk);
@ -834,7 +834,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$level = $pos->getLevel(); $level = $pos->getLevel();
} }
$this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level);
$pk = new SetSpawnPositionPacket; $pk = SetSpawnPositionPacket::getFromPool();
$pk->x = (int) $this->spawnPosition->x; $pk->x = (int) $this->spawnPosition->x;
$pk->y = (int) $this->spawnPosition->y; $pk->y = (int) $this->spawnPosition->y;
$pk->z = (int) $this->spawnPosition->z; $pk->z = (int) $this->spawnPosition->z;
@ -947,7 +947,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$spawnPosition = $this->getSpawn(); $spawnPosition = $this->getSpawn();
$pk = new StartGamePacket; $pk = StartGamePacket::getFromPool();
$pk->seed = $this->level->getSeed(); $pk->seed = $this->level->getSeed();
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y + $this->getEyeHeight(); $pk->y = $this->y + $this->getEyeHeight();
@ -1017,7 +1017,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$flags |= 0x20; //Show Nametags $flags |= 0x20; //Show Nametags
} }
$pk = new AdventureSettingsPacket; $pk = AdventureSettingsPacket::getFromPool();
$pk->flags = $flags; $pk->flags = $flags;
$this->dataPacket($pk); $this->dataPacket($pk);
} }
@ -1125,7 +1125,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($to->distance($ev->getTo()) > 0.1){ //If plugins modify the destination if($to->distance($ev->getTo()) > 0.1){ //If plugins modify the destination
$this->teleport($ev->getTo()); $this->teleport($ev->getTo());
}else{ }else{
$pk = new MovePlayerPacket; $pk = MovePlayerPacket::getFromPool();
$pk->eid = $this->id; $pk->eid = $this->id;
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
@ -1140,7 +1140,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
if($revert){ if($revert){
$pk = new MovePlayerPacket; $pk = MovePlayerPacket::getFromPool();
$pk->eid = 0; $pk->eid = 0;
$pk->x = $from->x; $pk->x = $from->x;
$pk->y = $from->y + $this->getEyeHeight() + 0.01; $pk->y = $from->y + $this->getEyeHeight() + 0.01;
@ -1206,11 +1206,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
continue; continue;
} }
$pk = new TakeItemEntityPacket; $pk = TakeItemEntityPacket::getFromPool();
$pk->eid = 0; $pk->eid = 0;
$pk->target = $entity->getID(); $pk->target = $entity->getID();
$this->dataPacket($pk); $this->dataPacket($pk);
$pk = new TakeItemEntityPacket; $pk = TakeItemEntityPacket::getFromPool();
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->target = $entity->getID(); $pk->target = $entity->getID();
Server::broadcastPacket($entity->getViewers(), $pk); Server::broadcastPacket($entity->getViewers(), $pk);
@ -1240,11 +1240,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
break; break;
} }
$pk = new TakeItemEntityPacket; $pk = TakeItemEntityPacket::getFromPool();
$pk->eid = 0; $pk->eid = 0;
$pk->target = $entity->getID(); $pk->target = $entity->getID();
$this->dataPacket($pk); $this->dataPacket($pk);
$pk = new TakeItemEntityPacket; $pk = TakeItemEntityPacket::getFromPool();
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->target = $entity->getID(); $pk->target = $entity->getID();
Server::broadcastPacket($entity->getViewers(), $pk); Server::broadcastPacket($entity->getViewers(), $pk);
@ -1307,11 +1307,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
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 = LoginStatusPacket::getFromPool();
$pk->status = 1; $pk->status = 1;
$this->dataPacket($pk); $this->dataPacket($pk);
}else{ }else{
$pk = new LoginStatusPacket; $pk = LoginStatusPacket::getFromPool();
$pk->status = 2; $pk->status = 2;
$this->dataPacket($pk); $this->dataPacket($pk);
} }
@ -1413,7 +1413,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->inventory->setHeldItemSlot(0); $this->inventory->setHeldItemSlot(0);
} }
$pk = new LoginStatusPacket; $pk = LoginStatusPacket::getFromPool();
$pk->status = 0; $pk->status = 0;
$this->dataPacket($pk); $this->dataPacket($pk);
@ -1425,7 +1425,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->dead = false; $this->dead = false;
$pk = new StartGamePacket; $pk = StartGamePacket::getFromPool();
$pk->seed = $this->level->getSeed(); $pk->seed = $this->level->getSeed();
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
@ -1438,17 +1438,18 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->eid = 0; //Always use EntityID as zero for the actual player $pk->eid = 0; //Always use EntityID as zero for the actual player
$this->dataPacket($pk); $this->dataPacket($pk);
$pk = new SetTimePacket(); $pk = SetTimePacket::getFromPool();
$pk->time = $this->level->getTime(); $pk->time = $this->level->getTime();
$pk->started = $this->level->stopTime == false;
$this->dataPacket($pk); $this->dataPacket($pk);
$pk = new SetSpawnPositionPacket; $pk = SetSpawnPositionPacket::getFromPool();
$pk->x = (int) $spawnPosition->x; $pk->x = (int) $spawnPosition->x;
$pk->y = (int) $spawnPosition->y; $pk->y = (int) $spawnPosition->y;
$pk->z = (int) $spawnPosition->z; $pk->z = (int) $spawnPosition->z;
$this->dataPacket($pk); $this->dataPacket($pk);
$pk = new SetHealthPacket(); $pk = SetHealthPacket::getFromPool();
$pk->health = $this->getHealth(); $pk->health = $this->getHealth();
$this->dataPacket($pk); $this->dataPacket($pk);
if($this->getHealth() <= 0){ if($this->getHealth() <= 0){
@ -1474,7 +1475,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$revert = ($this->dead === true or $this->spawned !== true); $revert = ($this->dead === true or $this->spawned !== true);
if($revert or ($this->forceMovement instanceof Vector3 and $newPos->distance($this->forceMovement) > 0.2)){ if($revert or ($this->forceMovement instanceof Vector3 and $newPos->distance($this->forceMovement) > 0.2)){
$pk = new MovePlayerPacket(); $pk = MovePlayerPacket::getFromPool();
$pk->eid = 0; $pk->eid = 0;
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y + $this->getEyeHeight() + 0.01; $pk->y = $this->y + $this->getEyeHeight() + 0.01;
@ -1565,7 +1566,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$target = $this->level->getBlock($blockVector); $target = $this->level->getBlock($blockVector);
$block = $target->getSide($packet->face); $block = $target->getSide($packet->face);
$pk = new UpdateBlockPacket; $pk = UpdateBlockPacket::getFromPool();
$pk->x = $target->x; $pk->x = $target->x;
$pk->y = $target->y; $pk->y = $target->y;
$pk->z = $target->z; $pk->z = $target->z;
@ -1573,7 +1574,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->meta = $target->getDamage(); $pk->meta = $target->getDamage();
$this->dataPacket($pk); $this->dataPacket($pk);
$pk = new UpdateBlockPacket; $pk = UpdateBlockPacket::getFromPool();
$pk->x = $block->x; $pk->x = $block->x;
$pk->y = $block->y; $pk->y = $block->y;
$pk->z = $block->z; $pk->z = $block->z;
@ -1612,7 +1613,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$target = $this->level->getBlock($blockVector); $target = $this->level->getBlock($blockVector);
$block = $target->getSide($packet->face); $block = $target->getSide($packet->face);
$pk = new UpdateBlockPacket; $pk = UpdateBlockPacket::getFromPool();
$pk->x = $target->x; $pk->x = $target->x;
$pk->y = $target->y; $pk->y = $target->y;
$pk->z = $target->z; $pk->z = $target->z;
@ -1620,7 +1621,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->meta = $target->getDamage(); $pk->meta = $target->getDamage();
$this->dataPacket($pk); $this->dataPacket($pk);
$pk = new UpdateBlockPacket; $pk = UpdateBlockPacket::getFromPool();
$pk->x = $block->x; $pk->x = $block->x;
$pk->y = $block->y; $pk->y = $block->y;
$pk->z = $block->z; $pk->z = $block->z;
@ -1738,7 +1739,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$target = $this->level->getBlock($vector); $target = $this->level->getBlock($vector);
$tile = $this->level->getTile($vector); $tile = $this->level->getTile($vector);
$pk = new UpdateBlockPacket; $pk = UpdateBlockPacket::getFromPool();
$pk->x = $target->x; $pk->x = $target->x;
$pk->y = $target->y; $pk->y = $target->y;
$pk->z = $target->z; $pk->z = $target->z;
@ -1887,7 +1888,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
break; break;
} }
$pk = new AnimatePacket(); $pk = AnimatePacket::getFromPool();
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->action = $ev->getAnimationType(); $pk->action = $ev->getAnimationType();
Server::broadcastPacket($this->getViewers(), $pk); Server::broadcastPacket($this->getViewers(), $pk);
@ -1961,7 +1962,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
break; break;
} }
$pk = new EntityEventPacket(); $pk = EntityEventPacket::getFromPool();
$pk->eid = 0; $pk->eid = 0;
$pk->event = 9; $pk->event = 9;
$this->dataPacket($pk); $this->dataPacket($pk);
@ -2247,7 +2248,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$mes = explode("\n", $message); $mes = explode("\n", $message);
foreach($mes as $m){ foreach($mes as $m){
if($m !== ""){ if($m !== ""){
$pk = new MessagePacket; $pk = MessagePacket::getFromPool();
$pk->source = ""; //Do not use this ;) $pk->source = ""; //Do not use this ;)
$pk->message = $m; $pk->message = $m;
$this->dataPacket($pk); $this->dataPacket($pk);
@ -2462,7 +2463,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
public function setHealth($amount){ public function setHealth($amount){
parent::setHealth($amount); parent::setHealth($amount);
if($this->spawned === true){ if($this->spawned === true){
$pk = new SetHealthPacket(); $pk = SetHealthPacket::getFromPool();
$pk->health = $this->getHealth(); $pk->health = $this->getHealth();
$this->dataPacket($pk); $this->dataPacket($pk);
} }
@ -2492,7 +2493,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
parent::attack($damage, $source); parent::attack($damage, $source);
if($this->getLastDamageCause() === $source){ if($this->getLastDamageCause() === $source){
$pk = new EntityEventPacket(); $pk = EntityEventPacket::getFromPool();
$pk->eid = 0; $pk->eid = 0;
$pk->event = 2; $pk->event = 2;
$this->dataPacket($pk); $this->dataPacket($pk);
@ -2537,7 +2538,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->forceMovement = $pos; $this->forceMovement = $pos;
$this->newPosition = $pos; $this->newPosition = $pos;
$pk = new MovePlayerPacket; $pk = MovePlayerPacket::getFromPool();
$pk->eid = 0; $pk->eid = 0;
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y + $this->getEyeHeight() + 0.01; $pk->y = $this->y + $this->getEyeHeight() + 0.01;

View File

@ -53,7 +53,7 @@ class Bed extends Transparent{
$isNight = ($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE); $isNight = ($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE);
if($player instanceof Player and !$isNight){ if($player instanceof Player and !$isNight){
$pk = new ChatPacket; $pk = ChatPacket::getFromPool();
$pk->message = "You can only sleep at night"; $pk->message = "You can only sleep at night";
$player->dataPacket($pk); $player->dataPacket($pk);
@ -77,7 +77,7 @@ class Bed extends Transparent{
$b = $blockWest; $b = $blockWest;
}else{ }else{
if($player instanceof Player){ if($player instanceof Player){
$pk = new ChatPacket; $pk = ChatPacket::getFromPool();
$pk->message = "This bed is incomplete"; $pk->message = "This bed is incomplete";
$player->dataPacket($pk); $player->dataPacket($pk);
} }
@ -87,7 +87,7 @@ class Bed extends Transparent{
} }
if($player instanceof Player and $player->sleepOn($b) === false){ if($player instanceof Player and $player->sleepOn($b) === false){
$pk = new ChatPacket; $pk = ChatPacket::getFromPool();
$pk->message = "This bed is occupied"; $pk->message = "This bed is occupied";
$player->dataPacket($pk); $player->dataPacket($pk);
} }

View File

@ -271,7 +271,7 @@ abstract class Door extends Transparent{
if($player instanceof Player){ if($player instanceof Player){
unset($players[$player->getID()]); unset($players[$player->getID()]);
} }
$pk = new LevelEventPacket; $pk = LevelEventPacket::getFromPool();
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
$pk->z = $this->z; $pk->z = $this->z;
@ -290,7 +290,7 @@ abstract class Door extends Transparent{
if($player instanceof Player){ if($player instanceof Player){
unset($players[$player->getID()]); unset($players[$player->getID()]);
} }
$pk = new LevelEventPacket; $pk = LevelEventPacket::getFromPool();
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
$pk->z = $this->z; $pk->z = $this->z;

View File

@ -209,7 +209,7 @@ class Arrow extends Projectile{
} }
public function spawnTo(Player $player){ public function spawnTo(Player $player){
$pk = new AddEntityPacket(); $pk = AddEntityPacket::getFromPool();
$pk->type = Arrow::NETWORK_ID; $pk->type = Arrow::NETWORK_ID;
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->x = $this->x; $pk->x = $this->x;
@ -218,7 +218,7 @@ class Arrow extends Projectile{
$pk->did = 0; //TODO: send motion here $pk->did = 0; //TODO: send motion here
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new SetEntityMotionPacket(); $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];

View File

@ -219,7 +219,7 @@ class DroppedItem extends Entity{
} }
public function spawnTo(Player $player){ public function spawnTo(Player $player){
$pk = new AddItemEntityPacket(); $pk = AddItemEntityPacket::getFromPool();
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
@ -230,7 +230,7 @@ class DroppedItem extends Entity{
$pk->item = $this->getItem(); $pk->item = $this->getItem();
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new SetEntityMotionPacket; $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];

View File

@ -270,7 +270,7 @@ abstract class Entity extends Location implements Metadatable{
$player = [$player]; $player = [$player];
} }
$pk = new SetEntityDataPacket(); $pk = SetEntityDataPacket::getFromPool();
$pk->eid = $this->id; $pk->eid = $this->id;
$pk->metadata = $this->getData(); $pk->metadata = $this->getData();
$pk->encode(); $pk->encode();
@ -278,7 +278,7 @@ abstract class Entity extends Location implements Metadatable{
foreach($player as $p){ foreach($player as $p){
if($p === $this){ if($p === $this){
/** @var Player $p */ /** @var Player $p */
$pk2 = new SetEntityDataPacket(); $pk2 = SetEntityDataPacket::getFromPool();
$pk2->eid = 0; $pk2->eid = 0;
$pk2->metadata = $this->getData(); $pk2->metadata = $this->getData();
$p->dataPacket($pk2); $p->dataPacket($pk2);
@ -293,7 +293,7 @@ abstract class Entity extends Location implements Metadatable{
*/ */
public function despawnFrom(Player $player){ public function despawnFrom(Player $player){
if(isset($this->hasSpawned[$player->getID()])){ if(isset($this->hasSpawned[$player->getID()])){
$pk = new RemoveEntityPacket; $pk = RemoveEntityPacket::getFromPool();
$pk->eid = $this->id; $pk->eid = $this->id;
$player->dataPacket($pk); $player->dataPacket($pk);
unset($this->hasSpawned[$player->getID()]); unset($this->hasSpawned[$player->getID()]);
@ -532,7 +532,7 @@ abstract class Entity extends Location implements Metadatable{
$this->lastPitch = $this->pitch; $this->lastPitch = $this->pitch;
if($this instanceof Human){ if($this instanceof Human){
$pk = new MovePlayerPacket; $pk = MovePlayerPacket::getFromPool();
$pk->eid = $this->id; $pk->eid = $this->id;
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
@ -542,7 +542,7 @@ abstract class Entity extends Location implements Metadatable{
$pk->bodyYaw = $this->yaw; $pk->bodyYaw = $this->yaw;
}else{ }else{
//TODO: add to move list //TODO: add to move list
$pk = new MoveEntityPacket(); $pk = MoveEntityPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch] [$this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch]
]; ];
@ -556,7 +556,7 @@ abstract class Entity extends Location implements Metadatable{
$this->lastMotionY = $this->motionY; $this->lastMotionY = $this->motionY;
$this->lastMotionZ = $this->motionZ; $this->lastMotionZ = $this->motionZ;
$pk = new SetEntityMotionPacket; $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];
@ -726,7 +726,7 @@ abstract class Entity extends Location implements Metadatable{
$this->level->addEntity($this); $this->level->addEntity($this);
if($this instanceof Player){ if($this instanceof Player){
$this->usedChunks = []; $this->usedChunks = [];
$pk = new SetTimePacket(); $pk = SetTimePacket::getFromPool();
$pk->time = $this->level->getTime(); $pk->time = $this->level->getTime();
$pk->started = $this->level->stopTime == false; $pk->started = $this->level->stopTime == false;
$this->dataPacket($pk); $this->dataPacket($pk);
@ -1100,7 +1100,7 @@ abstract class Entity extends Location implements Metadatable{
if(!$this->justCreated){ if(!$this->justCreated){
if($this instanceof Player){ if($this instanceof Player){
$pk = new SetEntityMotionPacket; $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[0, $this->motionX, $this->motionY, $this->motionZ] [0, $this->motionX, $this->motionY, $this->motionZ]
]; ];

View File

@ -150,7 +150,7 @@ class FallingBlock extends Entity{
} }
public function spawnTo(Player $player){ public function spawnTo(Player $player){
$pk = new AddEntityPacket; $pk = AddEntityPacket::getFromPool();
$pk->type = FallingBlock::NETWORK_ID; $pk->type = FallingBlock::NETWORK_ID;
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->x = $this->x; $pk->x = $this->x;
@ -159,7 +159,7 @@ class FallingBlock extends Entity{
$pk->did = -$this->getBlock(); $pk->did = -$this->getBlock();
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new SetEntityMotionPacket; $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];

View File

@ -156,7 +156,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
if($player !== $this and !isset($this->hasSpawned[$player->getID()])){ if($player !== $this and !isset($this->hasSpawned[$player->getID()])){
$this->hasSpawned[$player->getID()] = $player; $this->hasSpawned[$player->getID()] = $player;
$pk = new AddPlayerPacket; $pk = AddPlayerPacket::getFromPool();
$pk->clientID = 0; $pk->clientID = 0;
if($player->getRemoveFormat()){ if($player->getRemoveFormat()){
$pk->username = TextFormat::clean($this->nameTag); $pk->username = TextFormat::clean($this->nameTag);
@ -174,7 +174,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$pk->metadata = $this->getData(); $pk->metadata = $this->getData();
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new SetEntityMotionPacket; $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];
@ -188,7 +188,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
public function despawnFrom(Player $player){ public function despawnFrom(Player $player){
if(isset($this->hasSpawned[$player->getID()])){ if(isset($this->hasSpawned[$player->getID()])){
$pk = new RemovePlayerPacket; $pk = RemovePlayerPacket::getFromPool();
$pk->eid = $this->id; $pk->eid = $this->id;
$pk->clientID = 0; $pk->clientID = 0;
$player->dataPacket($pk); $player->dataPacket($pk);

View File

@ -76,7 +76,7 @@ abstract class Living extends Entity implements Damageable{
} }
} }
$pk = new EntityEventPacket(); $pk = EntityEventPacket::getFromPool();
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->event = 2; //Ouch! $pk->event = 2; //Ouch!
Server::broadcastPacket($this->hasSpawned, $pk); Server::broadcastPacket($this->hasSpawned, $pk);

View File

@ -140,7 +140,7 @@ class PrimedTNT extends Entity implements Explosive{
} }
public function spawnTo(Player $player){ public function spawnTo(Player $player){
$pk = new AddEntityPacket(); $pk = AddEntityPacket::getFromPool();
$pk->type = PrimedTNT::NETWORK_ID; $pk->type = PrimedTNT::NETWORK_ID;
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->x = $this->x; $pk->x = $this->x;
@ -149,7 +149,7 @@ class PrimedTNT extends Entity implements Explosive{
$pk->did = 0; $pk->did = 0;
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new SetEntityMotionPacket(); $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];

View File

@ -55,7 +55,7 @@ class Villager extends Creature implements NPC, Ageable{
} }
public function spawnTo(Player $player){ public function spawnTo(Player $player){
$pk = new AddMobPacket(); $pk = AddMobPacket::getFromPool();
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->type = Villager::NETWORK_ID; $pk->type = Villager::NETWORK_ID;
$pk->x = $this->x; $pk->x = $this->x;
@ -66,7 +66,7 @@ class Villager extends Creature implements NPC, Ageable{
$pk->metadata = $this->getData(); $pk->metadata = $this->getData();
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new SetEntityMotionPacket(); $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];

View File

@ -46,7 +46,7 @@ class Zombie extends Monster{
public function spawnTo(Player $player){ public function spawnTo(Player $player){
$pk = new AddMobPacket(); $pk = AddMobPacket::getFromPool();
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->type = Zombie::NETWORK_ID; $pk->type = Zombie::NETWORK_ID;
$pk->x = $this->x; $pk->x = $this->x;
@ -57,7 +57,7 @@ class Zombie extends Monster{
$pk->metadata = $this->getData(); $pk->metadata = $this->getData();
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new SetEntityMotionPacket(); $pk = SetEntityMotionPacket::getFromPool();
$pk->entities = [ $pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ] [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
]; ];

View File

@ -384,7 +384,7 @@ abstract class BaseInventory implements Inventory{
$target = [$target]; $target = [$target];
} }
$pk = new ContainerSetContentPacket(); $pk = ContainerSetContentPacket::getFromPool();
$pk->slots = []; $pk->slots = [];
for($i = 0; $i < $this->getSize(); ++$i){ for($i = 0; $i < $this->getSize(); ++$i){
$pk->slots[$i] = $this->getItem($i); $pk->slots[$i] = $this->getItem($i);
@ -409,7 +409,7 @@ abstract class BaseInventory implements Inventory{
$target = [$target]; $target = [$target];
} }
$pk = new ContainerSetSlotPacket; $pk = ContainerSetSlotPacket::getFromPool();
$pk->slot = $index; $pk->slot = $index;
$pk->item = clone $this->getItem($index); $pk->item = clone $this->getItem($index);

View File

@ -43,7 +43,7 @@ class ChestInventory extends ContainerInventory{
parent::onOpen($who); parent::onOpen($who);
if(count($this->getViewers()) === 1){ if(count($this->getViewers()) === 1){
$pk = new TileEventPacket; $pk = TileEventPacket::getFromPool();
$pk->x = $this->getHolder()->getX(); $pk->x = $this->getHolder()->getX();
$pk->y = $this->getHolder()->getY(); $pk->y = $this->getHolder()->getY();
$pk->z = $this->getHolder()->getZ(); $pk->z = $this->getHolder()->getZ();
@ -57,7 +57,7 @@ class ChestInventory extends ContainerInventory{
public function onClose(Player $who){ public function onClose(Player $who){
if(count($this->getViewers()) === 1){ if(count($this->getViewers()) === 1){
$pk = new TileEventPacket; $pk = TileEventPacket::getFromPool();
$pk->x = $this->getHolder()->getX(); $pk->x = $this->getHolder()->getX();
$pk->y = $this->getHolder()->getY(); $pk->y = $this->getHolder()->getY();
$pk->z = $this->getHolder()->getZ(); $pk->z = $this->getHolder()->getZ();

View File

@ -29,7 +29,7 @@ use pocketmine\Player;
abstract class ContainerInventory extends BaseInventory{ abstract class ContainerInventory extends BaseInventory{
public function onOpen(Player $who){ public function onOpen(Player $who){
parent::onOpen($who); parent::onOpen($who);
$pk = new ContainerOpenPacket; $pk = ContainerOpenPacket::getFromPool();
$pk->windowid = $who->getWindowId($this); $pk->windowid = $who->getWindowId($this);
$pk->type = $this->getType()->getNetworkType(); $pk->type = $this->getType()->getNetworkType();
$pk->slots = $this->getSize(); $pk->slots = $this->getSize();
@ -47,7 +47,7 @@ abstract class ContainerInventory extends BaseInventory{
} }
public function onClose(Player $who){ public function onClose(Player $who){
$pk = new ContainerClosePacket; $pk = ContainerClosePacket::getFromPool();
$pk->windowid = $who->getWindowId($this); $pk->windowid = $who->getWindowId($this);
$who->dataPacket($pk); $who->dataPacket($pk);
parent::onClose($who); parent::onClose($who);

View File

@ -66,7 +66,7 @@ class PlayerInventory extends BaseInventory{
$this->itemInHandIndex = $index; $this->itemInHandIndex = $index;
$item = $this->getItemInHand(); $item = $this->getItemInHand();
$pk = new PlayerEquipmentPacket; $pk = PlayerEquipmentPacket::getFromPool();
$pk->eid = $this->getHolder()->getID(); $pk->eid = $this->getHolder()->getID();
$pk->item = $item->getID(); $pk->item = $item->getID();
$pk->meta = $item->getDamage(); $pk->meta = $item->getDamage();
@ -126,7 +126,7 @@ class PlayerInventory extends BaseInventory{
$item = $this->getItemInHand(); $item = $this->getItemInHand();
$pk = new PlayerEquipmentPacket; $pk = PlayerEquipmentPacket::getFromPool();
$pk->eid = $this->getHolder()->getID(); $pk->eid = $this->getHolder()->getID();
$pk->item = $item->getID(); $pk->item = $item->getID();
$pk->meta = $item->getDamage(); $pk->meta = $item->getDamage();
@ -286,7 +286,7 @@ class PlayerInventory extends BaseInventory{
} }
} }
$pk = new PlayerArmorEquipmentPacket; $pk = PlayerArmorEquipmentPacket::getFromPool();
$pk->eid = $this->getHolder()->getID(); $pk->eid = $this->getHolder()->getID();
$pk->slots = $slots; $pk->slots = $slots;
$pk->encode(); $pk->encode();
@ -298,7 +298,7 @@ class PlayerInventory extends BaseInventory{
//$pk2 = clone $pk; //$pk2 = clone $pk;
//$pk2->eid = 0; //$pk2->eid = 0;
$pk2 = new ContainerSetContentPacket; $pk2 = ContainerSetContentPacket::getFromPool();
$pk2->windowid = 0x78; //Armor window id constant $pk2->windowid = 0x78; //Armor window id constant
$pk2->slots = $armor; $pk2->slots = $armor;
$player->dataPacket($pk2); $player->dataPacket($pk2);
@ -333,7 +333,7 @@ class PlayerInventory extends BaseInventory{
$target = [$target]; $target = [$target];
} }
$pk = new ContainerSetContentPacket(); $pk = ContainerSetContentPacket::getFromPool();
$pk->slots = []; $pk->slots = [];
for($i = 0; $i < $this->getSize(); ++$i){ //Do not send armor by error here for($i = 0; $i < $this->getSize(); ++$i){ //Do not send armor by error here
$pk->slots[$i] = $this->getItem($i); $pk->slots[$i] = $this->getItem($i);
@ -365,7 +365,7 @@ class PlayerInventory extends BaseInventory{
$target = [$target]; $target = [$target];
} }
$pk = new ContainerSetSlotPacket; $pk = ContainerSetSlotPacket::getFromPool();
$pk->slot = $index; $pk->slot = $index;
$pk->item = clone $this->getItem($index); $pk->item = clone $this->getItem($index);

View File

@ -217,7 +217,7 @@ class Explosion{
$this->level->setBlockIdAt($block->x, $block->y, $block->z, 0); $this->level->setBlockIdAt($block->x, $block->y, $block->z, 0);
$send[] = new Vector3($block->x - $source->x, $block->y - $source->y, $block->z - $source->z); $send[] = new Vector3($block->x - $source->x, $block->y - $source->y, $block->z - $source->z);
} }
$pk = new ExplodePacket; $pk = ExplodePacket::getFromPool();
$pk->x = $this->source->x; $pk->x = $this->source->x;
$pk->y = $this->source->y; $pk->y = $this->source->y;
$pk->z = $this->source->z; $pk->z = $this->source->z;

View File

@ -442,7 +442,7 @@ class Level implements ChunkManager, Metadatable{
* Changes to this function won't be recorded on the version. * Changes to this function won't be recorded on the version.
*/ */
public function sendTime(){ public function sendTime(){
$pk = new SetTimePacket; $pk = SetTimePacket::getFromPool();
$pk->time = (int) $this->time; $pk->time = (int) $this->time;
$pk->started = $this->stopTime == false; $pk->started = $this->stopTime == false;
@ -539,7 +539,7 @@ class Level implements ChunkManager, Metadatable{
foreach($mini as $blocks){ foreach($mini as $blocks){
/** @var Block $b */ /** @var Block $b */
foreach($blocks as $b){ foreach($blocks as $b){
$pk = new UpdateBlockPacket(); $pk = UpdateBlockPacket::getFromPool();
$pk->x = $b->x; $pk->x = $b->x;
$pk->y = $b->y; $pk->y = $b->y;
$pk->z = $b->z; $pk->z = $b->z;
@ -949,7 +949,7 @@ class Level implements ChunkManager, Metadatable{
} }
//if($direct === true){ //if($direct === true){
$pk = new UpdateBlockPacket; $pk = UpdateBlockPacket::getFromPool();
$pk->x = $pos->x; $pk->x = $pos->x;
$pk->y = $pos->y; $pk->y = $pos->y;
$pk->z = $pos->z; $pk->z = $pos->z;

View File

@ -83,6 +83,8 @@ use raklib\server\ServerInstance;
class RakLibInterface implements ServerInstance, SourceInterface{ class RakLibInterface implements ServerInstance, SourceInterface{
private $packetPool = [];
private $server; private $server;
/** @var Player[] */ /** @var Player[] */
private $players = []; private $players = [];
@ -103,6 +105,9 @@ class RakLibInterface implements ServerInstance, SourceInterface{
private $externalThreaded; private $externalThreaded;
public function __construct(Server $server){ public function __construct(Server $server){
$this->registerPackets();
$this->server = $server; $this->server = $server;
$this->identifiers = new \SplObjectStorage(); $this->identifiers = new \SplObjectStorage();
@ -115,6 +120,8 @@ class RakLibInterface implements ServerInstance, SourceInterface{
} }
public function doTick(){ public function doTick(){
$this->cleanPacketPool();
EncapsulatedPacket::cleanPacketPool();
$this->interface->sendTick(); $this->interface->sendTick();
} }
@ -218,7 +225,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{
if(!$packet->isEncoded){ if(!$packet->isEncoded){
$packet->encode(); $packet->encode();
} }
$pk = new EncapsulatedPacket(); $pk = EncapsulatedPacket::getPacketFromPool();
$pk->buffer = $packet->buffer; $pk->buffer = $packet->buffer;
$pk->reliability = 2; $pk->reliability = 2;
if($needACK === true){ if($needACK === true){
@ -232,150 +239,87 @@ class RakLibInterface implements ServerInstance, SourceInterface{
return null; return null;
} }
private function getPacket($buffer){ public function registerPacket($id, $class){
$pid = ord($buffer{0}); $this->packetPool[$id] = $class;
switch($pid){ //TODO: more efficient selection based on range }
case ProtocolInfo::LOGIN_PACKET:
$data = new LoginPacket(); /**
break; * @param $id
case ProtocolInfo::LOGIN_STATUS_PACKET: *
$data = new LoginStatusPacket(); * @return DataPacket
break; */
case ProtocolInfo::MESSAGE_PACKET: public function getPacketFromPool($id){
$data = new MessagePacket(); if(isset($this->packetPool[$id])){
break; /** @var DataPacket $class */
case ProtocolInfo::SET_TIME_PACKET: $class = $this->packetPool[$id];
$data = new SetTimePacket(); return $class::getFromPool();
break;
case ProtocolInfo::START_GAME_PACKET:
$data = new StartGamePacket();
break;
case ProtocolInfo::ADD_MOB_PACKET:
$data = new AddMobPacket();
break;
case ProtocolInfo::ADD_PLAYER_PACKET:
$data = new AddPlayerPacket();
break;
case ProtocolInfo::REMOVE_PLAYER_PACKET:
$data = new RemovePlayerPacket();
break;
case ProtocolInfo::ADD_ENTITY_PACKET:
$data = new AddEntityPacket();
break;
case ProtocolInfo::REMOVE_ENTITY_PACKET:
$data = new RemoveEntityPacket();
break;
case ProtocolInfo::ADD_ITEM_ENTITY_PACKET:
$data = new AddItemEntityPacket();
break;
case ProtocolInfo::TAKE_ITEM_ENTITY_PACKET:
$data = new TakeItemEntityPacket();
break;
case ProtocolInfo::MOVE_ENTITY_PACKET:
$data = new MoveEntityPacket();
break;
case ProtocolInfo::ROTATE_HEAD_PACKET:
$data = new RotateHeadPacket();
break;
case ProtocolInfo::MOVE_PLAYER_PACKET:
$data = new MovePlayerPacket();
break;
case ProtocolInfo::REMOVE_BLOCK_PACKET:
$data = new RemoveBlockPacket();
break;
case ProtocolInfo::UPDATE_BLOCK_PACKET:
$data = new UpdateBlockPacket();
break;
case ProtocolInfo::ADD_PAINTING_PACKET:
$data = new AddPaintingPacket();
break;
case ProtocolInfo::EXPLODE_PACKET:
$data = new ExplodePacket();
break;
case ProtocolInfo::LEVEL_EVENT_PACKET:
$data = new LevelEventPacket();
break;
case ProtocolInfo::TILE_EVENT_PACKET:
$data = new TileEventPacket();
break;
case ProtocolInfo::ENTITY_EVENT_PACKET:
$data = new EntityEventPacket();
break;
case ProtocolInfo::PLAYER_EQUIPMENT_PACKET:
$data = new PlayerEquipmentPacket();
break;
case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET:
$data = new PlayerArmorEquipmentPacket();
break;
case ProtocolInfo::INTERACT_PACKET:
$data = new InteractPacket();
break;
case ProtocolInfo::USE_ITEM_PACKET:
$data = new UseItemPacket();
break;
case ProtocolInfo::PLAYER_ACTION_PACKET:
$data = new PlayerActionPacket();
break;
case ProtocolInfo::HURT_ARMOR_PACKET:
$data = new HurtArmorPacket();
break;
case ProtocolInfo::SET_ENTITY_DATA_PACKET:
$data = new SetEntityDataPacket();
break;
case ProtocolInfo::SET_ENTITY_MOTION_PACKET:
$data = new SetEntityMotionPacket();
break;
case ProtocolInfo::SET_HEALTH_PACKET:
$data = new SetHealthPacket();
break;
case ProtocolInfo::SET_SPAWN_POSITION_PACKET:
$data = new SetSpawnPositionPacket();
break;
case ProtocolInfo::ANIMATE_PACKET:
$data = new AnimatePacket();
break;
case ProtocolInfo::RESPAWN_PACKET:
$data = new RespawnPacket();
break;
case ProtocolInfo::SEND_INVENTORY_PACKET:
$data = new SendInventoryPacket();
break;
case ProtocolInfo::DROP_ITEM_PACKET:
$data = new DropItemPacket();
break;
case ProtocolInfo::CONTAINER_OPEN_PACKET:
$data = new ContainerOpenPacket();
break;
case ProtocolInfo::CONTAINER_CLOSE_PACKET:
$data = new ContainerClosePacket();
break;
case ProtocolInfo::CONTAINER_SET_SLOT_PACKET:
$data = new ContainerSetSlotPacket();
break;
case ProtocolInfo::CONTAINER_SET_DATA_PACKET:
$data = new ContainerSetDataPacket();
break;
case ProtocolInfo::CONTAINER_SET_CONTENT_PACKET:
$data = new ContainerSetContentPacket();
break;
case ProtocolInfo::CHAT_PACKET:
$data = new ChatPacket();
break;
case ProtocolInfo::ADVENTURE_SETTINGS_PACKET:
$data = new AdventureSettingsPacket();
break;
case ProtocolInfo::ENTITY_DATA_PACKET:
$data = new EntityDataPacket();
break;
case ProtocolInfo::UNLOAD_CHUNK_PACKET:
$data = new UnloadChunkPacket();
break;
default:
$data = new UnknownPacket();
$data->packetID = $pid;
break;
} }
return null;
}
public function cleanPacketPool(){
foreach($this->packetPool as $class){
/** @var DataPacket $class */
$class::cleanPool();
}
}
private function registerPackets(){
$this->registerPacket(ProtocolInfo::LOGIN_PACKET, LoginPacket::class);
$this->registerPacket(ProtocolInfo::LOGIN_STATUS_PACKET, LoginStatusPacket::class);
$this->registerPacket(ProtocolInfo::MESSAGE_PACKET, MessagePacket::class);
$this->registerPacket(ProtocolInfo::SET_TIME_PACKET, SetTimePacket::class);
$this->registerPacket(ProtocolInfo::START_GAME_PACKET, StartGamePacket::class);
$this->registerPacket(ProtocolInfo::ADD_MOB_PACKET, AddMobPacket::class);
$this->registerPacket(ProtocolInfo::ADD_PLAYER_PACKET, AddPlayerPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_PLAYER_PACKET, RemovePlayerPacket::class);
$this->registerPacket(ProtocolInfo::ADD_ENTITY_PACKET, AddEntityPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_ENTITY_PACKET, RemoveEntityPacket::class);
$this->registerPacket(ProtocolInfo::ADD_ITEM_ENTITY_PACKET, AddItemEntityPacket::class);
$this->registerPacket(ProtocolInfo::TAKE_ITEM_ENTITY_PACKET, TakeItemEntityPacket::class);
$this->registerPacket(ProtocolInfo::MOVE_ENTITY_PACKET, MoveEntityPacket::class);
$this->registerPacket(ProtocolInfo::ROTATE_HEAD_PACKET, RotateHeadPacket::class);
$this->registerPacket(ProtocolInfo::MOVE_PLAYER_PACKET, MovePlayerPacket::class);
$this->registerPacket(ProtocolInfo::REMOVE_BLOCK_PACKET, RemoveBlockPacket::class);
$this->registerPacket(ProtocolInfo::UPDATE_BLOCK_PACKET, UpdateBlockPacket::class);
$this->registerPacket(ProtocolInfo::ADD_PAINTING_PACKET, AddPaintingPacket::class);
$this->registerPacket(ProtocolInfo::EXPLODE_PACKET, ExplodePacket::class);
$this->registerPacket(ProtocolInfo::LEVEL_EVENT_PACKET, LevelEventPacket::class);
$this->registerPacket(ProtocolInfo::TILE_EVENT_PACKET, TileEventPacket::class);
$this->registerPacket(ProtocolInfo::ENTITY_EVENT_PACKET, EntityEventPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_EQUIPMENT_PACKET, PlayerEquipmentPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET, PlayerArmorEquipmentPacket::class);
$this->registerPacket(ProtocolInfo::INTERACT_PACKET, InteractPacket::class);
$this->registerPacket(ProtocolInfo::USE_ITEM_PACKET, UseItemPacket::class);
$this->registerPacket(ProtocolInfo::PLAYER_ACTION_PACKET, PlayerActionPacket::class);
$this->registerPacket(ProtocolInfo::HURT_ARMOR_PACKET, HurtArmorPacket::class);
$this->registerPacket(ProtocolInfo::SET_ENTITY_DATA_PACKET, SetEntityDataPacket::class);
$this->registerPacket(ProtocolInfo::SET_ENTITY_MOTION_PACKET, SetEntityMotionPacket::class);
$this->registerPacket(ProtocolInfo::SET_HEALTH_PACKET, SetHealthPacket::class);
$this->registerPacket(ProtocolInfo::SET_SPAWN_POSITION_PACKET, SetSpawnPositionPacket::class);
$this->registerPacket(ProtocolInfo::ANIMATE_PACKET, AnimatePacket::class);
$this->registerPacket(ProtocolInfo::RESPAWN_PACKET, RespawnPacket::class);
$this->registerPacket(ProtocolInfo::SEND_INVENTORY_PACKET, SendInventoryPacket::class);
$this->registerPacket(ProtocolInfo::DROP_ITEM_PACKET, DropItemPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_OPEN_PACKET, ContainerOpenPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_CLOSE_PACKET, ContainerClosePacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_SLOT_PACKET, ContainerSetSlotPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_DATA_PACKET, ContainerSetDataPacket::class);
$this->registerPacket(ProtocolInfo::CONTAINER_SET_CONTENT_PACKET, ContainerSetContentPacket::class);
$this->registerPacket(ProtocolInfo::CHAT_PACKET, ChatPacket::class);
$this->registerPacket(ProtocolInfo::ADVENTURE_SETTINGS_PACKET, AdventureSettingsPacket::class);
$this->registerPacket(ProtocolInfo::ENTITY_DATA_PACKET, EntityDataPacket::class);
$this->registerPacket(ProtocolInfo::UNLOAD_CHUNK_PACKET, UnloadChunkPacket::class);
}
private function getPacket($buffer){
$pid = ord($buffer{0});
if(($data = $this->getPacketFromPool($pid)) === null){
$data = UnknownPacket::getFromPool();
$data->packetID = $pid;
}
$data->setBuffer(substr($buffer, 1)); $data->setBuffer(substr($buffer, 1));
return $data; return $data;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class AddEntityPacket extends DataPacket{ class AddEntityPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $type; public $type;
public $x; public $x;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class AddItemEntityPacket extends DataPacket{ class AddItemEntityPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $item; public $item;
public $x; public $x;

View File

@ -24,6 +24,9 @@ namespace pocketmine\network\protocol;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
class AddMobPacket extends DataPacket{ class AddMobPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $type; public $type;
public $x; public $x;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class AddPaintingPacket extends DataPacket{ class AddPaintingPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $x; public $x;
public $y; public $y;

View File

@ -24,6 +24,9 @@ namespace pocketmine\network\protocol;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
class AddPlayerPacket extends DataPacket{ class AddPlayerPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $clientID; public $clientID;
public $username; public $username;
public $eid; public $eid;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class AdventureSettingsPacket extends DataPacket{ class AdventureSettingsPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $flags; public $flags;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class AnimatePacket extends DataPacket{ class AnimatePacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $action; public $action;
public $eid; public $eid;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class ChatPacket extends DataPacket{ class ChatPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $message; public $message;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class ContainerClosePacket extends DataPacket{ class ContainerClosePacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $windowid; public $windowid;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class ContainerOpenPacket extends DataPacket{ class ContainerOpenPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $windowid; public $windowid;
public $type; public $type;
public $slots; public $slots;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class ContainerSetContentPacket extends DataPacket{ class ContainerSetContentPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $windowid; public $windowid;
public $slots = []; public $slots = [];
public $hotbar = []; public $hotbar = [];
@ -31,6 +34,12 @@ class ContainerSetContentPacket extends DataPacket{
return Info::CONTAINER_SET_CONTENT_PACKET; return Info::CONTAINER_SET_CONTENT_PACKET;
} }
public function clean(){
$this->slots = [];
$this->hotbar = [];
return parent::clean();
}
public function decode(){ public function decode(){
$this->windowid = $this->getByte(); $this->windowid = $this->getByte();
$count = $this->getShort(); $count = $this->getShort();

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class ContainerSetDataPacket extends DataPacket{ class ContainerSetDataPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $windowid; public $windowid;
public $property; public $property;
public $value; public $value;

View File

@ -24,6 +24,9 @@ namespace pocketmine\network\protocol;
use pocketmine\item\Item; use pocketmine\item\Item;
class ContainerSetSlotPacket extends DataPacket{ class ContainerSetSlotPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $windowid; public $windowid;
public $slot; public $slot;
/** @var Item */ /** @var Item */

View File

@ -25,6 +25,26 @@ use pocketmine\item\Item;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
abstract class DataPacket extends \stdClass{ abstract class DataPacket extends \stdClass{
/** @var DataPacket[] */
public static $pool = [];
public static $next = 0;
public static function getFromPool(){
if(static::$next >= count(static::$pool)){
$pk = static::class;
static::$pool[] = new $pk;
}
return static::$pool[static::$next++]->clean();
}
public static function cleanPool(){
if(static::$next > 4096){
static::$pool = [];
}
static::$next = 0;
}
private $offset = 0; private $offset = 0;
public $buffer = ""; public $buffer = "";
public $isEncoded = false; public $isEncoded = false;
@ -172,4 +192,11 @@ abstract class DataPacket extends \stdClass{
protected function feof(){ protected function feof(){
return !isset($this->buffer{$this->offset}); return !isset($this->buffer{$this->offset});
} }
public function clean(){
$this->buffer = null;
$this->isEncoded = false;
$this->offset = 0;
return $this;
}
} }

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class DropItemPacket extends DataPacket{ class DropItemPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $unknown; public $unknown;
public $item; public $item;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class EntityDataPacket extends DataPacket{ class EntityDataPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $x; public $x;
public $y; public $y;
public $z; public $z;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class EntityEventPacket extends DataPacket{ class EntityEventPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $event; public $event;

View File

@ -23,16 +23,24 @@ namespace pocketmine\network\protocol;
class ExplodePacket extends DataPacket{ class ExplodePacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $x; public $x;
public $y; public $y;
public $z; public $z;
public $radius; public $radius;
public $records; public $records = [];
public function pid(){ public function pid(){
return Info::EXPLODE_PACKET; return Info::EXPLODE_PACKET;
} }
public function clean(){
$this->records = [];
return parent::clean();
}
public function decode(){ public function decode(){
} }

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class FullChunkDataPacket extends DataPacket{ class FullChunkDataPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $chunkX; public $chunkX;
public $chunkZ; public $chunkZ;
public $data; public $data;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class HurtArmorPacket extends DataPacket{ class HurtArmorPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $health; public $health;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class InteractPacket extends DataPacket{ class InteractPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $action; public $action;
public $eid; public $eid;
public $target; public $target;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class LevelEventPacket extends DataPacket{ class LevelEventPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $evid; public $evid;
public $x; public $x;
public $y; public $y;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class LoginPacket extends DataPacket{ class LoginPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $username; public $username;
public $protocol1; public $protocol1;
public $protocol2; public $protocol2;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class LoginStatusPacket extends DataPacket{ class LoginStatusPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $status; public $status;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class MessagePacket extends DataPacket{ class MessagePacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $source; public $source;
public $message; public $message;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class MoveEntityPacket extends DataPacket{ class MoveEntityPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
// eid, x, y, z, yaw, pitch // eid, x, y, z, yaw, pitch
/** @var array[] */ /** @var array[] */
@ -32,6 +35,11 @@ class MoveEntityPacket extends DataPacket{
return Info::MOVE_ENTITY_PACKET; return Info::MOVE_ENTITY_PACKET;
} }
public function clean(){
$this->entities = [];
return parent::clean();
}
public function decode(){ public function decode(){
} }

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class MovePlayerPacket extends DataPacket{ class MovePlayerPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $x; public $x;
public $y; public $y;
@ -36,6 +39,11 @@ class MovePlayerPacket extends DataPacket{
return Info::MOVE_PLAYER_PACKET; return Info::MOVE_PLAYER_PACKET;
} }
public function clean(){
$this->teleport = false;
return parent::clean();
}
public function decode(){ public function decode(){
$this->eid = $this->getInt(); $this->eid = $this->getInt();
$this->x = $this->getFloat(); $this->x = $this->getFloat();

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class PlayerActionPacket extends DataPacket{ class PlayerActionPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $action; public $action;
public $x; public $x;
public $y; public $y;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class PlayerArmorEquipmentPacket extends DataPacket{ class PlayerArmorEquipmentPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $slots = []; public $slots = [];

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class PlayerEquipmentPacket extends DataPacket{ class PlayerEquipmentPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $item; public $item;
public $meta; public $meta;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class RemoveBlockPacket extends DataPacket{ class RemoveBlockPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $x; public $x;
public $y; public $y;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class RemoveEntityPacket extends DataPacket{ class RemoveEntityPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class RemovePlayerPacket extends DataPacket{ class RemovePlayerPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $clientID; public $clientID;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class RespawnPacket extends DataPacket{ class RespawnPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $x; public $x;
public $y; public $y;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class RotateHeadPacket extends DataPacket{ class RotateHeadPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
// eid, yaw // eid, yaw
/** @var array[] */ /** @var array[] */
@ -32,6 +35,11 @@ class RotateHeadPacket extends DataPacket{
return Info::ROTATE_HEAD_PACKET; return Info::ROTATE_HEAD_PACKET;
} }
public function clean(){
$this->entities = [];
return parent::clean();
}
public function decode(){ public function decode(){
} }

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class SendInventoryPacket extends DataPacket{ class SendInventoryPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $windowid; public $windowid;
public $slots = []; public $slots = [];

View File

@ -24,6 +24,9 @@ namespace pocketmine\network\protocol;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
class SetEntityDataPacket extends DataPacket{ class SetEntityDataPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $eid; public $eid;
public $metadata; public $metadata;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class SetEntityMotionPacket extends DataPacket{ class SetEntityMotionPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
// eid, motX, motY, motZ // eid, motX, motY, motZ
/** @var array[] */ /** @var array[] */
@ -32,6 +35,11 @@ class SetEntityMotionPacket extends DataPacket{
return Info::SET_ENTITY_MOTION_PACKET; return Info::SET_ENTITY_MOTION_PACKET;
} }
public function clean(){
$this->entities = [];
return parent::clean();
}
public function decode(){ public function decode(){
} }

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class SetHealthPacket extends DataPacket{ class SetHealthPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $health; public $health;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class SetSpawnPositionPacket extends DataPacket{ class SetSpawnPositionPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $x; public $x;
public $z; public $z;
public $y; public $y;

View File

@ -25,6 +25,9 @@ namespace pocketmine\network\protocol;
use pocketmine\level\Level; use pocketmine\level\Level;
class SetTimePacket extends DataPacket{ class SetTimePacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $time; public $time;
public $started = true; public $started = true;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class StartGamePacket extends DataPacket{ class StartGamePacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $seed; public $seed;
public $generator; public $generator;
public $gamemode; public $gamemode;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class TakeItemEntityPacket extends DataPacket{ class TakeItemEntityPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $target; public $target;
public $eid; public $eid;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class TileEventPacket extends DataPacket{ class TileEventPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $x; public $x;
public $y; public $y;
public $z; public $z;

View File

@ -24,6 +24,9 @@ namespace pocketmine\network\protocol;
class UnknownPacket extends DataPacket{ class UnknownPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $packetID = -1; public $packetID = -1;
public function pid(){ public function pid(){

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class UnloadChunkPacket extends DataPacket{ class UnloadChunkPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $chunkX; public $chunkX;
public $chunkZ; public $chunkZ;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class UpdateBlockPacket extends DataPacket{ class UpdateBlockPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $x; public $x;
public $z; public $z;
public $y; public $y;

View File

@ -23,6 +23,9 @@ namespace pocketmine\network\protocol;
class UseItemPacket extends DataPacket{ class UseItemPacket extends DataPacket{
public static $pool = [];
public static $next = 0;
public $x; public $x;
public $y; public $y;
public $z; public $z;

View File

@ -252,13 +252,13 @@ class Furnace extends Tile implements InventoryHolder, Container{
foreach($this->getInventory()->getViewers() as $player){ foreach($this->getInventory()->getViewers() as $player){
$windowId = $player->getWindowId($this->getInventory()); $windowId = $player->getWindowId($this->getInventory());
if($windowId > 0){ if($windowId > 0){
$pk = new ContainerSetDataPacket; $pk = ContainerSetDataPacket::getFromPool();
$pk->windowid = $windowId; $pk->windowid = $windowId;
$pk->property = 0; //Smelting $pk->property = 0; //Smelting
$pk->value = floor($this->namedtag["CookTime"]); $pk->value = floor($this->namedtag["CookTime"]);
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new ContainerSetDataPacket; $pk = ContainerSetDataPacket::getFromPool();
$pk->windowid = $windowId; $pk->windowid = $windowId;
$pk->property = 1; //Fire icon $pk->property = 1; //Fire icon
$pk->value = $this->namedtag["BurnTicks"]; $pk->value = $this->namedtag["BurnTicks"];

View File

@ -36,7 +36,7 @@ abstract class Spawnable extends Tile{
$nbt = new NBT(NBT::LITTLE_ENDIAN); $nbt = new NBT(NBT::LITTLE_ENDIAN);
$nbt->setData($this->getSpawnCompound()); $nbt->setData($this->getSpawnCompound());
$pk = new EntityDataPacket; $pk = EntityDataPacket::getFromPool();
$pk->x = $this->x; $pk->x = $this->x;
$pk->y = $this->y; $pk->y = $this->y;
$pk->z = $this->z; $pk->z = $this->z;