move more packet logic to net session

it seems like net session is turning into a giant dumping ground for sending API ... this needs to be cleaned up somehow.
This commit is contained in:
Dylan K. Taylor 2019-05-18 20:00:05 +01:00
parent 57219abc9d
commit 67affcea32
3 changed files with 31 additions and 33 deletions

View File

@ -25,8 +25,6 @@ namespace pocketmine\inventory;
use pocketmine\entity\Living; use pocketmine\entity\Living;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\network\mcpe\protocol\InventoryContentPacket;
use pocketmine\network\mcpe\protocol\InventorySlotPacket;
use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket; use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket;
use pocketmine\Player; use pocketmine\Player;
use function array_merge; use function array_merge;
@ -90,14 +88,11 @@ class ArmorInventory extends BaseInventory{
/** @var Player[] $target */ /** @var Player[] $target */
if(($k = array_search($this->holder, $target, true)) !== false){ if(($k = array_search($this->holder, $target, true)) !== false){
$pk = new InventorySlotPacket(); $target[$k]->getNetworkSession()->syncInventorySlot($this, $index);
$pk->windowId = $target[$k]->getWindowId($this);
$pk->inventorySlot = $index;
$pk->item = $this->getItem($index);
$target[$k]->sendDataPacket($pk);
unset($target[$k]); unset($target[$k]);
} }
if(!empty($target)){ if(!empty($target)){
//TODO: this should be handled by change listeners
$pk = new MobArmorEquipmentPacket(); $pk = new MobArmorEquipmentPacket();
$pk->entityRuntimeId = $this->getHolder()->getId(); $pk->entityRuntimeId = $this->getHolder()->getId();
$pk->slots = $this->getContents(true); $pk->slots = $this->getContents(true);
@ -113,13 +108,11 @@ class ArmorInventory extends BaseInventory{
$armor = $this->getContents(true); $armor = $this->getContents(true);
if(($k = array_search($this->holder, $target, true)) !== false){ if(($k = array_search($this->holder, $target, true)) !== false){
$pk = new InventoryContentPacket(); $target[$k]->getNetworkSession()->syncInventoryContents($this);
$pk->windowId = $target[$k]->getWindowId($this);
$pk->items = $armor;
$target[$k]->sendDataPacket($pk);
unset($target[$k]); unset($target[$k]);
} }
if(!empty($target)){ if(!empty($target)){
//TODO: this should be handled by change listeners
$pk = new MobArmorEquipmentPacket(); $pk = new MobArmorEquipmentPacket();
$pk->entityRuntimeId = $this->getHolder()->getId(); $pk->entityRuntimeId = $this->getHolder()->getId();
$pk->slots = $armor; $pk->slots = $armor;

View File

@ -26,9 +26,6 @@ namespace pocketmine\inventory;
use pocketmine\event\inventory\InventoryOpenEvent; use pocketmine\event\inventory\InventoryOpenEvent;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemFactory; use pocketmine\item\ItemFactory;
use pocketmine\network\mcpe\protocol\InventoryContentPacket;
use pocketmine\network\mcpe\protocol\InventorySlotPacket;
use pocketmine\network\mcpe\protocol\types\ContainerIds;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use function array_slice; use function array_slice;
@ -414,16 +411,8 @@ abstract class BaseInventory implements Inventory{
$target = [$target]; $target = [$target];
} }
$pk = new InventoryContentPacket();
$pk->items = $this->getContents(true);
foreach($target as $player){ foreach($target as $player){
if(($id = $player->getWindowId($this)) === ContainerIds::NONE){ $player->getNetworkSession()->syncInventoryContents($this);
$this->close($player);
continue;
}
$pk->windowId = $id;
$player->sendDataPacket($pk);
} }
} }
@ -436,17 +425,8 @@ abstract class BaseInventory implements Inventory{
$target = [$target]; $target = [$target];
} }
$pk = new InventorySlotPacket();
$pk->inventorySlot = $index;
$pk->item = $this->getItem($index);
foreach($target as $player){ foreach($target as $player){
if(($id = $player->getWindowId($this)) === ContainerIds::NONE){ $player->getNetworkSession()->syncInventorySlot($this, $index);
$this->close($player);
continue;
}
$pk->windowId = $id;
$player->sendDataPacket($pk);
} }
} }

