Clean up block update sending, remove UpdateBlockPacket flag constants

These flags are not intended for network use, instead they are supposed to be used internally by the game. For network, we only need to care about the 0x02 flag (send to players) because that's necessary for the client to render the block.
This commit is contained in:
Dylan K. Taylor 2018-09-27 15:56:08 +01:00
parent 8a8f1d84ff
commit 16f2ac14b3
3 changed files with 25 additions and 65 deletions

View File

@ -122,7 +122,6 @@ use pocketmine\network\mcpe\protocol\types\CommandParameter;
use pocketmine\network\mcpe\protocol\types\ContainerIds; 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\mcpe\protocol\UpdateBlockPacket;
use pocketmine\network\mcpe\ProcessLoginTask; use pocketmine\network\mcpe\ProcessLoginTask;
use pocketmine\permission\PermissibleBase; use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissionAttachment; use pocketmine\permission\PermissionAttachment;
@ -2208,7 +2207,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$blocks = $target->getAllSides(); $blocks = $target->getAllSides();
$blocks[] = $target; $blocks[] = $target;
$this->level->sendBlocks([$this], $blocks, UpdateBlockPacket::FLAG_ALL_PRIORITY); $this->level->sendBlocks([$this], $blocks);
foreach($blocks as $b){ foreach($blocks as $b){
$tile = $this->level->getTile($b); $tile = $this->level->getTile($b);
@ -2255,7 +2254,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
/** @var Block[] $blocks */ /** @var Block[] $blocks */
$blocks = array_merge($target->getAllSides(), $block->getAllSides()); //getAllSides() on each of these will include $target and $block because they are next to each other $blocks = array_merge($target->getAllSides(), $block->getAllSides()); //getAllSides() on each of these will include $target and $block because they are next to each other
$this->level->sendBlocks([$this], $blocks, UpdateBlockPacket::FLAG_ALL_PRIORITY); $this->level->sendBlocks([$this], $blocks);
return false; return false;
} }

View File

