Sign: Do not allow edits by any player except the one who placed it, and only while that player is online

signs now become finalized if:
- the player quits and rejoins (because the entity runtime ID of the player will not be the same)
- the chunk is unloaded and reloaded (because the tagged entity runtime ID is not saved).

closes #4198
This commit is contained in:
Dylan K. Taylor 2021-05-02 13:22:31 +01:00
parent c19d2fe891
commit c47ecb55c0
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 26 additions and 1 deletions

View File

@ -68,7 +68,10 @@ class SignPost extends Transparent{
$this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get(Block::WALL_SIGN, $this->meta), true);
}
Tile::createTile(Tile::SIGN, $this->getLevelNonNull(), TileSign::createNBT($this, $face, $item, $player));
$sign = Tile::createTile(Tile::SIGN, $this->getLevelNonNull(), TileSign::createNBT($this, $face, $item, $player));
if($player !== null && $sign instanceof TileSign){
$sign->setEditorEntityRuntimeId($player->getId());
}
return true;
}

View File

@ -52,6 +52,9 @@ class Sign extends Spawnable{
/** @var string[] */
protected $text = ["", "", "", ""];
/** @var int|null */
protected $editorEntityRuntimeId = null;
protected function readSaveData(CompoundTag $nbt) : void{
if($nbt->hasTag(self::TAG_TEXT_BLOB, StringTag::class)){ //MCPE 1.2 save format
$this->text = self::fixTextBlob($nbt->getString(self::TAG_TEXT_BLOB));
@ -132,6 +135,22 @@ class Sign extends Spawnable{
return $this->text;
}
/**
* Returns the entity runtime ID of the player who placed this sign. Only the player whose entity ID matches this
* one may edit the sign text.
* This is needed because as of 1.16.220, there is still no reliable way to detect when the MCPE client closed the
* sign edit GUI, so we have no way to know when the text is finalized. This limits editing of the text to only the
* player who placed it, and only while that player is online.
* We can say for sure that the sign is finalized if either of the following occurs:
* - The player quits (after rejoin, the player's entity runtimeID will be different).
* - The chunk is unloaded (on next load, the entity runtimeID will be null, because it's not saved).
*/
public function getEditorEntityRuntimeId() : ?int{ return $this->editorEntityRuntimeId; }
public function setEditorEntityRuntimeId(?int $editorEntityRuntimeId) : void{
$this->editorEntityRuntimeId = $editorEntityRuntimeId;
}
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
$nbt->setString(self::TAG_TEXT_BLOB, implode("\n", $this->text));
}
@ -158,6 +177,9 @@ class Sign extends Spawnable{
$removeFormat = $player->getRemoveFormat();
$ev = new SignChangeEvent($this->getBlock(), $player, array_map(function(string $line) use ($removeFormat) : string{ return TextFormat::clean($line, $removeFormat); }, $lines));
if($this->editorEntityRuntimeId === null || $this->editorEntityRuntimeId !== $player->getId()){
$ev->setCancelled();
}
$ev->call();
if(!$ev->isCancelled()){