Remove DataPacketBroadcastEvent, DataPacketSendEvent now supports multiple recipients & packets in one swoop

this makes it simpler to consistently process outbound packets from a plugin. Previously it was necessary to handle 2 events to do 1 job.
This commit is contained in:
Dylan K. Taylor 2019-08-10 17:59:02 +01:00
parent 4c694c57f4
commit d1775030c3
4 changed files with 36 additions and 97 deletions

View File

@ -38,7 +38,7 @@ use pocketmine\entity\EntityFactory;
use pocketmine\event\HandlerListManager;
use pocketmine\event\player\PlayerDataSaveEvent;
use pocketmine\event\server\CommandEvent;
use pocketmine\event\server\DataPacketBroadcastEvent;
use pocketmine\event\server\DataPacketSendEvent;
use pocketmine\event\server\QueryRegenerateEvent;
use pocketmine\inventory\CreativeInventory;
use pocketmine\item\enchantment\Enchantment;
@ -1453,7 +1453,7 @@ class Server{
return false;
}
$ev = new DataPacketBroadcastEvent($recipients, $packets);
$ev = new DataPacketSendEvent($recipients, $packets);
$ev->call();
if($ev->isCancelled()){
return false;

View File

@ -1,78 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\event\server;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\ClientboundPacket;
/**
* Called when a list of packets is broadcasted to 1 or more players.
*/
class DataPacketBroadcastEvent extends ServerEvent implements Cancellable{
use CancellableTrait;
/** @var NetworkSession[] */
private $targets;
/** @var ClientboundPacket[] */
private $packets;
/**
* @param NetworkSession[] $targets
* @param ClientboundPacket[] $packets
*/
public function __construct(array $targets, array $packets){
$this->targets = $targets;
$this->packets = $packets;
}
/**
* @return NetworkSession[]
*/
public function getTargets() : array{
return $this->targets;
}
/**
* @param NetworkSession[] $targets
*/
public function setTargets(array $targets) : void{
$this->targets = $targets;
}
/**
* @return ClientboundPacket[]
*/
public function getPackets() : array{
return $this->packets;
}
/**
* @param ClientboundPacket[] $packets
*/
public function setPackets(array $packets) : void{
$this->packets = $packets;
}
}

View File

@ -28,34 +28,51 @@ use pocketmine\event\CancellableTrait;
use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\ClientboundPacket;
/**
* Called when packets are sent to network sessions.
*/
class DataPacketSendEvent extends ServerEvent implements Cancellable{
use CancellableTrait;
/** @var ClientboundPacket */
private $packet;
/** @var NetworkSession */
private $target;
/** @var NetworkSession[] */
private $targets;
/** @var ClientboundPacket[] */
private $packets;
/**
* @param NetworkSession $target
* @param ClientboundPacket $packet
* @param NetworkSession[] $targets
* @param ClientboundPacket[] $packets
*/
public function __construct(NetworkSession $target, ClientboundPacket $packet){
$this->packet = $packet;
$this->target = $target;
public function __construct(array $targets, array $packets){
$this->targets = $targets;
$this->packets = $packets;
}
/**
* @return ClientboundPacket
* @return NetworkSession[]
*/
public function getPacket() : ClientboundPacket{
return $this->packet;
public function getTargets() : array{
return $this->targets;
}
/**
* @return NetworkSession
* @param NetworkSession[] $targets
*/
public function getTarget() : NetworkSession{
return $this->target;
public function setTargets(array $targets) : void{
$this->targets = $targets;
}
/**
* @return ClientboundPacket[]
*/
public function getPackets() : array{
return $this->packets;
}
/**
* @param ClientboundPacket[] $packets
*/
public function setPackets(array $packets) : void{
$this->packets = $packets;
}
}

View File

@ -28,8 +28,8 @@ use pocketmine\entity\effect\EffectInstance;
use pocketmine\entity\Human;
use pocketmine\entity\Living;
use pocketmine\event\player\PlayerCreationEvent;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\event\server\DataPacketSendEvent;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\form\Form;
use pocketmine\math\Vector3;
use pocketmine\network\BadPacketException;
@ -363,7 +363,7 @@ class NetworkSession{
$timings = Timings::getSendDataPacketTimings($packet);
$timings->startTiming();
try{
$ev = new DataPacketSendEvent($this, $packet);
$ev = new DataPacketSendEvent([$this], [$packet]);
$ev->call();
if($ev->isCancelled()){
return false;