@ -799,7 +799,7 @@ class Level implements ChunkManager, Metadatable{
$p->onChunkChanged($chunk); $p->onChunkChanged($chunk);
} }
}else{ }else{
$this->sendBlocks($this->getChunkPlayers($chunkX, $chunkZ), $blocks, UpdateBlockPacket::FLAG_ALL); $this->sendBlocks($this->getChunkPlayers($chunkX, $chunkZ), $blocks);
} }
} }
}else{ }else{
@ -870,62 +870,28 @@ class Level implements ChunkManager, Metadatable{
/** /**
* @param Player[] $target * @param Player[] $target
* @param Vector3[] $blocks * @param Vector3[] $blocks
* @param int $flags
* @param bool $optimizeRebuilds
*/ */
public function sendBlocks(array $target, array $blocks, int $flags = UpdateBlockPacket::FLAG_NONE, bool $optimizeRebuilds = false){ public function sendBlocks(array $target, array $blocks){
$packets = []; $packets = [];
if($optimizeRebuilds){
$chunks = [];
foreach($blocks as $b){
if(!($b instanceof Vector3)){
throw new \TypeError("Expected Vector3 in blocks array, got " . (is_object($b) ? get_class($b) : gettype($b)));
}
$pk = new UpdateBlockPacket();
$first = false; foreach($blocks as $b){
if(!isset($chunks[$index = Level::chunkHash($b->x >> 4, $b->z >> 4)])){ if(!($b instanceof Vector3)){
$chunks[$index] = true; throw new \TypeError("Expected Vector3 in blocks array, got " . (is_object($b) ? get_class($b) : gettype($b)));
$first = true;
}
$pk->x = $b->x;
$pk->y = $b->y;
$pk->z = $b->z;
if($b instanceof Block){
$pk->blockRuntimeId = $b->getRuntimeId();
}else{
$fullBlock = $this->getFullBlock($b->x, $b->y, $b->z);
$pk->blockRuntimeId = BlockFactory::toStaticRuntimeId($fullBlock >> 4, $fullBlock & 0xf);
}
$pk->flags = $first ? $flags : UpdateBlockPacket::FLAG_NONE;
$packets[] = $pk;
} }
}else{ $pk = new UpdateBlockPacket();
foreach($blocks as $b){
if(!($b instanceof Vector3)){
throw new \TypeError("Expected Vector3 in blocks array, got " . (is_object($b) ? get_class($b) : gettype($b)));
}
$pk = new UpdateBlockPacket();
$pk->x = $b->x; $pk->x = $b->x;
$pk->y = $b->y; $pk->y = $b->y;
$pk->z = $b->z; $pk->z = $b->z;
if($b instanceof Block){ if($b instanceof Block){
$pk->blockRuntimeId = $b->getRuntimeId(); $pk->blockRuntimeId = $b->getRuntimeId();
}else{ }else{
$fullBlock = $this->getFullBlock($b->x, $b->y, $b->z); $fullBlock = $this->getFullBlock($b->x, $b->y, $b->z);
$pk->blockRuntimeId = BlockFactory::toStaticRuntimeId($fullBlock >> 4, $fullBlock & 0xf); $pk->blockRuntimeId = BlockFactory::toStaticRuntimeId($fullBlock >> 4, $fullBlock & 0xf);
}
$pk->flags = $flags;
$packets[] = $pk;
} }
$packets[] = $pk;
} }
$this->server->broadcastPackets($target, $packets); $this->server->broadcastPackets($target, $packets);
@ -1547,7 +1513,7 @@ class Level implements ChunkManager, Metadatable{
unset($this->blockCache[$chunkHash][$blockHash]); unset($this->blockCache[$chunkHash][$blockHash]);
if($direct){ if($direct){
$this->sendBlocks($this->getChunkPlayers($pos->x >> 4, $pos->z >> 4), [$block], UpdateBlockPacket::FLAG_ALL_PRIORITY); $this->sendBlocks($this->getChunkPlayers($pos->x >> 4, $pos->z >> 4), [$block]);
unset($this->chunkCache[$chunkHash], $this->changedBlocks[$chunkHash][$blockHash]); unset($this->chunkCache[$chunkHash], $this->changedBlocks[$chunkHash][$blockHash]);
}else{ }else{
if(!isset($this->changedBlocks[$chunkHash])){ if(!isset($this->changedBlocks[$chunkHash])){

View File

@ -31,15 +31,6 @@ use pocketmine\network\mcpe\handler\SessionHandler;
class UpdateBlockPacket extends DataPacket{ class UpdateBlockPacket extends DataPacket{
public const NETWORK_ID = ProtocolInfo::UPDATE_BLOCK_PACKET; public const NETWORK_ID = ProtocolInfo::UPDATE_BLOCK_PACKET;
public const FLAG_NONE = 0b0000;
public const FLAG_NEIGHBORS = 0b0001;
public const FLAG_NETWORK = 0b0010;
public const FLAG_NOGRAPHIC = 0b0100;
public const FLAG_PRIORITY = 0b1000;
public const FLAG_ALL = self::FLAG_NEIGHBORS | self::FLAG_NETWORK;
public const FLAG_ALL_PRIORITY = self::FLAG_ALL | self::FLAG_PRIORITY;
public const DATA_LAYER_NORMAL = 0; public const DATA_LAYER_NORMAL = 0;
public const DATA_LAYER_LIQUID = 1; public const DATA_LAYER_LIQUID = 1;
@ -51,8 +42,12 @@ class UpdateBlockPacket extends DataPacket{
public $y; public $y;
/** @var int */ /** @var int */
public $blockRuntimeId; public $blockRuntimeId;
/** @var int */ /**
public $flags; * @var int
* Flags are used by MCPE internally for block setting, but only flag 2 (network flag) is relevant for network.
* This field is pointless really.
*/
public $flags = 0x02;
/** @var int */ /** @var int */
public $dataLayerId = self::DATA_LAYER_NORMAL; public $dataLayerId = self::DATA_LAYER_NORMAL;