diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 82811922b..4a0f422a2 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -581,7 +581,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ return; } - $pk = new FullChunkDataPacket; + $pk = FullChunkDataPacket::getFromPool(); $pk->chunkX = $x; $pk->chunkZ = $z; $pk->data = $payload; @@ -645,7 +645,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->blocked = false; - $pk = new SetTimePacket; + $pk = SetTimePacket::getFromPool(); $pk->time = $this->level->getTime(); $pk->started = $this->level->stopTime == false; $this->dataPacket($pk); @@ -834,7 +834,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $level = $pos->getLevel(); } $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); - $pk = new SetSpawnPositionPacket; + $pk = SetSpawnPositionPacket::getFromPool(); $pk->x = (int) $this->spawnPosition->x; $pk->y = (int) $this->spawnPosition->y; $pk->z = (int) $this->spawnPosition->z; @@ -947,7 +947,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $spawnPosition = $this->getSpawn(); - $pk = new StartGamePacket; + $pk = StartGamePacket::getFromPool(); $pk->seed = $this->level->getSeed(); $pk->x = $this->x; $pk->y = $this->y + $this->getEyeHeight(); @@ -1017,7 +1017,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $flags |= 0x20; //Show Nametags } - $pk = new AdventureSettingsPacket; + $pk = AdventureSettingsPacket::getFromPool(); $pk->flags = $flags; $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 $this->teleport($ev->getTo()); }else{ - $pk = new MovePlayerPacket; + $pk = MovePlayerPacket::getFromPool(); $pk->eid = $this->id; $pk->x = $this->x; $pk->y = $this->y; @@ -1140,7 +1140,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } if($revert){ - $pk = new MovePlayerPacket; + $pk = MovePlayerPacket::getFromPool(); $pk->eid = 0; $pk->x = $from->x; $pk->y = $from->y + $this->getEyeHeight() + 0.01; @@ -1206,11 +1206,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ continue; } - $pk = new TakeItemEntityPacket; + $pk = TakeItemEntityPacket::getFromPool(); $pk->eid = 0; $pk->target = $entity->getID(); $this->dataPacket($pk); - $pk = new TakeItemEntityPacket; + $pk = TakeItemEntityPacket::getFromPool(); $pk->eid = $this->getID(); $pk->target = $entity->getID(); Server::broadcastPacket($entity->getViewers(), $pk); @@ -1240,11 +1240,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; } - $pk = new TakeItemEntityPacket; + $pk = TakeItemEntityPacket::getFromPool(); $pk->eid = 0; $pk->target = $entity->getID(); $this->dataPacket($pk); - $pk = new TakeItemEntityPacket; + $pk = TakeItemEntityPacket::getFromPool(); $pk->eid = $this->getID(); $pk->target = $entity->getID(); 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){ - $pk = new LoginStatusPacket; + $pk = LoginStatusPacket::getFromPool(); $pk->status = 1; $this->dataPacket($pk); }else{ - $pk = new LoginStatusPacket; + $pk = LoginStatusPacket::getFromPool(); $pk->status = 2; $this->dataPacket($pk); } @@ -1413,7 +1413,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->inventory->setHeldItemSlot(0); } - $pk = new LoginStatusPacket; + $pk = LoginStatusPacket::getFromPool(); $pk->status = 0; $this->dataPacket($pk); @@ -1425,7 +1425,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->dead = false; - $pk = new StartGamePacket; + $pk = StartGamePacket::getFromPool(); $pk->seed = $this->level->getSeed(); $pk->x = $this->x; $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 $this->dataPacket($pk); - $pk = new SetTimePacket(); + $pk = SetTimePacket::getFromPool(); $pk->time = $this->level->getTime(); + $pk->started = $this->level->stopTime == false; $this->dataPacket($pk); - $pk = new SetSpawnPositionPacket; + $pk = SetSpawnPositionPacket::getFromPool(); $pk->x = (int) $spawnPosition->x; $pk->y = (int) $spawnPosition->y; $pk->z = (int) $spawnPosition->z; $this->dataPacket($pk); - $pk = new SetHealthPacket(); + $pk = SetHealthPacket::getFromPool(); $pk->health = $this->getHealth(); $this->dataPacket($pk); if($this->getHealth() <= 0){ @@ -1474,7 +1475,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $revert = ($this->dead === true or $this->spawned !== true); if($revert or ($this->forceMovement instanceof Vector3 and $newPos->distance($this->forceMovement) > 0.2)){ - $pk = new MovePlayerPacket(); + $pk = MovePlayerPacket::getFromPool(); $pk->eid = 0; $pk->x = $this->x; $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); $block = $target->getSide($packet->face); - $pk = new UpdateBlockPacket; + $pk = UpdateBlockPacket::getFromPool(); $pk->x = $target->x; $pk->y = $target->y; $pk->z = $target->z; @@ -1573,7 +1574,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $pk->meta = $target->getDamage(); $this->dataPacket($pk); - $pk = new UpdateBlockPacket; + $pk = UpdateBlockPacket::getFromPool(); $pk->x = $block->x; $pk->y = $block->y; $pk->z = $block->z; @@ -1612,7 +1613,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); - $pk = new UpdateBlockPacket; + $pk = UpdateBlockPacket::getFromPool(); $pk->x = $target->x; $pk->y = $target->y; $pk->z = $target->z; @@ -1620,7 +1621,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $pk->meta = $target->getDamage(); $this->dataPacket($pk); - $pk = new UpdateBlockPacket; + $pk = UpdateBlockPacket::getFromPool(); $pk->x = $block->x; $pk->y = $block->y; $pk->z = $block->z; @@ -1738,7 +1739,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $target = $this->level->getBlock($vector); $tile = $this->level->getTile($vector); - $pk = new UpdateBlockPacket; + $pk = UpdateBlockPacket::getFromPool(); $pk->x = $target->x; $pk->y = $target->y; $pk->z = $target->z; @@ -1887,7 +1888,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; } - $pk = new AnimatePacket(); + $pk = AnimatePacket::getFromPool(); $pk->eid = $this->getID(); $pk->action = $ev->getAnimationType(); Server::broadcastPacket($this->getViewers(), $pk); @@ -1961,7 +1962,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; } - $pk = new EntityEventPacket(); + $pk = EntityEventPacket::getFromPool(); $pk->eid = 0; $pk->event = 9; $this->dataPacket($pk); @@ -2247,7 +2248,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $mes = explode("\n", $message); foreach($mes as $m){ if($m !== ""){ - $pk = new MessagePacket; + $pk = MessagePacket::getFromPool(); $pk->source = ""; //Do not use this ;) $pk->message = $m; $this->dataPacket($pk); @@ -2462,7 +2463,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ public function setHealth($amount){ parent::setHealth($amount); if($this->spawned === true){ - $pk = new SetHealthPacket(); + $pk = SetHealthPacket::getFromPool(); $pk->health = $this->getHealth(); $this->dataPacket($pk); } @@ -2492,7 +2493,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ parent::attack($damage, $source); if($this->getLastDamageCause() === $source){ - $pk = new EntityEventPacket(); + $pk = EntityEventPacket::getFromPool(); $pk->eid = 0; $pk->event = 2; $this->dataPacket($pk); @@ -2537,7 +2538,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->forceMovement = $pos; $this->newPosition = $pos; - $pk = new MovePlayerPacket; + $pk = MovePlayerPacket::getFromPool(); $pk->eid = 0; $pk->x = $this->x; $pk->y = $this->y + $this->getEyeHeight() + 0.01; diff --git a/src/pocketmine/block/Bed.php b/src/pocketmine/block/Bed.php index e012b867f..f8137cf78 100644 --- a/src/pocketmine/block/Bed.php +++ b/src/pocketmine/block/Bed.php @@ -53,7 +53,7 @@ class Bed extends Transparent{ $isNight = ($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE); if($player instanceof Player and !$isNight){ - $pk = new ChatPacket; + $pk = ChatPacket::getFromPool(); $pk->message = "You can only sleep at night"; $player->dataPacket($pk); @@ -77,7 +77,7 @@ class Bed extends Transparent{ $b = $blockWest; }else{ if($player instanceof Player){ - $pk = new ChatPacket; + $pk = ChatPacket::getFromPool(); $pk->message = "This bed is incomplete"; $player->dataPacket($pk); } @@ -87,7 +87,7 @@ class Bed extends Transparent{ } if($player instanceof Player and $player->sleepOn($b) === false){ - $pk = new ChatPacket; + $pk = ChatPacket::getFromPool(); $pk->message = "This bed is occupied"; $player->dataPacket($pk); } diff --git a/src/pocketmine/block/Door.php b/src/pocketmine/block/Door.php index 04506d99b..7092e6530 100644 --- a/src/pocketmine/block/Door.php +++ b/src/pocketmine/block/Door.php @@ -271,7 +271,7 @@ abstract class Door extends Transparent{ if($player instanceof Player){ unset($players[$player->getID()]); } - $pk = new LevelEventPacket; + $pk = LevelEventPacket::getFromPool(); $pk->x = $this->x; $pk->y = $this->y; $pk->z = $this->z; @@ -290,7 +290,7 @@ abstract class Door extends Transparent{ if($player instanceof Player){ unset($players[$player->getID()]); } - $pk = new LevelEventPacket; + $pk = LevelEventPacket::getFromPool(); $pk->x = $this->x; $pk->y = $this->y; $pk->z = $this->z; diff --git a/src/pocketmine/entity/Arrow.php b/src/pocketmine/entity/Arrow.php index b24ad1116..1e4ec4c78 100644 --- a/src/pocketmine/entity/Arrow.php +++ b/src/pocketmine/entity/Arrow.php @@ -209,7 +209,7 @@ class Arrow extends Projectile{ } public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); + $pk = AddEntityPacket::getFromPool(); $pk->type = Arrow::NETWORK_ID; $pk->eid = $this->getID(); $pk->x = $this->x; @@ -218,7 +218,7 @@ class Arrow extends Projectile{ $pk->did = 0; //TODO: send motion here $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; diff --git a/src/pocketmine/entity/DroppedItem.php b/src/pocketmine/entity/DroppedItem.php index 6d685134b..9584e2fe5 100644 --- a/src/pocketmine/entity/DroppedItem.php +++ b/src/pocketmine/entity/DroppedItem.php @@ -219,7 +219,7 @@ class DroppedItem extends Entity{ } public function spawnTo(Player $player){ - $pk = new AddItemEntityPacket(); + $pk = AddItemEntityPacket::getFromPool(); $pk->eid = $this->getID(); $pk->x = $this->x; $pk->y = $this->y; @@ -230,7 +230,7 @@ class DroppedItem extends Entity{ $pk->item = $this->getItem(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket; + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 990142918..3c2118f65 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -270,7 +270,7 @@ abstract class Entity extends Location implements Metadatable{ $player = [$player]; } - $pk = new SetEntityDataPacket(); + $pk = SetEntityDataPacket::getFromPool(); $pk->eid = $this->id; $pk->metadata = $this->getData(); $pk->encode(); @@ -278,7 +278,7 @@ abstract class Entity extends Location implements Metadatable{ foreach($player as $p){ if($p === $this){ /** @var Player $p */ - $pk2 = new SetEntityDataPacket(); + $pk2 = SetEntityDataPacket::getFromPool(); $pk2->eid = 0; $pk2->metadata = $this->getData(); $p->dataPacket($pk2); @@ -293,7 +293,7 @@ abstract class Entity extends Location implements Metadatable{ */ public function despawnFrom(Player $player){ if(isset($this->hasSpawned[$player->getID()])){ - $pk = new RemoveEntityPacket; + $pk = RemoveEntityPacket::getFromPool(); $pk->eid = $this->id; $player->dataPacket($pk); unset($this->hasSpawned[$player->getID()]); @@ -532,7 +532,7 @@ abstract class Entity extends Location implements Metadatable{ $this->lastPitch = $this->pitch; if($this instanceof Human){ - $pk = new MovePlayerPacket; + $pk = MovePlayerPacket::getFromPool(); $pk->eid = $this->id; $pk->x = $this->x; $pk->y = $this->y; @@ -542,7 +542,7 @@ abstract class Entity extends Location implements Metadatable{ $pk->bodyYaw = $this->yaw; }else{ //TODO: add to move list - $pk = new MoveEntityPacket(); + $pk = MoveEntityPacket::getFromPool(); $pk->entities = [ [$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->lastMotionZ = $this->motionZ; - $pk = new SetEntityMotionPacket; + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; @@ -726,7 +726,7 @@ abstract class Entity extends Location implements Metadatable{ $this->level->addEntity($this); if($this instanceof Player){ $this->usedChunks = []; - $pk = new SetTimePacket(); + $pk = SetTimePacket::getFromPool(); $pk->time = $this->level->getTime(); $pk->started = $this->level->stopTime == false; $this->dataPacket($pk); @@ -1100,7 +1100,7 @@ abstract class Entity extends Location implements Metadatable{ if(!$this->justCreated){ if($this instanceof Player){ - $pk = new SetEntityMotionPacket; + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [0, $this->motionX, $this->motionY, $this->motionZ] ]; diff --git a/src/pocketmine/entity/FallingBlock.php b/src/pocketmine/entity/FallingBlock.php index 250777810..e05a6aa92 100644 --- a/src/pocketmine/entity/FallingBlock.php +++ b/src/pocketmine/entity/FallingBlock.php @@ -150,7 +150,7 @@ class FallingBlock extends Entity{ } public function spawnTo(Player $player){ - $pk = new AddEntityPacket; + $pk = AddEntityPacket::getFromPool(); $pk->type = FallingBlock::NETWORK_ID; $pk->eid = $this->getID(); $pk->x = $this->x; @@ -159,7 +159,7 @@ class FallingBlock extends Entity{ $pk->did = -$this->getBlock(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket; + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index d31e9f092..962860f1a 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -156,7 +156,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ if($player !== $this and !isset($this->hasSpawned[$player->getID()])){ $this->hasSpawned[$player->getID()] = $player; - $pk = new AddPlayerPacket; + $pk = AddPlayerPacket::getFromPool(); $pk->clientID = 0; if($player->getRemoveFormat()){ $pk->username = TextFormat::clean($this->nameTag); @@ -174,7 +174,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ $pk->metadata = $this->getData(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket; + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; @@ -188,7 +188,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ public function despawnFrom(Player $player){ if(isset($this->hasSpawned[$player->getID()])){ - $pk = new RemovePlayerPacket; + $pk = RemovePlayerPacket::getFromPool(); $pk->eid = $this->id; $pk->clientID = 0; $player->dataPacket($pk); diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index f201d736e..c563ca964 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -76,7 +76,7 @@ abstract class Living extends Entity implements Damageable{ } } - $pk = new EntityEventPacket(); + $pk = EntityEventPacket::getFromPool(); $pk->eid = $this->getID(); $pk->event = 2; //Ouch! Server::broadcastPacket($this->hasSpawned, $pk); diff --git a/src/pocketmine/entity/PrimedTNT.php b/src/pocketmine/entity/PrimedTNT.php index d1e871132..335c3943a 100644 --- a/src/pocketmine/entity/PrimedTNT.php +++ b/src/pocketmine/entity/PrimedTNT.php @@ -140,7 +140,7 @@ class PrimedTNT extends Entity implements Explosive{ } public function spawnTo(Player $player){ - $pk = new AddEntityPacket(); + $pk = AddEntityPacket::getFromPool(); $pk->type = PrimedTNT::NETWORK_ID; $pk->eid = $this->getID(); $pk->x = $this->x; @@ -149,7 +149,7 @@ class PrimedTNT extends Entity implements Explosive{ $pk->did = 0; $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; diff --git a/src/pocketmine/entity/Villager.php b/src/pocketmine/entity/Villager.php index b2f01cc50..ed01d39e9 100644 --- a/src/pocketmine/entity/Villager.php +++ b/src/pocketmine/entity/Villager.php @@ -55,7 +55,7 @@ class Villager extends Creature implements NPC, Ageable{ } public function spawnTo(Player $player){ - $pk = new AddMobPacket(); + $pk = AddMobPacket::getFromPool(); $pk->eid = $this->getID(); $pk->type = Villager::NETWORK_ID; $pk->x = $this->x; @@ -66,7 +66,7 @@ class Villager extends Creature implements NPC, Ageable{ $pk->metadata = $this->getData(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; diff --git a/src/pocketmine/entity/Zombie.php b/src/pocketmine/entity/Zombie.php index 5d7c95442..8b45b8787 100644 --- a/src/pocketmine/entity/Zombie.php +++ b/src/pocketmine/entity/Zombie.php @@ -46,7 +46,7 @@ class Zombie extends Monster{ public function spawnTo(Player $player){ - $pk = new AddMobPacket(); + $pk = AddMobPacket::getFromPool(); $pk->eid = $this->getID(); $pk->type = Zombie::NETWORK_ID; $pk->x = $this->x; @@ -57,7 +57,7 @@ class Zombie extends Monster{ $pk->metadata = $this->getData(); $player->dataPacket($pk); - $pk = new SetEntityMotionPacket(); + $pk = SetEntityMotionPacket::getFromPool(); $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 6353e2ca3..fce466242 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -384,7 +384,7 @@ abstract class BaseInventory implements Inventory{ $target = [$target]; } - $pk = new ContainerSetContentPacket(); + $pk = ContainerSetContentPacket::getFromPool(); $pk->slots = []; for($i = 0; $i < $this->getSize(); ++$i){ $pk->slots[$i] = $this->getItem($i); @@ -409,7 +409,7 @@ abstract class BaseInventory implements Inventory{ $target = [$target]; } - $pk = new ContainerSetSlotPacket; + $pk = ContainerSetSlotPacket::getFromPool(); $pk->slot = $index; $pk->item = clone $this->getItem($index); diff --git a/src/pocketmine/inventory/ChestInventory.php b/src/pocketmine/inventory/ChestInventory.php index 5ed416101..8d4ba3ff4 100644 --- a/src/pocketmine/inventory/ChestInventory.php +++ b/src/pocketmine/inventory/ChestInventory.php @@ -43,7 +43,7 @@ class ChestInventory extends ContainerInventory{ parent::onOpen($who); if(count($this->getViewers()) === 1){ - $pk = new TileEventPacket; + $pk = TileEventPacket::getFromPool(); $pk->x = $this->getHolder()->getX(); $pk->y = $this->getHolder()->getY(); $pk->z = $this->getHolder()->getZ(); @@ -57,7 +57,7 @@ class ChestInventory extends ContainerInventory{ public function onClose(Player $who){ if(count($this->getViewers()) === 1){ - $pk = new TileEventPacket; + $pk = TileEventPacket::getFromPool(); $pk->x = $this->getHolder()->getX(); $pk->y = $this->getHolder()->getY(); $pk->z = $this->getHolder()->getZ(); diff --git a/src/pocketmine/inventory/ContainerInventory.php b/src/pocketmine/inventory/ContainerInventory.php index a027e68ad..ea16b0574 100644 --- a/src/pocketmine/inventory/ContainerInventory.php +++ b/src/pocketmine/inventory/ContainerInventory.php @@ -29,7 +29,7 @@ use pocketmine\Player; abstract class ContainerInventory extends BaseInventory{ public function onOpen(Player $who){ parent::onOpen($who); - $pk = new ContainerOpenPacket; + $pk = ContainerOpenPacket::getFromPool(); $pk->windowid = $who->getWindowId($this); $pk->type = $this->getType()->getNetworkType(); $pk->slots = $this->getSize(); @@ -47,7 +47,7 @@ abstract class ContainerInventory extends BaseInventory{ } public function onClose(Player $who){ - $pk = new ContainerClosePacket; + $pk = ContainerClosePacket::getFromPool(); $pk->windowid = $who->getWindowId($this); $who->dataPacket($pk); parent::onClose($who); diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php index 8c5cd325e..05e3eb894 100644 --- a/src/pocketmine/inventory/PlayerInventory.php +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -66,7 +66,7 @@ class PlayerInventory extends BaseInventory{ $this->itemInHandIndex = $index; $item = $this->getItemInHand(); - $pk = new PlayerEquipmentPacket; + $pk = PlayerEquipmentPacket::getFromPool(); $pk->eid = $this->getHolder()->getID(); $pk->item = $item->getID(); $pk->meta = $item->getDamage(); @@ -126,7 +126,7 @@ class PlayerInventory extends BaseInventory{ $item = $this->getItemInHand(); - $pk = new PlayerEquipmentPacket; + $pk = PlayerEquipmentPacket::getFromPool(); $pk->eid = $this->getHolder()->getID(); $pk->item = $item->getID(); $pk->meta = $item->getDamage(); @@ -286,7 +286,7 @@ class PlayerInventory extends BaseInventory{ } } - $pk = new PlayerArmorEquipmentPacket; + $pk = PlayerArmorEquipmentPacket::getFromPool(); $pk->eid = $this->getHolder()->getID(); $pk->slots = $slots; $pk->encode(); @@ -298,7 +298,7 @@ class PlayerInventory extends BaseInventory{ //$pk2 = clone $pk; //$pk2->eid = 0; - $pk2 = new ContainerSetContentPacket; + $pk2 = ContainerSetContentPacket::getFromPool(); $pk2->windowid = 0x78; //Armor window id constant $pk2->slots = $armor; $player->dataPacket($pk2); @@ -333,7 +333,7 @@ class PlayerInventory extends BaseInventory{ $target = [$target]; } - $pk = new ContainerSetContentPacket(); + $pk = ContainerSetContentPacket::getFromPool(); $pk->slots = []; for($i = 0; $i < $this->getSize(); ++$i){ //Do not send armor by error here $pk->slots[$i] = $this->getItem($i); @@ -365,7 +365,7 @@ class PlayerInventory extends BaseInventory{ $target = [$target]; } - $pk = new ContainerSetSlotPacket; + $pk = ContainerSetSlotPacket::getFromPool(); $pk->slot = $index; $pk->item = clone $this->getItem($index); diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index 74a2f36cb..47ce62703 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -217,7 +217,7 @@ class Explosion{ $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); } - $pk = new ExplodePacket; + $pk = ExplodePacket::getFromPool(); $pk->x = $this->source->x; $pk->y = $this->source->y; $pk->z = $this->source->z; diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 63bf1ed7d..5d3154ea8 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -442,7 +442,7 @@ class Level implements ChunkManager, Metadatable{ * Changes to this function won't be recorded on the version. */ public function sendTime(){ - $pk = new SetTimePacket; + $pk = SetTimePacket::getFromPool(); $pk->time = (int) $this->time; $pk->started = $this->stopTime == false; @@ -539,7 +539,7 @@ class Level implements ChunkManager, Metadatable{ foreach($mini as $blocks){ /** @var Block $b */ foreach($blocks as $b){ - $pk = new UpdateBlockPacket(); + $pk = UpdateBlockPacket::getFromPool(); $pk->x = $b->x; $pk->y = $b->y; $pk->z = $b->z; @@ -949,7 +949,7 @@ class Level implements ChunkManager, Metadatable{ } //if($direct === true){ - $pk = new UpdateBlockPacket; + $pk = UpdateBlockPacket::getFromPool(); $pk->x = $pos->x; $pk->y = $pos->y; $pk->z = $pos->z; diff --git a/src/pocketmine/network/RakLibInterface.php b/src/pocketmine/network/RakLibInterface.php index 76d660a30..c01c50a7f 100644 --- a/src/pocketmine/network/RakLibInterface.php +++ b/src/pocketmine/network/RakLibInterface.php @@ -83,6 +83,8 @@ use raklib\server\ServerInstance; class RakLibInterface implements ServerInstance, SourceInterface{ + private $packetPool = []; + private $server; /** @var Player[] */ private $players = []; @@ -103,6 +105,9 @@ class RakLibInterface implements ServerInstance, SourceInterface{ private $externalThreaded; public function __construct(Server $server){ + + $this->registerPackets(); + $this->server = $server; $this->identifiers = new \SplObjectStorage(); @@ -115,6 +120,8 @@ class RakLibInterface implements ServerInstance, SourceInterface{ } public function doTick(){ + $this->cleanPacketPool(); + EncapsulatedPacket::cleanPacketPool(); $this->interface->sendTick(); } @@ -218,7 +225,7 @@ class RakLibInterface implements ServerInstance, SourceInterface{ if(!$packet->isEncoded){ $packet->encode(); } - $pk = new EncapsulatedPacket(); + $pk = EncapsulatedPacket::getPacketFromPool(); $pk->buffer = $packet->buffer; $pk->reliability = 2; if($needACK === true){ @@ -232,150 +239,87 @@ class RakLibInterface implements ServerInstance, SourceInterface{ return null; } - private function getPacket($buffer){ - $pid = ord($buffer{0}); - switch($pid){ //TODO: more efficient selection based on range - case ProtocolInfo::LOGIN_PACKET: - $data = new LoginPacket(); - break; - case ProtocolInfo::LOGIN_STATUS_PACKET: - $data = new LoginStatusPacket(); - break; - case ProtocolInfo::MESSAGE_PACKET: - $data = new MessagePacket(); - break; - case ProtocolInfo::SET_TIME_PACKET: - $data = new SetTimePacket(); - 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; + public function registerPacket($id, $class){ + $this->packetPool[$id] = $class; + } + + /** + * @param $id + * + * @return DataPacket + */ + public function getPacketFromPool($id){ + if(isset($this->packetPool[$id])){ + /** @var DataPacket $class */ + $class = $this->packetPool[$id]; + return $class::getFromPool(); } + 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)); return $data; diff --git a/src/pocketmine/network/protocol/AddEntityPacket.php b/src/pocketmine/network/protocol/AddEntityPacket.php index aff5ae6cc..adf2d82ca 100644 --- a/src/pocketmine/network/protocol/AddEntityPacket.php +++ b/src/pocketmine/network/protocol/AddEntityPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class AddEntityPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $type; public $x; diff --git a/src/pocketmine/network/protocol/AddItemEntityPacket.php b/src/pocketmine/network/protocol/AddItemEntityPacket.php index 8a7fa8384..fadf84ee5 100644 --- a/src/pocketmine/network/protocol/AddItemEntityPacket.php +++ b/src/pocketmine/network/protocol/AddItemEntityPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class AddItemEntityPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $item; public $x; diff --git a/src/pocketmine/network/protocol/AddMobPacket.php b/src/pocketmine/network/protocol/AddMobPacket.php index aafb9ea3f..e0ce55383 100644 --- a/src/pocketmine/network/protocol/AddMobPacket.php +++ b/src/pocketmine/network/protocol/AddMobPacket.php @@ -24,6 +24,9 @@ namespace pocketmine\network\protocol; use pocketmine\utils\Binary; class AddMobPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $type; public $x; diff --git a/src/pocketmine/network/protocol/AddPaintingPacket.php b/src/pocketmine/network/protocol/AddPaintingPacket.php index 97ff094b3..1cb292ebe 100644 --- a/src/pocketmine/network/protocol/AddPaintingPacket.php +++ b/src/pocketmine/network/protocol/AddPaintingPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class AddPaintingPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $x; public $y; diff --git a/src/pocketmine/network/protocol/AddPlayerPacket.php b/src/pocketmine/network/protocol/AddPlayerPacket.php index 681e0c9d0..7137be69a 100644 --- a/src/pocketmine/network/protocol/AddPlayerPacket.php +++ b/src/pocketmine/network/protocol/AddPlayerPacket.php @@ -24,6 +24,9 @@ namespace pocketmine\network\protocol; use pocketmine\utils\Binary; class AddPlayerPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $clientID; public $username; public $eid; diff --git a/src/pocketmine/network/protocol/AdventureSettingsPacket.php b/src/pocketmine/network/protocol/AdventureSettingsPacket.php index 66ad09d91..8c4de6e73 100644 --- a/src/pocketmine/network/protocol/AdventureSettingsPacket.php +++ b/src/pocketmine/network/protocol/AdventureSettingsPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class AdventureSettingsPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $flags; public function pid(){ diff --git a/src/pocketmine/network/protocol/AnimatePacket.php b/src/pocketmine/network/protocol/AnimatePacket.php index b418cc148..fa17c317d 100644 --- a/src/pocketmine/network/protocol/AnimatePacket.php +++ b/src/pocketmine/network/protocol/AnimatePacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class AnimatePacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $action; public $eid; diff --git a/src/pocketmine/network/protocol/ChatPacket.php b/src/pocketmine/network/protocol/ChatPacket.php index 4dd9dc762..94f27a922 100644 --- a/src/pocketmine/network/protocol/ChatPacket.php +++ b/src/pocketmine/network/protocol/ChatPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class ChatPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $message; public function pid(){ diff --git a/src/pocketmine/network/protocol/ContainerClosePacket.php b/src/pocketmine/network/protocol/ContainerClosePacket.php index 46f2be2d8..23859a5fe 100644 --- a/src/pocketmine/network/protocol/ContainerClosePacket.php +++ b/src/pocketmine/network/protocol/ContainerClosePacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class ContainerClosePacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $windowid; public function pid(){ diff --git a/src/pocketmine/network/protocol/ContainerOpenPacket.php b/src/pocketmine/network/protocol/ContainerOpenPacket.php index 5c146f615..b2f78492c 100644 --- a/src/pocketmine/network/protocol/ContainerOpenPacket.php +++ b/src/pocketmine/network/protocol/ContainerOpenPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class ContainerOpenPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $windowid; public $type; public $slots; diff --git a/src/pocketmine/network/protocol/ContainerSetContentPacket.php b/src/pocketmine/network/protocol/ContainerSetContentPacket.php index dda4bb220..713fdf6c3 100644 --- a/src/pocketmine/network/protocol/ContainerSetContentPacket.php +++ b/src/pocketmine/network/protocol/ContainerSetContentPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class ContainerSetContentPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $windowid; public $slots = []; public $hotbar = []; @@ -31,6 +34,12 @@ class ContainerSetContentPacket extends DataPacket{ return Info::CONTAINER_SET_CONTENT_PACKET; } + public function clean(){ + $this->slots = []; + $this->hotbar = []; + return parent::clean(); + } + public function decode(){ $this->windowid = $this->getByte(); $count = $this->getShort(); diff --git a/src/pocketmine/network/protocol/ContainerSetDataPacket.php b/src/pocketmine/network/protocol/ContainerSetDataPacket.php index 160453832..9f68827bb 100644 --- a/src/pocketmine/network/protocol/ContainerSetDataPacket.php +++ b/src/pocketmine/network/protocol/ContainerSetDataPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class ContainerSetDataPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $windowid; public $property; public $value; diff --git a/src/pocketmine/network/protocol/ContainerSetSlotPacket.php b/src/pocketmine/network/protocol/ContainerSetSlotPacket.php index c24119150..dce05fdb6 100644 --- a/src/pocketmine/network/protocol/ContainerSetSlotPacket.php +++ b/src/pocketmine/network/protocol/ContainerSetSlotPacket.php @@ -24,6 +24,9 @@ namespace pocketmine\network\protocol; use pocketmine\item\Item; class ContainerSetSlotPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $windowid; public $slot; /** @var Item */ diff --git a/src/pocketmine/network/protocol/DataPacket.php b/src/pocketmine/network/protocol/DataPacket.php index b3ab23f74..7d7a599e0 100644 --- a/src/pocketmine/network/protocol/DataPacket.php +++ b/src/pocketmine/network/protocol/DataPacket.php @@ -25,6 +25,26 @@ use pocketmine\item\Item; use pocketmine\utils\Binary; 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; public $buffer = ""; public $isEncoded = false; @@ -172,4 +192,11 @@ abstract class DataPacket extends \stdClass{ protected function feof(){ return !isset($this->buffer{$this->offset}); } + + public function clean(){ + $this->buffer = null; + $this->isEncoded = false; + $this->offset = 0; + return $this; + } } \ No newline at end of file diff --git a/src/pocketmine/network/protocol/DropItemPacket.php b/src/pocketmine/network/protocol/DropItemPacket.php index 99de843b3..83f57a1df 100644 --- a/src/pocketmine/network/protocol/DropItemPacket.php +++ b/src/pocketmine/network/protocol/DropItemPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class DropItemPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $unknown; public $item; diff --git a/src/pocketmine/network/protocol/EntityDataPacket.php b/src/pocketmine/network/protocol/EntityDataPacket.php index ccc45f51e..78ed32963 100644 --- a/src/pocketmine/network/protocol/EntityDataPacket.php +++ b/src/pocketmine/network/protocol/EntityDataPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class EntityDataPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $x; public $y; public $z; diff --git a/src/pocketmine/network/protocol/EntityEventPacket.php b/src/pocketmine/network/protocol/EntityEventPacket.php index 2e27bcd91..40530f0a5 100644 --- a/src/pocketmine/network/protocol/EntityEventPacket.php +++ b/src/pocketmine/network/protocol/EntityEventPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class EntityEventPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $event; diff --git a/src/pocketmine/network/protocol/ExplodePacket.php b/src/pocketmine/network/protocol/ExplodePacket.php index 410c9a9d2..f3ff0062c 100644 --- a/src/pocketmine/network/protocol/ExplodePacket.php +++ b/src/pocketmine/network/protocol/ExplodePacket.php @@ -23,16 +23,24 @@ namespace pocketmine\network\protocol; class ExplodePacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $x; public $y; public $z; public $radius; - public $records; + public $records = []; public function pid(){ return Info::EXPLODE_PACKET; } + public function clean(){ + $this->records = []; + return parent::clean(); + } + public function decode(){ } diff --git a/src/pocketmine/network/protocol/FullChunkDataPacket.php b/src/pocketmine/network/protocol/FullChunkDataPacket.php index 23e3b2487..e596457a2 100644 --- a/src/pocketmine/network/protocol/FullChunkDataPacket.php +++ b/src/pocketmine/network/protocol/FullChunkDataPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class FullChunkDataPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $chunkX; public $chunkZ; public $data; diff --git a/src/pocketmine/network/protocol/HurtArmorPacket.php b/src/pocketmine/network/protocol/HurtArmorPacket.php index 503e2a174..e99c40aa3 100644 --- a/src/pocketmine/network/protocol/HurtArmorPacket.php +++ b/src/pocketmine/network/protocol/HurtArmorPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class HurtArmorPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $health; public function pid(){ diff --git a/src/pocketmine/network/protocol/InteractPacket.php b/src/pocketmine/network/protocol/InteractPacket.php index 1b7de5ff3..679dcaa44 100644 --- a/src/pocketmine/network/protocol/InteractPacket.php +++ b/src/pocketmine/network/protocol/InteractPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class InteractPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $action; public $eid; public $target; diff --git a/src/pocketmine/network/protocol/LevelEventPacket.php b/src/pocketmine/network/protocol/LevelEventPacket.php index 232ec9812..912960ba0 100644 --- a/src/pocketmine/network/protocol/LevelEventPacket.php +++ b/src/pocketmine/network/protocol/LevelEventPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class LevelEventPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $evid; public $x; public $y; diff --git a/src/pocketmine/network/protocol/LoginPacket.php b/src/pocketmine/network/protocol/LoginPacket.php index d02c47cff..ee8b98f4e 100644 --- a/src/pocketmine/network/protocol/LoginPacket.php +++ b/src/pocketmine/network/protocol/LoginPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class LoginPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $username; public $protocol1; public $protocol2; diff --git a/src/pocketmine/network/protocol/LoginStatusPacket.php b/src/pocketmine/network/protocol/LoginStatusPacket.php index 1de99be2e..6bfb3fb87 100644 --- a/src/pocketmine/network/protocol/LoginStatusPacket.php +++ b/src/pocketmine/network/protocol/LoginStatusPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class LoginStatusPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $status; public function pid(){ diff --git a/src/pocketmine/network/protocol/MessagePacket.php b/src/pocketmine/network/protocol/MessagePacket.php index a4a36e810..da9849028 100644 --- a/src/pocketmine/network/protocol/MessagePacket.php +++ b/src/pocketmine/network/protocol/MessagePacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class MessagePacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $source; public $message; diff --git a/src/pocketmine/network/protocol/MoveEntityPacket.php b/src/pocketmine/network/protocol/MoveEntityPacket.php index 40f91b99d..254a67bb9 100644 --- a/src/pocketmine/network/protocol/MoveEntityPacket.php +++ b/src/pocketmine/network/protocol/MoveEntityPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class MoveEntityPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + // eid, x, y, z, yaw, pitch /** @var array[] */ @@ -32,6 +35,11 @@ class MoveEntityPacket extends DataPacket{ return Info::MOVE_ENTITY_PACKET; } + public function clean(){ + $this->entities = []; + return parent::clean(); + } + public function decode(){ } diff --git a/src/pocketmine/network/protocol/MovePlayerPacket.php b/src/pocketmine/network/protocol/MovePlayerPacket.php index 2f465baf7..08f618ee6 100644 --- a/src/pocketmine/network/protocol/MovePlayerPacket.php +++ b/src/pocketmine/network/protocol/MovePlayerPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class MovePlayerPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $x; public $y; @@ -36,6 +39,11 @@ class MovePlayerPacket extends DataPacket{ return Info::MOVE_PLAYER_PACKET; } + public function clean(){ + $this->teleport = false; + return parent::clean(); + } + public function decode(){ $this->eid = $this->getInt(); $this->x = $this->getFloat(); diff --git a/src/pocketmine/network/protocol/PlayerActionPacket.php b/src/pocketmine/network/protocol/PlayerActionPacket.php index 4c322a1bf..7083ac53d 100644 --- a/src/pocketmine/network/protocol/PlayerActionPacket.php +++ b/src/pocketmine/network/protocol/PlayerActionPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class PlayerActionPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $action; public $x; public $y; diff --git a/src/pocketmine/network/protocol/PlayerArmorEquipmentPacket.php b/src/pocketmine/network/protocol/PlayerArmorEquipmentPacket.php index 8fa462ac7..12b2b5b45 100644 --- a/src/pocketmine/network/protocol/PlayerArmorEquipmentPacket.php +++ b/src/pocketmine/network/protocol/PlayerArmorEquipmentPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class PlayerArmorEquipmentPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $slots = []; diff --git a/src/pocketmine/network/protocol/PlayerEquipmentPacket.php b/src/pocketmine/network/protocol/PlayerEquipmentPacket.php index 52cbf64ce..ef49395ce 100644 --- a/src/pocketmine/network/protocol/PlayerEquipmentPacket.php +++ b/src/pocketmine/network/protocol/PlayerEquipmentPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class PlayerEquipmentPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $item; public $meta; diff --git a/src/pocketmine/network/protocol/RemoveBlockPacket.php b/src/pocketmine/network/protocol/RemoveBlockPacket.php index 75cdd6eb3..e38a61d8d 100644 --- a/src/pocketmine/network/protocol/RemoveBlockPacket.php +++ b/src/pocketmine/network/protocol/RemoveBlockPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class RemoveBlockPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $x; public $y; diff --git a/src/pocketmine/network/protocol/RemoveEntityPacket.php b/src/pocketmine/network/protocol/RemoveEntityPacket.php index d5306fae3..a6e0bdd9f 100644 --- a/src/pocketmine/network/protocol/RemoveEntityPacket.php +++ b/src/pocketmine/network/protocol/RemoveEntityPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class RemoveEntityPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public function pid(){ diff --git a/src/pocketmine/network/protocol/RemovePlayerPacket.php b/src/pocketmine/network/protocol/RemovePlayerPacket.php index bc35b25e6..1f621244c 100644 --- a/src/pocketmine/network/protocol/RemovePlayerPacket.php +++ b/src/pocketmine/network/protocol/RemovePlayerPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class RemovePlayerPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $clientID; diff --git a/src/pocketmine/network/protocol/RespawnPacket.php b/src/pocketmine/network/protocol/RespawnPacket.php index 076c9b4dd..7dd6086ea 100644 --- a/src/pocketmine/network/protocol/RespawnPacket.php +++ b/src/pocketmine/network/protocol/RespawnPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class RespawnPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $x; public $y; diff --git a/src/pocketmine/network/protocol/RotateHeadPacket.php b/src/pocketmine/network/protocol/RotateHeadPacket.php index 92497f609..f47d05a5a 100644 --- a/src/pocketmine/network/protocol/RotateHeadPacket.php +++ b/src/pocketmine/network/protocol/RotateHeadPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class RotateHeadPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + // eid, yaw /** @var array[] */ @@ -32,6 +35,11 @@ class RotateHeadPacket extends DataPacket{ return Info::ROTATE_HEAD_PACKET; } + public function clean(){ + $this->entities = []; + return parent::clean(); + } + public function decode(){ } diff --git a/src/pocketmine/network/protocol/SendInventoryPacket.php b/src/pocketmine/network/protocol/SendInventoryPacket.php index c8322a91d..443a4f6e2 100644 --- a/src/pocketmine/network/protocol/SendInventoryPacket.php +++ b/src/pocketmine/network/protocol/SendInventoryPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class SendInventoryPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $windowid; public $slots = []; diff --git a/src/pocketmine/network/protocol/SetEntityDataPacket.php b/src/pocketmine/network/protocol/SetEntityDataPacket.php index f1abadac7..3e6091f0b 100644 --- a/src/pocketmine/network/protocol/SetEntityDataPacket.php +++ b/src/pocketmine/network/protocol/SetEntityDataPacket.php @@ -24,6 +24,9 @@ namespace pocketmine\network\protocol; use pocketmine\utils\Binary; class SetEntityDataPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $eid; public $metadata; diff --git a/src/pocketmine/network/protocol/SetEntityMotionPacket.php b/src/pocketmine/network/protocol/SetEntityMotionPacket.php index 4b207be77..16e76eedd 100644 --- a/src/pocketmine/network/protocol/SetEntityMotionPacket.php +++ b/src/pocketmine/network/protocol/SetEntityMotionPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class SetEntityMotionPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + // eid, motX, motY, motZ /** @var array[] */ @@ -32,6 +35,11 @@ class SetEntityMotionPacket extends DataPacket{ return Info::SET_ENTITY_MOTION_PACKET; } + public function clean(){ + $this->entities = []; + return parent::clean(); + } + public function decode(){ } diff --git a/src/pocketmine/network/protocol/SetHealthPacket.php b/src/pocketmine/network/protocol/SetHealthPacket.php index 036402faa..2f9cc81fb 100644 --- a/src/pocketmine/network/protocol/SetHealthPacket.php +++ b/src/pocketmine/network/protocol/SetHealthPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class SetHealthPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $health; public function pid(){ diff --git a/src/pocketmine/network/protocol/SetSpawnPositionPacket.php b/src/pocketmine/network/protocol/SetSpawnPositionPacket.php index 0862adcad..785b9b553 100644 --- a/src/pocketmine/network/protocol/SetSpawnPositionPacket.php +++ b/src/pocketmine/network/protocol/SetSpawnPositionPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class SetSpawnPositionPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $x; public $z; public $y; diff --git a/src/pocketmine/network/protocol/SetTimePacket.php b/src/pocketmine/network/protocol/SetTimePacket.php index bfa51a5da..670f0e204 100644 --- a/src/pocketmine/network/protocol/SetTimePacket.php +++ b/src/pocketmine/network/protocol/SetTimePacket.php @@ -25,6 +25,9 @@ namespace pocketmine\network\protocol; use pocketmine\level\Level; class SetTimePacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $time; public $started = true; diff --git a/src/pocketmine/network/protocol/StartGamePacket.php b/src/pocketmine/network/protocol/StartGamePacket.php index eb610c1a2..dcf652b66 100644 --- a/src/pocketmine/network/protocol/StartGamePacket.php +++ b/src/pocketmine/network/protocol/StartGamePacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class StartGamePacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $seed; public $generator; public $gamemode; diff --git a/src/pocketmine/network/protocol/TakeItemEntityPacket.php b/src/pocketmine/network/protocol/TakeItemEntityPacket.php index 918382c18..6dee90837 100644 --- a/src/pocketmine/network/protocol/TakeItemEntityPacket.php +++ b/src/pocketmine/network/protocol/TakeItemEntityPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class TakeItemEntityPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $target; public $eid; diff --git a/src/pocketmine/network/protocol/TileEventPacket.php b/src/pocketmine/network/protocol/TileEventPacket.php index 28c01b7d6..8c461c7a8 100644 --- a/src/pocketmine/network/protocol/TileEventPacket.php +++ b/src/pocketmine/network/protocol/TileEventPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class TileEventPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $x; public $y; public $z; diff --git a/src/pocketmine/network/protocol/UnknownPacket.php b/src/pocketmine/network/protocol/UnknownPacket.php index f873d1919..93ac17343 100644 --- a/src/pocketmine/network/protocol/UnknownPacket.php +++ b/src/pocketmine/network/protocol/UnknownPacket.php @@ -24,6 +24,9 @@ namespace pocketmine\network\protocol; class UnknownPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $packetID = -1; public function pid(){ diff --git a/src/pocketmine/network/protocol/UnloadChunkPacket.php b/src/pocketmine/network/protocol/UnloadChunkPacket.php index 6a363152e..1a4ee0c3b 100644 --- a/src/pocketmine/network/protocol/UnloadChunkPacket.php +++ b/src/pocketmine/network/protocol/UnloadChunkPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class UnloadChunkPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $chunkX; public $chunkZ; diff --git a/src/pocketmine/network/protocol/UpdateBlockPacket.php b/src/pocketmine/network/protocol/UpdateBlockPacket.php index b24b86b30..f4aeb81bf 100644 --- a/src/pocketmine/network/protocol/UpdateBlockPacket.php +++ b/src/pocketmine/network/protocol/UpdateBlockPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class UpdateBlockPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $x; public $z; public $y; diff --git a/src/pocketmine/network/protocol/UseItemPacket.php b/src/pocketmine/network/protocol/UseItemPacket.php index 9a32f9d69..d198d9e4e 100644 --- a/src/pocketmine/network/protocol/UseItemPacket.php +++ b/src/pocketmine/network/protocol/UseItemPacket.php @@ -23,6 +23,9 @@ namespace pocketmine\network\protocol; class UseItemPacket extends DataPacket{ + public static $pool = []; + public static $next = 0; + public $x; public $y; public $z; diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 5207aa827..2f722f500 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -252,13 +252,13 @@ class Furnace extends Tile implements InventoryHolder, Container{ foreach($this->getInventory()->getViewers() as $player){ $windowId = $player->getWindowId($this->getInventory()); if($windowId > 0){ - $pk = new ContainerSetDataPacket; + $pk = ContainerSetDataPacket::getFromPool(); $pk->windowid = $windowId; $pk->property = 0; //Smelting $pk->value = floor($this->namedtag["CookTime"]); $player->dataPacket($pk); - $pk = new ContainerSetDataPacket; + $pk = ContainerSetDataPacket::getFromPool(); $pk->windowid = $windowId; $pk->property = 1; //Fire icon $pk->value = $this->namedtag["BurnTicks"]; diff --git a/src/pocketmine/tile/Spawnable.php b/src/pocketmine/tile/Spawnable.php index 4e62c94d2..5f8d157b2 100644 --- a/src/pocketmine/tile/Spawnable.php +++ b/src/pocketmine/tile/Spawnable.php @@ -36,7 +36,7 @@ abstract class Spawnable extends Tile{ $nbt = new NBT(NBT::LITTLE_ENDIAN); $nbt->setData($this->getSpawnCompound()); - $pk = new EntityDataPacket; + $pk = EntityDataPacket::getFromPool(); $pk->x = $this->x; $pk->y = $this->y; $pk->z = $this->z;