Better BlockEntityDataPacket handling

This commit is contained in:
Dylan K. Taylor 2016-11-28 10:38:56 +00:00
parent 2987c7a80c
commit c4d4277a6c
3 changed files with 54 additions and 29 deletions

View File

@ -32,7 +32,6 @@ use pocketmine\entity\Human;
use pocketmine\entity\Item as DroppedItem;
use pocketmine\entity\Living;
use pocketmine\entity\Projectile;
use pocketmine\event\block\SignChangeEvent;
use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
@ -2906,28 +2905,12 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
}
$t = $this->level->getTile($pos);
if($t instanceof Sign){
if($t instanceof Spawnable){
$nbt = new NBT(NBT::LITTLE_ENDIAN);
$nbt->read($packet->namedtag, false, true);
$nbt = $nbt->getData();
if($nbt["id"] !== Tile::SIGN){
if(!$t->updateCompoundTag($nbt, $this)){
$t->spawnTo($this);
}else{
$ev = new SignChangeEvent($t->getBlock(), $this, [
TextFormat::clean($nbt["Text1"], $this->removeFormat), TextFormat::clean($nbt["Text2"], $this->removeFormat), TextFormat::clean($nbt["Text3"], $this->removeFormat), TextFormat::clean($nbt["Text4"], $this->removeFormat)
]);
if(!isset($t->namedtag->Creator) or $t->namedtag["Creator"] !== $this->getRawUniqueId()){
$ev->setCancelled();
}
$this->server->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$t->setText($ev->getLine(0), $ev->getLine(1), $ev->getLine(2), $ev->getLine(3));
}else{
$t->spawnTo($this);
}
}
}
break;

View File

@ -2,11 +2,11 @@
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* 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
@ -15,16 +15,19 @@
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*
*/
namespace pocketmine\tile;
use pocketmine\event\block\SignChangeEvent;
use pocketmine\level\format\Chunk;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
use pocketmine\utils\TextFormat;
class Sign extends Spawnable{
@ -82,4 +85,30 @@ class Sign extends Spawnable{
]);
}
public function updateCompoundTag(CompoundTag $nbt, Player $player) : bool{
if($nbt["id"] !== Tile::SIGN){
return false;
}
$ev = new SignChangeEvent($this->getBlock(), $player, [
TextFormat::clean($nbt["Text1"], ($removeFormat = $player->getRemoveFormat())),
TextFormat::clean($nbt["Text2"], $removeFormat),
TextFormat::clean($nbt["Text3"], $removeFormat),
TextFormat::clean($nbt["Text4"], $removeFormat)
]);
if(!isset($this->namedtag->Creator) or $this->namedtag["Creator"] !== $player->getRawUniqueId()){
$ev->setCancelled();
}
$this->level->getServer()->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$this->setText(...$ev->getLines());
return true;
}else{
return false;
}
}
}

View File

@ -46,11 +46,6 @@ abstract class Spawnable extends Tile{
return true;
}
/**
* @return CompoundTag
*/
public abstract function getSpawnCompound();
public function __construct(Chunk $chunk, CompoundTag $nbt){
parent::__construct($chunk, $nbt);
$this->spawnToAll();
@ -76,4 +71,22 @@ abstract class Spawnable extends Tile{
$this->level->clearChunkCache($this->chunk->getX(), $this->chunk->getZ());
}
}
/**
* @return CompoundTag
*/
public abstract function getSpawnCompound();
/**
* Called when a player updates a block entity's NBT data
* for example when writing on a sign.
*
* @param CompoundTag $nbt
* @param Player $player
*
* @return bool indication of success, will respawn the tile to the player if false.
*/
public function updateCompoundTag(CompoundTag $nbt, Player $player) : bool{
return false;
}
}