View File

@ -30,6 +30,7 @@ use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\event\server\DataPacketSendEvent; use pocketmine\event\server\DataPacketSendEvent;
use pocketmine\form\Form; use pocketmine\form\Form;
use pocketmine\GameMode; use pocketmine\GameMode;
use pocketmine\inventory\Inventory;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\network\BadPacketException; use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\handler\DeathSessionHandler; use pocketmine\network\mcpe\handler\DeathSessionHandler;
@ -45,6 +46,8 @@ use pocketmine\network\mcpe\protocol\AvailableCommandsPacket;
use pocketmine\network\mcpe\protocol\ChunkRadiusUpdatedPacket; use pocketmine\network\mcpe\protocol\ChunkRadiusUpdatedPacket;
use pocketmine\network\mcpe\protocol\ClientboundPacket; use pocketmine\network\mcpe\protocol\ClientboundPacket;
use pocketmine\network\mcpe\protocol\DisconnectPacket; use pocketmine\network\mcpe\protocol\DisconnectPacket;
use pocketmine\network\mcpe\protocol\InventoryContentPacket;
use pocketmine\network\mcpe\protocol\InventorySlotPacket;
use pocketmine\network\mcpe\protocol\MobEffectPacket; use pocketmine\network\mcpe\protocol\MobEffectPacket;
use pocketmine\network\mcpe\protocol\ModalFormRequestPacket; use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
use pocketmine\network\mcpe\protocol\MovePlayerPacket; use pocketmine\network\mcpe\protocol\MovePlayerPacket;
@ -60,6 +63,7 @@ use pocketmine\network\mcpe\protocol\TransferPacket;
use pocketmine\network\mcpe\protocol\types\CommandData; use pocketmine\network\mcpe\protocol\types\CommandData;
use pocketmine\network\mcpe\protocol\types\CommandEnum; use pocketmine\network\mcpe\protocol\types\CommandEnum;
use pocketmine\network\mcpe\protocol\types\CommandParameter; use pocketmine\network\mcpe\protocol\types\CommandParameter;
use pocketmine\network\mcpe\protocol\types\ContainerIds;
use pocketmine\network\mcpe\protocol\types\PlayerPermissions; use pocketmine\network\mcpe\protocol\types\PlayerPermissions;
use pocketmine\network\mcpe\protocol\UpdateAttributesPacket; use pocketmine\network\mcpe\protocol\UpdateAttributesPacket;
use pocketmine\network\NetworkInterface; use pocketmine\network\NetworkInterface;
@ -792,6 +796,27 @@ class NetworkSession{
} }
public function syncInventorySlot(Inventory $inventory, int $slot) : void{
$windowId = $this->player->getWindowId($inventory);
if($windowId !== ContainerIds::NONE){
$pk = new InventorySlotPacket();
$pk->inventorySlot = $slot;
$pk->item = $inventory->getItem($slot);
$pk->windowId = $windowId;
$this->sendDataPacket($pk);
}
}
public function syncInventoryContents(Inventory $inventory) : void{
$windowId = $this->player->getWindowId($inventory);
if($windowId !== ContainerIds::NONE){
$pk = new InventoryContentPacket();
$pk->items = $inventory->getContents(true);
$pk->windowId = $windowId;
$this->sendDataPacket($pk);
}
}
public function tick() : bool{ public function tick() : bool{
if($this->handler instanceof LoginSessionHandler){ if($this->handler instanceof LoginSessionHandler){
if(time() >= $this->connectTime + 10){ if(time() >= $this->connectTime + 10){