Fixed signs

This commit is contained in:
Dylan K. Taylor 2017-08-07 19:24:07 +01:00
parent 7958fffa07
commit 2ba601b6e9
2 changed files with 46 additions and 35 deletions

View File

@ -46,7 +46,7 @@ class SignChangeEvent extends BlockEvent implements Cancellable{
public function __construct(Block $theBlock, Player $thePlayer, array $theLines){ public function __construct(Block $theBlock, Player $thePlayer, array $theLines){
parent::__construct($theBlock); parent::__construct($theBlock);
$this->player = $thePlayer; $this->player = $thePlayer;
$this->lines = $theLines; $this->setLines($theLines);
} }
/** /**

View File

@ -32,18 +32,21 @@ use pocketmine\utils\TextFormat;
class Sign extends Spawnable{ class Sign extends Spawnable{
/** @var string[] */
protected $text = ["", "", "", ""];
public function __construct(Level $level, CompoundTag $nbt){ public function __construct(Level $level, CompoundTag $nbt){
if(!isset($nbt->Text1)){ if(isset($nbt->Text)){ //MCPE 1.2 save format
$nbt->Text1 = new StringTag("Text1", ""); $this->text = explode("\n", $nbt->Text->getValue());
} unset($nbt->Text);
if(!isset($nbt->Text2)){ }else{
$nbt->Text2 = new StringTag("Text2", ""); for($i = 1; $i < 4; ++$i){
} $textKey = "Text$i";
if(!isset($nbt->Text3)){ if(isset($nbt->$textKey)){
$nbt->Text3 = new StringTag("Text3", ""); $this->text[$i - 1] = $nbt->$textKey->getValue();
} unset($nbt->$textKey);
if(!isset($nbt->Text4)){ }
$nbt->Text4 = new StringTag("Text4", ""); }
} }
parent::__construct($level, $nbt); parent::__construct($level, $nbt);
@ -51,14 +54,18 @@ class Sign extends Spawnable{
public function saveNBT(){ public function saveNBT(){
parent::saveNBT(); parent::saveNBT();
$this->namedtag->Text = new StringTag("Text", implode("\n", $this->text));
foreach($this->text as $i => $line){ //Backwards-compatibility
$textKey = "Text$i";
$this->namedtag->$textKey = new StringTag($textKey, $this->getLine($i));
}
unset($this->namedtag->Creator); unset($this->namedtag->Creator);
} }
public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){ public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){
$this->namedtag->Text1->setValue($line1); $this->text = [$line1, $line2, $line3, $line4];
$this->namedtag->Text2->setValue($line2);
$this->namedtag->Text3->setValue($line3);
$this->namedtag->Text4->setValue($line4);
$this->onChanged(); $this->onChanged();
} }
@ -71,7 +78,8 @@ class Sign extends Spawnable{
if($index < 0 or $index > 3){ if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-3!"); throw new \InvalidArgumentException("Index must be in the range 0-3!");
} }
$this->namedtag->{"Text" . ($index + 1)}->setValue($line);
$this->text[$index] = $line;
if($update){ if($update){
$this->onChanged(); $this->onChanged();
} }
@ -86,23 +94,18 @@ class Sign extends Spawnable{
if($index < 0 or $index > 3){ if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-3!"); throw new \InvalidArgumentException("Index must be in the range 0-3!");
} }
return $this->namedtag->{"Text" . ($index + 1)}->getValue(); return $this->text[$index];
} }
public function getText(){ /**
return [ * @return string[]
$this->namedtag->Text1->getValue(), */
$this->namedtag->Text2->getValue(), public function getText() : array{
$this->namedtag->Text3->getValue(), return $this->text;
$this->namedtag->Text4->getValue()
];
} }
public function addAdditionalSpawnData(CompoundTag $nbt){ public function addAdditionalSpawnData(CompoundTag $nbt){
for($i = 1; $i <= 4; $i++){ $nbt->Text = new StringTag("Text", implode("\n", $this->text));
$textKey = "Text" . $i;
$nbt->$textKey = $this->namedtag->$textKey;
}
return $nbt; return $nbt;
} }
@ -111,12 +114,20 @@ class Sign extends Spawnable{
return false; return false;
} }
$ev = new SignChangeEvent($this->getBlock(), $player, [ if(isset($nbt->Text)){
TextFormat::clean($nbt->Text1->getValue(), ($removeFormat = $player->getRemoveFormat())), $lines = array_pad(explode("\n", $nbt->Text->getValue()), 4, "");
TextFormat::clean($nbt->Text2->getValue(), $removeFormat), }else{
TextFormat::clean($nbt->Text3->getValue(), $removeFormat), $lines = [
TextFormat::clean($nbt->Text4->getValue(), $removeFormat) $nbt->Text1->getValue(),
]); $nbt->Text2->getValue(),
$nbt->Text3->getValue(),
$nbt->Text4->getValue()
];
}
$removeFormat = $player->getRemoveFormat();
$ev = new SignChangeEvent($this->getBlock(), $player, array_map(function(string $line) use ($removeFormat){ return TextFormat::clean($line, $removeFormat); }, $lines));
if(!isset($this->namedtag->Creator) or $this->namedtag->Creator->getValue() !== $player->getRawUniqueId()){ if(!isset($this->namedtag->Creator) or $this->namedtag->Creator->getValue() !== $player->getRawUniqueId()){
$ev->setCancelled(); $ev->setCancelled();